Skip to content

Commit 32bdfbd

Browse files
feat(apollo): integrate with apollo kmm
BREAKING CHANGE: Updated Apollo public interface to be more in line with Cryptographic abstraction
1 parent 80377b1 commit 32bdfbd

File tree

56 files changed

+288
-18976
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+288
-18976
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ concurrency:
1515
jobs:
1616
lint:
1717
name: build
18-
runs-on: macos-latest
18+
runs-on: macos-13
1919

2020
steps:
2121
- name: Checkout Code
2222
uses: actions/checkout@v3
2323

24+
- uses: maxim-lobanov/setup-xcode@v1
25+
with:
26+
xcode-version: '15.0.1'
27+
2428
- name: Install SSH Key
2529
uses: shimataro/[email protected]
2630
with:

AtalaPrismSDK/Apollo/Sources/ApolloImpl+KeyRestoration.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,28 @@ extension ApolloImpl: KeyRestoration {
1717
public func restorePrivateKey(_ key: StorableKey) throws -> PrivateKey {
1818
switch key.restorationIdentifier {
1919
case "secp256k1+priv":
20-
return Secp256k1PrivateKey(
21-
lockedPrivateKey: .init(data: key.storableData),
22-
derivationPath: key.index.map { DerivationPath(index: $0) } ?? DerivationPath(index: 0)
23-
)
20+
guard let index = key.index else {
21+
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
22+
}
23+
return Secp256k1PrivateKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()), derivationPath: DerivationPath(index: index))
2424
case "x25519+priv":
25-
return X25519PrivateKey(appleCurve: try .init(rawRepresentation: key.storableData))
25+
return try CreateX25519KeyPairOperation(logger: Self.logger).compute(fromPrivateKey: key.storableData)
2626
case "ed25519+priv":
27-
return Ed25519PrivateKey(appleCurve: try .init(rawRepresentation: key.storableData))
27+
return try CreateEd25519KeyPairOperation(logger: Self.logger).compute(fromPrivateKey: key.storableData)
2828
default:
2929
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
3030
}
31+
3132
}
3233

