EthkeyBridge.swift 10.2 KB
Newer Older
fro's avatar
fro committed
1
2
3
4
5
//
//  EthkeyBridge.swift
//  NativeSigner
//
//  Created by Marek Kotewicz on 19/02/2017.
Thibaut Sardan's avatar
Thibaut Sardan committed
6
//  Copyright © 2019 Facebook. All rights reserved.
fro's avatar
fro committed
7
8
9
10
//

import Foundation

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func handle_error<T, U>(
  resolve: RCTPromiseResolveBlock,
  reject: RCTPromiseRejectBlock,
  get_result: (UnsafeMutablePointer<ExternError>) -> T,
  success: (T) -> U
) -> Void {
  var err = ExternError()
  let err_ptr: UnsafeMutablePointer<ExternError> = UnsafeMutablePointer(&err)
  let res = get_result(err_ptr)
  if err_ptr.pointee.code == 0 {
    resolve(success(res))
  } else {
    let val = String(cString: err_ptr.pointee.message)
    signer_destroy_string(err_ptr.pointee.message)
    reject(String(describing: err_ptr.pointee.code), val, nil)
  }
}

fro's avatar
fro committed
29
30
@objc(EthkeyBridge)
class EthkeyBridge: NSObject {
31

Maciej Hirsz's avatar
Maciej Hirsz committed
32
  public static func requiresMainQueueSetup() -> Bool {
fro's avatar
fro committed
33
34
    return true;
  }
35

fro's avatar
fro committed
36
  @objc func brainWalletAddress(_ seed: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
37
38
39
40
41
42
43
44
45
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { ethkey_brainwallet_address($0, seed) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
46
  }
47

fro's avatar
fro committed
48
  @objc func brainWalletSign(_ seed: String, message: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
49
50
51
52
53
54
55
56
57
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { ethkey_brainwallet_sign($0, seed, message) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
58
  }
59

fro's avatar
fro committed
60
  @objc func rlpItem(_ rlp: String, position: UInt32, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
61
62
63
64
65
66
67
68
69
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { rlp_item($0, rlp, position) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
70
  }
71

fro's avatar
fro committed
72
  @objc func keccak(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
73
74
75
76
77
78
79
80
81
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { keccak256($0, data) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
82
  }
83

84
  @objc func blake2b(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
85
86
87
88
89
90
91
92
93
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { blake($0, data) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
94
  }
95

fro's avatar
fro committed
96
  @objc func ethSign(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
97
98
99
100
101
102
103
104
105
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { eth_sign($0, data) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
106
  }
107

fro's avatar
fro committed
108
  @objc func blockiesIcon(_ seed: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
109
110
111
112
113
114
115
116
117
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { blockies_icon($0, seed) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
118
  }
119

120
  @objc func randomPhrase(_ wordsNumber:NSInteger, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
121
122
123
124
125
126
127
128
129
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { random_phrase($0, Int32(wordsNumber)) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
130
  }
131

fro's avatar
fro committed
132
  @objc func encryptData(_ data: String, password: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
133
134
135
136
137
138
139
140
141
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { encrypt_data($0, data, password) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
142
  }
143

fro's avatar
fro committed
144
  @objc func decryptData(_ data: String, password: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
145
146
147
148
149
150
151
152
153
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { decrypt_data($0, data, password) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
fro's avatar
fro committed
154
  }
155

Maciej Hirsz's avatar
Maciej Hirsz committed
156
  @objc func qrCode(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
157
158
159
160
161
162
163
164
165
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { qrcode($0, data) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
Maciej Hirsz's avatar
Maciej Hirsz committed
166
  }
167

168
  @objc func qrCodeHex(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
169
170
171
172
173
174
175
176
177
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { qrcode_hex($0, data) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
178
  }
179

180
  @objc func substrateAddress(_ seed: String, prefix: UInt32, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
181
182
183
184
185
186
187
188
189
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_brainwallet_address($0, seed, prefix) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
190
  }
191

192
  @objc func substrateSign(_ seed: String, message: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
193
194
195
196
197
198
199
200
201
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_brainwallet_sign($0, seed, message) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
202
  }
203

204
  @objc func schnorrkelVerify(_ seed: String, message: String, signature: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
205
206
207
208
209
210
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { schnorrkel_verify($0, seed, message, signature) },
      // return a bool. no cleanup
      success: { return $0 })
211
  }
212

213
  @objc func decryptDataRef(_ data: String, password: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
214
215
216
217
218
219
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { decrypt_data_ref($0, data, password) },
      // return a long. no cleanup
      success: { return $0 })
220
  }
221

222
  @objc func destroyDataRef(_ data_ref: Int64, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
223
224
225
226
227
228
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { destroy_data_ref($0, data_ref) },
      // return zero. no cleanup
      success: { return 0 })
229
  }
230

231
  @objc func brainWalletSignWithRef(_ seed_ref: Int64, message: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
232
233
234
235
236
237
238
239
240
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { ethkey_brainwallet_sign_with_ref($0, seed_ref, message) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
241
  }
242

243
  @objc func substrateSignWithRef(_ seed_ref: Int64, suri_suffix: String, message: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
244
245
246
247
248
249
250
251
252
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_brainwallet_sign_with_ref($0, seed_ref, suri_suffix, message) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
253
  }
254

255
  @objc func brainWalletAddressWithRef(_ seed_ref: Int64, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
256
257
258
259
260
261
262
263
264
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { brain_wallet_address_with_ref($0, seed_ref) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
265
  }
266
267


268
  @objc func substrateAddressWithRef(_ seed_ref: Int64, suri_suffix: String, prefix: UInt32, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
269
270
271
272
273
274
275
276
277
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_address_with_ref($0, seed_ref, suri_suffix, prefix) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
278
  }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

  @objc func substrateSecret(_ suri: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_mini_secret_key($0, suri) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
  }

  @objc func substrateSecretWithRef(_ seed_ref: Int64, suri_suffix: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
    handle_error(
      resolve: resolve,
      reject: reject,
      get_result: { substrate_mini_secret_key_with_ref($0, seed_ref, suri_suffix) },
      success: { (res: Optional<UnsafePointer<CChar>>) -> String in
        let val = String(cString: res!)
        signer_destroy_string(res!)
        return val
    })
  }
fro's avatar
fro committed
303
}