Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(credential-eip712): chainId for any did method #1334

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be we can also add is NAN as the parseInt result

if it is nan, then throw error, hence the Eip712 fallback to use chainId 1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

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
Loading