Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(castor): add capacity so you can create and resolve prism dids with ed25519 and x25519 keys #158

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions EdgeAgentSDK/Castor/Tests/PrismDIDPublicKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
)
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
Loading