From 7e9c4b6641c9d146311f9ad51886fa3fac282327 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Tue, 30 Jan 2024 15:17:28 +0200 Subject: [PATCH 1/2] fix(credential-eip712): chainId for any did method --- __tests__/shared/utils.ts | 4 +-- .../src/agent/CredentialEIP712.ts | 25 ++++++++++++------- .../utils/src/__tests__/did-utils.test.ts | 21 ++++++++++++---- packages/utils/src/did-utils.ts | 2 +- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/__tests__/shared/utils.ts b/__tests__/shared/utils.ts index fdf3c9c97..c474572da 100644 --- a/__tests__/shared/utils.ts +++ b/__tests__/shared/utils.ts @@ -1,7 +1,7 @@ // noinspection ES6PreferShortImport import { IAgentOptions, IDIDManager, IResolver, MinimalImportableKey, TAgent } from '../../packages/core-types/src' -import { getChainIdForDidEthr, mapIdentifierKeysToDoc, resolveDidOrThrow } from '../../packages/utils/src' +import { getChainId, mapIdentifierKeysToDoc, resolveDidOrThrow } from '../../packages/utils/src' type ConfiguredAgent = TAgent @@ -24,7 +24,7 @@ export default (testContext: { const didUrl = 'did:ethr:mainnet:0xb09b66026ba5909a7cfe99b76875431d2b8d5190' const didDoc = await resolveDidOrThrow(didUrl, { agent }) if (didDoc.verificationMethod) { - const chainId = getChainIdForDidEthr(didDoc.verificationMethod[0]) + const chainId = getChainId(didDoc.verificationMethod[0]) expect(chainId).toEqual(1) } }) diff --git a/packages/credential-eip712/src/agent/CredentialEIP712.ts b/packages/credential-eip712/src/agent/CredentialEIP712.ts index 9a86bf527..950b5083e 100644 --- a/packages/credential-eip712/src/agent/CredentialEIP712.ts +++ b/packages/credential-eip712/src/agent/CredentialEIP712.ts @@ -9,7 +9,7 @@ import { } from '@veramo/core-types' import { extractIssuer, - getChainIdForDidEthr, + getChainId, getEthereumAddress, intersect, isDefined, @@ -89,10 +89,12 @@ export class CredentialIssuerEIP712 implements IAgentPlugin { if (!extendedKey) throw Error('key_not_found: The signing key is not available in the issuer DID document') - let chainId = 1 - if (identifier.did.split(':')[1] === 'ethr') - chainId = getChainIdForDidEthr(extendedKey.meta.verificationMethod) - + let chainId + try { + chainId = getChainId(extendedKey.meta.verificationMethod) + } catch (e) { + chainId = 1 + } const credential: CredentialPayload = { ...args?.credential, '@context': credentialContext, @@ -145,7 +147,7 @@ export class CredentialIssuerEIP712 implements IAgentPlugin { const verificationMessage = { ...signingInput, proof: verifyInputProof, - } + } const compat = { ...eip712Domain, @@ -253,9 +255,14 @@ export class CredentialIssuerEIP712 implements IAgentPlugin { const extendedKey = extendedKeys.find((key) => key.kid === keyRef) if (!extendedKey) throw Error('key_not_found: The signing key is not available in the issuer DID document') - let chainId = 1 - if (identifier.did.split(':')[1] === 'ethr') - chainId = getChainIdForDidEthr(extendedKey.meta.verificationMethod) + + let chainId + try { + chainId = getChainId(extendedKey.meta.verificationMethod) + } catch (e) { + chainId = 1 + } + presentation['proof'] = { verificationMethod: extendedKey.meta.verificationMethod.id, created: issuanceDate, diff --git a/packages/utils/src/__tests__/did-utils.test.ts b/packages/utils/src/__tests__/did-utils.test.ts index 5e2967df5..3f88eef4a 100644 --- a/packages/utils/src/__tests__/did-utils.test.ts +++ b/packages/utils/src/__tests__/did-utils.test.ts @@ -1,10 +1,10 @@ -import { extractPublicKeyHex, getChainIdForDidEthr, getEthereumAddress } from '../did-utils.js' +import { extractPublicKeyHex, getChainId, getEthereumAddress } from '../did-utils.js' import { bytesToMultibase, hexToBytes } from '../encodings.js' describe('@veramo/utils did utils', () => { it(`should return correct chainId for did:ethr`, () => { expect(() => - getChainIdForDidEthr({ + getChainId({ id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', type: 'EcdsaSecp256k1RecoveryMethod2020', controller: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', @@ -12,7 +12,7 @@ describe('@veramo/utils did utils', () => { }), ).toThrow() expect( - getChainIdForDidEthr({ + getChainId({ id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', type: 'EcdsaSecp256k1RecoveryMethod2020', controller: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', @@ -20,7 +20,7 @@ describe('@veramo/utils did utils', () => { }), ).toEqual(1) expect( - getChainIdForDidEthr({ + getChainId({ id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', type: 'EcdsaSecp256k1RecoveryMethod2020', controller: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', @@ -28,7 +28,7 @@ describe('@veramo/utils did utils', () => { }), ).toEqual(1) expect( - getChainIdForDidEthr({ + getChainId({ id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', type: 'EcdsaSecp256k1RecoveryMethod2020', controller: 'did:ethr:goerli:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', @@ -37,6 +37,17 @@ describe('@veramo/utils did utils', () => { ).toEqual(5) }) + it('should return correct chainId for did:pkh', () => { + expect( + getChainId({ + "id": "did:pkh:eip155:59144:0x19711CD19e609FEBdBF607960220898268B7E24b#blockchainAccountId", + "type": "EcdsaSecp256k1RecoveryMethod2020", + "controller": "did:pkh:eip155:59144:0x19711CD19e609FEBdBF607960220898268B7E24b", + "blockchainAccountId": "eip155:59144:0x19711CD19e609FEBdBF607960220898268B7E24b" + }), + ).toEqual(59144) + }) + it('should return blockchainAccountId for did:ethr', () => { const verificationMethod = { id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', diff --git a/packages/utils/src/did-utils.ts b/packages/utils/src/did-utils.ts index baadf6874..aece5b331 100644 --- a/packages/utils/src/did-utils.ts +++ b/packages/utils/src/did-utils.ts @@ -161,7 +161,7 @@ export function getEthereumAddress(verificationMethod: VerificationMethod): stri * * @beta This API may change without a BREAKING CHANGE notice. */ -export function getChainIdForDidEthr(verificationMethod: _NormalizedVerificationMethod): number { +export function getChainId(verificationMethod: _NormalizedVerificationMethod): number { if (verificationMethod.blockchainAccountId?.includes('@eip155')) { return parseInt(verificationMethod.blockchainAccountId!.split(':').slice(-1)[0]) } else if (verificationMethod.blockchainAccountId?.startsWith('eip155')) { From 6421891b6de13136a5cf610a6a998739270aa70d Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Wed, 31 Jan 2024 14:48:28 +0200 Subject: [PATCH 2/2] fix(utils): handle NaN chainId --- packages/utils/src/__tests__/did-utils.test.ts | 13 +++++++++++++ packages/utils/src/did-utils.ts | 11 +++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/__tests__/did-utils.test.ts b/packages/utils/src/__tests__/did-utils.test.ts index 3f88eef4a..17958ae23 100644 --- a/packages/utils/src/__tests__/did-utils.test.ts +++ b/packages/utils/src/__tests__/did-utils.test.ts @@ -48,6 +48,19 @@ describe('@veramo/utils did utils', () => { ).toEqual(59144) }) + + it('should throw on invalid chainId', () => { + expect( () => { + getChainId({ + "id": "did:pkh:eip155:59144:0x19711CD19e609FEBdBF607960220898268B7E24b#blockchainAccountId", + "type": "EcdsaSecp256k1RecoveryMethod2020", + "controller": "did:pkh:eip155:59144:0x19711CD19e609FEBdBF607960220898268B7E24b", + "blockchainAccountId": "eip155:linea:0x19711CD19e609FEBdBF607960220898268B7E24b" + }) + }).toThrowError("chainId is not a number") + }) + + it('should return blockchainAccountId for did:ethr', () => { const verificationMethod = { id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', diff --git a/packages/utils/src/did-utils.ts b/packages/utils/src/did-utils.ts index aece5b331..6d84a0759 100644 --- a/packages/utils/src/did-utils.ts +++ b/packages/utils/src/did-utils.ts @@ -162,10 +162,17 @@ export function getEthereumAddress(verificationMethod: VerificationMethod): stri * @beta This API may change without a BREAKING CHANGE notice. */ export function getChainId(verificationMethod: _NormalizedVerificationMethod): number { + let result if (verificationMethod.blockchainAccountId?.includes('@eip155')) { - return parseInt(verificationMethod.blockchainAccountId!.split(':').slice(-1)[0]) + result = parseInt(verificationMethod.blockchainAccountId!.split(':').slice(-1)[0]) } else if (verificationMethod.blockchainAccountId?.startsWith('eip155')) { - return parseInt(verificationMethod.blockchainAccountId!.split(':')[1]) + result = parseInt(verificationMethod.blockchainAccountId!.split(':')[1]) + } + if (!Number.isInteger(result)) { + throw new Error('chainId is not a number') + } + if (result) { + return result } throw new Error('blockchainAccountId does not include eip155 designation') }