Skip to content

Commit

Permalink
feat: Ethr-did export/import
Browse files Browse the repository at this point in the history
Fixes #105
  • Loading branch information
simonas-notcat committed Mar 19, 2020
1 parent fa6b6c5 commit 0f8ab11
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
49 changes: 48 additions & 1 deletion packages/daf-cli/src/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ program
.option('-t, --types', 'List available identity controller types')
.option('-c, --create', 'Create identity')
.option('-d, --delete', 'Delete identity')
.option('-e, --export', 'Export identity')
.option('-i, --import', 'Import identity')
.option('-s, --service', 'Add service endpoint')
.option('-p, --publicKey', 'Add public key')
.option('--encrypt', 'Encrypt data to a recipient DID')
Expand Down Expand Up @@ -45,7 +47,7 @@ program
type: 'list',
name: 'type',
choices: types.map(item => ({ name: `${item.type} - ${item.description}`, value: item.type })),
message: 'Select identity controller',
message: 'Select identity provider',
},
])
const identity = await core.identityManager.createIdentity(answers.type)
Expand Down Expand Up @@ -204,4 +206,49 @@ program
console.error(e)
}
}

if (cmd.export) {
try {
const identities = await core.identityManager.getIdentities()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identities.map(item => item.did),
message: 'Select DID',
},
])

const identity = await core.identityManager.getIdentity(answers.did)
const secret = await core.identityManager.exportIdentity(identity.identityProviderType, identity.did)
console.log(secret)
} catch (e) {
console.error(e)
}
}

if (cmd.import) {
try {
const providers = await core.identityManager.getIdentityProviderTypes()

const answers = await inquirer.prompt([
{
type: 'list',
name: 'provider',
choices: providers.map(item => item.type),
message: 'Select identity provider',
},
{
type: 'text',
name: 'secret',
message: 'Secret',
},
])

const identity = await core.identityManager.importIdentity(answers.provider, answers.secret)
console.log(identity)
} catch (e) {
console.error(e)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ export abstract class AbstractKeyManagementSystem {
abstract createKey(type: KeyType): Promise<AbstractKey>
abstract getKey(kid: string): Promise<AbstractKey>
abstract deleteKey(kid: string): Promise<boolean>
importKey(key: SerializedKey): Promise<AbstractKey> {
return Promise.reject('Method importKey not implemented')
}
}
14 changes: 14 additions & 0 deletions packages/daf-ethr-did/src/identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ export class IdentityProvider extends AbstractIdentityProvider {
const serializedIdentity = await this.identityStore.get(did)
return this.identityFromSerialized(serializedIdentity)
}

async exportIdentity(did: string) {
const serializedIdentity = await this.identityStore.get(did)
return JSON.stringify(serializedIdentity)
}

async importIdentity(secret: string) {
const serializedIdentity: SerializedIdentity = JSON.parse(secret)
for (const serializedKey of serializedIdentity.keys) {
await this.kms.importKey(serializedKey)
}
await this.identityStore.set(serializedIdentity.did, serializedIdentity)
return this.identityFromSerialized(serializedIdentity)
}
}
5 changes: 5 additions & 0 deletions packages/daf-libsodium/src/key-management-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ export class KeyManagementSystem extends AbstractKeyManagementSystem {
debug('Deleting', kid)
return await this.keyStore.delete(kid)
}

async importKey(serializedKey: SerializedKey) {
await this.keyStore.set(serializedKey.kid, serializedKey)
return new Key(serializedKey)
}
}

0 comments on commit 0f8ab11

Please sign in to comment.