@@ -40,7 +40,8 @@ public extension secp256k1 {
40
40
public static func create( _ context: Context = . none) throws -> OpaquePointer {
41
41
var randomBytes = SecureBytes ( count: secp256k1. ByteDetails. count) . bytes
42
42
guard let context = secp256k1_context_create ( context. rawValue) ,
43
- secp256k1_context_randomize ( context, & randomBytes) . boolValue else {
43
+ secp256k1_context_randomize ( context, & randomBytes) . boolValue
44
+ else {
44
45
throw secp256k1Error. underlyingCryptoError
45
46
}
46
47
@@ -151,16 +152,14 @@ extension secp256k1 {
151
152
152
153
/// Backing initialization that creates a random secp256k1 private key for signing
153
154
@usableFromInline init ( format: secp256k1 . Format = . compressed) throws {
154
- let privateKey = SecureBytes ( count: secp256k1. ByteDetails. count)
155
- self . keyParity = 0
156
- self . format = format
157
- self . privateBytes = privateKey
158
- self . publicBytes = try PublicKeyImplementation . generate ( bytes: & privateBytes, format: format)
159
- self . xonlyBytes = try XonlyKeyImplementation . generate (
160
- bytes: publicBytes,
161
- keyParity: & keyParity,
162
- format: format
163
- )
155
+ for _ in 0 ..< 10 {
156
+ let randomBytes = SecureBytes ( count: secp256k1. ByteDetails. count)
157
+ if let privateKey = try ? PrivateKeyImplementation ( dataRepresentation: Data ( randomBytes) , format: format) {
158
+ self = privateKey
159
+ return
160
+ }
161
+ }
162
+ fatalError ( " Looped more than 10 times trying to generate a key " )
164
163
}
165
164
166
165
/// Backing initialization that creates a secp256k1 private key for signing from a data representation.
@@ -224,7 +223,8 @@ extension secp256k1 {
224
223
var bytes = [ UInt8] ( repeating: 0 , count: keyLength)
225
224
226
225
guard secp256k1_ec_pubkey_negate ( context, & key) . boolValue,
227
- secp256k1_ec_pubkey_serialize ( context, & bytes, & keyLength, & key, format. rawValue) . boolValue else {
226
+ secp256k1_ec_pubkey_serialize ( context, & bytes, & keyLength, & key, format. rawValue) . boolValue
227
+ else {
228
228
throw secp256k1Error. underlyingCryptoError
229
229
}
230
230
@@ -299,7 +299,8 @@ extension secp256k1 {
299
299
signature. dataRepresentation. copyToUnsafeMutableBytes ( of: & recoverySignature. data)
300
300
301
301
guard secp256k1_ecdsa_recover ( context, & pubKey, & recoverySignature, Array ( digest) ) . boolValue,
302
- secp256k1_ec_pubkey_serialize ( context, & pubBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue else {
302
+ secp256k1_ec_pubkey_serialize ( context, & pubBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue
303
+ else {
303
304
throw secp256k1Error. underlyingCryptoError
304
305
}
305
306
@@ -402,7 +403,8 @@ extension secp256k1 {
402
403
403
404
guard secp256k1_ec_pubkey_parse ( context, & pubKey, publicBytes, format. length) . boolValue,
404
405
secp256k1_xonly_pubkey_from_pubkey ( context, & xonlyPubKey, & keyParity, & pubKey) . boolValue,
405
- secp256k1_xonly_pubkey_serialize ( context, & xonlyBytes, & xonlyPubKey) . boolValue else {
406
+ secp256k1_xonly_pubkey_serialize ( context, & xonlyBytes, & xonlyPubKey) . boolValue
407
+ else {
406
408
throw secp256k1Error. underlyingCryptoError
407
409
}
408
410
0 commit comments