From d5375bd03e814a2d3fcd96274607f880db77fbe3 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Wed, 31 Jan 2024 18:38:23 +0200 Subject: [PATCH] fix(utils): get chainId for any did method (#1334) BREAKING CHANGE: The `getChainIdForDidEthr` method has been renamed to `getChainId` --- __tests__/shared/utils.ts | 4 +-- .../src/agent/CredentialEIP712.ts | 25 +++++++++----- .../utils/src/__tests__/did-utils.test.ts | 34 ++++++++++++++++--- packages/utils/src/did-utils.ts | 13 +++++-- 4 files changed, 57 insertions(+), 19 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..17958ae23 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,30 @@ 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 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 baadf6874..6d84a0759 100644 --- a/packages/utils/src/did-utils.ts +++ b/packages/utils/src/did-utils.ts @@ -161,11 +161,18 @@ 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 { + 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') }