Skip to content

Commit

Permalink
fix: Improved promise handling (#174)
Browse files Browse the repository at this point in the history
* fix: Fixed auto-load on resource & type fixes

* fix: Generic promise handling & fixes on private key

* fix: Improved promise handling
  • Loading branch information
Eengineer1 authored Jan 27, 2023
1 parent b96f6e7 commit f84a20c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 39 deletions.
28 changes: 26 additions & 2 deletions src/agent/ICheqd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const CreateResourceMethodName = 'cheqdCreateResource'
const GenerateDidDocMethodName = 'cheqdGenerateDidDoc'
const GenerateDidDocWithLinkedResourceMethodName = 'cheqdGenerateDidDocWithLinkedResource'
const GenerateKeyPairMethodName = 'cheqdGenerateIdentityKeys'
const GenerateVersionIdMethodName = 'cheqdGenerateVersionId'

const DidPrefix = 'did'
const CheqdDidMethod = 'cheqd'
Expand All @@ -51,6 +52,7 @@ export interface ICheqd extends IPluginMethodMap {
[GenerateDidDocMethodName]: (args: any, context: IContext) => Promise<TExportedDIDDocWithKeys>,
[GenerateDidDocWithLinkedResourceMethodName]: (args: any, context: IContext) => Promise<TExportedDIDDocWithLinkedResourceWithKeys>,
[GenerateKeyPairMethodName]: (args: any, context: IContext) => Promise<TImportableEd25519Key>
[GenerateVersionIdMethodName]: (args: any, context: IContext) => Promise<string>
}

export class Cheqd implements IAgentPlugin {
Expand Down Expand Up @@ -181,6 +183,21 @@ export class Cheqd implements IAgentPlugin {
"returnType": {
"type": "object"
}
},
"cheqdGenerateVersionId": {
"description": "Generate a random uuid",
"arguments": {
"type": "object",
"properties": {
"args": {
"type": "object",
"description": "A cheqdGenerateVersionIdArgs object as any for extensibility"
}
}
},
"returnType": {
"type": "object"
}
}
}
}
Expand All @@ -205,7 +222,8 @@ export class Cheqd implements IAgentPlugin {
[CreateResourceMethodName]: this.CreateResource.bind(this),
[GenerateDidDocMethodName]: this.GenerateDidDoc.bind(this),
[GenerateDidDocWithLinkedResourceMethodName]: this.GenerateDidDocWithLinkedResource.bind(this),
[GenerateKeyPairMethodName]: this.GenerateIdentityKeys.bind(this)
[GenerateKeyPairMethodName]: this.GenerateIdentityKeys.bind(this),
[GenerateVersionIdMethodName]: this.GenerateVersionId.bind(this)
}
}

Expand Down Expand Up @@ -238,6 +256,7 @@ export class Cheqd implements IAgentPlugin {
options: {
document: args.document,
keys: args.keys,
versionId: args?.versionId,
fee: args?.fee
}
})
Expand Down Expand Up @@ -268,6 +287,7 @@ export class Cheqd implements IAgentPlugin {
options: {
kms: args.kms,
keys: args.keys,
versionId: args?.versionId,
fee: args?.fee
}
})
Expand Down Expand Up @@ -420,8 +440,12 @@ export class Cheqd implements IAgentPlugin {
}
}

private async GenerateVersionId(args: any, context: IContext): Promise<string> {
return v4()
}

static async loadProvider(document: DIDDocument, providers: CheqdDIDProvider[]): Promise<CheqdDIDProvider> {
const provider = providers.find((provider) => document.id.includes(`${DidPrefix}:${CheqdDidMethod}:${provider.network}:`))
const provider = providers.find((provider) => document.id.includes(`${DidPrefix}:${CheqdDidMethod}:${provider.network}`))
if (!provider) {
throw new Error(`[did-provider-cheqd]: Provider namespace not found`)
}
Expand Down
103 changes: 67 additions & 36 deletions src/did-manager/cheqd-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
} from '@veramo/core'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import Debug from 'debug'
import { Bip39, EnglishMnemonic as _, Secp256k1 } from '@cosmjs/crypto'
import { fromString } from 'uint8arrays/from-string'
import { EnglishMnemonic as _, Ed25519 } from '@cosmjs/crypto'
import { fromString, toString } from 'uint8arrays'

const debug = Debug('veramo:did-provider-cheqd')

