Skip to content

Commit

Permalink
feat(did-provider-key): add option to create the identifier from a gi…
Browse files Browse the repository at this point in the history
…ven private key (#1165)
  • Loading branch information
tadejpodrekar authored Apr 14, 2023
1 parent e9474e2 commit ad79a22
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
16 changes: 16 additions & 0 deletions __tests__/shared/didManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ export default (testContext: {
expect(identifier.provider).toEqual('did:jwk')
})

it('should create identifier using did:key with an imported key', async () => {
// keyType supports 'Secp256k1', 'Ed25519', 'X25519'
const keyType = 'Secp256k1'
identifier = await agent.didManagerCreate({
provider: 'did:key',
alias: 'keyTest',
options: {
keyType,
privateKeyHex: 'f3157fbbb356a0d56a84a1a9752f81d0638cce4153168bd1b46f68a6e62b82b1',
}
})
expect(identifier.provider).toEqual('did:key')
expect(identifier.keys[0].type).toEqual(keyType)
expect(identifier.controllerKeyId).toEqual(identifier.keys[0].kid)
})

it('should throw error for existing alias provider combo', async () => {
await expect(
agent.didManagerCreate({
Expand Down
48 changes: 44 additions & 4 deletions packages/did-provider-key/src/key-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IIdentifier, IKey, IService, IAgentContext, IKeyManager } from '@veramo/core-types'
import { IIdentifier, IKey, IService, IAgentContext, IKeyManager, RequireOnly } from '@veramo/core-types'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import Multibase from 'multibase'
import Multicodec from 'multicodec'
Expand All @@ -7,6 +7,16 @@ import Debug from 'debug'
const debug = Debug('veramo:did-key:identifier-provider')

type IContext = IAgentContext<IKeyManager>
type CreateKeyDidOptions = {
keyType?: keyof typeof keyOptions
privateKeyHex?: string,
}

const keyOptions = {
'Ed25519': 'ed25519-pub',
'X25519': 'x25519-pub',
'Secp256k1': 'secp256k1-pub'
} as const

/**
* {@link @veramo/did-manager#DIDManager} identifier provider for `did:key` identifiers
Expand All @@ -22,15 +32,25 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
}

async createIdentifier(
{ kms, options }: { kms?: string; options?: any },
{ kms, options }: { kms?: string; options?: CreateKeyDidOptions },
context: IContext,
): Promise<Omit<IIdentifier, 'provider'>> {
const key = await context.agent.keyManagerCreate({ kms: kms || this.defaultKms, type: 'Ed25519' })
const keyType = (options?.keyType && keyOptions[options?.keyType] && options.keyType) || 'Ed25519'
const key = await this.importOrGenerateKey(
{
kms: kms || this.defaultKms,
options: {
keyType,
...(options?.privateKeyHex && { privateKeyHex: options.privateKeyHex }),
},
},
context,
)

const methodSpecificId = Buffer.from(
Multibase.encode(
'base58btc',
Multicodec.addPrefix('ed25519-pub', Buffer.from(key.publicKeyHex, 'hex')),
Multicodec.addPrefix(keyOptions[keyType], Buffer.from(key.publicKeyHex, 'hex')),
),
).toString()

Expand Down Expand Up @@ -82,4 +102,24 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
): Promise<any> {
throw Error('KeyDIDProvider removeService not supported')
}

private async importOrGenerateKey(
args: {
kms: string,
options: RequireOnly<CreateKeyDidOptions, 'keyType'>
},
context: IContext
): Promise<IKey> {
if (args.options.privateKeyHex) {
return context.agent.keyManagerImport({
kms: args.kms || this.defaultKms,
type: args.options.keyType,
privateKeyHex: args.options.privateKeyHex,
})
}
return context.agent.keyManagerCreate({
kms: args.kms || this.defaultKms,
type: args.options.keyType,
})
}
}
2 changes: 1 addition & 1 deletion packages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"files": [],
"references": [
{ "path": "core" },
{ "path": "core-types" }
{ "path": "core-types" },
{ "path": "credential-eip712" },
{ "path": "credential-ld" },
{ "path": "credential-status"},
Expand Down

0 comments on commit ad79a22

Please sign in to comment.