@@ -53,44 +53,44 @@ extension SECP256K1 {
53
53
static func combineSerializedPublicKeys( keys: [ Data ] , outputCompressed: Bool = false ) -> Data ? {
54
54
let numToCombine = keys. count
55
55
guard numToCombine >= 1 else { return nil }
56
- var publicKeys = [ UnsafePointer < secp256k1_pubkey > ? ] ( )
57
- var result : Int32
58
- for i in 0 ..< numToCombine {
59
- var publicKey = secp256k1_pubkey ( )
56
+ var storage = ContiguousArray < secp256k1_pubkey > ( )
57
+ let arrayOfPointers = UnsafeMutablePointer< UnsafePointer< secp256k1_pubkey>? > . allocate ( capacity: numToCombine)
58
+ defer {
59
+ arrayOfPointers. deinitialize ( count: numToCombine)
60
+ arrayOfPointers. deallocate ( )
61
+ }
62
+ for i in 0 ..< numToCombine {
60
63
let key = keys [ i]
61
- let keyLen = key. count
62
- result = key. withUnsafeBytes { ( publicKeyPointer: UnsafePointer < UInt8 > ) -> Int32 in
63
- let res = secp256k1_ec_pubkey_parse ( context!, UnsafeMutablePointer < secp256k1_pubkey > ( & publicKey) , publicKeyPointer, keyLen)
64
- return res
65
- }
66
- if result == 0 {
67
- return nil
64
+ guard let pubkey = SECP256K1 . parsePublicKey ( serializedKey: key) else { return nil }
65
+ storage. append ( pubkey)
66
+ }
67
+ for i in 0 ..< numToCombine {
68
+ withUnsafePointer ( to: & storage[ i] ) { ( ptr) -> Void in
69
+ arrayOfPointers. advanced ( by: i) . pointee = ptr
68
70
}
69
- let pointer = UnsafePointer < secp256k1_pubkey > ( UnsafeMutablePointer < secp256k1_pubkey > ( & publicKey) )
70
- publicKeys. append ( pointer)
71
71
}
72
-
72
+ let immutablePointer = UnsafePointer ( arrayOfPointers )
73
73
var publicKey : secp256k1_pubkey = secp256k1_pubkey ( )
74
- let arrayPointer = UnsafePointer ( publicKeys)
75
- result = secp256k1_ec_pubkey_combine ( context!, UnsafeMutablePointer < secp256k1_pubkey > ( & publicKey) , arrayPointer, numToCombine)
74
+
75
+ // let bufferPointer = UnsafeBufferPointer(start: immutablePointer, count: numToCombine)
76
+ // for (index, value) in bufferPointer.enumerated() {
77
+ // print("pointer value \(index): \(value!)")
78
+ // let val = value?.pointee
79
+ // print("value \(index): \(val!)")
80
+ // }
81
+ //
82
+ let result = withUnsafeMutablePointer ( to: & publicKey) { ( pubKeyPtr: UnsafeMutablePointer < secp256k1_pubkey > ) -> Int32 in
83
+ let res = secp256k1_ec_pubkey_combine ( context!, pubKeyPtr, immutablePointer, numToCombine)
84
+ return res
85
+ }
76
86
if result == 0 {
77
87
return nil
78
88
}
79
-
80
- var keyLength = outputCompressed ? 33 : 65
81
- var serializedPubkey = Data ( repeating: 0x00 , count: keyLength)
82
-
83
- result = serializedPubkey. withUnsafeMutableBytes { ( serializedPubkeyPointer: UnsafeMutablePointer < UInt8 > ) -> Int32 in
84
- let res = secp256k1_ec_pubkey_serialize ( context!,
85
- serializedPubkeyPointer,
86
- UnsafeMutablePointer < Int > ( & keyLength) ,
87
- UnsafeMutablePointer < secp256k1_pubkey > ( & publicKey) ,
88
- UInt32 ( outputCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED) )
89
- return res
90
- }
91
- return Data ( serializedPubkey)
89
+ let serializedKey = SECP256K1 . serializePublicKey ( publicKey: & publicKey, compressed: outputCompressed)
90
+ return serializedKey
92
91
}
93
92
93
+
94
94
static func recoverPublicKey( hash: Data , recoverableSignature: inout secp256k1_ecdsa_recoverable_signature ) -> secp256k1_pubkey ? {
95
95
guard hash. count == 32 else { return nil }
96
96
var publicKey : secp256k1_pubkey = secp256k1_pubkey ( )
0 commit comments