From 1897daef0943fbccf962fcd0831f482e94d13fee Mon Sep 17 00:00:00 2001 From: Goncalo Frade Date: Tue, 6 Feb 2024 23:16:17 +0000 Subject: [PATCH] feat(resolver): change verification method key id Specs changed this will make take that in consideration. Instead of using ecnumbasis as the id, it will use a increasing count key-1, key-2, etc --- .../PeerDID/PeerDIDHelper+ResolveAlgo0.swift | 6 +++- .../PeerDID/PeerDIDHelper+ResolveAlgo2.swift | 33 ++++++++++++------- Sources/PeerDID/PeerDID/PeerDIDHelper.swift | 9 ++--- .../PeerDIDTests/DecodeEcnumbasisTests.swift | 12 +++---- ...s.swift => ResolvePeerDIDAlgo2Tests.swift} | 18 +++++----- 5 files changed, 46 insertions(+), 32 deletions(-) rename Tests/PeerDIDTests/{ReselvePeerDIDAlgo2Tests.swift => ResolvePeerDIDAlgo2Tests.swift} (91%) diff --git a/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo0.swift b/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo0.swift index d25b5a1..a53c493 100644 --- a/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo0.swift +++ b/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo0.swift @@ -16,7 +16,11 @@ extension PeerDIDHelper { return DIDDocument( id: peerDID.string, verificationMethods: [ - try .init(did: peerDID.string, ecnumbasis: keyStr, material: decoded.material) + try .init( + did: peerDID.string, + id: keyStr, + material: decoded + ) ], services: [] ) diff --git a/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo2.swift b/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo2.swift index 4af1485..379a18e 100644 --- a/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo2.swift +++ b/Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo2.swift @@ -15,21 +15,30 @@ extension PeerDIDHelper { let agreementKeys = peerDID.algo2KeyAgreementKeys let service = peerDID.algo2Service + var keyIdCount = 1 let authenticationVerificationMethods = try authenticationKeys - .map { (try decodeMultibaseEcnumbasis(ecnumbasis: $0, format: format), $0) } - .map { try DIDDocument.VerificationMethod( - did: peerDID.string, - ecnumbasis: String($1.dropFirst()), - material: $0.material - ) } + .map { try decodeMultibaseEcnumbasis(ecnumbasis: $0, format: format) } + .map { + let method = try DIDDocument.VerificationMethod( + did: peerDID.string, + id: "key-\(keyIdCount)", + material: $0 + ) + keyIdCount+=1 + return method + } let agreementVerificationMethods = try agreementKeys - .map { (try decodeMultibaseEcnumbasis(ecnumbasis: $0, format: format), $0) } - .map { try DIDDocument.VerificationMethod( - did: peerDID.string, - ecnumbasis: String($1.dropFirst()), - material: $0.material - ) } + .map { try decodeMultibaseEcnumbasis(ecnumbasis: $0, format: format) } + .map { + let method = try DIDDocument.VerificationMethod( + did: peerDID.string, + id: "key-\(keyIdCount)", + material: $0 + ) + keyIdCount+=1 + return method + } let documentService = try service.map { try decodedPeerDIDService(did: peerDID.string, serviceString: $0) } diff --git a/Sources/PeerDID/PeerDID/PeerDIDHelper.swift b/Sources/PeerDID/PeerDID/PeerDIDHelper.swift index 63f733b..0478a6b 100644 --- a/Sources/PeerDID/PeerDID/PeerDIDHelper.swift +++ b/Sources/PeerDID/PeerDID/PeerDIDHelper.swift @@ -71,7 +71,7 @@ public extension PeerDIDHelper { func decodeMultibaseEcnumbasis( ecnumbasis: String, format: VerificationMaterialFormat - ) throws -> (ecnumbasis: String, material: PeerDIDVerificationMaterial) { + ) throws -> PeerDIDVerificationMaterial { let (base, decodedMultibase) = try BaseEncoding.decode(ecnumbasis) let (codec, decodedMulticodec) = try Multicodec().fromMulticodec(value: decodedMultibase) @@ -120,7 +120,7 @@ public extension PeerDIDHelper { ) } - return (base.charPrefix, material) + return material } } @@ -129,6 +129,7 @@ public extension PeerDIDHelper { extension PeerDIDHelper { struct PeerDIDService: Codable { + let t: String // Type let s: String // Service Endpoint let r: [String]? // Routing keys @@ -273,9 +274,9 @@ private extension Data { extension DIDDocument.VerificationMethod { - init(did: String, ecnumbasis: String, material: PeerDIDVerificationMaterial) throws { + init(did: String, id: String, material: PeerDIDVerificationMaterial) throws { self.init( - id: did + "#\(ecnumbasis)", + id: did + "#\(id)", controller: did, type: material.type.rawValue, material: .init(format: material.format, value: material.value) diff --git a/Tests/PeerDIDTests/DecodeEcnumbasisTests.swift b/Tests/PeerDIDTests/DecodeEcnumbasisTests.swift index b3a59d5..766b642 100644 --- a/Tests/PeerDIDTests/DecodeEcnumbasisTests.swift +++ b/Tests/PeerDIDTests/DecodeEcnumbasisTests.swift @@ -21,7 +21,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .base58 ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } func testDecodeFormatBase58KeyX25519() throws { @@ -36,7 +36,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .base58 ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } func testDecodeFormatMultibaseKeyEd25519() throws { @@ -51,7 +51,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .multibase ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } func testDecodeFormatMultibaseKeyX25519() throws { @@ -66,7 +66,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .multibase ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } func testDecodeFormatJWKKeyEd25519() throws { @@ -89,7 +89,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .jwk ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } func testDecodeFormatJWKKeyX25519() throws { @@ -112,7 +112,7 @@ final class DecodeEcnumbasisTest: XCTestCase { format: .jwk ) - XCTAssertEqual(expected, decoded.material) + XCTAssertEqual(expected, decoded) } } diff --git a/Tests/PeerDIDTests/ReselvePeerDIDAlgo2Tests.swift b/Tests/PeerDIDTests/ResolvePeerDIDAlgo2Tests.swift similarity index 91% rename from Tests/PeerDIDTests/ReselvePeerDIDAlgo2Tests.swift rename to Tests/PeerDIDTests/ResolvePeerDIDAlgo2Tests.swift index fbf7279..e3e6983 100644 --- a/Tests/PeerDIDTests/ReselvePeerDIDAlgo2Tests.swift +++ b/Tests/PeerDIDTests/ResolvePeerDIDAlgo2Tests.swift @@ -32,9 +32,9 @@ final class ReselvePeerDIDAlgo2Tests: XCTestCase { XCTAssertTrue(encodedDocument.contains("\"publicKeyBase58\":\"ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7\"")) XCTAssertTrue(encodedDocument.contains("\"publicKeyBase58\":\"3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J\"")) XCTAssertTrue(encodedDocument.contains("\"publicKeyBase58\":\"JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-1\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-2\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-3\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"Ed25519VerificationKey2018\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"X25519KeyAgreementKey2019\"")) } @@ -51,9 +51,9 @@ final class ReselvePeerDIDAlgo2Tests: XCTestCase { XCTAssertTrue(encodedDocument.contains("\"publicKeyJwk\":{\"crv\":\"Ed25519\",\"kty\":\"OKP\",\"x\":\"owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA\"}")) XCTAssertTrue(encodedDocument.contains("\"publicKeyJwk\":{\"crv\":\"Ed25519\",\"kty\":\"OKP\",\"x\":\"Itv8B__b1-Jos3LCpUe8EdTFGTCa_Dza6_3848P3R70\"}")) XCTAssertTrue(encodedDocument.contains("\"publicKeyJwk\":{\"crv\":\"X25519\",\"kty\":\"OKP\",\"x\":\"BIiFcQEn3dfvB2pjlhOQQour6jXy9d5s2FKEJNTOJik\"}")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-1\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-2\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-3\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"JsonWebKey2020\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"JsonWebKey2020\"")) } @@ -70,9 +70,9 @@ final class ReselvePeerDIDAlgo2Tests: XCTestCase { XCTAssertTrue(encodedDocument.contains("\"publicKeyMultibase\":\"z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\"")) XCTAssertTrue(encodedDocument.contains("\"publicKeyMultibase\":\"z6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\"")) XCTAssertTrue(encodedDocument.contains("\"publicKeyMultibase\":\"z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\"")) - XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-1\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-2\"")) + XCTAssertTrue(encodedDocument.contains("\"id\":\"\(validDid)#key-3\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"Ed25519VerificationKey2020\"")) XCTAssertTrue(encodedDocument.contains("\"type\":\"X25519KeyAgreementKey2020\"")) }