diff --git a/src/castor/resolver/PeerDIDResolver.ts b/src/castor/resolver/PeerDIDResolver.ts index 244a4bc0e..cf0e584de 100644 --- a/src/castor/resolver/PeerDIDResolver.ts +++ b/src/castor/resolver/PeerDIDResolver.ts @@ -51,7 +51,7 @@ export class PeerDIDResolver implements DIDResolver { const keyAgreementMethods: VerificationMethod[] = []; const services: DIDDocumentService[] = []; - composition.forEach((part) => { + composition.forEach((part, index) => { let decoded: [ string, VerificationMaterialAuthentication | VerificationMaterialAgreement, @@ -61,14 +61,14 @@ export class PeerDIDResolver implements DIDResolver { switch (type) { case Numalgo2Prefix.authentication: decoded = this.decodeMultibaseEncnumbasisAuth(part.slice(1), format); - authenticationMethods.push(this.getVerificationMethod(did, decoded)); + authenticationMethods.push(this.getVerificationMethod(did, decoded, index)); break; case Numalgo2Prefix.keyAgreement: decoded = this.decodeMultibaseEcnumbasisAgreement( part.slice(1), format ); - keyAgreementMethods.push(this.getVerificationMethod(did, decoded)); + keyAgreementMethods.push(this.getVerificationMethod(did, decoded, index)); break; case Numalgo2Prefix.service: services.push(...this.decodeService(did, part.slice(1))); @@ -193,14 +193,17 @@ export class PeerDIDResolver implements DIDResolver { public getVerificationMethod( did: DID, - decodedEncnumbasis: [string, VerificationMaterialPeerDID] + decodedEncnumbasis: [string, VerificationMaterialPeerDID], + index: number ): VerificationMethod { const jsonObject = JSON.parse(decodedEncnumbasis[1].value); + const keyId = "key-" + (index + 1) - jsonObject["kid"] = did.toString() + "#" + decodedEncnumbasis[0]; + // jsonObject["kid"] = did.toString() + "#" + decodedEncnumbasis[0]; //Before https://github.com/decentralized-identity/peer-did-method-spec/pull/62 + jsonObject["kid"] = did.toString() + "#" + keyId; return { - id: new DIDUrl(did, [], new Map(), decodedEncnumbasis[0]).string(), + id: new DIDUrl(did, [], new Map(), keyId).string(), controller: did.toString(), type: decodedEncnumbasis[1].keyType.value, publicKeyJwk: jsonObject, diff --git a/tests/castor/PeerDID.test.ts b/tests/castor/PeerDID.test.ts index 94a02a163..372d30b62 100644 --- a/tests/castor/PeerDID.test.ts +++ b/tests/castor/PeerDID.test.ts @@ -7,6 +7,7 @@ import { PublicKey, KeyTypes, Curve, + VerificationMethods, } from "../../src/domain"; import Apollo from "../../src/apollo/Apollo"; @@ -103,6 +104,29 @@ describe("PEERDID CreateTest", () => { expect(document.id.toString()).to.equal(mypeerDID.toString()); }); + it("Should resolver peerdid's kid of the keys correctly according to https://github.com/decentralized-identity/peer-did-method-spec/pull/62", async () => { + const mypeerDID = new DID( + "did", + "peer", + "2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0" + ); + const apollo = new Apollo(); + const castor = new Castor(apollo); + const document = await castor.resolveDID(mypeerDID.toString()); + document.coreProperties.forEach((element) => { + if (element instanceof VerificationMethods) { + expect(element.values.length).to.equal(2); + expect(element.values[0].id) + .to.equal(element.values[0].publicKeyJwk?.kid) + .to.equal("did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0#key-2"); + expect(element.values[1].id) + .to.equal(element.values[1].publicKeyJwk?.kid) + .to.equal("did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0#key-1"); + } + }); + expect(document.id.toString()).to.equal(mypeerDID.toString()); + }); + it("Create a PeerDID and verify a signature", async () => { const apollo = new Apollo(); const castor = new Castor(apollo);