diff --git a/packages/utils/src/__tests__/did-utils.test.ts b/packages/utils/src/__tests__/did-utils.test.ts new file mode 100644 index 000000000..542d01912 --- /dev/null +++ b/packages/utils/src/__tests__/did-utils.test.ts @@ -0,0 +1,41 @@ +import { getChainIdForDidEthr, getEthereumAddress } from '../did-utils' + +describe('@veramo/utils did utils', () => { + it(`should return correct chainId for did:ethr`, () => { + expect(() => getChainIdForDidEthr({ + 'id': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', + 'type': 'EcdsaSecp256k1RecoveryMethod2020', + 'controller': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', + 'blockchainAccountId':'did:key:0x32234234234234' + })).toThrow() + expect(getChainIdForDidEthr({ + 'id': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', + 'type': 'EcdsaSecp256k1RecoveryMethod2020', + 'controller': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', + 'blockchainAccountId':'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51@eip155:1' + })).toEqual(1) + expect(getChainIdForDidEthr({ + 'id': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', + 'type': 'EcdsaSecp256k1RecoveryMethod2020', + 'controller': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', + 'blockchainAccountId':'eip155:1:did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51' + })).toEqual(1) + expect(getChainIdForDidEthr({ + 'id': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', + 'type': 'EcdsaSecp256k1RecoveryMethod2020', + 'controller': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', + 'blockchainAccountId':'did:ethr:rinkeby:0x1B54DaD834f2017ab66C1a1ffF74425889141e51@eip155:4' + })).toEqual(4) + }) + + it('should return blockchainAccountId for did:ethr', () => { + const verificationMethod = { + 'id': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51#controller', + 'type': 'EcdsaSecp256k1RecoveryMethod2020', + 'controller': 'did:ethr:0x1B54DaD834f2017ab66C1a1ffF74425889141e51', + 'blockchainAccountId': '0x1B54DaD834f2017ab66C1a1ffF74425889141e51@eip155:1' + } + + expect(getEthereumAddress(verificationMethod)).toEqual("0x1B54DaD834f2017ab66C1a1ffF74425889141e51".toLowerCase()) + }) +}) diff --git a/packages/utils/src/did-utils.ts b/packages/utils/src/did-utils.ts index 0211a9122..9bfaa46df 100644 --- a/packages/utils/src/did-utils.ts +++ b/packages/utils/src/did-utils.ts @@ -63,6 +63,27 @@ function compareBlockchainAccountId(localKey: IKey, verificationMethod: _Normali return computedAddr === vmEthAddr } +export function getEthereumAddress(verificationMethod: _NormalizedVerificationMethod) { + let vmEthAddr = verificationMethod.ethereumAddress?.toLowerCase() + if (!vmEthAddr) { + if (verificationMethod.blockchainAccountId?.includes('@eip155')) { + vmEthAddr = verificationMethod.blockchainAccountId?.split('@eip155')[0].toLowerCase() + } else if (verificationMethod.blockchainAccountId?.startsWith('eip155')) { + vmEthAddr = verificationMethod.blockchainAccountId.split(':')[2]?.toLowerCase() + } + } + return vmEthAddr +} + +export function getChainIdForDidEthr(verificationMethod: _NormalizedVerificationMethod): number { + if (verificationMethod.blockchainAccountId?.includes('@eip155')) { + return parseInt(verificationMethod.blockchainAccountId!.split(':').slice(-1)[0]) + } else if (verificationMethod.blockchainAccountId?.startsWith('eip155')) { + return parseInt(verificationMethod.blockchainAccountId!.split(':')[1]) + } + throw new Error('blockchainAccountId does not include eip155 designation') +} + export async function mapIdentifierKeysToDoc( identifier: IIdentifier, section: DIDDocumentSection = 'keyAgreement',