Skip to content

Commit

Permalink
fix: key's id name according to the DID Peer new specs #126
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Feb 6, 2024
1 parent 4770233 commit 1b4e6bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/castor/resolver/PeerDIDResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)));
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions tests/castor/PeerDID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PublicKey,
KeyTypes,
Curve,
VerificationMethods,
} from "../../src/domain";
import Apollo from "../../src/apollo/Apollo";

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1b4e6bd

Please sign in to comment.