Skip to content

Commit

Permalink
feat: Added identityManagerImportIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Sep 29, 2020
1 parent e4e5be0 commit ea7ba3a
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 13 deletions.
53 changes: 52 additions & 1 deletion __tests__/shared/identityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,58 @@ export default (testContext: {
expect(webIdentity2.keys[0].kid).toEqual(webIdentity.keys[0].kid)
})

it.todo('should import identity')
it('should import identity', async () => {
const identity = await agent.identityManagerGetOrCreateIdentity({
alias: 'example.org',
provider: 'did:web',
})

await agent.identityManagerAddService({
did: identity.did,
service: {
id: 'did:web:example.org#msg',
type: 'Messaging',
serviceEndpoint: 'https://example.org/messaging',
description: 'Handles incoming messages',
},
})

const signingKeyFull = await agent.keyManagerGetKey({
kid: identity.keys[0].kid,
})

const encryptionKey = await agent.keyManagerCreateKey({
kms: 'local',
type: 'Ed25519',
})

const encryptionKeyFull = await agent.keyManagerGetKey({
kid: encryptionKey.kid,
})

await agent.identityManagerAddKey({
did: identity.did,
key: encryptionKey,
})

const exportedIdentity = await agent.identityManagerGetIdentity({
did: identity.did,
})

await agent.identityManagerDeleteIdentity({
did: identity.did,
})

await agent.identityManagerImportIdentity({
...exportedIdentity,
keys: [signingKeyFull, encryptionKeyFull],
})

const importedIdentity = await agent.identityManagerGetIdentity({
did: identity.did,
})
expect(importedIdentity).toEqual(exportedIdentity)
})

it.todo('should add key for did:ethr')
it.todo('should remove key for did:ethr')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Imports identity
<b>Signature:</b>

```typescript
identityManagerImportIdentity(args: IIdentity): Promise<IIdentity>;
identityManagerImportIdentity(args: IIdentity, context: IAgentContext<IKeyManager>): Promise<IIdentity>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| args | [IIdentity](./daf-core.iidentity.md) | |
| context | [IAgentContext](./daf-core.iagentcontext.md)<!-- -->&lt;[IKeyManager](./daf-core.ikeymanager.md)<!-- -->&gt; | |

<b>Returns:</b>

Expand Down
2 changes: 1 addition & 1 deletion docs/api/daf-core.iidentitymanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface IIdentityManager extends IPluginMethodMap
| [identityManagerGetIdentityByAlias(args)](./daf-core.iidentitymanager.identitymanagergetidentitybyalias.md) | Returns a specific identity by alias |
| [identityManagerGetOrCreateIdentity(args, context)](./daf-core.iidentitymanager.identitymanagergetorcreateidentity.md) | Returns an existing identity or creates a new one for a specific alias |
| [identityManagerGetProviders()](./daf-core.iidentitymanager.identitymanagergetproviders.md) | Returns a list of available identity providers |
| [identityManagerImportIdentity(args)](./daf-core.iidentitymanager.identitymanagerimportidentity.md) | Imports identity |
| [identityManagerImportIdentity(args, context)](./daf-core.iidentitymanager.identitymanagerimportidentity.md) | Imports identity |
| [identityManagerRemoveKey(args, context)](./daf-core.iidentitymanager.identitymanagerremovekey.md) | Removes a key from a DID Document |
| [identityManagerRemoveService(args, context)](./daf-core.iidentitymanager.identitymanagerremoveservice.md) | Removes a service from a DID Document |
2 changes: 1 addition & 1 deletion docs/api/daf-graphql.basetypedef.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
baseTypeDef = "\ntype Query\ntype Mutation\n\nscalar KeyMeta\n\ntype Key {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ntype Service {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ntype Identity {\n did: String!\n provider: String\n alias: String\n controllerKeyId: String\n keys: [Key]\n services: [Service]\n}\n\nscalar Object\nscalar Date\nscalar VerifiablePresentation\nscalar VerifiableCredential\nscalar Presentation\nscalar Credential\n\ntype Message {\n id: ID!\n createdAt: Date\n expiresAt: Date\n threadId: String\n type: String!\n raw: String\n data: Object\n replyTo: [String]\n replyUrl: String\n from: String\n to: String\n metaData: [MetaData]\n presentations: [VerifiablePresentation]\n credentials: [VerifiableCredential]\n}\n\ntype MetaData {\n type: String!\n value: String\n}\n\n\n"
baseTypeDef = "\ntype Query\ntype Mutation\n\nscalar KeyMeta\n\ntype Key {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ninput KeyInput {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ntype Service {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ninput ServiceInput {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ntype Identity {\n did: String!\n provider: String\n alias: String\n controllerKeyId: String\n keys: [Key]\n services: [Service]\n}\n\nscalar Object\nscalar Date\nscalar VerifiablePresentation\nscalar VerifiableCredential\nscalar Presentation\nscalar Credential\n\ntype Message {\n id: ID!\n createdAt: Date\n expiresAt: Date\n threadId: String\n type: String!\n raw: String\n data: Object\n replyTo: [String]\n replyUrl: String\n from: String\n to: String\n metaData: [MetaData]\n presentations: [VerifiablePresentation]\n credentials: [VerifiableCredential]\n}\n\ntype MetaData {\n type: String!\n value: String\n}\n\n\n"
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Imports identity
<b>Signature:</b>

```typescript
identityManagerImportIdentity(identity: IIdentity): Promise<IIdentity>;
identityManagerImportIdentity(identity: IIdentity, context: IAgentContext<IKeyManager>): Promise<IIdentity>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| identity | [IIdentity](./daf-core.iidentity.md) | |
| context | [IAgentContext](./daf-core.iagentcontext.md)<!-- -->&lt;[IKeyManager](./daf-core.ikeymanager.md)<!-- -->&gt; | |

