Skip to content

Commit

Permalink
feat(castor): add capacity so you can create and resolve prism dids w…
Browse files Browse the repository at this point in the history
…ith ed25519 and x25519 keys

Fixes ATL-7160

Signed-off-by: goncalo-frade-iohk <goncalo.frade@iohk.io>
  • Loading branch information
goncalo-frade-iohk committed Aug 13, 2024
1 parent 0b302f5 commit 31b162a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
14 changes: 9 additions & 5 deletions EdgeAgentSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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: "")
}
}

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)],
Expand Down
44 changes: 26 additions & 18 deletions EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+DIDHigherFucntions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand All @@ -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
}

Expand Down

0 comments on commit 31b162a

Please sign in to comment.