From 3ec399adc6e908f421cdae671659d938ae2747d2 Mon Sep 17 00:00:00 2001 From: goncalo-frade-iohk Date: Tue, 13 Aug 2024 18:35:24 +0100 Subject: [PATCH] feat(castor): add capacity so you can create and resolve prism dids with ed25519 and x25519 keys Fixes ATL-7160 Signed-off-by: goncalo-frade-iohk --- .../DID/PrismDID/PrismDIDPublicKey.swift | 14 +++--- .../Operations/CreatePrismDIDOperation.swift | 5 +++ .../Castor/Tests/PrismDIDPublicKeyTests.swift | 5 ++- .../EdgeAgent+DIDHigherFucntions.swift | 44 +++++++++++-------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift b/EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift index 1c797fa0..11aa4218 100644 --- a/EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift +++ b/EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift @@ -61,12 +61,14 @@ struct PrismDIDPublicKey { let apollo: Apollo let id: String + let curve: String let usage: Usage let keyData: PublicKey - init(apollo: Apollo, id: String, usage: Usage, keyData: PublicKey) { + init(apollo: Apollo, id: String, curve: String, usage: Usage, keyData: PublicKey) { self.apollo = apollo self.id = id + self.curve = curve self.usage = usage self.keyData = keyData } @@ -77,20 +79,22 @@ struct PrismDIDPublicKey { usage = proto.usage.fromProto() switch proto.keyData { case let .ecKeyData(value): + curve = value.curve.lowercased() keyData = try apollo.createPublicKey(parameters: [ KeyProperties.type.rawValue: "EC", - KeyProperties.curve.rawValue: "secp256k1", + KeyProperties.curve.rawValue: value.curve.lowercased(), KeyProperties.curvePointX.rawValue: value.x.base64EncodedString(), KeyProperties.curvePointY.rawValue: value.y.base64EncodedString() ]) case let .compressedEcKeyData(value): + curve = value.curve.lowercased() keyData = try apollo.createPublicKey(parameters: [ KeyProperties.type.rawValue: "EC", - KeyProperties.curve.rawValue: "secp256k1", + KeyProperties.curve.rawValue: value.curve.lowercased(), KeyProperties.rawKey.rawValue: value.data.base64EncodedString() ]) default: - throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "secp256k1") + throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "") } } @@ -112,7 +116,7 @@ struct PrismDIDPublicKey { var protoEC = Io_Iohk_Atala_Prism_Protos_ECKeyData() protoEC.x = pointX protoEC.y = pointY - protoEC.curve = "secp256k1" + protoEC.curve = curve protoKey.keyData = .ecKeyData(protoEC) return protoKey } diff --git a/EdgeAgentSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift b/EdgeAgentSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift index 92592480..2255c1f4 100644 --- a/EdgeAgentSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift +++ b/EdgeAgentSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift @@ -10,16 +10,21 @@ struct CreatePrismDIDOperation { func compute() throws -> DID { var operation = Io_Iohk_Atala_Prism_Protos_AtalaOperation() + guard let masterKeyCurve = masterPublicKey.getProperty(.curve) else { + throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "no curve") + } operation.createDid = try createDIDAtalaOperation( publicKeys: [PrismDIDPublicKey( apollo: apollo, id: PrismDIDPublicKey.Usage.authenticationKey.defaultId, + curve: masterKeyCurve, usage: .authenticationKey, keyData: masterPublicKey ), PrismDIDPublicKey( apollo: apollo, id: PrismDIDPublicKey.Usage.masterKey.defaultId, + curve: masterKeyCurve, usage: .masterKey, keyData: masterPublicKey )], diff --git a/EdgeAgentSDK/Castor/Tests/PrismDIDPublicKeyTests.swift b/EdgeAgentSDK/Castor/Tests/PrismDIDPublicKeyTests.swift index 31d78b72..82557af3 100644 --- a/EdgeAgentSDK/Castor/Tests/PrismDIDPublicKeyTests.swift +++ b/EdgeAgentSDK/Castor/Tests/PrismDIDPublicKeyTests.swift @@ -12,7 +12,7 @@ final class PrismDIDPublicKeyTests: XCTestCase { override func setUp() async throws { apollo = ApolloImpl() seed = apollo.createRandomSeed().seed - privateKey = try await apollo.createPrivateKey(parameters: [ + privateKey = try apollo.createPrivateKey(parameters: [ KeyProperties.type.rawValue: "EC", KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue, KeyProperties.seed.rawValue: seed.value.base64Encoded(), @@ -23,7 +23,8 @@ final class PrismDIDPublicKeyTests: XCTestCase { func testFromProto() throws { let publicKey = PrismDIDPublicKey( apollo: apollo, - id: PrismDIDPublicKey.Usage.masterKey.id(index: 0), + id: PrismDIDPublicKey.Usage.masterKey.id(index: 0), + curve: "secp256k1", usage: .masterKey, keyData: privateKey.publicKey() ) diff --git a/EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift b/EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift index 2921a561..45a65724 100644 --- a/EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift +++ b/EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift @@ -60,6 +60,7 @@ Could not find key in storage please use Castor instead and provide the private /// - services: an array of services associated to the DID /// - Returns: The new created DID func createNewPrismDID( + masterPrivateKey: PrivateKey? = nil, keyPathIndex: Int? = nil, alias: String? = nil, services: [DIDDocument.Service] = [] @@ -68,31 +69,38 @@ Could not find key in storage please use Castor instead and provide the private let apollo = self.apollo let castor = self.castor - let lastKeyPairIndex = try await pluto - .getPrismLastKeyPairIndex() - .first() - .await() + let usingPrivateKey: PrivateKey - // If the user provided a key path index use it, if not use the last + 1 - let index = keyPathIndex ?? (lastKeyPairIndex + 1) - // Create the key pair - let privateKey = try apollo.createPrivateKey(parameters: [ - KeyProperties.type.rawValue: "EC", - KeyProperties.seed.rawValue: seed.value.base64Encoded(), - KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue, - KeyProperties.derivationPath.rawValue: EdgeAgentDerivationPath( - keyPurpose: .master, - keyIndex: index - ).derivationPath.keyPathString() - ]) + if let masterPrivateKey { + usingPrivateKey = masterPrivateKey + } + else { + let lastKeyPairIndex = try await pluto + .getPrismLastKeyPairIndex() + .first() + .await() + + // If the user provided a key path index use it, if not use the last + 1 + let index = keyPathIndex ?? (lastKeyPairIndex + 1) + // Create the key pair + usingPrivateKey = try apollo.createPrivateKey(parameters: [ + KeyProperties.type.rawValue: "EC", + KeyProperties.seed.rawValue: seed.value.base64Encoded(), + KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue, + KeyProperties.derivationPath.rawValue: EdgeAgentDerivationPath( + keyPurpose: .master, + keyIndex: index + ).derivationPath.keyPathString() + ]) + } - let newDID = try castor.createPrismDID(masterPublicKey: privateKey.publicKey(), services: services) + let newDID = try castor.createPrismDID(masterPublicKey: usingPrivateKey.publicKey(), services: services) logger.debug(message: "Created new Prism DID", metadata: [ .maskedMetadataByLevel(key: "DID", value: newDID.string, level: .debug), .maskedMetadataByLevel(key: "keyPathIndex", value: "\(index)", level: .debug) ]) - try await registerPrismDID(did: newDID, privateKey: privateKey, alias: alias) + try await registerPrismDID(did: newDID, privateKey: usingPrivateKey, alias: alias) return newDID }