<b>Returns:</b>

Expand Down
2 changes: 1 addition & 1 deletion docs/api/daf-identity-manager.identitymanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export declare class IdentityManager implements IAgentPlugin
| [identityManagerGetIdentityByAlias({ alias, provider, })](./daf-identity-manager.identitymanager.identitymanagergetidentitybyalias.md) | | Returns a specific identity by alias |
| [identityManagerGetOrCreateIdentity({ provider, alias, kms, options }, context)](./daf-identity-manager.identitymanager.identitymanagergetorcreateidentity.md) | | Returns an existing identity or creates a new one for a specific alias |
| [identityManagerGetProviders()](./daf-identity-manager.identitymanager.identitymanagergetproviders.md) | | Returns a list of available identity providers |
| [identityManagerImportIdentity(identity)](./daf-identity-manager.identitymanager.identitymanagerimportidentity.md) | | Imports identity |
| [identityManagerImportIdentity(identity, context)](./daf-identity-manager.identitymanager.identitymanagerimportidentity.md) | | Imports identity |
| [identityManagerRemoveKey({ did, kid, options }, context)](./daf-identity-manager.identitymanager.identitymanagerremovekey.md) | | Removes a key from a DID Document |
| [identityManagerRemoveService({ did, id, options }, context)](./daf-identity-manager.identitymanager.identitymanagerremoveservice.md) | | Removes a service from a DID Document |
2 changes: 1 addition & 1 deletion packages/daf-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ export interface IIdentityManager extends IPluginMethodMap {
/**
* Imports identity
*/
identityManagerImportIdentity(args: IIdentity): Promise<IIdentity>
identityManagerImportIdentity(args: IIdentity, context: IAgentContext<IKeyManager>): Promise<IIdentity>

/**
* Deletes identity
Expand Down
33 changes: 32 additions & 1 deletion packages/daf-graphql/src/methods/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ export const identityManagerRemoveKey: IAgentGraphQLMethod = {
`,
}

export const identityManagerImportIdentity: IAgentGraphQLMethod = {
type: 'Mutation',
query: `
mutation identityManagerImportIdentity($did: String!, $alias: String, $provider: String!, $controllerKeyId: String, $keys: [KeyInput]!, $services: [ServiceInput]!) {
identityManagerImportIdentity(did: $did, provider: $provider, alias: $alias, controllerKeyId: $controllerKeyId, keys: $keys, services: $services) {
did
provider
alias
controllerKeyId
keys {
kid
kms
type
publicKeyHex
}
services {
id
type
serviceEndpoint
description
}
}
}
`,
typeDef: `
extend type Mutation {
identityManagerImportIdentity(did: String!, provider: String!, alias: String, controllerKeyId: String, keys: [KeyInput]!, services: [ServiceInput]!): Identity!
}
`,
}

export const supportedMethods: Record<string, IAgentGraphQLMethod> = {
identityManagerGetProviders,
identityManagerGetIdentities,
Expand All @@ -257,7 +288,7 @@ export const supportedMethods: Record<string, IAgentGraphQLMethod> = {
identityManagerRemoveService,
identityManagerAddKey,
identityManagerRemoveKey,
// TODO identityManagerImportIdentity
identityManagerImportIdentity,
}

export default supportedMethods
12 changes: 11 additions & 1 deletion packages/daf-identity-manager/src/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ export class IdentityManager implements IAgentPlugin {
}

/** {@inheritDoc daf-core#IIdentityManager.identityManagerImportIdentity} */
async identityManagerImportIdentity(identity: IIdentity): Promise<IIdentity> {
async identityManagerImportIdentity(
identity: IIdentity,
context: IAgentContext<IKeyManager>,
): Promise<IIdentity> {
for (const key of identity.keys) {
await context.agent.keyManagerImportKey(key)
}
await this.store.import(identity)
return identity
}
Expand All @@ -140,6 +146,10 @@ export class IdentityManager implements IAgentPlugin {
const identity = await this.store.get({ did })
const provider = this.getProvider(identity.provider)
await provider.deleteIdentity(identity, context)
if (identity.services.length > 0) {
identity.services = []
await this.store.import(identity)
}
return this.store.delete({ did })
}

Expand Down
2 changes: 1 addition & 1 deletion packages/daf-typeorm/src/entities/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Identity extends BaseEntity {
keys: Key[]

@OneToMany((type) => Service, (service) => service.identity, {
cascade: ['insert'],
cascade: true,
})
//@ts-ignore
services: Service[]
Expand Down
2 changes: 1 addition & 1 deletion report/daf-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface IIdentityManager extends IPluginMethodMap {
identityManagerGetIdentityByAlias(args: IIdentityManagerGetIdentityByAliasArgs): Promise<IIdentity>;
identityManagerGetOrCreateIdentity(args: IIdentityManagerGetOrCreateIdentityArgs, context: IAgentContext<IKeyManager>): Promise<IIdentity>;
identityManagerGetProviders(): Promise<Array<string>>;
identityManagerImportIdentity(args: IIdentity): Promise<IIdentity>;
identityManagerImportIdentity(args: IIdentity, context: IAgentContext<IKeyManager>): Promise<IIdentity>;
identityManagerRemoveKey(args: IIdentityManagerRemoveKeyArgs, context: IAgentContext<IKeyManager>): Promise<any>;
identityManagerRemoveService(args: IIdentityManagerRemoveServiceArgs, context: IAgentContext<IKeyManager>): Promise<any>;
}
Expand Down
2 changes: 1 addition & 1 deletion report/daf-graphql.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AgentGraphQLClient implements IAgentPlugin {
}

// @public (undocumented)
export const baseTypeDef = "\ntype Query\ntype Mutation\n\nscalar KeyMeta\n\ntype Key {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ntype Service {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ntype Identity {\n did: String!\n provider: String\n alias: String\n controllerKeyId: String\n keys: [Key]\n services: [Service]\n}\n\nscalar Object\nscalar Date\nscalar VerifiablePresentation\nscalar VerifiableCredential\nscalar Presentation\nscalar Credential\n\ntype Message {\n id: ID!\n createdAt: Date\n expiresAt: Date\n threadId: String\n type: String!\n raw: String\n data: Object\n replyTo: [String]\n replyUrl: String\n from: String\n to: String\n metaData: [MetaData]\n presentations: [VerifiablePresentation]\n credentials: [VerifiableCredential]\n}\n\ntype MetaData {\n type: String!\n value: String\n}\n\n\n";
export const baseTypeDef = "\ntype Query\ntype Mutation\n\nscalar KeyMeta\n\ntype Key {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ninput KeyInput {\n kid: String!\n kms: String!\n type: String!\n publicKeyHex: String!\n privateKeyHex: String\n meta: KeyMeta\n}\n\ntype Service {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ninput ServiceInput {\n id: String!\n type: String!\n serviceEndpoint: String!\n description: String\n}\n\ntype Identity {\n did: String!\n provider: String\n alias: String\n controllerKeyId: String\n keys: [Key]\n services: [Service]\n}\n\nscalar Object\nscalar Date\nscalar VerifiablePresentation\nscalar VerifiableCredential\nscalar Presentation\nscalar Credential\n\ntype Message {\n id: ID!\n createdAt: Date\n expiresAt: Date\n threadId: String\n type: String!\n raw: String\n data: Object\n replyTo: [String]\n replyUrl: String\n from: String\n to: String\n metaData: [MetaData]\n presentations: [VerifiablePresentation]\n credentials: [VerifiableCredential]\n}\n\ntype MetaData {\n type: String!\n value: String\n}\n\n\n";

// @public (undocumented)
export const createSchema: (options: {
Expand Down
2 changes: 1 addition & 1 deletion report/daf-identity-manager.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class IdentityManager implements IAgentPlugin {
// (undocumented)
identityManagerGetProviders(): Promise<string[]>;
// (undocumented)
identityManagerImportIdentity(identity: IIdentity): Promise<IIdentity>;
identityManagerImportIdentity(identity: IIdentity, context: IAgentContext<IKeyManager>): Promise<IIdentity>;
// (undocumented)
identityManagerRemoveKey({ did, kid, options }: IIdentityManagerRemoveKeyArgs, context: IAgentContext<IKeyManager>): Promise<any>;
// (undocumented)
Expand Down

0 comments on commit ea7ba3a

Please sign in to comment.