Skip to content

Commit

Permalink
fix(did-provider-key): use compressed keys for Secp256k1 did:key
Browse files Browse the repository at this point in the history
fixes #1213
  • Loading branch information
mirceanis committed Aug 4, 2023
1 parent a2f482e commit 9c38d4a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
78 changes: 78 additions & 0 deletions packages/did-provider-key/__tests__/key-did-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { IDIDManager, IKeyManager, IResolver } from '../../core-types/src'
import { createAgent } from '../../core/src'
import { DIDManager, MemoryDIDStore } from '../../did-manager/src'
import { DIDResolverPlugin } from '../../did-resolver/src'
import { KeyManager, MemoryKeyStore, MemoryPrivateKeyStore } from '../../key-manager/src'
import { KeyManagementSystem } from '../../kms-local/src'

import { getDidKeyResolver, KeyDIDProvider } from '../src'

const defaultKms = 'mem'

const agent = createAgent<IKeyManager & IDIDManager & IResolver>({
plugins: [
new KeyManager({
store: new MemoryKeyStore(),
kms: {
[defaultKms]: new KeyManagementSystem(new MemoryPrivateKeyStore()),
},
}),
new DIDManager({
providers: {
'did:key': new KeyDIDProvider({
defaultKms,
}),
},
defaultProvider: 'did:key',
store: new MemoryDIDStore(),
}),
new DIDResolverPlugin(getDidKeyResolver()),
],
})

describe('@veramo/did-provider-key', () => {
it('should create identifier with no params', async () => {
const did = await agent.didManagerCreate({})
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:.*/)
})

it('should create identifier with Ed25519 key', async () => {
const did = await agent.didManagerCreate({ options: { keyType: 'Ed25519' } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:z6Mk.*/)
})

it('should create identifier with X25519 key', async () => {
const did = await agent.didManagerCreate({ options: { keyType: 'X25519' } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:z6LS.*/)
})

it('should create identifier with Secp256k1 key', async () => {
const did = await agent.didManagerCreate({ options: { keyType: 'Secp256k1' } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:zQ3s.*/)
})

it('should create identifier with Ed25519 key given a private key', async () => {
const privateKeyHex = '06eb9e64569203679b36f834a4d9725c989d32a7fb52c341eae3517b3aff8ee6'
const did = await agent.didManagerCreate({ options: { keyType: 'Ed25519', privateKeyHex } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:z6Mkq3FR8bz4e3oDcbHhGAmfUUW7bdCtEL2vK2Fsw16Z99Vk$/)
})

it('should create identifier with X25519 key given a private key', async () => {
const privateKeyHex = '06eb9e64569203679b36f834a4d9725c989d32a7fb52c341eae3517b3aff8ee6'
const did = await agent.didManagerCreate({ options: { keyType: 'X25519', privateKeyHex } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:z6LSk74Z9nwqCr3M6Y2JNFEz1aQUaG2Ehnvc8XGjuK9LzbkS$/)
})

it('should create identifier with Secp256k1 key given a private key', async () => {
const privateKeyHex = '06eb9e64569203679b36f834a4d9725c989d32a7fb52c341eae3517b3aff8ee6'
const did = await agent.didManagerCreate({ options: { keyType: 'Secp256k1', privateKeyHex } })
expect(did).toBeDefined()
expect(did.did).toMatch(/^did:key:zQ3shmh97kcXoAqLZLjjc86HB5YNPGBekgFq7W7LmpEwE5mov$/)
})
})
1 change: 1 addition & 0 deletions packages/did-provider-key/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"extract-api": "node ../cli/bin/veramo.js dev extract-api"
},
"dependencies": {
"@ethersproject/signing-key": "^5.7.0",
"@transmute/did-key-ed25519": "^0.3.0-unstable.10",
"@transmute/did-key-secp256k1": "^0.3.0-unstable.10",
"@transmute/did-key-x25519": "^0.3.0-unstable.10",
Expand Down
9 changes: 5 additions & 4 deletions packages/did-provider-key/src/key-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, RequireOnly } from '@veramo/core-types'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import { hexToBytes, bytesToUtf8String } from '@veramo/utils'
import { hexToBytes } from '@veramo/utils'
import { computePublicKey } from '@ethersproject/signing-key'
import { base58btc } from 'multiformats/bases/base58'
import Multicodec from 'multicodec'

Expand Down Expand Up @@ -49,9 +50,9 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
context,
)

const methodSpecificId: string = base58btc.encode(
Multicodec.addPrefix(keyOptions[keyType], hexToBytes(key.publicKeyHex)),
)
const publicKeyHex = key.type === 'Secp256k1' ? computePublicKey('0x' + key.publicKeyHex, true) : key.publicKeyHex
const multicodecEncoded = Multicodec.addPrefix(keyOptions[keyType], hexToBytes(publicKeyHex))
const methodSpecificId: string = base58btc.encode(multicodecEncoded)

const identifier: Omit<IIdentifier, 'provider'> = {
did: 'did:key:' + methodSpecificId,
Expand Down
3 changes: 2 additions & 1 deletion packages/did-provider-key/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { DIDResolutionOptions, DIDResolutionResult, DIDResolver, ParsedDID, Reso
export const startsWithMap: Record<string, Function> = {
'did:key:z6Mk': resolveED25519,
'did:key:z6LS': resolveX25519,
'did:key:zQ3s': resolveSecp256k1,
'did:key:zQ3s': resolveSecp256k1, // compressed Secp256k1 keys
'did:key:z7r8': resolveSecp256k1, // uncompressed Secp256k1 keys
}

const resolveDidKey: DIDResolver = async (
Expand Down

0 comments on commit 9c38d4a

Please sign in to comment.