Expand Down Expand Up @@ -125,24 +125,32 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {

assert(tx.code === 0, `cosmos_transaction: Failed to create DID. Reason: ${tx.rawLog}`)

//* Currently, only one controller key is supported. This is subject to change in the near future.

const controllerKey: ManagedKeyInfo = await context.agent.keyManagerImport({
...options.keys[0],
kms: kms || this.defaultKms,
} as MinimalImportableKey)
//* Currently, only one controller key is supported.
//* We assume that the first key in the list is the controller key.
//* This is subject to change in the near future.

const keys: ManagedKeyInfo[] = []
for (const key of options.keys) {
let managedKey: ManagedKeyInfo | undefined
try {
managedKey = await context.agent.keyManagerImport({
...key,
kms: kms || this.defaultKms,
} as MinimalImportableKey)
} catch (e) {
debug(`Failed to import key ${key.kid}. Reason: ${e}`)
}
if (managedKey) {
keys.push(managedKey)
}
}

const _keys = <ManagedKeyInfo[]>(await Promise.all(options.keys.slice(1).map(
async (key: TImportableEd25519Key) => await context.agent.keyManagerImport({ ...key, kms: kms || this.defaultKms })
.catch(() => undefined)
))).filter(
(key: ManagedKeyInfo | undefined) => key !== undefined
) ?? []
const controllerKey = {...options.keys[0], kms: kms || this.defaultKms}

const identifier: IIdentifier = {
did: options.document.id!,
did: <string>options.document.id,
controllerKeyId: controllerKey.kid,
keys: [controllerKey, ..._keys],
keys: [controllerKey, ...keys],
services: options.document.service || [],
provider: 'cheqd',
}
Expand Down Expand Up @@ -177,24 +185,32 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {

assert(tx.code === 0, `cosmos_transaction: Failed to update DID. Reason: ${tx.rawLog}`)

//* Currently, only one controller key is supported. This is subject to change in the near future.

const controllerKey: ManagedKeyInfo = await context.agent.keyManagerImport({
...options.keys[0],
kms: options.kms || this.defaultKms,
} as MinimalImportableKey)
//* Currently, only one controller key is supported.
//* We assume that the first key in the list is the controller key.
//* This is subject to change in the near future.

const keys: ManagedKeyInfo[] = []
for (const key of options.keys) {
let managedKey: ManagedKeyInfo | undefined
try {
managedKey = await context.agent.keyManagerImport({
...key,
kms: options.kms || this.defaultKms,
} as MinimalImportableKey)
} catch (e) {
debug(`Failed to import key ${key.kid}. Reason: ${e}`)
}
if (managedKey) {
keys.push(managedKey)
}
}

const _keys = <ManagedKeyInfo[]>(await Promise.all(options.keys.slice(1).map(
async (key: TImportableEd25519Key) => await context.agent.keyManagerImport({ ...key, kms: options.kms || this.defaultKms })
.catch(() => undefined)
))).filter(
(key: ManagedKeyInfo | undefined) => key !== undefined
) ?? []
const controllerKey = {...options.keys[0], kms: options.kms || this.defaultKms}

const identifier: IIdentifier = {
did: <string>document.id,
controllerKeyId: controllerKey.kid,
keys: [controllerKey, ..._keys],
keys: [controllerKey, ...keys],
services: document.service || [],
provider: 'cheqd',
}
Expand Down Expand Up @@ -264,13 +280,28 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
}
}

await Promise.all(options.signInputs.filter(input => mapKeyType(input.keyType) !== undefined)
.map(async signInput => await context.agent.keyManagerImport({
privateKeyHex: signInput.privateKeyHex,
type: mapKeyType(signInput.keyType) as TSupportedKeyType,
kms: options.kms || this.defaultKms,
} as MinimalImportableKey).catch(() => undefined))
)
const signInput = options.signInputs.filter(input => mapKeyType(input.keyType) !== undefined)

const keys: ManagedKeyInfo[] = []
for (const input of options.signInputs) {
let managedKey: ManagedKeyInfo | undefined
try {
// get public key from private key in hex
const publicKey = toString((await Ed25519.makeKeypair(fromString(input.privateKeyHex, 'hex'))).pubkey, 'hex')
managedKey = await context.agent.keyManagerImport({
kid: publicKey,
publicKeyHex: publicKey,
privateKeyHex: input.privateKeyHex,
type: mapKeyType(input.keyType) as TSupportedKeyType,
kms: options.kms || this.defaultKms,
} as MinimalImportableKey)
} catch (e) {
debug(`Failed to import key ${input.verificationMethodId}. Reason: ${e}`)
}
if (managedKey) {
keys.push(managedKey)
}
}

debug('Created Resource', options.payload)
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"strict": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2018",
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down

0 comments on commit f84a20c

Please sign in to comment.