Skip to content

Commit 7a1878b

Browse files
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 <[email protected]>
1 parent 4c88360 commit 7a1878b

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ struct PrismDIDPublicKey {
6161

6262
let apollo: Apollo
6363
let id: String
64+
let curve: String
6465
let usage: Usage
6566
let keyData: PublicKey
6667

67-
init(apollo: Apollo, id: String, usage: Usage, keyData: PublicKey) {
68+
init(apollo: Apollo, id: String, curve: String, usage: Usage, keyData: PublicKey) {
6869
self.apollo = apollo
6970
self.id = id
71+
self.curve = curve
7072
self.usage = usage
7173
self.keyData = keyData
7274
}
@@ -77,20 +79,22 @@ struct PrismDIDPublicKey {
7779
usage = proto.usage.fromProto()
7880
switch proto.keyData {
7981
case let .ecKeyData(value):
82+
curve = value.curve.lowercased()
8083
keyData = try apollo.createPublicKey(parameters: [
8184
KeyProperties.type.rawValue: "EC",
82-
KeyProperties.curve.rawValue: "secp256k1",
85+
KeyProperties.curve.rawValue: value.curve.lowercased(),
8386
KeyProperties.curvePointX.rawValue: value.x.base64EncodedString(),
8487
KeyProperties.curvePointY.rawValue: value.y.base64EncodedString()
8588
])
8689
case let .compressedEcKeyData(value):
90+
curve = value.curve.lowercased()
8791
keyData = try apollo.createPublicKey(parameters: [
8892
KeyProperties.type.rawValue: "EC",
89-
KeyProperties.curve.rawValue: "secp256k1",
93+
KeyProperties.curve.rawValue: value.curve.lowercased(),
9094
KeyProperties.rawKey.rawValue: value.data.base64EncodedString()
9195
])
9296
default:
93-
throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "secp256k1")
97+
throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "")
9498
}
9599
}
96100

@@ -112,7 +116,7 @@ struct PrismDIDPublicKey {
112116
var protoEC = Io_Iohk_Atala_Prism_Protos_ECKeyData()
113117
protoEC.x = pointX
114118
protoEC.y = pointY
115-
protoEC.curve = "secp256k1"
119+
protoEC.curve = curve
116120
protoKey.keyData = .ecKeyData(protoEC)
117121
return protoKey
118122
}

EdgeAgentSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift

+5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@ struct CreatePrismDIDOperation {
1010

1111
func compute() throws -> DID {
1212
var operation = Io_Iohk_Atala_Prism_Protos_AtalaOperation()
13+
guard let masterKeyCurve = masterPublicKey.getProperty(.curve) else {
14+
throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "no curve")
15+
}
1316
operation.createDid = try createDIDAtalaOperation(
1417
publicKeys: [PrismDIDPublicKey(
1518
apollo: apollo,
1619
id: PrismDIDPublicKey.Usage.authenticationKey.defaultId,
20+
curve: masterKeyCurve,
1721
usage: .authenticationKey,
1822
keyData: masterPublicKey
1923
),
2024
PrismDIDPublicKey(
2125
apollo: apollo,
2226
id: PrismDIDPublicKey.Usage.masterKey.defaultId,
27+
curve: masterKeyCurve,
2328
usage: .masterKey,
2429
keyData: masterPublicKey
2530
)],

EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift

+26-18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Could not find key in storage please use Castor instead and provide the private
6060
/// - services: an array of services associated to the DID
6161
/// - Returns: The new created DID
6262
func createNewPrismDID(
63+
masterPrivateKey: PrivateKey? = nil,
6364
keyPathIndex: Int? = nil,
6465
alias: String? = nil,
6566
services: [DIDDocument.Service] = []
@@ -68,31 +69,38 @@ Could not find key in storage please use Castor instead and provide the private
6869
let apollo = self.apollo
6970
let castor = self.castor
7071

71-
let lastKeyPairIndex = try await pluto
72-
.getPrismLastKeyPairIndex()
73-
.first()
74-
.await()
72+
let usingPrivateKey: PrivateKey
7573

76-
// If the user provided a key path index use it, if not use the last + 1
77-
let index = keyPathIndex ?? (lastKeyPairIndex + 1)
78-
// Create the key pair
79-
let privateKey = try apollo.createPrivateKey(parameters: [
80-
KeyProperties.type.rawValue: "EC",
81-
KeyProperties.seed.rawValue: seed.value.base64Encoded(),
82-
KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue,
83-
KeyProperties.derivationPath.rawValue: EdgeAgentDerivationPath(
84-
keyPurpose: .master,
85-
keyIndex: index
86-
).derivationPath.keyPathString()
87-
])
74+
if let masterPrivateKey {
75+
usingPrivateKey = masterPrivateKey
76+
}
77+
else {
78+
let lastKeyPairIndex = try await pluto
79+
.getPrismLastKeyPairIndex()
80+
.first()
81+
.await()
82+
83+
// If the user provided a key path index use it, if not use the last + 1
84+
let index = keyPathIndex ?? (lastKeyPairIndex + 1)
85+
// Create the key pair
86+
usingPrivateKey = try apollo.createPrivateKey(parameters: [
87+
KeyProperties.type.rawValue: "EC",
88+
KeyProperties.seed.rawValue: seed.value.base64Encoded(),
89+
KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue,
90+
KeyProperties.derivationPath.rawValue: EdgeAgentDerivationPath(
91+
keyPurpose: .master,
92+
keyIndex: index
93+
).derivationPath.keyPathString()
94+
])
95+
}
8896

89-
let newDID = try castor.createPrismDID(masterPublicKey: privateKey.publicKey(), services: services)
97+
let newDID = try castor.createPrismDID(masterPublicKey: usingPrivateKey.publicKey(), services: services)
9098
logger.debug(message: "Created new Prism DID", metadata: [
9199
.maskedMetadataByLevel(key: "DID", value: newDID.string, level: .debug),
92100
.maskedMetadataByLevel(key: "keyPathIndex", value: "\(index)", level: .debug)
93101
])
94102

95-
try await registerPrismDID(did: newDID, privateKey: privateKey, alias: alias)
103+
try await registerPrismDID(did: newDID, privateKey: usingPrivateKey, alias: alias)
96104
return newDID
97105
}
98106

0 commit comments

Comments
 (0)