Skip to content

Commit

Permalink
feat!: move to swift based framework for didcomm, jose and peer did (#…
Browse files Browse the repository at this point in the history
…120)

BREAKING CHANGE: There is an update to the public API that is used directly with DIDComm
  • Loading branch information
goncalo-frade-iohk committed Feb 27, 2024
1 parent 392c52c commit 558164f
Show file tree
Hide file tree
Showing 63 changed files with 821 additions and 1,190 deletions.
32 changes: 26 additions & 6 deletions AtalaPrismSDK/Apollo/Sources/ApolloImpl+KeyRestoration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ extension ApolloImpl: KeyRestoration {
guard let index = key.index else {
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
}
return Secp256k1PrivateKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()), derivationPath: DerivationPath(index: index))
return Secp256k1PrivateKey(
identifier: key.identifier,
internalKey: .init(raw: key.storableData.toKotlinByteArray()), derivationPath: DerivationPath(index: index)
)
case "x25519+priv":
return try CreateX25519KeyPairOperation(logger: Self.logger).compute(fromPrivateKey: key.storableData)
return try CreateX25519KeyPairOperation(logger: Self.logger)
.compute(
identifier: key.identifier,
fromPrivateKey: key.storableData
)
case "ed25519+priv":
return try CreateEd25519KeyPairOperation(logger: Self.logger).compute(fromPrivateKey: key.storableData)
return try CreateEd25519KeyPairOperation(logger: Self.logger)
.compute(
identifier: key.identifier,
fromPrivateKey: key.storableData
)
default:
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
}
Expand All @@ -34,11 +45,20 @@ extension ApolloImpl: KeyRestoration {
public func restorePublicKey(_ key: StorableKey) throws -> PublicKey {
switch key.restorationIdentifier {
case "secp256k1+pub":
return Secp256k1PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
return Secp256k1PublicKey(
identifier: key.identifier,
internalKey: .init(raw: key.storableData.toKotlinByteArray())
)
case "x25519+pub":
return X25519PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
return X25519PublicKey(
identifier: key.identifier,
internalKey: .init(raw: key.storableData.toKotlinByteArray())
)
case "ed25519+pub":
return Ed25519PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
return Ed25519PublicKey(
identifier: key.identifier,
internalKey: .init(raw: key.storableData.toKotlinByteArray())
)
default:
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
}
Expand Down
14 changes: 12 additions & 2 deletions AtalaPrismSDK/Apollo/Sources/Model/Ed25519Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ struct Ed25519PrivateKey: PrivateKey {
let keySpecifications: [String : String] = [
"curve" : "Ed25519"
]
var identifier: String
var size: Int { raw.count }
var raw: Data { internalKey.raw.toData() }

init(internalKey: ApolloLibrary.KMMEdPrivateKey) {
init(
identifier: String = UUID().uuidString,
internalKey: ApolloLibrary.KMMEdPrivateKey
) {
self.identifier = identifier
self.internalKey = internalKey
}

Expand Down Expand Up @@ -53,10 +58,15 @@ struct Ed25519PublicKey: PublicKey {
let keySpecifications: [String : String] = [
"curve" : "Ed25519"
]
var identifier: String
var size: Int { raw.count }
var raw: Data { internalKey.raw.toData() }

init(internalKey: ApolloLibrary.KMMEdPublicKey) {
init(
identifier: String = UUID().uuidString,
internalKey: ApolloLibrary.KMMEdPublicKey
) {
self.identifier = identifier
self.internalKey = internalKey
}

Expand Down
1 change: 1 addition & 0 deletions AtalaPrismSDK/Apollo/Sources/Model/LinkSecret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct LinkSecret: Key {
let keyType = "LinkSecret"
let keySpecifications = [String : String]()
let raw: Data
var identifier = "linkSecret"
var size: Int { raw.count }

let anoncred: AnoncredsSwift.LinkSecret
Expand Down
16 changes: 8 additions & 8 deletions AtalaPrismSDK/Apollo/Sources/Model/Secp256k1Key+Exportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ extension Secp256k1PrivateKey: ExportableKey {

var jwk: JWK {
JWK(
kty: "OKP",
kty: "EC",
d: raw.base64UrlEncodedString(),
crv: getProperty(.curve)?.capitalized,
crv: getProperty(.curve)?.lowercased(),
x: publicKey().getProperty(.curvePointX).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() },
y: publicKey().getProperty(.curvePointY).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() }
)
}

func jwkWithKid(kid: String) -> JWK {
JWK(
kty: "OKP",
kty: "EC",
kid: kid,
d: raw.base64UrlEncodedString(),
crv: getProperty(.curve)?.capitalized,
crv: getProperty(.curve)?.lowercased(),
x: publicKey().getProperty(.curvePointX).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() },
y: publicKey().getProperty(.curvePointY).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() }
)
Expand All @@ -41,8 +41,8 @@ extension Secp256k1PublicKey: ExportableKey {

var jwk: JWK {
JWK(
kty: "OKP",
crv: getProperty(.curve)?.capitalized,
kty: "EC",
crv: getProperty(.curve)?.lowercased(),
x: getProperty(.curvePointX)
.flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() } ?? raw.base64UrlEncodedString(),
y: getProperty(.curvePointY).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() }
Expand All @@ -51,9 +51,9 @@ extension Secp256k1PublicKey: ExportableKey {

func jwkWithKid(kid: String) -> JWK {
JWK(
kty: "OKP",
kty: "EC",
kid: kid,
crv: getProperty(.curve)?.capitalized,
crv: getProperty(.curve)?.lowercased(),
x: getProperty(.curvePointX)
.flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() } ?? raw.base64UrlEncodedString(),
y: getProperty(.curvePointY).flatMap { Data(fromBase64URL: $0)?.base64UrlEncodedString() }
Expand Down
19 changes: 14 additions & 5 deletions AtalaPrismSDK/Apollo/Sources/Model/Secp256k1Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import Foundation

struct Secp256k1PrivateKey: PrivateKey {
private let internalKey: KMMECSecp256k1PrivateKey

let keyType: String = "EC"
let keySpecifications: [String : String]
let size: Int
let raw: Data
let derivationPath: Domain.DerivationPath

init(internalKey: KMMECSecp256k1PrivateKey, derivationPath: Domain.DerivationPath) {
var identifier: String

init(
identifier: String = UUID().uuidString,
internalKey: KMMECSecp256k1PrivateKey,
derivationPath: Domain.DerivationPath
) {
self.identifier = identifier
self.internalKey = internalKey
self.derivationPath = derivationPath
self.keySpecifications = [
Expand Down Expand Up @@ -60,13 +65,17 @@ extension Secp256k1PrivateKey: KeychainStorableKey {

struct Secp256k1PublicKey: PublicKey {
private let internalKey: ApolloLibrary.KMMECSecp256k1PublicKey

let keyType: String = "EC"
let keySpecifications: [String : String]
let size: Int
let raw: Data
var identifier = UUID().uuidString

init(internalKey: ApolloLibrary.KMMECSecp256k1PublicKey) {
init(
identifier: String = UUID().uuidString,
internalKey: ApolloLibrary.KMMECSecp256k1PublicKey
) {
self.identifier = identifier
self.internalKey = internalKey
var specs: [String: String] = [
KeyProperties.curve.rawValue: "secp256k1",
Expand Down
14 changes: 12 additions & 2 deletions AtalaPrismSDK/Apollo/Sources/Model/X25519Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ struct X25519PrivateKey: PrivateKey {
let keySpecifications: [String : String] = [
"curve" : "x25519"
]
var identifier:String
var size: Int { raw.count }
var raw: Data { internalKey.raw.toData() }

init(internalKey: ApolloLibrary.KMMX25519PrivateKey) {
init(
identifier: String = UUID().uuidString,
internalKey: ApolloLibrary.KMMX25519PrivateKey
) {
self.identifier = identifier
self.internalKey = internalKey
}

Expand Down Expand Up @@ -41,10 +46,15 @@ struct X25519PublicKey: PublicKey {
let keySpecifications: [String : String] = [
"curve" : "x25519"
]
var identifier: String
var size: Int { raw.count }
var raw: Data { internalKey.raw.toData() }

init(internalKey: ApolloLibrary.KMMX25519PublicKey) {
init(
identifier: String = UUID().uuidString,
internalKey: ApolloLibrary.KMMX25519PublicKey
) {
self.identifier = identifier
self.internalKey = internalKey
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ struct CreateEd25519KeyPairOperation {

}

func compute(fromPrivateKey: Data) throws -> PrivateKey {
return Ed25519PrivateKey(internalKey: KMMEdPrivateKey(raw: fromPrivateKey.toKotlinByteArray()))
func compute(identifier: String = UUID().uuidString, fromPrivateKey: Data) throws -> PrivateKey {
return Ed25519PrivateKey(
identifier: identifier,
internalKey: KMMEdPrivateKey(raw: fromPrivateKey.toKotlinByteArray())
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ struct CreateX25519KeyPairOperation {

}

func compute(fromPrivateKey: Data) throws -> PrivateKey {
func compute(identifier: String = UUID().uuidString, fromPrivateKey: Data) throws -> PrivateKey {
let privateKey = KMMX25519PrivateKey(raw: fromPrivateKey.toKotlinByteArray())
return X25519PrivateKey(internalKey: privateKey)
return X25519PrivateKey(
identifier: identifier,
internalKey: privateKey
)
}
}
18 changes: 0 additions & 18 deletions AtalaPrismSDK/Castor/Sources/CastorImpl+Public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,4 @@ extension CastorImpl: Castor {
}
return try await resolver.resolve(did: did)
}

/// getEcnumbasis generates a unique ECNUM basis string for a given DID and key pair. This function may throw an error if the DID or key pair are invalid.
///
/// - Parameters:
/// - did: The DID associated with the key pair
/// - keyPair: The key pair to use for generating the ECNUM basis
/// - Returns: The ECNUM basis string
/// - Throws: An error if the DID or key pair are invalid
public func getEcnumbasis(did: DID, publicKey: PublicKey) throws -> String {
logger.debug(message: "Getting ecnumbasis", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
return try CreatePeerDIDOperation(
autenticationPublicKey: publicKey,
agreementPublicKey: publicKey,
services: []
).computeEcnumbasis(did: did, publicKey: publicKey)
}
}
65 changes: 0 additions & 65 deletions AtalaPrismSDK/Castor/Sources/DID/PeerDID/PeerDID.swift

This file was deleted.

66 changes: 0 additions & 66 deletions AtalaPrismSDK/Castor/Sources/DID/PeerDID/Types.swift

This file was deleted.

Loading

0 comments on commit 558164f

Please sign in to comment.