Skip to content

Commit

Permalink
feat(resolver): change verification method key id
Browse files Browse the repository at this point in the history
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
  • Loading branch information
beatt83 committed Feb 6, 2024
1 parent 4f6f0bb commit 1897dae
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
6 changes: 5 additions & 1 deletion Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
)
Expand Down
33 changes: 21 additions & 12 deletions Sources/PeerDID/PeerDID/PeerDIDHelper+ResolveAlgo2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
9 changes: 5 additions & 4 deletions Sources/PeerDID/PeerDID/PeerDIDHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -120,7 +120,7 @@ public extension PeerDIDHelper {
)
}

return (base.charPrefix, material)
return material
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Tests/PeerDIDTests/DecodeEcnumbasisTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .base58
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}

func testDecodeFormatBase58KeyX25519() throws {
Expand All @@ -36,7 +36,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .base58
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}

func testDecodeFormatMultibaseKeyEd25519() throws {
Expand All @@ -51,7 +51,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .multibase
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}

func testDecodeFormatMultibaseKeyX25519() throws {
Expand All @@ -66,7 +66,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .multibase
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}

func testDecodeFormatJWKKeyEd25519() throws {
Expand All @@ -89,7 +89,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .jwk
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}

func testDecodeFormatJWKKeyX25519() throws {
Expand All @@ -112,7 +112,7 @@ final class DecodeEcnumbasisTest: XCTestCase {
format: .jwk
)

XCTAssertEqual(expected, decoded.material)
XCTAssertEqual(expected, decoded)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\""))
}
Expand All @@ -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\""))
}
Expand All @@ -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\""))
}
Expand Down

0 comments on commit 1897dae

Please sign in to comment.