1
+ import ApolloLibrary
1
2
import Domain
2
3
import Foundation
3
4
4
5
extension ApolloImpl : Apollo {
5
-
6
6
/// createRandomMnemonics creates a random set of mnemonic phrases that can be used as a seed for generating a private key.
7
7
///
8
8
/// - Returns: An array of mnemonic phrases
@@ -26,62 +26,18 @@ extension ApolloImpl: Apollo {
26
26
///
27
27
/// - Returns: A tuple containing an array of mnemonic phrases and a seed object
28
28
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 " )
46
37
else {
47
- throw ApolloError . invalidKeyCurve (
48
- invalid: publicKey. getProperty ( . curve) ? . lowercased ( ) ?? " " ,
49
- valid: [ KnownKeyCurves . secp256k1. rawValue]
50
- )
38
+ fatalError ( " This should never happen " )
51
39
}
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 ( ) ) )
85
41
}
86
42
87
43
public func createPrivateKey( parameters: [ String : String ] ) throws -> PrivateKey {
@@ -106,7 +62,7 @@ returns random mnemonics nerver returns invalid mnemonics
106
62
let derivationPathStr = parameters [ KeyProperties . derivationPath. rawValue]
107
63
{
108
64
let derivationPath = try DerivationPath ( string: derivationPathStr)
109
- return Secp256k1PrivateKey ( lockedPrivateKey : . init ( data : keyData) , derivationPath: derivationPath)
65
+ return Secp256k1PrivateKey ( raw : keyData, derivationPath: derivationPath)
110
66
} else {
111
67
guard
112
68
let derivationPathStr = parameters [ KeyProperties . derivationPath. rawValue] ,
@@ -118,10 +74,10 @@ returns random mnemonics nerver returns invalid mnemonics
118
74
] )
119
75
}
120
76
let derivationPath = try DerivationPath ( string: derivationPathStr)
121
- return try CreateSec256k1KeyPairOperation (
77
+ return try CreateSec256k1KeyPairOperation ( ) . compute (
122
78
seed: Seed ( value: seed) ,
123
79
keyPath: derivationPath
124
- ) . compute ( )
80
+ )
125
81
}
126
82
case . ed25519:
127
83
if
@@ -144,7 +100,58 @@ returns random mnemonics nerver returns invalid mnemonics
144
100
throw ApolloError . invalidKeyType ( invalid: keyType, valid: ValidCryptographicTypes . allCases. map ( \. rawValue) )
145
101
}
146
102
}
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
+
148
155
public func createNewLinkSecret( ) throws -> Key {
149
156
try LinkSecret ( )
150
157
}
0 commit comments