Skip to content

Commit

Permalink
fix(utils): bump did-jwt to 7.4.1 and reuse key conversion code from …
Browse files Browse the repository at this point in the history
…it (#1261)

fixes #1248
closes #1245
  • Loading branch information
mirceanis committed Sep 27, 2023
1 parent 5b198ec commit fb192e7
Show file tree
Hide file tree
Showing 36 changed files with 161 additions and 298 deletions.
9 changes: 6 additions & 3 deletions __tests__/localJsonStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import {
IResolver,
TAgent,
} from '../packages/core-types/src'
import {
createAgent
} from '../packages/core/src'
import { createAgent } from '../packages/core/src'
import { MessageHandler } from '../packages/message-handler/src'
import { KeyManager } from '../packages/key-manager/src'
import { DIDManager } from '../packages/did-manager/src'
Expand Down Expand Up @@ -53,6 +51,7 @@ import {
PrivateKeyStoreJson,
} from '../packages/data-store-json/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { PeerDIDProvider, getResolver as getDidPeerResolver } from '../packages/did-provider-peer/src'

import { Resolver } from 'did-resolver'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
Expand Down Expand Up @@ -162,6 +161,9 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
'did:key': new KeyDIDProvider({
defaultKms: 'local',
}),
'did:peer': new PeerDIDProvider({
defaultKms: 'local',
}),
'did:pkh': new PkhDIDProvider({
defaultKms: 'local',
}),
Expand All @@ -176,6 +178,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
...ethrDidResolver({ infuraProjectId }),
...webDidResolver(),
...getDidKeyResolver(),
...getDidPeerResolver(),
...getDidPkhResolver(),
...getDidJwkResolver(),
...new FakeDidResolver(() => agent).getDidFakeResolver(),
Expand Down
5 changes: 5 additions & 0 deletions __tests__/localMemoryStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { KeyManagementSystem } from '../packages/kms-local/src'
import { Web3KeyManagementSystem } from '../packages/kms-web3/src'
import { DataStore, DataStoreORM, Entities, migrations } from '../packages/data-store/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { PeerDIDProvider, getResolver as getDidPeerResolver } from "../packages/did-provider-peer/src";

import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
import { getResolver as webDidResolver } from 'web-did-resolver'
Expand Down Expand Up @@ -159,6 +160,9 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
'did:key': new KeyDIDProvider({
defaultKms: 'local',
}),
'did:peer': new PeerDIDProvider({
defaultKms: 'local'
}),
'did:pkh': new PkhDIDProvider({
defaultKms: 'local',
}),
Expand All @@ -172,6 +176,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
...ethrDidResolver({ infuraProjectId }),
...webDidResolver(),
...getDidKeyResolver(),
...getDidPeerResolver(),
...getDidPkhResolver(),
...getDidJwkResolver(),
...new FakeDidResolver(() => agent).getDidFakeResolver(),
Expand Down
5 changes: 5 additions & 0 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { WebDIDProvider } from '../packages/did-provider-web/src'
import { getDidKeyResolver, KeyDIDProvider } from '../packages/did-provider-key/src'
import { getDidPkhResolver, PkhDIDProvider } from '../packages/did-provider-pkh/src'
import { getDidJwkResolver, JwkDIDProvider } from '../packages/did-provider-jwk/src'
import { getResolver as getDidPeerResolver, PeerDIDProvider } from "../packages/did-provider-peer/src";
import { DIDComm, DIDCommHttpTransport, DIDCommMessageHandler, IDIDComm } from '../packages/did-comm/src'
import {
ISelectiveDisclosure,
Expand Down Expand Up @@ -189,6 +190,9 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
'did:key': new KeyDIDProvider({
defaultKms: 'local',
}),
'did:peer': new PeerDIDProvider({
defaultKms: 'local'
}),
'did:pkh': new PkhDIDProvider({
defaultKms: 'local',
}),
Expand All @@ -204,6 +208,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
...webDidResolver(),
// key: getUniversalResolver(), // resolve using remote resolver... when uniresolver becomes more stable,
...getDidKeyResolver(),
...getDidPeerResolver(),
...getDidPkhResolver(),
...getDidJwkResolver(),
...new FakeDidResolver(() => serverAgent as TAgent<IDIDManager>).getDidFakeResolver(),
Expand Down
23 changes: 23 additions & 0 deletions __tests__/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,28 @@ export default (testContext: {
`eip155:1:${account}`,
)
})

it('should verify JWT credential signed by did:peer (multibase + multicodec) (github #1248)', async () => {
// did:peer uses publicKeyMultibase
const issuer = await agent.didManagerCreate({
provider: 'did:peer',
options: {
num_algo: 0
}
})
const payload = {
issuer: issuer.did,
credentialSubject: {
nothing: 'else matters',
},
}
const credential = await agent.createVerifiableCredential({
credential: payload,
proofFormat: 'jwt',
})

const verifyResult = await agent.verifyCredential({ credential })
expect(verifyResult.verified).toBeTruthy()
})
})
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"caip": "1.1.0",
"credential-status": "2.0.6",
"cross-env": "7.0.3",
"did-jwt": "7.3.0",
"did-jwt-vc": "3.2.9",
"did-jwt": "7.4.1",
"did-jwt-vc": "3.2.10",
"did-resolver": "4.1.0",
"ethr-did-resolver": "8.1.2",
"express": "4.18.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dependencies": {
"credential-status": "^2.0.5",
"debug": "^4.3.3",
"did-jwt-vc": "^3.2.5",
"did-jwt-vc": "^3.2.10",
"did-resolver": "^4.1.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/core-types/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@
},
"TAlg": {
"type": "string",
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta.algorithms } property to see what is possible for a particular managed key."
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta | IKey.meta.algorithms } property to see what is possible for a particular managed key."
},
"ManagedKeyInfo": {
"type": "object",
Expand Down Expand Up @@ -1131,7 +1131,7 @@
},
"TAlg": {
"type": "string",
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta.algorithms } property to see what is possible for a particular managed key."
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta | IKey.meta.algorithms } property to see what is possible for a particular managed key."
},
"IDIDManagerAddServiceArgs": {
"type": "object",
Expand Down Expand Up @@ -2812,7 +2812,7 @@
},
"TAlg": {
"type": "string",
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta.algorithms } property to see what is possible for a particular managed key."
"description": "Known algorithms supported by some of the above key types defined by {@link TKeyType } .\n\nActual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems } can support more. One should check the {@link IKey.meta | IKey.meta.algorithms } property to see what is possible for a particular managed key."
},
"IService": {
"type": "object",
Expand Down
4 changes: 2 additions & 2 deletions packages/core-types/src/types/IIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export type TKeyType = 'Ed25519' | 'Secp256k1' | 'Secp256r1' | 'X25519' | 'Bls12
* Known algorithms supported by some of the above key types defined by {@link TKeyType}.
*
* Actual implementations of {@link @veramo/key-manager#AbstractKeyManagementSystem | Key Management Systems} can
* support more. One should check the {@link IKey.meta.algorithms} property to see what is possible
* for a particular managed key.
* support more. One should check the {@link IKey.meta | IKey.meta.algorithms} property to see what is possible for a
* particular managed key.
*
* @public
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/credential-ld/src/suites/Ed25519Signature2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class VeramoEd25519Signature2020 extends VeramoLdSignature {
const verificationKey = new Ed25519VerificationKey2020({
id,
controller,
publicKeyMultibase: bytesToMultibase(hexToBytes(key.publicKeyHex), 'Ed25519'),
publicKeyMultibase: bytesToMultibase(hexToBytes(key.publicKeyHex), 'base58btc', 'ed25519-pub'),
// signer: () => signer,
// type: this.getSupportedVerificationType(),
})
Expand Down Expand Up @@ -139,7 +139,7 @@ export class VeramoEd25519Signature2020 extends VeramoLdSignature {
// publicKeyMultibase is required by this suite
if (!vm.publicKeyMultibase) {
const publicKeyHex = extractPublicKeyHex(vm)
vm.publicKeyMultibase = bytesToMultibase(hexToBytes(publicKeyHex), 'Ed25519')
vm.publicKeyMultibase = bytesToMultibase(hexToBytes(publicKeyHex), 'base58btc', 'ed25519-pub')
}
}
return vm
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-status/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@veramo/core-types": "workspace:^",
"@veramo/utils": "workspace:^",
"credential-status": "^2.0.5",
"did-jwt": "^7.2.5",
"did-jwt": "^7.4.1",
"did-resolver": "^4.1.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/credential-w3c/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"@veramo/utils": "workspace:^",
"canonicalize": "^2.0.0",
"debug": "^4.3.3",
"did-jwt": "^7.2.5",
"did-jwt-vc": "^3.2.5",
"did-jwt": "^7.4.1",
"did-jwt-vc": "^3.2.10",
"did-resolver": "^4.1.0",
"uuid": "^9.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/data-store-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@veramo/key-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-jwt-vc": "^3.2.5",
"did-jwt-vc": "^3.2.10",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/data-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@veramo/key-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-jwt-vc": "^3.2.5",
"did-jwt-vc": "^3.2.10",
"typeorm": "^0.3.17",
"uuid": "^9.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/did-comm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@veramo/utils": "workspace:^",
"cross-fetch": "^4.0.0",
"debug": "^4.3.3",
"did-jwt": "^7.2.5",
"did-jwt": "^7.4.1",
"did-resolver": "^4.1.0",
"isomorphic-webcrypto": "^2.3.8",
"uuid": "^9.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/did-jwt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@veramo/core-types": "workspace:^",
"@veramo/message-handler": "workspace:^",
"debug": "^4.3.3",
"did-jwt": "^7.2.5",
"did-jwt": "^7.4.1",
"did-resolver": "^4.1.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/did-provider-key/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
"@veramo/did-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-resolver": "^4.1.0",
"multicodec": "^3.2.1",
"multiformats": "^12.0.1"
"did-resolver": "^4.1.0"
},
"devDependencies": {
"@types/debug": "4.1.8",
Expand Down
13 changes: 5 additions & 8 deletions packages/did-provider-key/src/key-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, RequireOnly } from '@veramo/core-types'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import { hexToBytes } from '@veramo/utils'
import { bytesToMultibase, hexToBytes } from '@veramo/utils'
import { computePublicKey } from '@ethersproject/signing-key'
import { base58btc } from 'multiformats/bases/base58'
import Multicodec from 'multicodec'

import Debug from 'debug'

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

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

const keyOptions = {
const keyCodecs = {
Ed25519: 'ed25519-pub',
X25519: 'x25519-pub',
Secp256k1: 'secp256k1-pub',
Expand All @@ -38,7 +36,7 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
{ kms, options }: { kms?: string; options?: CreateKeyDidOptions },
context: IContext,
): Promise<Omit<IIdentifier, 'provider'>> {
const keyType = (options?.keyType && keyOptions[options?.keyType] && options.keyType) || 'Ed25519'
const keyType = (options?.keyType && keyCodecs[options?.keyType] && options.keyType) || 'Ed25519'
const key = await this.importOrGenerateKey(
{
kms: kms || this.defaultKms,
Expand All @@ -51,8 +49,7 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
)

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 methodSpecificId: string = bytesToMultibase(hexToBytes(publicKeyHex), 'base58btc', keyCodecs[keyType])

const identifier: Omit<IIdentifier, 'provider'> = {
did: 'did:key:' + methodSpecificId,
Expand Down
4 changes: 1 addition & 3 deletions packages/did-provider-peer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"@veramo/did-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-resolver": "^4.1.0",
"multicodec": "^3.2.1",
"multiformats": "^12.0.1"
"did-resolver": "^4.1.0"
},
"devDependencies": {
"@types/debug": "4.1.8",
Expand Down
18 changes: 7 additions & 11 deletions packages/did-provider-peer/src/peer-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { IAgentContext, IIdentifier, IKey, IKeyManager, IService } from '@veramo/core-types'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import { base58btc } from 'multiformats/bases/base58'
import Multicodec from 'multicodec'
import { bytesToBase64url, hexToBytes, stringToUtf8Bytes } from '@veramo/utils'
import { bytesToBase64url, bytesToMultibase, hexToBytes, stringToUtf8Bytes } from '@veramo/utils'

import Debug from 'debug'

Expand Down Expand Up @@ -45,9 +43,7 @@ export class PeerDIDProvider extends AbstractIdentifierProvider {
): Promise<Omit<IIdentifier, 'provider'>> {
if (options.num_algo == 0) {
const key = await context.agent.keyManagerCreate({ kms: kms || this.defaultKms, type: 'Ed25519' })
const methodSpecificId = base58btc.encode(
Multicodec.addPrefix('ed25519-pub', hexToBytes(key.publicKeyHex)),
)
const methodSpecificId = bytesToMultibase(hexToBytes(key.publicKeyHex), 'base58btc', 'ed25519-pub')

const identifier: Omit<IIdentifier, 'provider'> = {
did: 'did:peer:0' + methodSpecificId,
Expand All @@ -67,12 +63,12 @@ export class PeerDIDProvider extends AbstractIdentifierProvider {
type: 'X25519',
})

const authKeyText = base58btc.encode(
Multicodec.addPrefix('ed25519-pub', hexToBytes(authKey.publicKeyHex)),
)
const authKeyText = bytesToMultibase(hexToBytes(authKey.publicKeyHex), 'base58btc', 'ed25519-pub')

const agreementKeyText = base58btc.encode(
Multicodec.addPrefix('x25519-pub', hexToBytes(agreementKey.publicKeyHex)),
const agreementKeyText = bytesToMultibase(
hexToBytes(agreementKey.publicKeyHex),
'base58btc',
'x25519-pub',
)

const ServiceEncoded = encodeService(options.service)
Expand Down
5 changes: 5 additions & 0 deletions packages/did-provider-peer/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { DIDDocument, DIDResolutionResult, DIDResolver, ParsedDID } from 'did-re
import { resolve } from '@aviarytech/did-peer'
import { IDIDDocumentServiceDescriptor } from '@aviarytech/did-peer/interfaces.js'

/**
* Creates a DID Resolver that can resolve Peer DIDs (for the 0 and 2 num_algo values)
*
* @public
*/
export function getResolver(): Record<string, DIDResolver> {
async function resolveInner(did: string, parsed: ParsedDID): Promise<DIDResolutionResult> {
const didDocumentMetadata = {}
Expand Down
5 changes: 5 additions & 0 deletions packages/did-provider-pkh/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ function toDidDoc(did: string, blockchainAccountId: string): any {
return doc;
}

/**
* Creates a DID resolver that resolves PKH DIDs
*
* @public
*/
export function getResolver(): ResolverRegistry {
return {
pkh: async (
Expand Down
2 changes: 1 addition & 1 deletion packages/key-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@veramo/core-types": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.4",
"did-jwt": "^7.2.5",
"did-jwt": "^7.4.1",
"uint8arrays": "^4.0.6",
"uuid": "^9.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kms-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@veramo/key-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-jwt": "^7.2.5"
"did-jwt": "^7.4.1"
},
"devDependencies": {
"@types/debug": "4.1.8",
Expand Down

0 comments on commit fb192e7

Please sign in to comment.