Skip to content

Commit 6a1c699

Browse files
authored
Merge pull request #6 from Bhutan-NDI/refactor/did-resolution
refactor: publicKeyHex to publicKeyBase58
2 parents 6c30630 + 11da38a commit 6a1c699

File tree

7 files changed

+76
-86
lines changed

7 files changed

+76
-86
lines changed

.changeset/blue-hounds-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@bhutan-ndi/ethr-credo-module': patch
3+
---
4+
5+
Refactor publicKeyHex to publicKeyBase58

patches/@credo-ts+core+0.5.3.patch

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/dids/EthrDidResolver.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export class EthereumDidResolver implements DidResolver {
2121

2222
const didDoc = JsonTransformer.fromJSON(didDocument, DidDocument)
2323

24-
didDoc.context = [
25-
// 'https://www.w3.org/ns/did/v1',
26-
// 'https://w3id.org/security/suites/secp256k1-2019/v1',
27-
'https://w3id.org/security/v3-unstable',
28-
]
2924
return {
3025
didDocument: didDoc,
3126
didDocumentMetadata,

src/ledger/EthereumLedgerService.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,57 @@ export class EthereumLedgerService {
228228

229229
const blockchainAccountId = getPreferredKey(didRecord.didDocument.verificationMethod)
230230

231-
const keyObj = didRecord.didDocument.verificationMethod.find((obj) => obj.publicKeyHex)
231+
// Look for publicKeyBase58 (new format) or fallback to publicKeyHex (legacy)
232+
const keyObj = didRecord.didDocument.verificationMethod.find((obj) => obj.publicKeyBase58 || obj.publicKeyHex)
232233

233-
if (!keyObj || !keyObj.publicKeyHex) {
234-
throw new CredoError('Public Key hex not found in wallet for did: ' + did)
234+
if (!keyObj) {
235+
throw new CredoError('Public Key not found in wallet for did: ' + did)
235236
}
236237

237-
const publicKey = TypedArrayEncoder.fromHex(keyObj.publicKeyHex)
238+
let publicKeyBase58: string
238239

239-
const publicKeyBase58 = TypedArrayEncoder.toBase58(publicKey)
240+
if (keyObj.publicKeyBase58) {
241+
publicKeyBase58 = keyObj.publicKeyBase58
242+
} else if (keyObj.publicKeyHex) {
243+
// Legacy support: convert hex to base58
244+
const publicKey = TypedArrayEncoder.fromHex(keyObj.publicKeyHex)
245+
publicKeyBase58 = TypedArrayEncoder.toBase58(publicKey)
246+
} else {
247+
throw new CredoError('Public Key not found in wallet for did: ' + did)
248+
}
240249

241250
return { publicKeyBase58, blockchainAccountId }
242251
}
243252

244253
public async resolveDID(did: string) {
245-
return await this.resolver.resolve(did)
254+
const result = await this.resolver.resolve(did)
255+
256+
// Update context to include secp256k1-2019/v1
257+
if (result.didDocument) {
258+
result.didDocument['@context'] = [
259+
'https://www.w3.org/ns/did/v1',
260+
'https://w3id.org/security/suites/secp256k1recovery-2020/v2',
261+
'https://w3id.org/security/suites/secp256k1-2019/v1',
262+
]
263+
264+
// Transform verification methods from publicKeyHex to publicKeyBase58
265+
if (result.didDocument.verificationMethod) {
266+
result.didDocument.verificationMethod = result.didDocument.verificationMethod.map((vm) => {
267+
if (vm.publicKeyHex) {
268+
const publicKey = TypedArrayEncoder.fromHex(vm.publicKeyHex)
269+
const publicKeyBase58 = TypedArrayEncoder.toBase58(publicKey)
270+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
271+
const { publicKeyHex, ...rest } = vm
272+
return {
273+
...rest,
274+
publicKeyBase58,
275+
}
276+
}
277+
return vm
278+
})
279+
}
280+
}
281+
282+
return result
246283
}
247284
}

src/signature-suites/EcdsaSecp256k1Signature2019.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
21
import type { DocumentLoader, JwsLinkedDataSignatureOptions, Proof } from '@credo-ts/core'
32
import type { JsonLdDoc } from '@credo-ts/core/build/modules/vc/data-integrity/jsonldUtil'
43

@@ -62,27 +61,29 @@ export class EcdsaSecp256k1Signature2019 extends JwsLinkedDataSignature {
6261
}
6362

6463
public async assertVerificationMethod(document: JsonLdDoc) {
65-
// if (!_includesCompatibleContext({ document: document })) {
66-
// // For DID Documents, since keys do not have their own contexts,
67-
// // the suite context is usually provided by the documentLoader logic
68-
// throw new TypeError(
69-
// `The '@context' of the verification method (key) MUST contain the context url "${this.contextUrl}".`
70-
// )
71-
// }
72-
// if (!_isSecp256k12019Key(document)) {
73-
// const verificationMethodType = jsonld.getValues(document, 'type')[0]
74-
// throw new Error(
75-
// `Unsupported verification method type '${verificationMethodType}'. Verification method type MUST be 'EcdsaSecp256k1VerificationKey2019'.`
76-
// )
77-
// } else if (_isSecp256k12019Key(document) && !_includesSecp256k12019Context(document)) {
78-
// throw new Error(
79-
// `For verification method type 'EcdsaSecp256k1VerificationKey2019' the '@context' MUST contain the context url "${SECURITY_CONTEXT_SECP256k1_URL}".`
80-
// )
81-
// }
82-
// // ensure verification method has not been revoked
83-
// if (document.revoked !== undefined) {
84-
// throw new Error('The verification method has been revoked.')
85-
// }
64+
if (!_includesCompatibleContext({ document: document })) {
65+
// For DID Documents, since keys do not have their own contexts,
66+
// the suite context is usually provided by the documentLoader logic
67+
throw new TypeError(
68+
`The '@context' of the verification method (key) MUST contain the context url "${this.contextUrl}".`
69+
)
70+
}
71+
72+
if (!_isSecp256k12019Key(document)) {
73+
const verificationMethodType = jsonld.getValues(document, 'type')[0]
74+
throw new Error(
75+
`Unsupported verification method type '${verificationMethodType}'. Verification method type MUST be 'EcdsaSecp256k1VerificationKey2019'.`
76+
)
77+
} else if (_isSecp256k12019Key(document) && !_includesSecp256k12019Context(document)) {
78+
throw new Error(
79+
`For verification method type 'EcdsaSecp256k1VerificationKey2019' the '@context' MUST contain the context url "${SECURITY_CONTEXT_SECP256k1_URL}".`
80+
)
81+
}
82+
83+
// ensure verification method has not been revoked
84+
if (document.revoked !== undefined) {
85+
throw new Error('The verification method has been revoked.')
86+
}
8687
}
8788

8889
public async getVerificationMethod(options: { proof: Proof; documentLoader?: DocumentLoader }) {

src/utils/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function parseAddress(address: string) {
7676
export function getPreferredKey(methods: VerificationMethod[]): string {
7777
for (const m of methods) {
7878
if (m.blockchainAccountId) return m.blockchainAccountId
79+
if (m.publicKeyBase58) return m.publicKeyBase58
7980
if (m.publicKeyHex) return m.publicKeyHex
8081
}
8182

tests/fixtures.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ export const EthereumDIDFixtures = {
185185
VALID_DID_DOCUMENT: {
186186
didDocument: {
187187
'@context': [
188-
// 'https://www.w3.org/ns/did/v1',
189-
// 'https://w3id.org/security/suites/secp256k1recovery-2020/v2',
190-
'https://w3id.org/security/v3-unstable',
188+
'https://www.w3.org/ns/did/v1',
189+
'https://w3id.org/security/suites/secp256k1recovery-2020/v2',
190+
'https://w3id.org/security/suites/secp256k1-2019/v1',
191191
],
192192
id: 'did:ethr:sepolia:0x022527341df022c9b898999cf6035ed3addca5d30e703028deeb4408f890f3baca',
193193
verificationMethod: [
@@ -201,7 +201,7 @@ export const EthereumDIDFixtures = {
201201
id: 'did:ethr:sepolia:0x022527341df022c9b898999cf6035ed3addca5d30e703028deeb4408f890f3baca#controllerKey',
202202
type: 'EcdsaSecp256k1VerificationKey2019',
203203
controller: 'did:ethr:sepolia:0x022527341df022c9b898999cf6035ed3addca5d30e703028deeb4408f890f3baca',
204-
publicKeyHex: '022527341df022c9b898999cf6035ed3addca5d30e703028deeb4408f890f3baca',
204+
publicKeyBase58: 'dxfVszfXXYwayBLxAijdcdnkSG9Hw1w3fxLcHqobxUj3',
205205
},
206206
],
207207
authentication: [

0 commit comments

Comments
 (0)