Skip to content

Commit

Permalink
feat(cli): add support for did:jwk and did:peer to CLI (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis authored Jan 22, 2024
1 parent 165de35 commit 3ac343e
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 56 deletions.
10 changes: 10 additions & 0 deletions packages/cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ didResolver:
$ref: /did-key-resolver
peer:
$require: '@veramo/did-provider-peer?t=function&p=/peer#getResolver'
jwk:
$require: '@veramo/did-provider-jwk?t=function&p=/jwk#getDidJwkResolver'
pkh:
$require: '@veramo/did-provider-pkh?t=function&p=/pkh#getDidPkhResolver'
elem:
Expand Down Expand Up @@ -248,6 +250,14 @@ didManager:
$require: '@veramo/did-provider-key#KeyDIDProvider'
$args:
- defaultKms: local
did:jwk:
$require: '@veramo/did-provider-jwk#JwkDIDProvider'
$args:
- defaultKms: local
did:peer:
$require: '@veramo/did-provider-peer#PeerDIDProvider'
$args:
- defaultKms: local
did:pkh:
$require: '@veramo/did-provider-pkh#PkhDIDProvider'
$args:
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@veramo/did-manager": "workspace:^",
"@veramo/did-provider-ethr": "workspace:^",
"@veramo/did-provider-key": "workspace:^",
"@veramo/did-provider-jwk": "workspace:^",
"@veramo/did-provider-peer": "workspace:^",
"@veramo/did-provider-pkh": "workspace:^",
"@veramo/did-provider-web": "workspace:^",
Expand All @@ -61,11 +62,9 @@
"@veramo/message-handler": "workspace:^",
"@veramo/remote-client": "workspace:^",
"@veramo/remote-server": "workspace:^",
"@veramo/kv-store": "workspace:^",
"@veramo/selective-disclosure": "workspace:^",
"@veramo/url-handler": "workspace:^",
"@veramo/utils": "workspace:^",
"@veramo/mediation-manager": "workspace:^",
"blessed": "^0.1.81",
"commander": "^11.0.0",
"console-table-printer": "^2.11.2",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{ "path": "../did-jwt" },
{ "path": "../did-manager" },
{ "path": "../did-provider-ethr" },
{ "path": "../did-provider-jwk" },
{ "path": "../did-provider-key" },
{ "path": "../did-provider-peer" },
{ "path": "../did-provider-pkh" },
Expand Down
1 change: 1 addition & 0 deletions packages/did-provider-jwk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@veramo/core": "workspace:*",
"@veramo/key-manager": "workspace:*",
"@veramo/kms-local": "workspace:*",
"@veramo/did-resolver": "workspace:*",
"typescript": "5.2.2"
},
"repository": {
Expand Down
166 changes: 148 additions & 18 deletions packages/did-provider-jwk/src/__tests__/jwk.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { JwkDIDProvider } from '../jwk-did-provider'
import { IDIDManager, IKeyManager } from "@veramo/core-types";
import { getDidJwkResolver } from '../resolver'
import { IDIDManager, IKeyManager, IResolver } from '@veramo/core-types'
import { createAgent } from '@veramo/core'
import { MemoryKeyStore, MemoryPrivateKeyStore, KeyManager } from '@veramo/key-manager'
import { KeyManager, MemoryKeyStore, MemoryPrivateKeyStore } from '@veramo/key-manager'
import { KeyManagementSystem } from '@veramo/kms-local'
import { DIDManager, MemoryDIDStore } from "@veramo/did-manager";
import { DIDManager, MemoryDIDStore } from '@veramo/did-manager'
import { DIDResolverPlugin } from '@veramo/did-resolver'

const defaultKms = 'memory'
const ionDIDProvider = new JwkDIDProvider({ defaultKms })
const jwkDIDProvider = new JwkDIDProvider({ defaultKms })

const agent = createAgent<IKeyManager & IDIDManager>({
const agent = createAgent<IKeyManager & IDIDManager & IResolver>({
plugins: [
new KeyManager({
store: new MemoryKeyStore(),
Expand All @@ -18,51 +20,179 @@ const agent = createAgent<IKeyManager & IDIDManager>({
}),
new DIDManager({
providers: {
'did:jwk': ionDIDProvider,
'did:jwk': jwkDIDProvider,
},
defaultProvider: 'did:jwk',
store: new MemoryDIDStore(),
}),
new DIDResolverPlugin({
...getDidJwkResolver(),
}),
],
})
describe('create did:jwk', () => {
it('Secp256k1', async () => {
const id = await agent.didManagerCreate({
options: {
keyType: 'Secp256k1',
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d'
}
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d',
},
})
expect(id.did).toEqual('did:jwk:eyJhbGciOiJFUzI1NksiLCJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsInVzZSI6InNpZyIsIngiOiJVNV85NlJMQWxMeEl0a3llNXhzcnJzNGt4eEM4clN4N3JNN1dGZllLNVRrIiwieSI6IlNjM0pVM25yVUZWdEVjc0stckRscHNxTXRIWFVFN0x4SXdmTUxYOVVPTjQifQ')
expect(id.did).toEqual(
'did:jwk:eyJhbGciOiJFUzI1NksiLCJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsInVzZSI6InNpZyIsIngiOiJVNV85NlJMQWxMeEl0a3llNXhzcnJzNGt4eEM4clN4N3JNN1dGZllLNVRrIiwieSI6IlNjM0pVM25yVUZWdEVjc0stckRscHNxTXRIWFVFN0x4SXdmTUxYOVVPTjQifQ',
)
})

it('Ed25519', async () => {
const id = await agent.didManagerCreate({
options: {
keyType: 'Ed25519',
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d'
}
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d',
},
})
expect(id.did).toEqual('did:jwk:eyJhbGciOiJFZERTQSIsImNydiI6IkVkMjU1MTkiLCJrdHkiOiJPS1AiLCJ1c2UiOiJzaWciLCJ4IjoiTTNodVJCZnJpU3lHemlJS3pUSE5nS1djSVhuX3IxUzYxRnZBcUQyVmhSUSJ9')
expect(id.did).toEqual(
'did:jwk:eyJhbGciOiJFZERTQSIsImNydiI6IkVkMjU1MTkiLCJrdHkiOiJPS1AiLCJ1c2UiOiJzaWciLCJ4IjoiTTNodVJCZnJpU3lHemlJS3pUSE5nS1djSVhuX3IxUzYxRnZBcUQyVmhSUSJ9',
)
})

it('X25519', async () => {
const id = await agent.didManagerCreate({
options: {
keyType: 'X25519',
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d'
}
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d',
},
})
expect(id.did).toEqual('did:jwk:eyJhbGciOiJFQ0RILUVTIiwiY3J2IjoiWDI1NTE5Iiwia3R5IjoiT0tQIiwidXNlIjoiZW5jIiwieCI6IlVuNFNEWk12R2dReENiZkRBOWpwNjlyNDdvVWdsSF93eU1aRjU2THAwbU0ifQ')
expect(id.did).toEqual(
'did:jwk:eyJhbGciOiJFQ0RILUVTIiwiY3J2IjoiWDI1NTE5Iiwia3R5IjoiT0tQIiwidXNlIjoiZW5jIiwieCI6IlVuNFNEWk12R2dReENiZkRBOWpwNjlyNDdvVWdsSF93eU1aRjU2THAwbU0ifQ',
)
})

it('Secp256r1', async () => {
const id = await agent.didManagerCreate({
options: {
keyType: 'Secp256r1',
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d'
}
privateKeyHex: 'a5e81a8cd50cf5c31d5b87db3e153e2817f86de350a60edc2335f76d5c3b4e0d',
},
})
expect(id.did).toEqual(
'did:jwk:eyJhbGciOiJFUzI1NiIsImNydiI6IlAtMjU2Iiwia3R5IjoiRUMiLCJ1c2UiOiJzaWciLCJ4IjoiejhTTlNYTVgxUjZlVEt6SkdtLUE3ZWpBZkZsdURsaUhKdW9nT2FQc0REUSIsInkiOiJLUUtBTWVwTU56dHJseTB6ODI3MTg0dDRQdkFuU0lULW1MMFFsaUg1enU0In0',
)
})
})

describe('resolve did:jwk', () => {
it('should resolve Secp256k1', async () => {
const did =
'did:jwk:eyJhbGciOiJFUzI1NksiLCJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsInVzZSI6InNpZyIsIngiOiJVNV85NlJMQWxMeEl0a3llNXhzcnJzNGt4eEM4clN4N3JNN1dGZllLNVRrIiwieSI6IlNjM0pVM25yVUZWdEVjc0stckRscHNxTXRIWFVFN0x4SXdmTUxYOVVPTjQifQ'
const result = await agent.resolveDid({ didUrl: did })

expect(result.didDocument).toEqual({
id: did,
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'],
verificationMethod: [
{
id: `${did}#0`,
type: 'JsonWebKey2020',
controller: did,
publicKeyJwk: {
alg: 'ES256K',
crv: 'secp256k1',
kty: 'EC',
use: 'sig',
x: 'U5_96RLAlLxItkye5xsrrs4kxxC8rSx7rM7WFfYK5Tk',
y: 'Sc3JU3nrUFVtEcsK-rDlpsqMtHXUE7LxIwfMLX9UON4',
},
},
],
assertionMethod: [`${did}#0`],
authentication: [`${did}#0`],
capabilityInvocation: [`${did}#0`],
capabilityDelegation: [`${did}#0`],
})
})

it('should resolve P-256', async () => {
const did =
'did:jwk:eyJhbGciOiJFUzI1NiIsImNydiI6IlAtMjU2Iiwia3R5IjoiRUMiLCJ1c2UiOiJzaWciLCJ4IjoiejhTTlNYTVgxUjZlVEt6SkdtLUE3ZWpBZkZsdURsaUhKdW9nT2FQc0REUSIsInkiOiJLUUtBTWVwTU56dHJseTB6ODI3MTg0dDRQdkFuU0lULW1MMFFsaUg1enU0In0'
const result = await agent.resolveDid({ didUrl: did })

expect(result.didDocument).toEqual({
id: did,
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'],
verificationMethod: [
{
id: `${did}#0`,
type: 'JsonWebKey2020',
controller: did,
publicKeyJwk: {
alg: 'ES256',
crv: 'P-256',
kty: 'EC',
use: 'sig',
x: 'z8SNSXMX1R6eTKzJGm-A7ejAfFluDliHJuogOaPsDDQ',
y: 'KQKAMepMNztrly0z827184t4PvAnSIT-mL0QliH5zu4',
},
},
],
assertionMethod: [`${did}#0`],
authentication: [`${did}#0`],
capabilityInvocation: [`${did}#0`],
capabilityDelegation: [`${did}#0`],
})
})

it('should resolve Ed25519', async () => {
const did =
'did:jwk:eyJhbGciOiJFZERTQSIsImNydiI6IkVkMjU1MTkiLCJrdHkiOiJPS1AiLCJ1c2UiOiJzaWciLCJ4IjoiTTNodVJCZnJpU3lHemlJS3pUSE5nS1djSVhuX3IxUzYxRnZBcUQyVmhSUSJ9'
const result = await agent.resolveDid({ didUrl: did })

expect(result.didDocument).toEqual({
id: did,
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'],
verificationMethod: [
{
id: `${did}#0`,
type: 'JsonWebKey2020',
controller: did,
publicKeyJwk: {
alg: 'EdDSA',
crv: 'Ed25519',
kty: 'OKP',
use: 'sig',
x: 'M3huRBfriSyGziIKzTHNgKWcIXn_r1S61FvAqD2VhRQ',
},
},
],
assertionMethod: [`${did}#0`],
authentication: [`${did}#0`],
capabilityInvocation: [`${did}#0`],
capabilityDelegation: [`${did}#0`],
})
})

it('should resolve X25519', async () => {
const did =
'did:jwk:eyJhbGciOiJFQ0RILUVTIiwiY3J2IjoiWDI1NTE5Iiwia3R5IjoiT0tQIiwidXNlIjoiZW5jIiwieCI6IlVuNFNEWk12R2dReENiZkRBOWpwNjlyNDdvVWdsSF93eU1aRjU2THAwbU0ifQ'
const result = await agent.resolveDid({ didUrl: did })

expect(result.didDocument).toEqual({
id: did,
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'],
verificationMethod: [
{
id: `${did}#0`,
type: 'JsonWebKey2020',
controller: did,
publicKeyJwk: {
alg: 'ECDH-ES',
crv: 'X25519',
kty: 'OKP',
use: 'enc',
x: 'Un4SDZMvGgQxCbfDA9jp69r47oUglH_wyMZF56Lp0mM',
},
},
],
keyAgreement: [`${did}#0`],
})
expect(id.did).toEqual('did:jwk:eyJhbGciOiJFUzI1NiIsImNydiI6IlAtMjU2Iiwia3R5IjoiRUMiLCJ1c2UiOiJzaWciLCJ4IjoiejhTTlNYTVgxUjZlVEt6SkdtLUE3ZWpBZkZsdURsaUhKdW9nT2FQc0REUSIsInkiOiJLUUtBTWVwTU56dHJseTB6ODI3MTg0dDRQdkFuU0lULW1MMFFsaUg1enU0In0')
})
})
3 changes: 3 additions & 0 deletions packages/did-provider-jwk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
{
"path": "../did-manager"
},
{
"path": "../did-resolver"
},
{
"path": "../key-manager"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/did-provider-peer/src/peer-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export class PeerDIDProvider extends AbstractIdentifierProvider {
}

async createIdentifier(
{ kms, options }: { kms?: string; options?: any },
{ kms, options = { num_algo: 0 } }: { kms?: string; options?: { num_algo: number; service?: any } },
context: IContext,
): Promise<Omit<IIdentifier, 'provider'>> {
options.num_algo = options?.num_algo ?? 0
if (options.num_algo == 0) {
const key = await context.agent.keyManagerCreate({ kms: kms || this.defaultKms, type: 'Ed25519' })
const methodSpecificId = bytesToMultibase(hexToBytes(key.publicKeyHex), 'base58btc', 'ed25519-pub')
Expand Down
42 changes: 7 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ac343e

Please sign in to comment.