Skip to content

Commit

Permalink
feat(utils): add 2 utility functions for inspecting ethr dids (#842)
Browse files Browse the repository at this point in the history
* feat(utils): Add 2 utility functions for inspecting ethr dids
  • Loading branch information
nickreynolds committed Mar 30, 2022
1 parent a39ba52 commit 473e7fa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/utils/src/__tests__/did-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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())
})
})
21 changes: 21 additions & 0 deletions packages/utils/src/did-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 473e7fa

Please sign in to comment.