3334
public func restorePublicKey(_ key: StorableKey) throws -> PublicKey {
3435
switch key.restorationIdentifier {
3536
case "secp256k1+pub":
36-
return Secp256k1PublicKey(lockedPublicKey: .init(bytes: key.storableData))
37+
return Secp256k1PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
3738
case "x25519+pub":
38-
return X25519PublicKey(appleCurve: try .init(rawRepresentation: key.storableData))
39+
return X25519PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
3940
case "ed25519+pub":
40-
return Ed25519PublicKey(appleCurve: try .init(rawRepresentation: key.storableData))
41+
return Ed25519PublicKey(internalKey: .init(raw: key.storableData.toKotlinByteArray()))
4142
default:
4243
throw ApolloError.restoratonFailedNoIdentifierOrInvalid
4344
}

AtalaPrismSDK/Apollo/Sources/ApolloImpl+Public.swift

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import ApolloLibrary
12
import Domain
23
import Foundation
34

45
extension ApolloImpl: Apollo {
5-
66
/// createRandomMnemonics creates a random set of mnemonic phrases that can be used as a seed for generating a private key.
77
///
88
/// - Returns: An array of mnemonic phrases
@@ -26,62 +26,18 @@ extension ApolloImpl: Apollo {
2626
///
2727
/// - Returns: A tuple containing an array of mnemonic phrases and a seed object
2828
public func createRandomSeed() -> (mnemonic: [String], seed: Seed) {
29-
let words = createRandomMnemonics()
30-
guard let seed = try? createSeed(mnemonics: words, passphrase: "") else {
31-
fatalError("""
32-
This should never happen since the function that
33-
returns random mnemonics nerver returns invalid mnemonics
34-
""")
35-
}
36-
return (words, seed)
37-
}
38-
39-
/// compressedPublicKey compresses a given public key into a shorter, more efficient form.
40-
///
41-
/// - Parameter publicKey: The public key to compress
42-
/// - Returns: The compressed public key
43-
public func compressedPublicKey(publicKey: PublicKey) throws -> PublicKey {
44-
guard
45-
publicKey.getProperty(.curve)?.lowercased() == KnownKeyCurves.secp256k1.rawValue
29+
let words = ApolloLibrary
30+
.Mnemonic
31+
.companion
32+
.createRandomMnemonics()
33+
guard let seed = try? ApolloLibrary
34+
.Mnemonic
35+
.companion
36+
.createSeed(mnemonics: words, passphrase: "AtalaPrism")
4637
else {
47-
throw ApolloError.invalidKeyCurve(
48-
invalid: publicKey.getProperty(.curve)?.lowercased() ?? "",
49-
valid: [KnownKeyCurves.secp256k1.rawValue]
50-
)
38+
fatalError("This should never happen")
5139
}
52-
return Secp256k1PublicKey(lockedPublicKey: LockPublicKey(bytes: publicKey.raw).compressedPublicKey())
53-
}
54-
55-
// /// compressedPublicKey decompresses a given compressed public key into its original form.
56-
// ///
57-
// /// - Parameter compressedData: The compressed public key data
58-
// /// - Returns: The decompressed public key
59-
// public func uncompressedPublicKey(compressedData: Data) -> PublicKey {
60-
// PublicKey(
61-
// curve: KeyCurve.secp256k1().name,
62-
// value: LockPublicKey(
63-
// bytes: compressedData
64-
// ).uncompressedPublicKey().data
65-
// )
66-
// }
67-
68-
/// compressedPublicKey decompresses a given compressed public key into its original form.
69-
///
70-
/// - Parameter compressedData: The compressed public key data
71-
/// - Returns: The decompressed public key
72-
public func uncompressedPublicKey(compressedData: Data) -> PublicKey {
73-
Secp256k1PublicKey(
74-
lockedPublicKey: LockPublicKey(bytes: compressedData).uncompressedPublicKey()
75-
)
76-
}
77-
78-
public func publicKeyFrom(x: Data, y: Data) -> PublicKey {
79-
Secp256k1PublicKey(lockedPublicKey: LockPublicKey(x: x, y: y))
80-
}
81-
82-
public func publicKeyPointCurve(publicKey: PublicKey) throws -> (x: Data, y: Data) {
83-
let points = try LockPublicKey(bytes: publicKey.raw).pointCurve()
84-
return (points.x.data, points.y.data)
40+
return (words, Seed(value: seed.toData()))
8541
}
8642

8743
public func createPrivateKey(parameters: [String : String]) throws -> PrivateKey {
@@ -106,7 +62,7 @@ returns random mnemonics nerver returns invalid mnemonics
10662
let derivationPathStr = parameters[KeyProperties.derivationPath.rawValue]
10763
{
10864
let derivationPath = try DerivationPath(string: derivationPathStr)
109-
return Secp256k1PrivateKey(lockedPrivateKey: .init(data: keyData), derivationPath: derivationPath)
65+
return Secp256k1PrivateKey(raw: keyData, derivationPath: derivationPath)
11066
} else {
11167
guard
11268
let derivationPathStr = parameters[KeyProperties.derivationPath.rawValue],
@@ -118,10 +74,10 @@ returns random mnemonics nerver returns invalid mnemonics
11874
])
11975
}
12076
let derivationPath = try DerivationPath(string: derivationPathStr)
121-
return try CreateSec256k1KeyPairOperation(
77+
return try CreateSec256k1KeyPairOperation().compute(
12278
seed: Seed(value: seed),
12379
keyPath: derivationPath
124-
).compute()
80+
)
12581
}
12682
case .ed25519:
12783
if
@@ -144,7 +100,58 @@ returns random mnemonics nerver returns invalid mnemonics
144100
throw ApolloError.invalidKeyType(invalid: keyType, valid: ValidCryptographicTypes.allCases.map(\.rawValue))
145101
}
146102
}
147-
103+
104+
public func createPublicKey(parameters: [String : String]) throws -> PublicKey {
105+
guard
106+
let keyType = parameters[KeyProperties.type.rawValue]
107+
else { throw ApolloError.invalidKeyType(invalid: "", valid: ValidCryptographicTypes.allCases.map(\.rawValue)) }
108+
switch keyType {
109+
case ValidCryptographicTypes.ec.rawValue:
110+
guard
111+
let curveStr = parameters[KeyProperties.curve.rawValue],
112+
let curve = ValidECCurves(rawValue: curveStr)
113+
else {
114+
throw ApolloError.invalidKeyCurve(
115+
invalid: parameters[KeyProperties.curve.rawValue] ?? "",
116+
valid: ValidECCurves.allCases.map(\.rawValue)
117+
)
118+
}
119+
switch curve {
120+
case .secp256k1:
121+
if let keyData = parameters[KeyProperties.rawKey.rawValue].flatMap({ Data(base64Encoded: $0) }) {
122+
return Secp256k1PublicKey(raw: keyData)
123+
} else if
124+
let x = parameters[KeyProperties.curvePointX.rawValue].flatMap({ Data(base64Encoded: $0) }),
125+
let y = parameters[KeyProperties.curvePointY.rawValue].flatMap({ Data(base64Encoded: $0) })
126+
{
127+
return Secp256k1PublicKey(x: x, y: y)
128+
} else {
129+
throw ApolloError.missingKeyParameters(missing: [
130+
KeyProperties.rawKey.rawValue,
131+
KeyProperties.curvePointX.rawValue,
132+
KeyProperties.curvePointY.rawValue
133+
])
134+
}
135+
case .ed25519:
136+
guard
137+
let keyData = parameters[KeyProperties.rawKey.rawValue].flatMap({ Data(base64Encoded: $0) })
138+
else {
139+
throw ApolloError.missingKeyParameters(missing: [KeyProperties.rawKey.rawValue])
140+
}
141+
return Ed25519PublicKey(raw: keyData)
142+
case .x25519:
143+
guard
144+
let keyData = parameters[KeyProperties.rawKey.rawValue].flatMap({ Data(base64Encoded: $0) })
145+
else {
146+
throw ApolloError.missingKeyParameters(missing: [KeyProperties.rawKey.rawValue])
147+
}
148+
return X25519PublicKey(raw: keyData)
149+
}
150+
default:
151+
throw ApolloError.invalidKeyType(invalid: keyType, valid: ValidCryptographicTypes.allCases.map(\.rawValue))
152+
}
153+
}
154+
148155
public func createNewLinkSecret() throws -> Key {
149156
try LinkSecret()
150157
}

AtalaPrismSDK/Apollo/Sources/BIPs/BIP32_44/HDKey.swift

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)