From 3334f6edea1ebd51dde1b416b153294f4b945e4b Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Fri, 10 Apr 2020 14:28:18 +0300 Subject: [PATCH] feat: Default IdentityProvider --- examples/expressjs-ethr/src/identity.ts | 3 +- examples/expressjs-ethr/yarn.lock | 50 +++++++++---------- examples/send-vc/index.ts | 3 +- examples/send-vc/yarn.lock | 38 +++++++------- packages/daf-cli/src/identity-manager.ts | 28 ++++++----- .../src/graphql/graphql-identity-manager.ts | 2 +- .../daf-core/src/identity/identity-manager.ts | 22 +++++--- 7 files changed, 77 insertions(+), 69 deletions(-) diff --git a/examples/expressjs-ethr/src/identity.ts b/examples/expressjs-ethr/src/identity.ts index 732f8291d..a4f531d77 100644 --- a/examples/expressjs-ethr/src/identity.ts +++ b/examples/expressjs-ethr/src/identity.ts @@ -8,8 +8,7 @@ export const getIdentity = async () => { if (identities.length > 0) { identity = identities[0] } else { - const identityProviders = await agent.identityManager.getIdentityProviders() - identity = await agent.identityManager.createIdentity(identityProviders[0].type) + identity = await agent.identityManager.createIdentity() } return identity diff --git a/examples/expressjs-ethr/yarn.lock b/examples/expressjs-ethr/yarn.lock index f60629d0e..3e777dedf 100644 --- a/examples/expressjs-ethr/yarn.lock +++ b/examples/expressjs-ethr/yarn.lock @@ -613,51 +613,51 @@ cross-fetch@^3.0.4: node-fetch "2.6.0" whatwg-fetch "3.0.0" -daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.39+63dd12d: - version "4.0.0-beta.39" +daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.42: + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" debug "^4.1.1" events "^3.0.0" typeorm "^0.2.24" -daf-data-store@../../packages/daf-data-store, daf-data-store@^4.0.0-beta.39+63dd12d: - version "4.0.0-beta.39" +daf-data-store@../../packages/daf-data-store, daf-data-store@^4.0.0-beta.42: + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" sql-bricks-sqlite "^0.1.0" daf-did-comm@../../packages/daf-did-comm: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" uuid "^3.3.3" -daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.39+63dd12d: - version "4.0.0-beta.39" +daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.42: + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" did-resolver "^1.1.0" daf-ethr-did@../../packages/daf-ethr-did: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" ethjs-provider-signer "^0.1.4" ethr-did "^1.1.0" js-sha3 "^0.8.0" daf-libsodium@../../packages/daf-libsodium: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: base-58 "^0.0.1" - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" elliptic "^6.5.2" @@ -665,13 +665,13 @@ daf-libsodium@../../packages/daf-libsodium: libsodium-wrappers "^0.7.6" daf-resolver-universal@../../packages/daf-resolver-universal: - version "3.0.1" + version "4.0.0-beta.42" dependencies: cross-fetch "^3.0.4" debug "^4.1.1" daf-resolver@../../packages/daf-resolver: - version "3.0.1" + version "4.0.0-beta.42" dependencies: debug "^4.1.1" did-resolver "^1.1.0" @@ -680,29 +680,29 @@ daf-resolver@../../packages/daf-resolver: web-did-resolver "^1.2.0" daf-selective-disclosure@../../packages/daf-selective-disclosure: - version "4.0.0-beta.41" + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" - daf-core "^4.0.0-beta.39+63dd12d" - daf-data-store "^4.0.0-beta.39+63dd12d" - daf-did-jwt "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" + daf-data-store "^4.0.0-beta.42" + daf-did-jwt "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" typeorm "^0.2.24" daf-url@../../packages/daf-url: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" url-parse "^1.4.7" daf-w3c@../../packages/daf-w3c: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" - daf-core "^4.0.0-beta.39+63dd12d" - daf-did-jwt "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" + daf-did-jwt "^4.0.0-beta.42" debug "^4.1.1" did-jwt-vc "^0.1.3" did-resolver "^1.1.0" diff --git a/examples/send-vc/index.ts b/examples/send-vc/index.ts index df9f9c8bd..00affc66f 100644 --- a/examples/send-vc/index.ts +++ b/examples/send-vc/index.ts @@ -10,8 +10,7 @@ async function main() { if (identities.length > 0) { identity = identities[0] } else { - const identityProviders = await agent.identityManager.getIdentityProviders() - identity = await agent.identityManager.createIdentity(identityProviders[0].type) + identity = await agent.identityManager.createIdentity() } // Sign verifiable credential diff --git a/examples/send-vc/yarn.lock b/examples/send-vc/yarn.lock index 9911b9218..190021750 100644 --- a/examples/send-vc/yarn.lock +++ b/examples/send-vc/yarn.lock @@ -479,8 +479,8 @@ cross-fetch@^3.0.4: node-fetch "2.6.0" whatwg-fetch "3.0.0" -daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.39+63dd12d: - version "4.0.0-beta.39" +daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.42: + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" debug "^4.1.1" @@ -488,40 +488,40 @@ daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.39+63dd12d: typeorm "^0.2.24" daf-did-comm@../../packages/daf-did-comm: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" uuid "^3.3.3" -daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.39+63dd12d: - version "4.0.0-beta.39" +daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.42: + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" did-resolver "^1.1.0" daf-ethr-did@../../packages/daf-ethr-did: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" ethjs-provider-signer "^0.1.4" ethr-did "^1.1.0" js-sha3 "^0.8.0" daf-fs@../../packages/daf-fs: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" daf-libsodium@../../packages/daf-libsodium: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: base-58 "^0.0.1" - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" elliptic "^6.5.2" @@ -529,7 +529,7 @@ daf-libsodium@../../packages/daf-libsodium: libsodium-wrappers "^0.7.6" daf-resolver@../../packages/daf-resolver: - version "3.0.1" + version "4.0.0-beta.42" dependencies: debug "^4.1.1" did-resolver "^1.1.0" @@ -538,7 +538,7 @@ daf-resolver@../../packages/daf-resolver: web-did-resolver "^1.2.0" daf-trust-graph@../../packages/daf-trust-graph: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: apollo-cache-inmemory "^1.6.3" apollo-client "^2.6.4" @@ -547,7 +547,7 @@ daf-trust-graph@../../packages/daf-trust-graph: apollo-link-ws "^1.0.19" apollo-utilities "^1.3.2" cross-fetch "^3.0.4" - daf-core "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" debug "^4.1.1" did-jwt "^4.0.0" graphql "^14.0.0" @@ -555,11 +555,11 @@ daf-trust-graph@../../packages/daf-trust-graph: subscriptions-transport-ws "^0.9.0" daf-w3c@../../packages/daf-w3c: - version "4.0.0-beta.39" + version "4.0.0-beta.42" dependencies: blakejs "^1.1.0" - daf-core "^4.0.0-beta.39+63dd12d" - daf-did-jwt "^4.0.0-beta.39+63dd12d" + daf-core "^4.0.0-beta.42" + daf-did-jwt "^4.0.0-beta.42" debug "^4.1.1" did-jwt-vc "^0.1.3" did-resolver "^1.1.0" diff --git a/packages/daf-cli/src/identity-manager.ts b/packages/daf-cli/src/identity-manager.ts index 145e7ab97..27bf48773 100644 --- a/packages/daf-cli/src/identity-manager.ts +++ b/packages/daf-cli/src/identity-manager.ts @@ -45,19 +45,23 @@ program if (cmd.create) { try { const providers = await agent.identityManager.getIdentityProviders() + let type + if (providers.length > 1) { + const answers = await inquirer.prompt([ + { + type: 'list', + name: 'type', + choices: providers.map(provider => ({ + name: `${provider.type} - ${provider.description}`, + value: provider.type, + })), + message: 'Select identity provider', + }, + ]) - const answers = await inquirer.prompt([ - { - type: 'list', - name: 'type', - choices: providers.map(provider => ({ - name: `${provider.type} - ${provider.description}`, - value: provider.type, - })), - message: 'Select identity provider', - }, - ]) - const identity = await agent.identityManager.createIdentity(answers.type) + type = answers.type + } + const identity = await agent.identityManager.createIdentity(type) printTable([{ type: identity.identityProviderType, did: identity.did }]) } catch (e) { console.error(e) diff --git a/packages/daf-core/src/graphql/graphql-identity-manager.ts b/packages/daf-core/src/graphql/graphql-identity-manager.ts index fd5338f34..b31cda77e 100644 --- a/packages/daf-core/src/graphql/graphql-identity-manager.ts +++ b/packages/daf-core/src/graphql/graphql-identity-manager.ts @@ -6,7 +6,7 @@ export interface Context { } const identityProviders = async (_: any, args: any, ctx: Context) => { - return await ctx.agent.identityManager.getIdentityProviders() + return ctx.agent.identityManager.getIdentityProviders() } const managedIdentities = async (_: any, args: any, ctx: Context) => { diff --git a/packages/daf-core/src/identity/identity-manager.ts b/packages/daf-core/src/identity/identity-manager.ts index ba3a30b9a..f62a9ead8 100644 --- a/packages/daf-core/src/identity/identity-manager.ts +++ b/packages/daf-core/src/identity/identity-manager.ts @@ -12,18 +12,18 @@ export class IdentityManager { this.identityProviders = options.identityProviders } - async getIdentityProviders(): Promise { + getIdentityProviders(): AbstractIdentityProvider[] { return this.identityProviders } - async getIdentityProvider(type: string): Promise { + getIdentityProvider(type: string): AbstractIdentityProvider { for (const identityProvider of this.identityProviders) { if (identityProvider.type === type) { return identityProvider } } - return Promise.reject('IdentityProvider not found for type: ' + type) + throw Error('IdentityProvider not found for type: ' + type) } async getIdentities(): Promise { @@ -45,23 +45,29 @@ export class IdentityManager { } } - async createIdentity(identityProviderType: string): Promise { - const identityProvider = await this.getIdentityProvider(identityProviderType) + async createIdentity(identityProviderType?: string): Promise { + const identityProvider = identityProviderType + ? this.getIdentityProvider(identityProviderType) + : this.getDefaultIdentityProvider() return identityProvider.createIdentity() } async importIdentity(identityProviderType: string, secret: string): Promise { - const identityProvider = await this.getIdentityProvider(identityProviderType) + const identityProvider = this.getIdentityProvider(identityProviderType) return identityProvider.importIdentity(secret) } async exportIdentity(identityProviderType: string, did: string): Promise { - const identityProvider = await this.getIdentityProvider(identityProviderType) + const identityProvider = this.getIdentityProvider(identityProviderType) return identityProvider.exportIdentity(did) } async deleteIdentity(identityProviderType: string, did: string): Promise { - const identityProvider = await this.getIdentityProvider(identityProviderType) + const identityProvider = this.getIdentityProvider(identityProviderType) return identityProvider.deleteIdentity(did) } + + private getDefaultIdentityProvider(): AbstractIdentityProvider { + return this.identityProviders[0] + } }