Skip to content

Commit

Permalink
fix(utils): get chainId for any did method (#1334)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `getChainIdForDidEthr` method has been renamed to `getChainId`
  • Loading branch information
simonas-notcat authored Jan 31, 2024
1 parent 8e3b94c commit d5375bd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
4 changes: 2 additions & 2 deletions __tests__/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -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<IResolver & IDIDManager>

Expand All @@ -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)
}
})
Expand Down
25 changes: 16 additions & 9 deletions packages/credential-eip712/src/agent/CredentialEIP712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@veramo/core-types'
import {
extractIssuer,
getChainIdForDidEthr,
getChainId,
getEthereumAddress,
intersect,
isDefined,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -145,7 +147,7 @@ export class CredentialIssuerEIP712 implements IAgentPlugin {
const verificationMessage = {
...signingInput,
proof: verifyInputProof,
}
}

const compat = {
...eip712Domain,
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 29 additions & 5 deletions packages/utils/src/__tests__/did-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
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',
blockchainAccountId: 'did:key:0x32234234234234',
}),
).toThrow()
expect(
getChainIdForDidEthr({
getChainId({
id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller',
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51',
blockchainAccountId: 'eip155:1:0x1B54DaD834f2017ab66C1a1ffF74425889141e51',
}),
).toEqual(1)
expect(
getChainIdForDidEthr({
getChainId({
id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller',
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51',
blockchainAccountId: 'eip155:1:did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51',
}),
).toEqual(1)
expect(
getChainIdForDidEthr({
getChainId({
id: 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller',
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: 'did:ethr:goerli:0x1B54DaD834f2017ab66C1a1ffF74425889141e51',
Expand All @@ -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',
Expand Down
13 changes: 10 additions & 3 deletions packages/utils/src/did-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down

0 comments on commit d5375bd

Please sign in to comment.