@functionland/react-native-fula 1.55.12 → 1.55.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +20 -20
  2. package/android/build.gradle +114 -114
  3. package/android/gradle.properties +8 -8
  4. package/android/src/main/AndroidManifest.xml +3 -3
  5. package/android/src/main/AndroidManifestNew.xml +2 -2
  6. package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
  7. package/android/src/main/java/land/fx/fula/Cryptography.java +62 -62
  8. package/android/src/main/java/land/fx/fula/FulaModule.java +2057 -1991
  9. package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
  10. package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
  11. package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
  12. package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
  13. package/ios/Cryptography.swift +59 -59
  14. package/ios/Fula-Bridging-Header.h +3 -3
  15. package/ios/Fula.mm +272 -262
  16. package/ios/Fula.swift +2284 -2207
  17. package/ios/Fula.xcodeproj/project.pbxproj +2 -2
  18. package/ios/UserDataHelper.swift +143 -143
  19. package/lib/commonjs/index.js +1 -2
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/interfaces/augment-api-consts.js.map +1 -1
  22. package/lib/commonjs/interfaces/augment-api-errors.js.map +1 -1
  23. package/lib/commonjs/interfaces/augment-api-events.js.map +1 -1
  24. package/lib/commonjs/interfaces/augment-api-query.js.map +1 -1
  25. package/lib/commonjs/interfaces/augment-api-rpc.js.map +1 -1
  26. package/lib/commonjs/interfaces/augment-api-runtime.js.map +1 -1
  27. package/lib/commonjs/interfaces/augment-api-tx.js.map +1 -1
  28. package/lib/commonjs/interfaces/augment-api.js.map +1 -1
  29. package/lib/commonjs/interfaces/augment-types.js.map +1 -1
  30. package/lib/commonjs/interfaces/definitions.js.map +1 -1
  31. package/lib/commonjs/interfaces/defintions.js.map +1 -1
  32. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  33. package/lib/commonjs/interfaces/index.js.map +1 -1
  34. package/lib/commonjs/interfaces/lookup.js +300 -300
  35. package/lib/commonjs/interfaces/lookup.js.map +1 -1
  36. package/lib/commonjs/interfaces/registry.js.map +1 -1
  37. package/lib/commonjs/interfaces/types-lookup.js.map +1 -1
  38. package/lib/commonjs/interfaces/types.js.map +1 -1
  39. package/lib/commonjs/protocols/blockchain.js +133 -67
  40. package/lib/commonjs/protocols/blockchain.js.map +1 -1
  41. package/lib/commonjs/protocols/chain-api.js.map +1 -1
  42. package/lib/commonjs/protocols/fula.js.map +1 -1
  43. package/lib/commonjs/protocols/fx-ai.js.map +1 -1
  44. package/lib/commonjs/protocols/fxblox.js +3 -3
  45. package/lib/commonjs/protocols/fxblox.js.map +1 -1
  46. package/lib/commonjs/types/blockchain.js.map +1 -1
  47. package/lib/commonjs/types/fxblox.js.map +1 -1
  48. package/lib/module/index.js.map +1 -1
  49. package/lib/module/interfaces/augment-api-consts.js.map +1 -1
  50. package/lib/module/interfaces/augment-api-errors.js.map +1 -1
  51. package/lib/module/interfaces/augment-api-events.js.map +1 -1
  52. package/lib/module/interfaces/augment-api-query.js.map +1 -1
  53. package/lib/module/interfaces/augment-api-rpc.js.map +1 -1
  54. package/lib/module/interfaces/augment-api-runtime.js.map +1 -1
  55. package/lib/module/interfaces/augment-api-tx.js.map +1 -1
  56. package/lib/module/interfaces/augment-api.js.map +1 -1
  57. package/lib/module/interfaces/augment-types.js.map +1 -1
  58. package/lib/module/interfaces/definitions.js.map +1 -1
  59. package/lib/module/interfaces/defintions.js.map +1 -1
  60. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  61. package/lib/module/interfaces/index.js.map +1 -1
  62. package/lib/module/interfaces/lookup.js +300 -300
  63. package/lib/module/interfaces/lookup.js.map +1 -1
  64. package/lib/module/interfaces/registry.js.map +1 -1
  65. package/lib/module/interfaces/types-lookup.js.map +1 -1
  66. package/lib/module/interfaces/types.js.map +1 -1
  67. package/lib/module/protocols/blockchain.js +130 -66
  68. package/lib/module/protocols/blockchain.js.map +1 -1
  69. package/lib/module/protocols/chain-api.js.map +1 -1
  70. package/lib/module/protocols/fula.js.map +1 -1
  71. package/lib/module/protocols/fx-ai.js.map +1 -1
  72. package/lib/module/protocols/fxblox.js +3 -3
  73. package/lib/module/protocols/fxblox.js.map +1 -1
  74. package/lib/module/types/blockchain.js.map +1 -1
  75. package/lib/module/types/fxblox.js.map +1 -1
  76. package/package.json +177 -176
  77. package/react-native-fula.podspec +47 -47
  78. package/src/index.tsx +4 -4
  79. package/src/interfaces/augment-api-consts.ts +273 -273
  80. package/src/interfaces/augment-api-errors.ts +474 -474
  81. package/src/interfaces/augment-api-events.ts +448 -448
  82. package/src/interfaces/augment-api-query.ts +466 -466
  83. package/src/interfaces/augment-api-rpc.ts +617 -617
  84. package/src/interfaces/augment-api-runtime.ts +223 -223
  85. package/src/interfaces/augment-api-tx.ts +709 -709
  86. package/src/interfaces/augment-api.ts +9 -9
  87. package/src/interfaces/augment-types.ts +1322 -1322
  88. package/src/interfaces/definitions.ts +1 -1
  89. package/src/interfaces/defintions.ts +1 -1
  90. package/src/interfaces/fulaNativeModule.ts +177 -175
  91. package/src/interfaces/index.ts +3 -3
  92. package/src/interfaces/lookup.ts +2011 -2011
  93. package/src/interfaces/registry.ts +163 -163
  94. package/src/interfaces/types-lookup.ts +2165 -2165
  95. package/src/interfaces/types.ts +2 -2
  96. package/src/protocols/blockchain.ts +716 -644
  97. package/src/protocols/fxblox.ts +442 -442
package/ios/Fula.swift CHANGED
@@ -1,2207 +1,2284 @@
1
- import Foundation
2
- import Foundation.NSDate // for TimeInterval
3
- import CommonCrypto
4
- import Wnfs
5
- import Fula
6
- import os.log
7
-
8
- extension OSLog {
9
-
10
- private static var subsystem = Bundle.main.bundleIdentifier!
11
-
12
- /// Logs the view cycles like a view that appeared.
13
- static let viewCycle = OSLog(subsystem: subsystem, category: "viewcycle")
14
-
15
- /// All logs related to tracking and analytics.
16
- static let statistics = OSLog(subsystem: subsystem, category: "statistics")
17
- }
18
-
19
- extension OSLog {
20
-
21
- func info(_ msg: String, _ args: CVarArg...) {
22
- os_log("%{public}@", log: self, type: .info, msg)
23
- }
24
-
25
- func error(_ msg: String, _ args: CVarArg...) {
26
- os_log("%{public}@", log: self, type: .error, msg)
27
- }
28
-
29
- // ... (more methods for different log levels, if needed)
30
- }
31
-
32
-
33
- @objc(FulaModule)
34
- class FulaModule: NSObject {
35
- public let NAME: String = "FulaModule"
36
- var fula: FulamobileClient?
37
-
38
- var client: Client?
39
- var wnfs: Wnfs?
40
- var fulaConfig: FulamobileConfig?
41
- var appDir: URL
42
- var fulaStorePath: String
43
- // Related to the Wnfs library
44
- var rootCid: Cid?
45
- var wnfsKey: Data?
46
- let userDataHelper = UserDataHelper()
47
- var secretKeyGlobal: Array<UInt8>?
48
- var identityEncryptedGlobal: String?
49
- static let PRIVATE_KEY_STORE_ID = "PRIVATE_KEY"
50
-
51
- // FIXME: Hardcoded ,fula should remove all the codec arguments as the rs-wnfs library removed its usage now.
52
- static let CODEC_DAG_CBOR = Int(113)
53
-
54
- enum MyError: Error {
55
- case runtimeError(String)
56
- }
57
-
58
- @objc(Client)
59
- public class Client: NSObject {
60
- let internalClient: FulamobileClient
61
-
62
- init(clientInput: FulamobileClient) {
63
- internalClient = clientInput
64
- }
65
-
66
- func get(_ cid: Data) throws -> Data {
67
- do {
68
- print(String(format: "ReactNative get cid: %s", cid.toHex()))
69
- return try internalClient.get(cid)
70
- } catch let error {
71
- throw error
72
- }
73
- }
74
-
75
- func put(_ cid: Data, _ data: Data) throws -> Void {
76
- do {
77
- print(String(format: "ReactNative put(%s) data: %s", cid.toHex(), data.toHex()))
78
- try internalClient.put(data, codec: 0xFF & Int64(cid[1]))
79
- } catch let error {
80
- print("ReactNative put: ", error.localizedDescription)
81
- throw error
82
- }
83
- }
84
- }
85
- override init() {
86
-
87
- self.appDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
88
- let fulaStoreURL = appDir.appendingPathComponent("/fula")
89
-
90
- self.fulaStorePath = fulaStoreURL.path
91
- let fileManager = FileManager.default
92
- var success = true
93
- if !fileManager.fileExists(atPath: fulaStorePath) {
94
- do{
95
- try fileManager.createDirectory(atPath: fulaStorePath, withIntermediateDirectories: true)
96
- }catch let error{
97
- print(error.localizedDescription)
98
- success = false
99
- }
100
-
101
- }
102
- if success {
103
- print("Fula store folder created")
104
- } else {
105
- print("Unable to create fula store folder!")
106
-
107
- }
108
- super.init()
109
-
110
- }
111
- func convertConfigToJson(config: Cid) -> String {
112
- return String(format: "{\"cid\": \"%@\"}", config)
113
- }
114
-
115
- func getName() -> String {
116
- return NAME
117
- }
118
-
119
- func toByte(_ input: String) -> Data {
120
- return input.data(using: .utf8)!
121
- }
122
-
123
-
124
- func stringArrToIntArr(_ s: Array<String>) -> Array<Int> {
125
- return s.map { Int($0)!}
126
- }
127
-
128
- func convertIntToByte(_ s: Array<Int>) -> Array<UInt8> {
129
- return s.map { UInt8(exactly: $0)!}
130
- }
131
-
132
- func convertStringToByte(_ input: String) -> Array<UInt8> {
133
- let splitted = input.split(separator: ",").map { String($0) }
134
- let keyInt = stringArrToIntArr(splitted)
135
- return convertIntToByte(keyInt)
136
- }
137
-
138
- @objc func applicationWillResignActive() {
139
- // Log that the app will resign active
140
- os_log("Application will resign active", log: OSLog.viewCycle, type: .info)
141
- }
142
-
143
- @objc func applicationDidEnterBackground() {
144
- // Log that the app has entered the background
145
- os_log("Application did enter background", log: OSLog.viewCycle, type: .info)
146
- }
147
-
148
- @objc func applicationWillTerminate() {
149
- // Attempt to shut down Fula cleanly (similar to onHostDestroy)
150
- os_log("Application will terminate - shutting down Fula", log: OSLog.viewCycle, type: .info)
151
- do {
152
- if let fulaClient = self.fula {
153
- try fulaClient.shutdown()
154
- os_log("Fula shutdown successfully.", log: OSLog.viewCycle, type: .info)
155
- }
156
- } catch {
157
- os_log("Error shutting down Fula: %{public}@", log: OSLog.viewCycle, type: .error, String(describing: error))
158
- }
159
- }
160
-
161
- @objc(registerLifecycleListener:withRejecter:)
162
- func registerLifecycleListener(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
163
- NotificationCenter.default.addObserver(
164
- self,
165
- selector: #selector(applicationWillResignActive),
166
- name: UIApplication.willResignActiveNotification,
167
- object: nil)
168
-
169
- NotificationCenter.default.addObserver(
170
- self,
171
- selector: #selector(applicationDidEnterBackground),
172
- name: UIApplication.didEnterBackgroundNotification,
173
- object: nil)
174
-
175
- NotificationCenter.default.addObserver(
176
- self,
177
- selector: #selector(applicationWillTerminate),
178
- name: UIApplication.willTerminateNotification,
179
- object: nil)
180
-
181
- // Assuming the operation is always successful
182
- resolve(true)
183
- }
184
-
185
- deinit {
186
- NotificationCenter.default.removeObserver(self)
187
- }
188
-
189
- @objc(checkConnection:withResolver:withRejecter:)
190
- func checkConnection(timeout: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
191
- NSLog("ReactNative checkConnection started with timeout=\(timeout)")
192
-
193
- if let timeoutInt = timeout as? Int {
194
- if self.fula != nil {
195
- DispatchQueue.global(qos: .default).async {
196
- do {
197
- let connectionStatus = try self.checkConnectionInternal(timeout: timeoutInt)
198
- NSLog("ReactNative checkConnection ended \(connectionStatus)")
199
- resolve(connectionStatus)
200
- }
201
- catch let error {
202
- NSLog("ReactNative checkConnection failed with Error: \(error.localizedDescription)")
203
- resolve(false)
204
- }
205
- }
206
- } else {
207
- NSLog("ReactNative checkConnection fula is null")
208
- resolve(false)
209
- }
210
- } else {
211
- OSLog.viewCycle.error("ReactNative checkConnection - invalid timeout value")
212
- reject("ERR_INVALID_TIMEOUT", "Invalid timeout value", nil)
213
- }
214
- }
215
-
216
-
217
- @objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:)
218
- func newClient(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
219
- print("ReactNative", "newClient storePath= " , storePath , " bloxAddr= " , bloxAddr , " exchange= " , exchange , " autoFlush= " , autoFlush , " useRelay= " , useRelay , " refresh= " , refresh)
220
- do {
221
- print("ReactNative", "newClient storePath= ", storePath)
222
- let identity = toByte(identityString)
223
- print("ReactNative", "newClient identity= ", identityString)
224
- try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
225
- let peerId = self.fula?.id_()
226
- resolve(peerId)
227
- } catch let error {
228
- print("ReactNative", "newClient failed with Error: ", error.localizedDescription)
229
- reject("ERR_FULA", "Can't create client", error)
230
- }
231
-
232
- }
233
-
234
- @objc(isReady:withResolver:withRejecter:)
235
- func isReady(filesystemCheck: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
236
- print("ReactNative", "isReady started")
237
- var initialized = false
238
- do {
239
- if (self.fula != nil && !self.fula!.id_().isEmpty) {
240
- if (filesystemCheck) {
241
- if (self.client != nil && rootCid != nil && !rootCid!.isEmpty) {
242
- initialized = true
243
- }
244
- } else {
245
- initialized = true
246
- }
247
- }
248
- resolve(initialized)
249
- } catch let error {
250
- print("ReactNative", "isReady failed with Error: " + error.localizedDescription)
251
- reject("ERR_FULA", "Check if fula is ready", error)
252
- }
253
- }
254
-
255
- //TODO: we should consider refactor the name of this \
256
- // function to be compatible with the android version.
257
- @objc(initFula:withStorePath:withBloxAddr:withExchange:withAutoFlush:withRootConfig:withUseRelay:withRefresh:withResolver:withRejecter:)
258
- func initFula(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, rootConfig: String, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
259
-
260
- NSLog("ReactNative - init started")
261
-
262
- do {
263
-
264
- var resultData = Dictionary<String, String>()
265
- NSLog("ReactNative init storePath= \(storePath)")
266
- let identity = self.toByte(identityString)
267
- NSLog("ReactNative init identity= \(identityString)")
268
- let obj = try initInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, _rootCid: rootConfig, useRelay: useRelay, refresh: refresh)
269
- NSLog("ReactNative init object created: [ \(obj[0]), \(obj[1]), \(obj[2]) ]")
270
- resultData["peerId"] = obj[0]
271
- resultData["rootCid"] = obj[1]
272
- resultData["wnfs_key"] = obj[2]
273
- resolve(resultData as NSDictionary)
274
-
275
- } catch let error {
276
- NSLog("ReactNative init failed with Error: \(error.localizedDescription)")
277
- reject("ERR_FULA", "init failed", error)
278
- }
279
-
280
- }
281
-
282
- @objc(logout:withStorePath:withResolver:withRejecter:)
283
- func logout(identityString: String, storePath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
284
-
285
- print("ReactNative", "logout started")
286
- do {
287
- let identity = toByte(identityString)
288
- try logoutInternal(identity: identity, _storePath: storePath)
289
- print("ReactNative", "logout completed")
290
- resolve("")
291
-
292
- } catch let error {
293
- print("ReactNative", "logout failed with Error: ", error.localizedDescription)
294
- reject("ERR_FULA", "logout failed", error)
295
- }
296
-
297
- }
298
-
299
- func checkConnectionInternal(timeout: Int) throws -> Bool {
300
- NSLog("ReactNative checkConnectionInternal started with timeout: \(timeout)")
301
- var connectionStatus = false
302
-
303
- if let fula = self.fula {
304
- let semaphore = DispatchSemaphore(value: 0)
305
- let queue = DispatchQueue(label: "com.yourapp.checkConnection", attributes: .concurrent)
306
-
307
- queue.async {
308
- do {
309
- NSLog("ReactNative connectToBlox started")
310
- try fula.connectToBlox()
311
- connectionStatus = true
312
- NSLog("ReactNative checkConnectionInternal succeeded")
313
- semaphore.signal()
314
- } catch let error {
315
- NSLog("ReactNative checkConnectionInternal failed with Error: \(error.localizedDescription)")
316
- semaphore.signal()
317
- }
318
- }
319
-
320
- let timeoutResult = semaphore.wait(timeout: .now() + .seconds(timeout))
321
- switch timeoutResult {
322
- case .timedOut:
323
- NSLog("ReactNative checkConnectionInternal timed out")
324
- return false
325
- case .success:
326
- return connectionStatus
327
- }
328
- } else {
329
- NSLog("ReactNative checkConnectionInternal failed because fula is not initialized")
330
- return false
331
- }
332
- }
333
-
334
- @objc(checkFailedActions:withTimeout:withResolver:withRejecter:)
335
- func checkFailedActions(retry: Bool, timeout: Int, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
336
- do {
337
- guard let fula = self.fula else {
338
- throw NSError(domain: "ERR_FULA", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula is not initialized"])
339
- }
340
-
341
- if !retry {
342
- NSLog("ReactNative checkFailedActions without retry")
343
- let failedLinks = try fula.listFailedPushes()
344
-
345
- let nextFailedLink = try failedLinks.next()
346
- if nextFailedLink != nil {
347
- // Assuming nextFailedLink is of type Data; replace `toHex()` with an appropriate method to convert Data to a hex string
348
- NSLog("ReactNative checkFailedActions found")
349
- resolve(true)
350
- } else {
351
- resolve(false)
352
- }
353
- } else {
354
- NSLog("ReactNative checkFailedActions with retry")
355
- let retryResults = try retryFailedActionsInternal(timeout: timeout) // Ensure retryFailedActionsInternal accepts a timeout parameter
356
- resolve(!retryResults)
357
- }
358
- } catch let error {
359
- NSLog("ReactNative checkFailedActions failed with Error: \(error.localizedDescription)")
360
- reject("ERR_FULA", "CheckFailedActions failed", error)
361
- }
362
- }
363
-
364
-
365
-
366
- func retryFailedActionsInternal(timeout: Int) throws -> Bool {
367
- NSLog("ReactNative retryFailedActionsInternal started")
368
-
369
- guard let fula = self.fula else {
370
- NSLog("ReactNative retryFailedActionsInternal failed because fula is not initialized")
371
- return false
372
- }
373
-
374
- do {
375
- let connectionCheck = try checkConnectionInternal(timeout: timeout)
376
-
377
- if connectionCheck {
378
- do {
379
- NSLog("ReactNative retryFailedPushes started")
380
- try fula.retryFailedPushes()
381
- NSLog("ReactNative flush started")
382
- try fula.flush()
383
- return true
384
- } catch let error {
385
- try fula.flush()
386
- NSLog("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
387
- return false
388
- }
389
- } else {
390
- NSLog("ReactNative retryFailedActionsInternal failed because blox is offline")
391
- return false
392
- }
393
- } catch let error {
394
- NSLog("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
395
- return false
396
- }
397
- }
398
-
399
- func createPeerIdentity(privateKey: Data) throws -> Data {
400
- let secretKey = try Cryptography.generateKey(privateKey)
401
-
402
- var encryptedKey: String? = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
403
- NSLog("ReactNative createPeerIdentity encryptedKey=\(encryptedKey ?? "nil")")
404
- if encryptedKey == nil {
405
- let privateKeyString = String(data: privateKey, encoding: .utf8) ?? ""
406
-
407
- guard !privateKeyString.isEmpty else {
408
- throw NSError(domain: "KeyGenerationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Private key string conversion failed"])
409
- }
410
- var error: NSError?
411
- guard let autoGeneratedIdentity = FulamobileGenerateEd25519KeyFromString(privateKeyString, &error)?.toUint8Array() else {
412
- throw error ?? NSError(domain: "KeyGenerationError", code: -1, userInfo: nil)
413
- }
414
- encryptedKey = try Cryptography.encryptMsg([UInt8](autoGeneratedIdentity), [UInt8](secretKey))
415
- NSLog("ReactNative createPeerIdentity encryptedKey2=\(encryptedKey)")
416
- userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey ?? "")
417
- }
418
-
419
- // Assuming decryptMsg returns Data or throws an error if decryption fails
420
- guard let encryptedKeyData = encryptedKey, !encryptedKeyData.isEmpty else {
421
- throw NSError(domain: "DecryptionError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Encrypted key is empty"])
422
- }
423
- let decryptedData = try Cryptography.decryptMsg(encryptedKeyData, Array(secretKey))
424
- let hexString = decryptedData.map { String(format: "%02hhx", $0) }.joined()
425
- NSLog("ReactNative createPeerIdentity decryptedData=\(hexString)")
426
- return Data(decryptedData)
427
- }
428
-
429
- func decryptLibp2pIdentity(_ encryptedLibp2pId: String, with encryptionSecretKey: Data) throws -> String {
430
- // Convert Data to [UInt8]
431
- let secretKeyBytes = [UInt8](encryptionSecretKey)
432
-
433
- // Attempt decryption
434
- guard let decryptedBytes = try? Cryptography.decryptMsg(encryptedLibp2pId, secretKeyBytes) else {
435
- throw NSError(domain: "DecryptionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to decrypt Libp2p ID"])
436
- }
437
-
438
- // Assuming decryptedBytes is an array of UInt8
439
- return String(decoding: decryptedBytes, as: UTF8.self)
440
- }
441
-
442
- func createEncryptedLibp2pId(from identity: Data, with encryptionSecretKey: Data) throws -> String {
443
- var error: NSError?
444
- guard let autoGeneratedIdentity = FulamobileGenerateEd25519Key(&error) else {
445
- throw error ?? NSError(domain: "KeyGenerationError", code: -1, userInfo: nil)
446
- }
447
- let encryptedLibp2pId = try Cryptography.encryptMsg(Array(autoGeneratedIdentity), Array(encryptionSecretKey))
448
- userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedLibp2pId)
449
- return encryptedLibp2pId
450
- }
451
-
452
- func createNewrootCid(identity: Data) throws -> Void {
453
- let hash32 = identity.sha256()
454
- NSLog("ReactNative createNewrootCid wnfsKey= \(identity.toHex()) , hash32 = \(hash32.toHex())")
455
- if (self.fula != nil) {
456
- NSLog("ReactNative createNewrootCid self.fula not null")
457
- try self.fula?.flush()
458
- }
459
- NSLog("ReactNative fula flushed")
460
- rootCid = try self.wnfs?.Init(wnfsKey: hash32)
461
- NSLog("ReactNative privateForest is created: \(rootCid!)")
462
- wnfsKey = identity
463
- NSLog("ReactNative rootCid is created: cid= \(rootCid!) wnfs_key= \(wnfsKey!.toHex()), hash32=\(hash32)")
464
- try encryptAndStoreConfig()
465
- }
466
-
467
- func loadWnfs(_ _wnfsKey: Data , _ _rootCid: String) throws {
468
- NSLog("ReactNative loadWnfs called: _rootCid=\(_rootCid)")
469
- let hash32 = _wnfsKey.sha256()
470
- NSLog("ReactNative wnfsKey= \(_wnfsKey.toHex()) ; hash32 = \(hash32.toHex())")
471
- try self.wnfs?.LoadWithWNFSKey(wnfsKey: hash32, cid: _rootCid)
472
- NSLog("ReactNative loadWnfs LoadWithWNFSKey")
473
- rootCid = _rootCid
474
- wnfsKey = _wnfsKey
475
- NSLog("ReactNative loadWnfs called: rootCid=\(rootCid)")
476
- if (self.fula != nil) {
477
- try self.fula?.flush()
478
- }
479
- NSLog("ReactNative loadWnfs completed")
480
- try encryptAndStoreConfig()
481
- }
482
-
483
-
484
- func encryptAndStoreConfig() throws {
485
- do {
486
- if let identityEncryptedGlobalUnwrapped = identityEncryptedGlobal {
487
- NSLog("ReactNative encryptAndStoreConfig started")
488
-
489
- if let rootCidUnwrapped = rootCid, let wnfsKeyUnwrapped = wnfsKey, let secretKeyGlobalUnwrapped = secretKeyGlobal {
490
- NSLog("ReactNative encryptAndStoreConfig started with rootCid: \(rootCidUnwrapped.toUint8Array()) and wnfsKey:\(wnfsKeyUnwrapped)")
491
-
492
- let cid_encrypted = try Cryptography.encryptMsg(rootCidUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
493
- NSLog("ReactNative encryptAndStoreConfig cid_encrypted: \(cid_encrypted)")
494
-
495
- let wnfs_key_encrypted = try Cryptography.encryptMsg(wnfsKeyUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
496
- NSLog("ReactNative encryptAndStoreConfig wnfs_key_encrypted: \(wnfs_key_encrypted)")
497
-
498
- userDataHelper.add("cid_encrypted_" + identityEncryptedGlobalUnwrapped, cid_encrypted)
499
- userDataHelper.add("wnfs_key_encrypted_" + identityEncryptedGlobalUnwrapped, wnfs_key_encrypted)
500
- } else {
501
- // Handle the case where rootCid, wnfsKey, or secretKeyGlobal is nil
502
- NSLog("ReactNative encryptAndStoreConfig failed because one of the values is nil")
503
- }
504
- }
505
- } catch let error {
506
- NSLog("ReactNative encryptAndStoreConfig failed with Error: \(error.localizedDescription)")
507
- throw error
508
- }
509
- }
510
-
511
- func logoutInternal(identity: Data, _storePath: String?) throws {
512
- do {
513
- if (self.fula != nil) {
514
- try self.fula?.flush()
515
- }
516
- let secretKey = try Cryptography.generateKey(identity)
517
- let identity_encrypted: String = try Cryptography.encryptMsg(identity.toUint8Array(), [UInt8](secretKey))
518
-
519
- userDataHelper.remove("cid_encrypted_"+identity_encrypted)
520
-
521
- //TODO: Should also remove peerid @Mahdi
522
-
523
- userDataHelper.remove("cid_encrypted_"+identity_encrypted)
524
-
525
- rootCid = nil
526
- secretKeyGlobal = nil
527
- identityEncryptedGlobal = nil
528
- var storePath = _storePath
529
- if (storePath == nil || storePath!.isEmpty) {
530
- storePath = fulaStorePath
531
- }
532
-
533
- do{
534
- try FileManager.default.removeItem(atPath: fulaStorePath)
535
- }catch let error{
536
- print("Deleting fula store path", error.localizedDescription)
537
- throw error
538
- }
539
- } catch let error {
540
- print("ReactNative", "logout internal failed with Error: ", error.localizedDescription)
541
- throw error
542
- }
543
- }
544
-
545
- func getFulaClient() -> FulamobileClient? {
546
- return self.fula
547
- }
548
-
549
- func newClientInternal(identity: Data, storePath: String?, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool) throws -> Void {
550
- do {
551
- NSLog("ReactNative fula newClientInternal refresh=\(refresh)")
552
- fulaConfig = FulamobileConfig()
553
- NSLog("ReactNative: cofig is set")
554
- if (storePath == nil || storePath!.isEmpty) {
555
- fulaConfig!.storePath = fulaStorePath
556
- } else {
557
- fulaConfig!.storePath = storePath!
558
- }
559
- NSLog("ReactNative storePath is set: \(fulaConfig!.storePath)")
560
-
561
- let peerIdentity = try createPeerIdentity(privateKey: identity)
562
- fulaConfig!.identity = peerIdentity
563
- NSLog("ReactNative peerIdentity is set: \(fulaConfig!.identity!.toHex())")
564
- fulaConfig!.bloxAddr = bloxAddr
565
- NSLog("ReactNative bloxAddr is set: \(fulaConfig!.bloxAddr)")
566
- fulaConfig!.exchange = exchange
567
- fulaConfig!.syncWrites = autoFlush
568
- if (useRelay) {
569
- fulaConfig!.allowTransientConnection = true
570
- fulaConfig!.forceReachabilityPrivate = true
571
- }
572
- if (self.fula == nil || refresh) {
573
- NSLog("ReactNative Creating a new Fula instance")
574
- do {
575
- try shutdownInternal()
576
- NSLog("ReactNative Creating a new Fula instance shutdown done")
577
- var error: NSError?
578
- let client = FulamobileNewClient(fulaConfig, &error)
579
- if let error = error {
580
- throw error
581
- }
582
-
583
- self.fula = client
584
- NSLog("ReactNative FulamobileClient created")
585
- if (self.fula != nil) {
586
- NSLog("ReactNative Creating a new Fula instance fula is not null, flushing")
587
- try self.fula?.flush()
588
- } else {
589
- NSLog("** ReactNative Creating a new Fula instance fula failed **")
590
- }
591
- } catch let error {
592
- NSLog("ReactNative Failed to create new Fula instance: \(error.localizedDescription)")
593
- throw MyError.runtimeError("ReactNative Failed to create new Fula instance")
594
- }
595
- }
596
- NSLog("ReactNative peerIdentity returned: \(peerIdentity)")
597
- } catch let error {
598
- NSLog("ReactNative newclientInternal failed with Error: \(error.localizedDescription)")
599
- throw error
600
- }
601
- }
602
-
603
- func initInternal(identity: Data, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, _rootCid: String, useRelay: Bool, refresh: Bool) throws -> [String] {
604
-
605
- do {
606
- os_log("ReactNative: This is an info message.", log: OSLog.viewCycle, type: .info)
607
- NSLog("ReactNative: This is a simple log message.")
608
-
609
- NSLog("ReactNative fula initInternal=\(refresh)")
610
- if (self.fula == nil || refresh) {
611
- NSLog("ReactNative fula self.fula is null or refresh is set")
612
- try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
613
- NSLog("ReactNative fula initialized")
614
- if (self.fula == nil) {
615
- NSLog("ReactNative: fula is not initialized")
616
- }
617
- guard let fulaId = self.fula?.id_() else {
618
- NSLog("ReactNative error: fula is not initialized")
619
- throw MyError.runtimeError("ReactNative: fula client not ready")
620
- }
621
- NSLog("ReactNative fula initialized: \(fulaId)")
622
- }
623
- if(self.client == nil || self.wnfs == nil || refresh) {
624
- NSLog("ReactNative fula self.client is null or refresh is set")
625
- self.client = Client(clientInput: self.fula!)
626
- self.wnfs = Wnfs(putFn: { cid, data in
627
- guard let c = self.client else {
628
- NSLog("ReactNative wnfs put: fula client not ready")
629
- throw MyError.runtimeError("ReactNative wnfs: fula client not ready")
630
- }
631
- try c.put(cid, data)
632
- }, getFn: { cid in
633
- guard let c = self.client else {
634
- NSLog("ReactNative wnfs get: fula client not ready")
635
- throw MyError.runtimeError("ReactNative wnfs: fula client not ready")
636
- }
637
- return try c.get(cid)
638
- })
639
- NSLog("ReactNative wnfs initialized")
640
- }
641
-
642
- let secretKey = try Cryptography.generateKey(identity)
643
- let identity_encrypted = try Cryptography.encryptMsg(identity.toUint8Array(), [UInt8](secretKey))
644
-
645
- identityEncryptedGlobal = identity_encrypted
646
- secretKeyGlobal = [UInt8](secretKey)
647
-
648
- if (rootCid == nil || rootCid!.isEmpty) {
649
- NSLog("ReactNative rootCid is empty.")
650
- //Load from keystore
651
-
652
- let cid_encrypted_fetched = userDataHelper.getValue("cid_encrypted_"+identity_encrypted)
653
- NSLog("ReactNative Here1")
654
- var cid: Array<UInt8>? = nil
655
- if(cid_encrypted_fetched != nil && !cid_encrypted_fetched!.isEmpty) {
656
- NSLog("ReactNative decrypting cid= \(cid_encrypted_fetched!) with secret \(secretKey.toHex())")
657
- cid = try Cryptography.decryptMsg(cid_encrypted_fetched!, [UInt8](secretKey))
658
-
659
- }
660
- NSLog("ReactNative Here2")
661
- //print("ReactNative", "Attempted to fetch cid from keystore cid="+cid+" & wnfs_key="+wnfs_key)
662
- if(cid == nil || cid!.isEmpty){
663
- NSLog("ReactNative cid or wnfs key was not found")
664
- if(!_rootCid.isEmpty){
665
- NSLog("ReactNative Re-setting cid from input: \(_rootCid)")
666
- cid = _rootCid.toUint8Array()
667
- }
668
-
669
- }
670
- if(cid == nil || cid!.isEmpty){
671
- NSLog("ReactNative Tried to recover cid but was not successful. Creating ones")
672
- try createNewrootCid(identity: identity)
673
- } else {
674
- NSLog("ReactNative Found cid and wnfs key in keychain store")
675
- NSLog("ReactNative Recovered cid and private ref from keychain store. cid=\(cid!) & wnfs_key=\(identity)")
676
- try loadWnfs(identity, cid!.toData().toUTF8String()!)
677
- }
678
- NSLog("ReactNative creating/reloading rootCid completed")
679
-
680
- /*
681
- byte[] testbyte = convertStringToByte("-104,40,24,-93,24,100,24,114,24,111,24,111,24,116,24,-126,24,-126,0,0,24,-128,24,103,24,118,24,101,24,114,24,115,24,105,24,111,24,110,24,101,24,48,24,46,24,49,24,46,24,48,24,105,24,115,24,116,24,114,24,117,24,99,24,116,24,117,24,114,24,101,24,100,24,104,24,97,24,109,24,116")
682
- long testcodec = 85
683
- byte[] testputcid = client!.put(testbyte, testcodec)
684
- print("ReactNative", "client!.put test done"+ Arrays.toString(testputcid))
685
- byte[] testfetchedcid = convertStringToByte("1,113,18,32,-6,-63,-128,79,-102,-89,57,77,-8,67,-98,8,-81,40,-87,123,122,29,-52,-124,-60,-53,100,105,125,123,-5,-99,41,106,-124,-64")
686
- byte[] testfetchedbytes = client!.get(testfetchedcid)
687
- print("ReactNative", "client!.get test done"+ Arrays.toString(testfetchedbytes))
688
- */
689
-
690
-
691
- NSLog("ReactNative rootCid is created: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
692
- } else {
693
- NSLog("ReactNative rootCid existed: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
694
- }
695
- guard let peerId = self.fula?.id_() else {
696
- NSLog("ReactNative error: fula is not initialized")
697
- throw MyError.runtimeError("ReactNative: fula client not ready")
698
- }
699
- NSLog("ReactNative fula peerId initialized: \(peerId)")
700
- var obj = [String]()
701
- obj.append(peerId)
702
- obj.append(rootCid!)
703
- obj.append(wnfsKey!.toHex())
704
- NSLog("ReactNative initInternal is completed successfully")
705
- if (self.fula != nil) {
706
- try self.fula?.flush()
707
- }
708
- return obj
709
- } catch let error {
710
- NSLog("ReactNative init internal failed with Error: \(error.localizedDescription)")
711
- throw error
712
- }
713
- }
714
-
715
-
716
- @objc(mkdir:withResolver:withRejecter:)
717
- func mkdir(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
718
- NSLog("ReactNative mkdir: path = \(path)")
719
- do {
720
- let cid = try self.wnfs?.MkDir(cid: rootCid!, remotePath: path)
721
- if(cid != nil) {
722
- rootCid = cid
723
- try encryptAndStoreConfig()
724
- if (self.fula != nil) {
725
- try self.fula?.flush()
726
- }
727
- resolve(rootCid)
728
- } else {
729
- NSLog("ReactNative mkdir Error: config is nil")
730
- reject("ERR_WNFS", "Can't make dir", nil)
731
- }
732
-
733
- } catch let error{
734
- print("mkdir", error.localizedDescription)
735
- reject("ERR_WNFS", "mkdir", error)
736
- }
737
- }
738
-
739
- @objc(writeFile:withLocalFilename:withResolver:withRejecter:)
740
- func writeFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
741
- /*
742
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
743
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
744
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
745
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
746
- // Returns: new cid of the root after this file is placed in the tree
747
- */
748
- NSLog("ReactNative writeFile to : path = \(fulaTargetFilename) + from: \(localFilename)")
749
- do {
750
- let cid = try self.wnfs?.WriteFileFromPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
751
- if(cid != nil) {
752
- rootCid = cid
753
- try encryptAndStoreConfig()
754
- if (self.fula != nil) {
755
- try self.fula?.flush()
756
- }
757
- resolve(rootCid)
758
- } else {
759
- NSLog("ReactNative writeFile Error: config is nil")
760
- reject("ERR_WNFS", "writeFile Error: config is nil", nil)
761
- }
762
- } catch let error {
763
- print("writeFile", error.localizedDescription)
764
- reject("ERR_WNFS", "writeFile", error)
765
- }
766
- }
767
-
768
- @objc(writeFileContent:withContentString:withResolver:withRejecter:)
769
- func writeFileContent(path: String, contentString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
770
-
771
- NSLog("ReactNative writeFile: contentString = \(contentString)")
772
- NSLog("ReactNative writeFile: path = \(path)")
773
- do {
774
- let content = convertStringToByte(contentString)
775
- let cid = try self.wnfs?.WriteFile(cid: rootCid!, remotePath: path, data: content.toData())
776
- rootCid = cid
777
- try encryptAndStoreConfig()
778
- if (self.fula != nil) {
779
- try self.fula?.flush()
780
- }
781
- resolve(rootCid)
782
- } catch let error {
783
- print("writeFileContent", error.localizedDescription)
784
- reject("ERR_WNFS", "writeFileContent", error)
785
- }
786
-
787
- }
788
-
789
- @objc(ls:withResolver:withRejecter:)
790
- func ls(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
791
- NSLog("ReactNative ls: path = \(path)")
792
- do {
793
- let res = try self.wnfs?.Ls(cid: rootCid!, remotePath: path)
794
-
795
- //JSONArray jsonArray = new JSONArray(res)
796
- guard let s = res?.toUTF8String() else {
797
- throw MyError.runtimeError("ReactNative converting bytes to utf8 string")
798
- }
799
- NSLog("ReactNative ls: res = \(s)")
800
- resolve(s)
801
- } catch let error {
802
- print("ls", error.localizedDescription)
803
- reject("ERR_WNFS", "ls", error)
804
- }
805
-
806
- }
807
-
808
- @objc(rm:withResolver:withRejecter:)
809
- func rm(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
810
-
811
- print("ReactNative", "rm: path = " + path)
812
- do {
813
- let cid = try self.wnfs?.Rm(cid: rootCid!, remotePath: path)
814
- if(cid != nil) {
815
- rootCid = cid
816
- try encryptAndStoreConfig()
817
- if (self.fula != nil) {
818
- try self.fula?.flush()
819
- }
820
- resolve(rootCid)
821
- } else {
822
- print("ReactNative", "rm Error: config is nil")
823
- reject("ERR_WNFS", "rm Error: config is nil", nil)
824
- }
825
- } catch let error {
826
- print("rm", error.localizedDescription)
827
- reject("ERR_WNFS", "rm", error)
828
- }
829
-
830
- }
831
-
832
- @objc(cp:withTargetPath:withResolver:withRejecter:)
833
- func cp(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
834
-
835
- print("ReactNative", "rm: sourcePath = " + sourcePath)
836
- do {
837
- let cid = try self.wnfs?.Cp(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
838
- if(cid != nil) {
839
- rootCid = cid
840
- try encryptAndStoreConfig()
841
- if (self.fula != nil) {
842
- try self.fula?.flush()
843
- }
844
- resolve(rootCid)
845
- } else {
846
- print("ReactNative", "cp Error: config is nil")
847
- reject("ERR_WNFS", "cp Error: config is nil", nil)
848
- }
849
- } catch let error {
850
- print("cp", error.localizedDescription)
851
- reject("ERR_WNFS", "cp", error)
852
- }
853
-
854
- }
855
-
856
- @objc(mv:withTargetPath:withResolver:withRejecter:)
857
- func mv(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
858
- print("ReactNative", "rm: sourcePath = " + sourcePath)
859
- do {
860
- let cid = try self.wnfs?.Mv(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
861
- if(cid != nil) {
862
- rootCid = cid
863
- try encryptAndStoreConfig()
864
- if (self.fula != nil) {
865
- try self.fula?.flush()
866
- }
867
- resolve(rootCid)
868
- } else {
869
- print("ReactNative", "mv Error: config is nil")
870
- reject("ERR_WNFS", "mv Error: config is nil", nil)
871
- }
872
- } catch let error {
873
- print("mv", error.localizedDescription)
874
- reject("ERR_WNFS", "mv", error)
875
- }
876
-
877
- }
878
-
879
- @objc(readFile:withLocalFilename:withResolver:withRejecter:)
880
- func readFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
881
- /*
882
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
883
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
884
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
885
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
886
- // Returns: new cid of the root after this file is placed in the tree
887
- */
888
- print("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename)
889
- do {
890
- let path = try self.wnfs?.ReadFileToPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
891
- resolve(path)
892
- } catch let error {
893
- print("readFile", error.localizedDescription)
894
- reject("ERR_WNFS", "readFile", error)
895
- }
896
-
897
- }
898
-
899
- @objc(readFileContent:withResolver:withRejecter:)
900
- func readFileContent(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
901
-
902
- print("ReactNative", "readFileContent: path = " + path)
903
- do {
904
- // FIXME: dhouldn't we output an NSData object instead?
905
- let res = try self.wnfs?.ReadFile(cid: rootCid!, remotePath: path)
906
- guard let resString = res?.toUTF8String() else{
907
- throw MyError.runtimeError(" ReactNative converting bytes to utf8 string")
908
- }
909
- resolve(resString)
910
- } catch let error {
911
- print("readFileContent", error.localizedDescription)
912
- reject("ERR_WNFS", "readFileContent", error)
913
- }
914
-
915
- }
916
-
917
- @objc(get:withResolver:withRejecter:)
918
- func get(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
919
-
920
- print("ReactNative", "get: keyString = " + keyString)
921
- do {
922
- let key: Data = convertStringToByte(keyString).toData()
923
- let value = try getInternal(key)
924
- let valueString: String = value.toUTF8String()!
925
- resolve(valueString)
926
- } catch let error {
927
- print("get", error.localizedDescription)
928
- reject("ERR_FULA", "get", error)
929
- }
930
-
931
- }
932
-
933
- func getInternal(_ key: Data) throws -> Data {
934
- do {
935
- print("ReactNative", "getInternal: key.toUTF8String() = " , key.toUTF8String()!)
936
- print("ReactNative", "getInternal: key.toHex().bytes = " , key.toHex())
937
- let value = try self.fula!.get(key)
938
- print("ReactNative", "getInternal: value.toHex() = " , value.toHex())
939
- return value
940
- } catch let error {
941
- print("ReactNative", "getInternal: error = " + error.localizedDescription)
942
- print("getInternal", error.localizedDescription)
943
- throw error
944
- }
945
- }
946
-
947
- @objc(has:withResolver:withRejecter:)
948
- func has(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
949
-
950
- print("ReactNative", "has: keyString = " + keyString)
951
- do {
952
- let key: Data = convertStringToByte(keyString).toData()
953
- let result = try hasInternal(key)
954
- resolve(result)
955
- } catch let error {
956
- print("has", error.localizedDescription)
957
- reject("ERR_FULA", "has", error)
958
- }
959
-
960
- }
961
-
962
- func hasInternal(_ key: Data) throws -> Bool {
963
- do {
964
- let ret = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
965
- try self.fula?.has(key, ret0_: ret)
966
- let res = ret.pointee.boolValue
967
- ret.deallocate()
968
- return res
969
- } catch let error {
970
- print("hasInternal", error.localizedDescription)
971
- throw error
972
- }
973
- }
974
-
975
- func pullInternal(key: Data) throws -> Void {
976
- do {
977
- try self.fula!.pull(key)
978
- } catch let error {
979
- print("pullInternal", error.localizedDescription)
980
- throw error
981
- }
982
- }
983
-
984
- @objc(push:withRejecter:)
985
- func push(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
986
- print("ReactNative", "push started")
987
- do {
988
- try pushInternal(key: convertStringToByte(rootCid!).toData())
989
- resolve(rootCid)
990
- } catch let error {
991
- print("get", error.localizedDescription)
992
- reject("ERR_FULA", "push", error)
993
- }
994
- }
995
-
996
- func pushInternal(key: Data) throws -> Void {
997
- do {
998
- let hasIt = try hasInternal(key)
999
- if (self.fula != nil && hasIt) {
1000
- try self.fula?.push(key)
1001
- try self.fula?.flush()
1002
- } else {
1003
- print("ReactNative", "pushInternal error: key wasn't found or fula is not initialized")
1004
- throw MyError.runtimeError("ReactNative pushInternal error: key wasn't found or fula is not initialized")
1005
- }
1006
- } catch let error {
1007
- print("ReactNative", "pushInternal", error.localizedDescription)
1008
- throw error
1009
- }
1010
- }
1011
-
1012
- // FIXME: unused codecString arg
1013
- @objc(put:withCodecString:withResolver:withRejecter:)
1014
- func put(valueString: String, codecString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1015
-
1016
- print("ReactNative", "put: codecString = " + codecString)
1017
- print("ReactNative", "put: valueString = " + valueString)
1018
- do {
1019
- //byte[] codec = convertStringToByte(CodecString)
1020
- let codec = FulaModule.CODEC_DAG_CBOR
1021
-
1022
-
1023
- print("ReactNative", "put: codec = ", codec)
1024
- let value = valueString.toData()
1025
-
1026
- print("ReactNative", "put: value.toHex() = " , value.toHex())
1027
- let key = try putInternal(value: value, codec: codec)
1028
- print("ReactNative", "put: key.toHex() = " , key.toUTF8String()!)
1029
- resolve(key.toUTF8String()!)
1030
- } catch let error {
1031
- print("ReactNative", "put: error = ", error.localizedDescription)
1032
- reject("ERR_FULA", "put", error)
1033
- }
1034
-
1035
- }
1036
-
1037
- // FIXME: unused codec arg
1038
- func putInternal(value: Data, codec: Int) throws -> Data {
1039
- do {
1040
- if(self.fula != nil) {
1041
- let key: Data = try self.fula!.put(value, codec: Int64(FulaModule.CODEC_DAG_CBOR))
1042
- try self.fula?.flush()
1043
- return key
1044
- } else {
1045
- print("ReactNative", "putInternal Error: fula is not initialized")
1046
- throw MyError.runtimeError("ReactNative putInternal Error: fula is not initialized")
1047
- }
1048
- } catch let error {
1049
- print("ReactNative", "putInternal", error.localizedDescription)
1050
- throw error
1051
- }
1052
- }
1053
-
1054
- @objc(setAuth:withAllow:withResolver:withRejecter:)
1055
- func setAuth(peerIdString: String, allow: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1056
-
1057
- print("ReactNative", "setAuth: peerIdString = " + peerIdString)
1058
- do {
1059
- if (self.fula != nil && !(self.fula?.id_().isEmpty)! && fulaConfig != nil && !fulaConfig!.bloxAddr.isEmpty) {
1060
- let bloxAddr = fulaConfig!.bloxAddr
1061
- print("ReactNative", "setAuth: bloxAddr = '",bloxAddr,"'"," peerIdString = '",peerIdString,"'")
1062
- let parts = bloxAddr.split(separator: "/").map(String.init)
1063
- try self.fula?.setAuth(parts.last, subject: peerIdString, allow: allow)
1064
- resolve(true)
1065
- } else {
1066
- print("ReactNative", "setAuth error: fula is not initialized")
1067
- throw MyError.runtimeError("ReactNative fula is not initialized")
1068
- }
1069
- resolve(false)
1070
- } catch let error {
1071
- print("get", error.localizedDescription)
1072
- reject("ERR_FULA", "setAuth", error)
1073
- }
1074
-
1075
- }
1076
-
1077
- @objc(clearCidsFromRecent:withResolver:withRejecter:)
1078
- func clearCidsFromRecent(cidArray: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1079
- DispatchQueue.global(qos: .default).async {
1080
- do {
1081
- guard let fulaClient = self.fula else {
1082
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
1083
- }
1084
-
1085
- // Concatenate all CID strings into a single string separated by "|"
1086
- let concatenatedCids = (cidArray as? [String])?.joined(separator: "|")
1087
-
1088
- guard let cidsData = concatenatedCids?.data(using: .utf8) else {
1089
- throw MyError.runtimeError("ReactNative Unable to encode CIDs as data")
1090
- }
1091
-
1092
- try fulaClient.clearCids(fromRecent: cidsData)
1093
- resolve(true)
1094
- } catch let error {
1095
- print("ReactNative", "clearCidsFromRecent failed with Error: \(error.localizedDescription)")
1096
- reject("ERR_FULA_CLEAR_CIDS", "Failed to clear CIDs from recent", error)
1097
- }
1098
- }
1099
- }
1100
-
1101
-
1102
- @objc(listRecentCidsAsString:withRejecter:)
1103
- func listRecentCidsAsString(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1104
- DispatchQueue.global(qos: .default).async {
1105
- do {
1106
- guard let fulaClient = self.fula else {
1107
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
1108
- }
1109
-
1110
- let recentLinksIterator = try fulaClient.listRecentCidsAsString()
1111
- var recentLinksList = [String]()
1112
-
1113
- while recentLinksIterator.hasNext() {
1114
- var error: NSError?
1115
- let nextLink = try recentLinksIterator.next(&error)
1116
-
1117
- if let error = error {
1118
- throw error
1119
- }
1120
-
1121
- recentLinksList.append(nextLink)
1122
- }
1123
-
1124
- if !recentLinksList.isEmpty {
1125
- // Return the whole list
1126
- resolve(recentLinksList)
1127
- } else {
1128
- resolve(false)
1129
- }
1130
- } catch let error {
1131
- print("ReactNative", "listRecentCidsAsString failed with Error: \(error.localizedDescription)")
1132
- reject("ERR_FULA_LIST_RECENT_CIDS", "Failed to list recent CIDs as string", error)
1133
- }
1134
- }
1135
- }
1136
-
1137
- @objc(listRecentCidsAsStringWithChildren:withRejecter:)
1138
- func listRecentCidsAsStringWithChildren(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1139
- DispatchQueue.global(qos: .default).async {
1140
- do {
1141
- guard let fulaClient = self.fula else {
1142
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
1143
- }
1144
-
1145
- let recentLinksIterator = try fulaClient.listRecentCidsAsStringWithChildren()
1146
- var recentLinksList = [String]()
1147
-
1148
- while recentLinksIterator.hasNext() {
1149
- var error: NSError?
1150
- let nextLink = try recentLinksIterator.next(&error)
1151
-
1152
- if let error = error {
1153
- throw error
1154
- }
1155
-
1156
- recentLinksList.append(nextLink)
1157
- }
1158
-
1159
- if !recentLinksList.isEmpty {
1160
- // Return the whole list
1161
- resolve(recentLinksList)
1162
- } else {
1163
- resolve(false)
1164
- }
1165
- } catch let error {
1166
- print("ReactNative", "listRecentCidsAsStringWithChildren failed with Error: \(error.localizedDescription)")
1167
- reject("ERR_FULA_LIST_RECENT_CIDS_WITH_CHILDREN", "Failed to list recent CIDs as string with children", error)
1168
- }
1169
- }
1170
- }
1171
-
1172
- @objc(batchUploadManifest:withPoolId:withReplicationFactor:withResolver:withRejecter:)
1173
- func batchUploadManifest(cidArray: NSArray, poolIDStr: String, replicationFactorStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1174
- DispatchQueue.global(qos: .default).async {
1175
- do {
1176
- guard let fulaClient = self.fula else {
1177
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
1178
- }
1179
- guard let poolID = Int64(poolIDStr) else {
1180
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Invalid poolID - not a valid number: \(poolIDStr)"])
1181
- reject("ERR_FULA", "Invalid poolID - not a valid number: \(poolIDStr)", error)
1182
- return
1183
- }
1184
- guard let replicationFactor = Int64(replicationFactorStr) else {
1185
- let error = NSError(domain: "FULAErrorDomain",
1186
- code: 1002,
1187
- userInfo: [NSLocalizedDescriptionKey: "Invalid replicationFactor - not a valid number: \(replicationFactorStr)"])
1188
- reject("ERR_FULA", "Invalid replicationFactorStr - not a valid number: \(replicationFactorStr)", error)
1189
- return
1190
- }
1191
-
1192
- // Concatenate all CID strings into a single string separated by "|"
1193
- let concatenatedCids = (cidArray as? [String])?.joined(separator: "|")
1194
-
1195
- guard let cidsData = concatenatedCids?.data(using: .utf8) else {
1196
- throw MyError.runtimeError("ReactNative Unable to encode CIDs as data")
1197
- }
1198
-
1199
- // Adjusted call to match the expected method signature and argument types
1200
- try fulaClient.batchUploadManifest(cidsData, poolID: Int(poolID), replicationFactor: Int(replicationFactor))
1201
- resolve(true)
1202
- } catch let error {
1203
- print("ReactNative", "batchUploadManifest failed with Error: \(error.localizedDescription)")
1204
- reject("ERR_FULA_BATCH_UPLOAD_MANIFEST", "Failed to batch upload CIDs", error)
1205
- }
1206
- }
1207
- }
1208
-
1209
-
1210
-
1211
- @objc(shutdown:withRejecter:)
1212
- func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
1213
- do {
1214
- try shutdownInternal()
1215
- resolve(true)
1216
- } catch let error {
1217
- NSLog("ReactNative shutdown \(error.localizedDescription)")
1218
- reject("ERR_FULA", "shutdown", error)
1219
- }
1220
-
1221
- }
1222
-
1223
- @objc
1224
- func deleteDsLock(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1225
- let lockFilePath = (fulaConfig!.storePath as NSString).appendingPathComponent("LOCK")
1226
- let fileManager = FileManager.default
1227
-
1228
- do {
1229
- if fileManager.fileExists(atPath: lockFilePath) {
1230
- try fileManager.removeItem(atPath: lockFilePath)
1231
- NSLog("ReactNative: Lock file deleted successfully.")
1232
- resolve(true)
1233
- } else {
1234
- NSLog("ReactNative: Lock file does not exist.")
1235
- resolve(false) // Resolve with false if the file doesn't exist
1236
- }
1237
- } catch let error as NSError {
1238
- NSLog("ReactNative: Failed to delete lock file: \(error.localizedDescription)")
1239
- reject("delete_error", "Failed to delete lock file", error)
1240
- }
1241
- }
1242
-
1243
- func shutdownInternal() {
1244
- NSLog("ReactNative shutdownInternal")
1245
- if self.fula != nil {
1246
- NSLog("ReactNative shutdownInternal fula is not null")
1247
- do {
1248
- try self.fula?.shutdown()
1249
- NSLog("ReactNative shutdownInternal fula.shutdown called")
1250
- } catch {
1251
- // Handle specific errors if needed or log them
1252
- NSLog("ReactNative shutdownInternal error: \(error.localizedDescription)")
1253
- }
1254
- // Ensure resources are cleaned up regardless of errors
1255
- self.fula = nil
1256
- self.client = nil
1257
- self.wnfs = nil
1258
- }
1259
- }
1260
-
1261
- ///////////////////////////////////////////////////////////
1262
- ///////////////////////////////////////////////////////////
1263
- ///////////////////////////////////////////////////////////
1264
- ///////////////////////////////////////////////////////////
1265
- //////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
1266
- ///////////////////////////////////////////////////////////
1267
-
1268
- @objc(checkAccountExists:withResolver:withRejecter:)
1269
- func checkAccountExists(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1270
- print("ReactNative", "checkAccountExists: accountString = ", accountString)
1271
- do {
1272
- let result = try self.fula!.accountExists(accountString)
1273
- let resultString = result.toUTF8String()!
1274
- resolve(resultString)
1275
- } catch let error {
1276
- print("checkAccountExists", error.localizedDescription)
1277
- reject("ERR_FULA", "checkAccountExists", error)
1278
- }
1279
-
1280
- }
1281
-
1282
- @objc(accountFund:withResolver:withRejecter:)
1283
- func accountFund(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1284
- print("ReactNative", "accountFund: accountString = ", accountString)
1285
- do {
1286
- let result = try self.fula!.accountFund(accountString)
1287
- let resultString = result.toUTF8String()!
1288
- resolve(resultString)
1289
- } catch let error {
1290
- print("accountFund", error.localizedDescription)
1291
- reject("ERR_FULA", "accountFund", error)
1292
- }
1293
-
1294
- }
1295
-
1296
- @objc(listPools:withRejecter:)
1297
- func listPools( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1298
- print("ReactNative", "listPools")
1299
- do {
1300
- let result = try self.fula!.poolList()
1301
- let resultString = result.toUTF8String()!
1302
- resolve(resultString)
1303
- } catch let error {
1304
- print("listPools", error.localizedDescription)
1305
- reject("ERR_FULA", "listPools", error)
1306
- }
1307
-
1308
- }
1309
-
1310
- @objc(listPoolJoinRequests:withResolver:withRejecter:)
1311
- func listPoolJoinRequests(poolIDStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1312
- print("ReactNative", "listPoolJoinRequests: poolIDStr = ", poolIDStr)
1313
- do {
1314
- if let poolID = Int64(poolIDStr), let intPoolID = Int(exactly: poolID) {
1315
- // Conversion to Int successful - use intPoolID
1316
- let result = try self.fula!.poolRequests(intPoolID)
1317
- let resultString = result.toUTF8String()!
1318
- resolve(resultString)
1319
- } else {
1320
- // Handle invalid input or Int64 not convertible to Int
1321
- let error = NSError(domain: "FULAErrorDomain",
1322
- code: 1003,
1323
- userInfo: [NSLocalizedDescriptionKey: "Invalid poolIDStr - not a valid number or too large: \(poolIDStr)"])
1324
- reject("ERR_FULA", "Invalid poolIDStr - not a valid number or too large: \(poolIDStr)", error)
1325
- }
1326
- } catch let error {
1327
- print("listPoolJoinRequests", error.localizedDescription)
1328
- let nsError = error as NSError
1329
- reject("ERR_FULA", "Failed listPoolJoinRequests due to error", nsError)
1330
- }
1331
- }
1332
-
1333
- @objc(listAvailableReplicationRequests:withResolver:withRejecter:)
1334
- func listAvailableReplicationRequests(poolIDStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1335
- print("ReactNative", "listAvailableReplicationRequests: poolIDStr = ", poolIDStr)
1336
- do {
1337
- guard let poolID = Int64(poolIDStr), let intPoolID = Int(exactly: poolID) else {
1338
- let error = NSError(domain: "FULAErrorDomain",
1339
- code: 1004, // Use appropriate error code
1340
- userInfo: [NSLocalizedDescriptionKey: "Invalid poolID - not a valid number: \(poolIDStr)"])
1341
- reject("ERR_FULA", "Invalid poolID - not a valid number: \(poolIDStr)", error)
1342
- return
1343
- }
1344
- let result = try self.fula!.manifestAvailable(intPoolID)
1345
- guard let resultString = result.toUTF8String() else {
1346
- let error = NSError(domain: "FULAErrorDomain",
1347
- code: 1005, // Use appropriate error code
1348
- userInfo: [NSLocalizedDescriptionKey: "Failed to convert result to UTF8 String"])
1349
- reject("ERR_FULA", "Conversion Error", error)
1350
- return
1351
- }
1352
- resolve(resultString)
1353
- } catch let error as NSError {
1354
- print("listAvailableReplicationRequests", error.localizedDescription)
1355
- reject("ERR_FULA", "listAvailableReplicationRequests failed with error: \(error.localizedDescription)", error)
1356
- }
1357
- }
1358
-
1359
-
1360
- @objc(bloxFreeSpace:withRejecter:)
1361
- func bloxFreeSpace(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
1362
- print("ReactNative", "bloxFreeSpace")
1363
- guard let fulaClient = self.fula else {
1364
- print("bloxFreeSpace", "fula client is nil")
1365
- let error = NSError(domain: "FulaModuleError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1366
- reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1367
- return
1368
- }
1369
-
1370
- do {
1371
- let result = try fulaClient.bloxFreeSpace()
1372
- guard let resultString = result.toUTF8String() else {
1373
- let error = NSError(domain: "FulaModuleError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to convert result to String"])
1374
- reject("ERR_FULA_RESULT_CONVERSION", "Failed to convert result to String", error)
1375
- return
1376
- }
1377
- resolve(resultString)
1378
- } catch let error {
1379
- print("bloxFreeSpace", error.localizedDescription)
1380
- reject("ERR_FULA", "bloxFreeSpace", error)
1381
- }
1382
- }
1383
-
1384
- @objc(transferToFula:wallet:chain:withResolver:withRejecter:)
1385
- func transferToFula(amount: String, wallet: String, chain: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1386
- DispatchQueue.global(qos: .default).async {
1387
- do {
1388
- print("ReactNative", "transferToFula called")
1389
- let result = try self.fula!.transfer(toFula: amount, walletAccount: wallet, chain: chain)
1390
- let resultString = String(data: result, encoding: .utf8)
1391
- resolve(resultString)
1392
- } catch let error {
1393
- print("ReactNative", "transferToFula failed with Error: \(error.localizedDescription)")
1394
- reject("ERR_FULA_TRANSFER", "transferToFula failed", error)
1395
- }
1396
- }
1397
- }
1398
-
1399
-
1400
- @objc(getAccount:withRejecter:)
1401
- func getAccount(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1402
- guard let fulaClient = self.fula else {
1403
- let error = NSError(domain: "FulaModuleError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1404
- reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1405
- return
1406
- }
1407
-
1408
- do {
1409
- let account = try fulaClient.getAccount()
1410
- guard let accountString = String(data: account, encoding: .utf8) else {
1411
- let conversionError = NSError(domain: "FulaModuleError", code: -2, userInfo: [NSLocalizedDescriptionKey: "Unable to convert account data to String"])
1412
- reject("ERR_FULA_CONVERSION_FAILED", "Unable to convert account data to String", conversionError)
1413
- return
1414
- }
1415
- resolve(accountString)
1416
- } catch let error {
1417
- reject("ERR_FULA", "getAccount: \(error.localizedDescription)", error)
1418
- }
1419
- }
1420
-
1421
- @objc(assetsBalance:withAssetId:withClassId:withResolver:withRejecter:)
1422
- func assetsBalance(account: String, assetId: String, classId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1423
- guard let assetIdInt = Int(assetId), let classIdInt = Int(classId) else {
1424
- reject("ERR_FULA", "Invalid assetId or classId", nil)
1425
- return
1426
- }
1427
-
1428
- do {
1429
- let balance = try self.fula!.assetsBalance(account, assetId: assetIdInt, classId: classIdInt)
1430
- let balanceString = String(data: balance, encoding: .utf8)
1431
- resolve(balanceString)
1432
- } catch let error {
1433
- reject("ERR_FULA", "assetsBalance: \(error.localizedDescription)", error)
1434
- }
1435
- }
1436
-
1437
- @objc(joinPool:withResolver:withRejecter:)
1438
- func joinPool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1439
- print("ReactNative", "joinPool: poolID = ", poolID)
1440
- do {
1441
- guard let poolIdInt = Int(poolID) else {
1442
- throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1443
- }
1444
- let result = try self.fula!.poolJoin(poolIdInt)
1445
- let resultString = String(data: result, encoding: .utf8)
1446
- resolve(resultString)
1447
- } catch let error {
1448
- reject("ERR_FULA", "joinPool: \(error.localizedDescription)", error)
1449
- }
1450
- }
1451
-
1452
- @objc(cancelPoolJoin:withResolver:withRejecter:)
1453
- func cancelPoolJoin(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1454
- do {
1455
- guard let poolIdInt = Int(poolID) else {
1456
- throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1457
- }
1458
- let result = try self.fula!.poolCancelJoin(poolIdInt)
1459
- let resultString = String(data: result, encoding: .utf8)
1460
- resolve(resultString)
1461
- } catch let error {
1462
- reject("ERR_FULA", "cancelPoolJoin: \(error.localizedDescription)", error)
1463
- }
1464
- }
1465
-
1466
- @objc(leavePool:withResolver:withRejecter:)
1467
- func leavePool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1468
- print("ReactNative", "leavePool: poolID = ", poolID)
1469
- do {
1470
- guard let poolIdInt = Int(poolID) else {
1471
- throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1472
- }
1473
- let result = try self.fula!.poolLeave(poolIdInt)
1474
- let resultString = String(data: result, encoding: .utf8)
1475
- resolve(resultString)
1476
- } catch let error {
1477
- reject("ERR_FULA", "leavePool: \(error.localizedDescription)", error)
1478
- }
1479
- }
1480
-
1481
-
1482
- @objc(eraseBlData:withRejecter:)
1483
- func eraseBlData(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1484
- do {
1485
- let result = try self.fula!.eraseBlData()
1486
- let resultString = String(data: result, encoding: .utf8)
1487
- resolve(resultString)
1488
- } catch let error {
1489
- reject("ERR_FULA", "eraseBlData: \(error.localizedDescription)", error)
1490
- }
1491
- }
1492
-
1493
- @objc(reboot:withRejecter:)
1494
- func reboot(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1495
- do {
1496
- let result = try self.fula!.reboot()
1497
- let resultString = result.toUTF8String()!
1498
- resolve(resultString)
1499
- } catch let error {
1500
- print("reboot", error.localizedDescription)
1501
- reject("ERR_FULA", "reboot", error)
1502
- }
1503
- }
1504
-
1505
- @objc(partition:withRejecter:)
1506
- func partition(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1507
- do {
1508
- let result = try self.fula!.partition()
1509
- let resultString = result.toUTF8String()!
1510
- resolve(resultString)
1511
- } catch let error {
1512
- print("partition", error.localizedDescription)
1513
- reject("ERR_FULA", "partition", error)
1514
- }
1515
- }
1516
-
1517
- @objc(wifiRemoveall:withRejecter:)
1518
- func wifiRemoveall(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1519
- do {
1520
- let result = try self.fula!.wifiRemoveall()
1521
- let resultString = result.toUTF8String()!
1522
- resolve(resultString)
1523
- } catch let error {
1524
- print("wifiRemoveall", error.localizedDescription)
1525
- reject("ERR_FULA", "wifiRemoveall", error)
1526
- }
1527
- }
1528
-
1529
- @objc(fetchContainerLogs:withTailCount:withResolver:withRejecter:)
1530
- func fetchContainerLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1531
- do {
1532
- // Since fetchContainerLogs expects a String for tailCount, pass it directly
1533
- let result = try self.fula!.fetchContainerLogs(containerName, tailCount: tailCount)
1534
- guard let resultString = result.toUTF8String() else {
1535
- // Handle the case where result.toUTF8String() returns nil
1536
- let error = NSError(domain: "FULAErrorDomain",
1537
- code: 1007, // Choose a suitable error code
1538
- userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."])
1539
- reject("ERR_FULA", "Log Conversion Error", error)
1540
- return
1541
- }
1542
- resolve(resultString)
1543
- } catch let error as NSError {
1544
- print("fetchContainerLogs", error.localizedDescription)
1545
- reject("ERR_FULA", "fetchContainerLogs failed", error)
1546
- }
1547
- }
1548
- @objc(findBestAndTargetInLogs:withTailCount:withResolver:withRejecter:)
1549
- func findBestAndTargetInLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1550
- do {
1551
- // Since fetchContainerLogs expects a String for tailCount, pass it directly
1552
- let result = try self.fula!.findBestAndTarget(inLogs: containerName, tailCount: tailCount)
1553
- guard let resultString = result.toUTF8String() else {
1554
- // Handle the case where result.toUTF8String() returns nil
1555
- let error = NSError(domain: "FULAErrorDomain",
1556
- code: 1007, // Choose a suitable error code
1557
- userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."])
1558
- reject("ERR_FULA", "Log Conversion Error", error)
1559
- return
1560
- }
1561
- resolve(resultString)
1562
- } catch let error as NSError {
1563
- print("fetchContainerLogs", error.localizedDescription)
1564
- reject("ERR_FULA", "fetchContainerLogs failed", error)
1565
- }
1566
- }
1567
-
1568
-
1569
-
1570
- @objc(getFolderSize:withResolver:withRejecter:)
1571
- func getFolderSize(folderPath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1572
- // Validate the folder path
1573
- guard !folderPath.isEmpty else {
1574
- reject("ERR_INVALID_PATH", "The folder path is empty.", nil)
1575
- return
1576
- }
1577
-
1578
- do {
1579
- // Ensure fula is not nil before calling getFolderSize
1580
- guard let fula = self.fula else {
1581
- reject("ERR_FULA", "Fula instance is not initialized.", nil)
1582
- return
1583
- }
1584
-
1585
- // Attempt to get the folder size
1586
- let result = try fula.getFolderSize(folderPath)
1587
-
1588
- // Convert the result to a UTF-8 string safely
1589
- if let resultString = result.toUTF8String() {
1590
- resolve(resultString)
1591
- } else {
1592
- reject("ERR_CONVERSION", "Failed to convert result to UTF-8 string.", nil)
1593
- }
1594
- } catch let error {
1595
- // Log and reject with detailed error information
1596
- print("getFolderSize error:", error.localizedDescription)
1597
- reject("ERR_FULA", "Failed to get folder size: \(error.localizedDescription)", error)
1598
- }
1599
- }
1600
-
1601
- @objc
1602
- func getDatastoreSize(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
1603
- DispatchQueue.global(qos: .background).async {
1604
- // Safely unwrap `self.fula` using `guard let`
1605
- guard let fulaClient = self.fula else {
1606
- let error = NSError(domain: "FulaModuleError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1607
- DispatchQueue.main.async {
1608
- reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1609
- }
1610
- return
1611
- }
1612
-
1613
- do {
1614
- let result = try fulaClient.getDatastoreSize()
1615
- let resultString = String(decoding: result, as: UTF8.self)
1616
- DispatchQueue.main.async {
1617
- resolve(resultString)
1618
- }
1619
- } catch let error {
1620
- DispatchQueue.main.async {
1621
- reject("ERR_FULA", "Failed to get datastore size: \(error.localizedDescription)", error)
1622
- }
1623
- }
1624
- }
1625
- }
1626
-
1627
- @objc(listPlugins:withRejecter:)
1628
- func listPlugins(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1629
- DispatchQueue.global(qos: .background).async {
1630
- do {
1631
- guard let fula = self.fula else {
1632
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1633
- DispatchQueue.main.async {
1634
- reject("ERR_FULA", "Fula not initialized", error)
1635
- }
1636
- return
1637
- }
1638
-
1639
- let result = try fula.listPlugins()
1640
- guard let resultString = result.toUTF8String() else {
1641
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert plugin list to string"])
1642
- DispatchQueue.main.async {
1643
- reject("ERR_FULA", "Plugin List Conversion Error", error)
1644
- }
1645
- return
1646
- }
1647
-
1648
- DispatchQueue.main.async {
1649
- resolve(resultString)
1650
- }
1651
- } catch let error {
1652
- print("listPlugins", error.localizedDescription)
1653
- DispatchQueue.main.async {
1654
- reject("ERR_FULA", "listPlugins failed", error)
1655
- }
1656
- }
1657
- }
1658
- }
1659
-
1660
- @objc(listActivePlugins:withRejecter:)
1661
- func listActivePlugins(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1662
- DispatchQueue.global(qos: .background).async {
1663
- do {
1664
- guard let fula = self.fula else {
1665
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1666
- DispatchQueue.main.async {
1667
- reject("ERR_FULA", "Fula not initialized", error)
1668
- }
1669
- return
1670
- }
1671
-
1672
- let result: Data
1673
- do {
1674
- result = try fula.listActivePlugins()
1675
- } catch {
1676
- print("Error listing active plugins: \(error)")
1677
- DispatchQueue.main.async {
1678
- reject("ERR_FULA", "Failed to list active plugins", error)
1679
- }
1680
- return
1681
- }
1682
-
1683
- guard !result.isEmpty else {
1684
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Active plugin list is empty"])
1685
- DispatchQueue.main.async {
1686
- reject("ERR_FULA", "Empty Active Plugin List", error)
1687
- }
1688
- return
1689
- }
1690
-
1691
- guard let resultString = String(data: result, encoding: .utf8) else {
1692
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert active plugin list to string"])
1693
- DispatchQueue.main.async {
1694
- reject("ERR_FULA", "Active Plugin List Conversion Error", error)
1695
- }
1696
- return
1697
- }
1698
-
1699
- DispatchQueue.main.async {
1700
- resolve(resultString)
1701
- }
1702
- } catch let error {
1703
- print("listActivePlugins unexpected error:", error.localizedDescription)
1704
- DispatchQueue.main.async {
1705
- reject("ERR_FULA", "listActivePlugins failed unexpectedly", error)
1706
- }
1707
- }
1708
- }
1709
- }
1710
-
1711
- @objc(installPlugin:withParams:withResolver:withRejecter:)
1712
- func installPlugin(pluginName: String, params: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1713
- DispatchQueue.global(qos: .background).async {
1714
- do {
1715
- guard let fula = self.fula else {
1716
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1717
- DispatchQueue.main.async {
1718
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1719
- }
1720
- return
1721
- }
1722
-
1723
- let result = try fula.installPlugin(pluginName, params: params)
1724
- guard let resultString = result.toUTF8String() else {
1725
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install result to string"])
1726
- DispatchQueue.main.async {
1727
- reject("ERR_FULA_RESULT_CONVERSION", "Install Result Conversion Error", error)
1728
- }
1729
- return
1730
- }
1731
-
1732
- DispatchQueue.main.async {
1733
- resolve(resultString)
1734
- }
1735
- } catch let error {
1736
- print("installPlugin error:", error.localizedDescription)
1737
- DispatchQueue.main.async {
1738
- reject("ERR_FULA_INSTALL_PLUGIN", "Failed to install plugin: \(pluginName)", error)
1739
- }
1740
- }
1741
- }
1742
- }
1743
-
1744
- @objc(uninstallPlugin:withResolver:withRejecter:)
1745
- func uninstallPlugin(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1746
- DispatchQueue.global(qos: .background).async {
1747
- do {
1748
- guard let fula = self.fula else {
1749
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1750
- DispatchQueue.main.async {
1751
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1752
- }
1753
- return
1754
- }
1755
-
1756
- let result = try fula.uninstallPlugin(pluginName)
1757
- guard let resultString = result.toUTF8String() else {
1758
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert uninstall result to string"])
1759
- DispatchQueue.main.async {
1760
- reject("ERR_FULA_RESULT_CONVERSION", "Uninstall Result Conversion Error", error)
1761
- }
1762
- return
1763
- }
1764
-
1765
- DispatchQueue.main.async {
1766
- resolve(resultString)
1767
- }
1768
- } catch let error {
1769
- print("uninstallPlugin error:", error.localizedDescription)
1770
- DispatchQueue.main.async {
1771
- reject("ERR_FULA_UNINSTALL_PLUGIN", "Failed to uninstall plugin: \(pluginName)", error)
1772
- }
1773
- }
1774
- }
1775
- }
1776
-
1777
- @objc(showPluginStatus:withLines:withResolver:withRejecter:)
1778
- func showPluginStatus(pluginName: String, lines: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1779
- DispatchQueue.global(qos: .background).async {
1780
- do {
1781
- guard let fula = self.fula else {
1782
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1783
- DispatchQueue.main.async {
1784
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1785
- }
1786
- return
1787
- }
1788
-
1789
- let result: Data
1790
- do {
1791
- result = try fula.showPluginStatus(pluginName, lines: lines)
1792
- } catch {
1793
- print("Error showing plugin status: \(error)")
1794
- DispatchQueue.main.async {
1795
- reject("ERR_FULA_SHOW_PLUGIN_STATUS", "Failed to show plugin status", error)
1796
- }
1797
- return
1798
- }
1799
-
1800
- guard !result.isEmpty else {
1801
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Plugin status result is empty"])
1802
- DispatchQueue.main.async {
1803
- reject("ERR_FULA_EMPTY_RESULT", "Empty Plugin Status Result", error)
1804
- }
1805
- return
1806
- }
1807
-
1808
- guard let resultString = String(data: result, encoding: .utf8) else {
1809
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert plugin status to string"])
1810
- DispatchQueue.main.async {
1811
- reject("ERR_FULA_RESULT_CONVERSION", "Plugin Status Conversion Error", error)
1812
- }
1813
- return
1814
- }
1815
-
1816
- DispatchQueue.main.async {
1817
- resolve(resultString)
1818
- }
1819
- } catch {
1820
- print("showPluginStatus unexpected error:", error.localizedDescription)
1821
- DispatchQueue.main.async {
1822
- reject("ERR_FULA_SHOW_PLUGIN_STATUS", "showPluginStatus failed unexpectedly", error)
1823
- }
1824
- }
1825
- }
1826
- }
1827
-
1828
- @objc(getInstallOutput:withParams:withResolver:withRejecter:)
1829
- func getInstallOutput(pluginName: String, params: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1830
- DispatchQueue.global(qos: .background).async {
1831
- do {
1832
- guard let fula = self.fula else {
1833
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1834
- DispatchQueue.main.async {
1835
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1836
- }
1837
- return
1838
- }
1839
-
1840
- let result: Data
1841
- do {
1842
- result = try fula.getInstallOutput(pluginName, params: params)
1843
- } catch {
1844
- print("Error getting install output: \(error)")
1845
- DispatchQueue.main.async {
1846
- reject("ERR_FULA_GET_INSTALL_OUTPUT", "Failed to get install output", error)
1847
- }
1848
- return
1849
- }
1850
-
1851
- guard !result.isEmpty else {
1852
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Install output is empty"])
1853
- DispatchQueue.main.async {
1854
- reject("ERR_FULA_EMPTY_RESULT", "Empty Install Output", error)
1855
- }
1856
- return
1857
- }
1858
-
1859
- guard let resultString = String(data: result, encoding: .utf8) else {
1860
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install output to string"])
1861
- DispatchQueue.main.async {
1862
- reject("ERR_FULA_RESULT_CONVERSION", "Install Output Conversion Error", error)
1863
- }
1864
- return
1865
- }
1866
-
1867
- DispatchQueue.main.async {
1868
- resolve(resultString)
1869
- }
1870
- } catch {
1871
- print("getInstallOutput unexpected error:", error.localizedDescription)
1872
- DispatchQueue.main.async {
1873
- reject("ERR_FULA_GET_INSTALL_OUTPUT", "getInstallOutput failed unexpectedly", error)
1874
- }
1875
- }
1876
- }
1877
- }
1878
-
1879
- @objc(getInstallStatus:withResolver:withRejecter:)
1880
- func getInstallStatus(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1881
- DispatchQueue.global(qos: .background).async {
1882
- do {
1883
- guard let fula = self.fula else {
1884
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1885
- DispatchQueue.main.async {
1886
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1887
- }
1888
- return
1889
- }
1890
-
1891
- let result: Data
1892
- do {
1893
- result = try fula.getInstallStatus(pluginName)
1894
- } catch {
1895
- print("Error getting install status: \(error)")
1896
- DispatchQueue.main.async {
1897
- reject("ERR_FULA_GET_INSTALL_STATUS", "Failed to get install status", error)
1898
- }
1899
- return
1900
- }
1901
-
1902
- guard !result.isEmpty else {
1903
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Install status result is empty"])
1904
- DispatchQueue.main.async {
1905
- reject("ERR_FULA_EMPTY_RESULT", "Empty Install Status Result", error)
1906
- }
1907
- return
1908
- }
1909
-
1910
- guard let resultString = String(data: result, encoding: .utf8) else {
1911
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install status to string"])
1912
- DispatchQueue.main.async {
1913
- reject("ERR_FULA_RESULT_CONVERSION", "Install Status Conversion Error", error)
1914
- }
1915
- return
1916
- }
1917
-
1918
- DispatchQueue.main.async {
1919
- resolve(resultString)
1920
- }
1921
- } catch {
1922
- print("getInstallStatus unexpected error:", error.localizedDescription)
1923
- DispatchQueue.main.async {
1924
- reject("ERR_FULA_GET_INSTALL_STATUS", "getInstallStatus failed unexpectedly", error)
1925
- }
1926
- }
1927
- }
1928
- }
1929
-
1930
- @objc(updatePlugin:withResolver:withRejecter:)
1931
- func updatePlugin(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1932
- DispatchQueue.global(qos: .background).async {
1933
- do {
1934
- guard let fula = self.fula else {
1935
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1936
- DispatchQueue.main.async {
1937
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1938
- }
1939
- return
1940
- }
1941
-
1942
- let result: Data
1943
- do {
1944
- result = try fula.updatePlugin(pluginName)
1945
- } catch {
1946
- print("Error updating plugin: \(error)")
1947
- DispatchQueue.main.async {
1948
- reject("ERR_FULA_UPDATE_PLUGIN", "Failed to update plugin: \(pluginName)", error)
1949
- }
1950
- return
1951
- }
1952
-
1953
- guard !result.isEmpty else {
1954
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Update plugin result is empty"])
1955
- DispatchQueue.main.async {
1956
- reject("ERR_FULA_EMPTY_RESULT", "Empty Update Plugin Result", error)
1957
- }
1958
- return
1959
- }
1960
-
1961
- guard let resultString = String(data: result, encoding: .utf8) else {
1962
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert update plugin result to string"])
1963
- DispatchQueue.main.async {
1964
- reject("ERR_FULA_RESULT_CONVERSION", "Update Plugin Result Conversion Error", error)
1965
- }
1966
- return
1967
- }
1968
-
1969
- DispatchQueue.main.async {
1970
- resolve(resultString)
1971
- }
1972
- } catch {
1973
- print("updatePlugin unexpected error:", error.localizedDescription)
1974
- DispatchQueue.main.async {
1975
- reject("ERR_FULA_UPDATE_PLUGIN", "updatePlugin failed unexpectedly", error)
1976
- }
1977
- }
1978
- }
1979
- }
1980
-
1981
- @objc(replicateInPool:withAccount:withPoolID:withResolver:withRejecter:)
1982
- func replicateInPool(cidArray: [String], account: String, poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1983
- DispatchQueue.global(qos: .background).async {
1984
- do {
1985
- guard let fula = self.fula else {
1986
- let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1987
- DispatchQueue.main.async {
1988
- reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1989
- }
1990
- return
1991
- }
1992
-
1993
- guard let poolIDLong = Int64(poolID) else {
1994
- let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Invalid poolID"])
1995
- DispatchQueue.main.async {
1996
- reject("ERR_FULA_INVALID_POOL_ID", "Invalid poolID", error)
1997
- }
1998
- return
1999
- }
2000
-
2001
- guard let poolIDInt = Int(exactly: poolIDLong) else {
2002
- let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "PoolID is too large for Int"])
2003
- DispatchQueue.main.async {
2004
- reject("ERR_FULA_POOL_ID_OVERFLOW", "PoolID is too large", error)
2005
- }
2006
- return
2007
- }
2008
-
2009
- let cidString = cidArray.joined(separator: "|")
2010
- guard let cidsBytes = cidString.data(using: .utf8) else {
2011
- let error = NSError(domain: "FULAErrorDomain", code: 1004, userInfo: [NSLocalizedDescriptionKey: "Failed to encode CIDs as data"])
2012
- DispatchQueue.main.async {
2013
- reject("ERR_FULA_CID_ENCODING", "Failed to encode CIDs", error)
2014
- }
2015
- return
2016
- }
2017
-
2018
- let result: Data
2019
- do {
2020
- if let replicationResult = try fula.replicate(inPool: cidsBytes, account: account, poolID: poolIDInt) {
2021
- result = replicationResult
2022
- } else {
2023
- throw NSError(domain: "FULAErrorDomain", code: 1007, userInfo: [NSLocalizedDescriptionKey: "Replication result is nil"])
2024
- }
2025
- } catch {
2026
- print("Error replicating in pool: \(error)")
2027
- DispatchQueue.main.async {
2028
- reject("ERR_FULA_REPLICATION", "Failed to replicate in pool", error)
2029
- }
2030
- return
2031
- }
2032
-
2033
- guard !result.isEmpty else {
2034
- let error = NSError(domain: "FULAErrorDomain", code: 1005, userInfo: [NSLocalizedDescriptionKey: "Replication result is empty"])
2035
- DispatchQueue.main.async {
2036
- reject("ERR_FULA_EMPTY_RESULT", "Empty replication result", error)
2037
- }
2038
- return
2039
- }
2040
-
2041
- guard let resultString = String(data: result, encoding: .utf8) else {
2042
- let error = NSError(domain: "FULAErrorDomain", code: 1006, userInfo: [NSLocalizedDescriptionKey: "Failed to decode result data to string"])
2043
- DispatchQueue.main.async {
2044
- reject("ERR_FULA_RESULT_DECODING", "Failed to decode result", error)
2045
- }
2046
- return
2047
- }
2048
-
2049
- DispatchQueue.main.async {
2050
- resolve(resultString)
2051
- }
2052
- } catch {
2053
- print("replicateInPool unexpected error:", error.localizedDescription)
2054
- DispatchQueue.main.async {
2055
- reject("ERR_FULA_REPLICATE_IN_POOL", "replicateInPool failed unexpectedly", error)
2056
- }
2057
- }
2058
- }
2059
- }
2060
-
2061
- // AI Chat methods
2062
-
2063
- @objc(chatWithAI:withUserMessage:withResolver:withRejecter:)
2064
- func chatWithAI(aiModel: String, userMessage: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2065
- NSLog("ReactNative chatWithAI: aiModel = \(aiModel), userMessage = \(userMessage)")
2066
- DispatchQueue.global(qos: .default).async {
2067
- do {
2068
- guard let fula = self.fula else {
2069
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
2070
- }
2071
-
2072
- // Call the Go Mobile method, which returns Data
2073
- let streamIDData = try fula.chatWithAI(aiModel, userMessage: userMessage)
2074
-
2075
- // Convert Data to String (assuming UTF-8 encoding)
2076
- guard let streamID = String(data: streamIDData, encoding: .utf8) else {
2077
- throw MyError.runtimeError("Failed to convert stream ID to string")
2078
- }
2079
-
2080
- // Resolve the promise with the stream ID
2081
- DispatchQueue.main.async {
2082
- resolve(streamID)
2083
- }
2084
- } catch let error {
2085
- NSLog("ReactNative ERROR in chatWithAI: \(error.localizedDescription)")
2086
- DispatchQueue.main.async {
2087
- reject("ERR_CHAT_WITH_AI", "Chat with AI failed", error)
2088
- }
2089
- }
2090
- }
2091
- }
2092
-
2093
- @objc(getChatChunk:withResolver:withRejecter:)
2094
- func getChatChunk(streamID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2095
- NSLog("ReactNative getChatChunk: streamID = \(streamID)")
2096
- DispatchQueue.global(qos: .default).async {
2097
- do {
2098
- guard let fula = self.fula else {
2099
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
2100
- }
2101
-
2102
- // Call the Go Mobile method, which returns a String
2103
- let chunk = try fula.getChatChunk(streamID)
2104
-
2105
- // Handle null or empty response
2106
- if chunk.isEmpty {
2107
- NSLog("ReactNative getChatChunk: No data received for streamID = \(streamID)")
2108
- DispatchQueue.main.async {
2109
- resolve("") // Resolve with an empty string
2110
- }
2111
- return
2112
- }
2113
-
2114
- // Resolve the promise with the chunk of data
2115
- NSLog("ReactNative getChatChunk: Successfully received chunk for streamID = \(streamID)")
2116
- DispatchQueue.main.async {
2117
- resolve(chunk)
2118
- }
2119
- } catch let error {
2120
- // Log and reject the promise with the error
2121
- NSLog("ReactNative ERROR in getChatChunk: \(error.localizedDescription)")
2122
- DispatchQueue.main.async {
2123
- reject("ERR_GET_CHAT_CHUNK", "Get chat chunk failed", error)
2124
- }
2125
- }
2126
- }
2127
- }
2128
-
2129
- @objc(streamChunks:withResolver:withRejecter:)
2130
- func streamChunks(streamID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2131
- if streamID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
2132
- reject("INVALID_ARGUMENT", "streamID cannot be null or empty", nil)
2133
- return
2134
- }
2135
-
2136
- DispatchQueue.global(qos: .default).async {
2137
- do {
2138
- guard let fula = self.fula else {
2139
- throw MyError.runtimeError("ReactNative Fula client is not initialized")
2140
- }
2141
-
2142
- guard let iterator = try fula.getStreamIterator(streamID) else {
2143
- throw MyError.runtimeError("Failed to create StreamIterator")
2144
- }
2145
-
2146
- // Start listening for chunks on the main thread
2147
- DispatchQueue.main.async {
2148
- self.pollIterator(iterator: iterator, resolve: resolve, reject: reject)
2149
- }
2150
- } catch let error {
2151
- NSLog("ReactNative ERROR in streamChunks: \(error.localizedDescription)")
2152
- DispatchQueue.main.async {
2153
- reject("STREAM_ERROR", "Stream error", error)
2154
- }
2155
- }
2156
- }
2157
- }
2158
-
2159
- private func pollIterator(iterator: FulamobileStreamIterator, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
2160
- do {
2161
- let chunk = try iterator.next()
2162
- if !chunk.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
2163
- self.emitEvent(eventName: "onChunkReceived", data: chunk)
2164
- }
2165
-
2166
- if iterator.isComplete() {
2167
- self.emitEvent(eventName: "onStreamingCompleted", data: nil)
2168
- resolve(nil)
2169
- } else {
2170
- // Schedule another poll with a short delay
2171
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { // 50ms delay for better responsiveness
2172
- self.pollIterator(iterator: iterator, resolve: resolve, reject: reject)
2173
- }
2174
- }
2175
- } catch let error {
2176
- let errorMessage = error.localizedDescription
2177
- if errorMessage.contains("EOF") {
2178
- self.emitEvent(eventName: "onStreamingCompleted", data: nil)
2179
- resolve(nil)
2180
- } else if errorMessage.contains("timeout") {
2181
- // Retry on timeout
2182
- DispatchQueue.main.async {
2183
- self.pollIterator(iterator: iterator, resolve: resolve, reject: reject)
2184
- }
2185
- } else {
2186
- self.emitEvent(eventName: "onStreamError", data: errorMessage)
2187
- reject("STREAM_ERROR", errorMessage, error)
2188
- }
2189
- }
2190
- }
2191
-
2192
- private func emitEvent(eventName: String, data: String?) {
2193
- do {
2194
- guard let eventEmitter = RCTBridge.current()?.eventDispatcher() else {
2195
- NSLog("ReactNative ERROR: Could not get event dispatcher")
2196
- return
2197
- }
2198
-
2199
- eventEmitter.sendAppEvent(withName: eventName, body: data)
2200
- } catch {
2201
- NSLog("ReactNative Error emitting event: \(eventName), \(error.localizedDescription)")
2202
- }
2203
- }
2204
-
2205
- }
2206
-
2207
-
1
+ import Foundation
2
+ import Foundation.NSDate // for TimeInterval
3
+ import CommonCrypto
4
+ import Wnfs
5
+ import Fula
6
+ import os.log
7
+ import React
8
+
9
+ extension OSLog {
10
+
11
+ private static var subsystem = Bundle.main.bundleIdentifier!
12
+
13
+ /// Logs the view cycles like a view that appeared.
14
+ static let viewCycle = OSLog(subsystem: subsystem, category: "viewcycle")
15
+
16
+ /// All logs related to tracking and analytics.
17
+ static let statistics = OSLog(subsystem: subsystem, category: "statistics")
18
+ }
19
+
20
+ extension OSLog {
21
+
22
+ func info(_ msg: String, _ args: CVarArg...) {
23
+ os_log("%{public}@", log: self, type: .info, msg)
24
+ }
25
+
26
+ func error(_ msg: String, _ args: CVarArg...) {
27
+ os_log("%{public}@", log: self, type: .error, msg)
28
+ }
29
+
30
+ // ... (more methods for different log levels, if needed)
31
+ }
32
+
33
+
34
+ @objc(FulaModule)
35
+ class FulaModule: RCTEventEmitter {
36
+ public let NAME: String = "FulaModule"
37
+ var fula: FulamobileClient?
38
+
39
+ var client: Client?
40
+ var wnfs: Wnfs?
41
+ var fulaConfig: FulamobileConfig?
42
+ var appDir: URL
43
+ var fulaStorePath: String
44
+ // Related to the Wnfs library
45
+ var rootCid: Cid?
46
+ var wnfsKey: Data?
47
+ let userDataHelper = UserDataHelper()
48
+ var secretKeyGlobal: Array<UInt8>?
49
+ var identityEncryptedGlobal: String?
50
+ static let PRIVATE_KEY_STORE_ID = "PRIVATE_KEY"
51
+
52
+ // FIXME: Hardcoded ,fula should remove all the codec arguments as the rs-wnfs library removed its usage now.
53
+ static let CODEC_DAG_CBOR = Int(113)
54
+
55
+ enum MyError: Error {
56
+ case runtimeError(String)
57
+ }
58
+
59
+ @objc(Client)
60
+ public class Client: NSObject {
61
+ let internalClient: FulamobileClient
62
+
63
+ init(clientInput: FulamobileClient) {
64
+ internalClient = clientInput
65
+ }
66
+
67
+ func get(_ cid: Data) throws -> Data {
68
+ do {
69
+ print(String(format: "ReactNative get cid: %s", cid.toHex()))
70
+ return try internalClient.get(cid)
71
+ } catch let error {
72
+ throw error
73
+ }
74
+ }
75
+
76
+ func put(_ cid: Data, _ data: Data) throws -> Void {
77
+ do {
78
+ print(String(format: "ReactNative put(%s) data: %s", cid.toHex(), data.toHex()))
79
+ try internalClient.put(data, codec: 0xFF & Int64(cid[1]))
80
+ } catch let error {
81
+ print("ReactNative put: ", error.localizedDescription)
82
+ throw error
83
+ }
84
+ }
85
+ }
86
+ override init() {
87
+
88
+ self.appDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
89
+ let fulaStoreURL = appDir.appendingPathComponent("/fula")
90
+
91
+ self.fulaStorePath = fulaStoreURL.path
92
+ let fileManager = FileManager.default
93
+ var success = true
94
+ if !fileManager.fileExists(atPath: fulaStorePath) {
95
+ do{
96
+ try fileManager.createDirectory(atPath: fulaStorePath, withIntermediateDirectories: true)
97
+ }catch let error{
98
+ print(error.localizedDescription)
99
+ success = false
100
+ }
101
+
102
+ }
103
+ if success {
104
+ print("Fula store folder created")
105
+ } else {
106
+ print("Unable to create fula store folder!")
107
+
108
+ }
109
+ super.init()
110
+
111
+ }
112
+ func convertConfigToJson(config: Cid) -> String {
113
+ return String(format: "{\"cid\": \"%@\"}", config)
114
+ }
115
+
116
+ func getName() -> String {
117
+ return NAME
118
+ }
119
+
120
+ func toByte(_ input: String) -> Data {
121
+ return input.data(using: .utf8)!
122
+ }
123
+
124
+
125
+ func stringArrToIntArr(_ s: Array<String>) -> Array<Int> {
126
+ return s.map { Int($0)!}
127
+ }
128
+
129
+ func convertIntToByte(_ s: Array<Int>) -> Array<UInt8> {
130
+ return s.map { UInt8(exactly: $0)!}
131
+ }
132
+
133
+ func convertStringToByte(_ input: String) -> Array<UInt8> {
134
+ let splitted = input.split(separator: ",").map { String($0) }
135
+ let keyInt = stringArrToIntArr(splitted)
136
+ return convertIntToByte(keyInt)
137
+ }
138
+
139
+ @objc func applicationWillResignActive() {
140
+ // Log that the app will resign active
141
+ os_log("Application will resign active", log: OSLog.viewCycle, type: .info)
142
+ }
143
+
144
+ @objc func applicationDidEnterBackground() {
145
+ // Log that the app has entered the background
146
+ os_log("Application did enter background", log: OSLog.viewCycle, type: .info)
147
+ }
148
+
149
+ @objc func applicationWillTerminate() {
150
+ // Attempt to shut down Fula cleanly (similar to onHostDestroy)
151
+ os_log("Application will terminate - shutting down Fula", log: OSLog.viewCycle, type: .info)
152
+ do {
153
+ if let fulaClient = self.fula {
154
+ try fulaClient.shutdown()
155
+ os_log("Fula shutdown successfully.", log: OSLog.viewCycle, type: .info)
156
+ }
157
+ } catch {
158
+ os_log("Error shutting down Fula: %{public}@", log: OSLog.viewCycle, type: .error, String(describing: error))
159
+ }
160
+ }
161
+
162
+ @objc(registerLifecycleListener:withRejecter:)
163
+ func registerLifecycleListener(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
164
+ NotificationCenter.default.addObserver(
165
+ self,
166
+ selector: #selector(applicationWillResignActive),
167
+ name: UIApplication.willResignActiveNotification,
168
+ object: nil)
169
+
170
+ NotificationCenter.default.addObserver(
171
+ self,
172
+ selector: #selector(applicationDidEnterBackground),
173
+ name: UIApplication.didEnterBackgroundNotification,
174
+ object: nil)
175
+
176
+ NotificationCenter.default.addObserver(
177
+ self,
178
+ selector: #selector(applicationWillTerminate),
179
+ name: UIApplication.willTerminateNotification,
180
+ object: nil)
181
+
182
+ // Assuming the operation is always successful
183
+ resolve(true)
184
+ }
185
+
186
+ deinit {
187
+ NotificationCenter.default.removeObserver(self)
188
+ }
189
+
190
+ @objc(checkConnection:withResolver:withRejecter:)
191
+ func checkConnection(timeout: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
192
+ NSLog("ReactNative checkConnection started with timeout=\(timeout)")
193
+
194
+ if let timeoutInt = timeout as? Int {
195
+ if self.fula != nil {
196
+ DispatchQueue.global(qos: .default).async {
197
+ do {
198
+ let connectionStatus = try self.checkConnectionInternal(timeout: timeoutInt)
199
+ NSLog("ReactNative checkConnection ended \(connectionStatus)")
200
+ resolve(connectionStatus)
201
+ }
202
+ catch let error {
203
+ NSLog("ReactNative checkConnection failed with Error: \(error.localizedDescription)")
204
+ resolve(false)
205
+ }
206
+ }
207
+ } else {
208
+ NSLog("ReactNative checkConnection fula is null")
209
+ resolve(false)
210
+ }
211
+ } else {
212
+ OSLog.viewCycle.error("ReactNative checkConnection - invalid timeout value")
213
+ reject("ERR_INVALID_TIMEOUT", "Invalid timeout value", nil)
214
+ }
215
+ }
216
+
217
+
218
+ @objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:)
219
+ func newClient(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
220
+ print("ReactNative", "newClient storePath= " , storePath , " bloxAddr= " , bloxAddr , " exchange= " , exchange , " autoFlush= " , autoFlush , " useRelay= " , useRelay , " refresh= " , refresh)
221
+ do {
222
+ print("ReactNative", "newClient storePath= ", storePath)
223
+ let identity = toByte(identityString)
224
+ print("ReactNative", "newClient identity= ", identityString)
225
+ try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
226
+ let peerId = self.fula?.id_()
227
+ resolve(peerId)
228
+ } catch let error {
229
+ print("ReactNative", "newClient failed with Error: ", error.localizedDescription)
230
+ reject("ERR_FULA", "Can't create client", error)
231
+ }
232
+
233
+ }
234
+
235
+ @objc(isReady:withResolver:withRejecter:)
236
+ func isReady(filesystemCheck: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
237
+ print("ReactNative", "isReady started")
238
+ var initialized = false
239
+ do {
240
+ if (self.fula != nil && !self.fula!.id_().isEmpty) {
241
+ if (filesystemCheck) {
242
+ if (self.client != nil && rootCid != nil && !rootCid!.isEmpty) {
243
+ initialized = true
244
+ }
245
+ } else {
246
+ initialized = true
247
+ }
248
+ }
249
+ resolve(initialized)
250
+ } catch let error {
251
+ print("ReactNative", "isReady failed with Error: " + error.localizedDescription)
252
+ reject("ERR_FULA", "Check if fula is ready", error)
253
+ }
254
+ }
255
+
256
+ //TODO: we should consider refactor the name of this \
257
+ // function to be compatible with the android version.
258
+ @objc(initFula:withStorePath:withBloxAddr:withExchange:withAutoFlush:withRootConfig:withUseRelay:withRefresh:withResolver:withRejecter:)
259
+ func initFula(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, rootConfig: String, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
260
+
261
+ NSLog("ReactNative - init started")
262
+
263
+ do {
264
+
265
+ var resultData = Dictionary<String, String>()
266
+ NSLog("ReactNative init storePath= \(storePath)")
267
+ let identity = self.toByte(identityString)
268
+ NSLog("ReactNative init identity= \(identityString)")
269
+ let obj = try initInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, _rootCid: rootConfig, useRelay: useRelay, refresh: refresh)
270
+ NSLog("ReactNative init object created: [ \(obj[0]), \(obj[1]), \(obj[2]) ]")
271
+ resultData["peerId"] = obj[0]
272
+ resultData["rootCid"] = obj[1]
273
+ resultData["wnfs_key"] = obj[2]
274
+ resolve(resultData as NSDictionary)
275
+
276
+ } catch let error {
277
+ NSLog("ReactNative init failed with Error: \(error.localizedDescription)")
278
+ reject("ERR_FULA", "init failed", error)
279
+ }
280
+
281
+ }
282
+
283
+ @objc(logout:withStorePath:withResolver:withRejecter:)
284
+ func logout(identityString: String, storePath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
285
+
286
+ print("ReactNative", "logout started")
287
+ do {
288
+ let identity = toByte(identityString)
289
+ try logoutInternal(identity: identity, _storePath: storePath)
290
+ print("ReactNative", "logout completed")
291
+ resolve("")
292
+
293
+ } catch let error {
294
+ print("ReactNative", "logout failed with Error: ", error.localizedDescription)
295
+ reject("ERR_FULA", "logout failed", error)
296
+ }
297
+
298
+ }
299
+
300
+ func checkConnectionInternal(timeout: Int) throws -> Bool {
301
+ NSLog("ReactNative checkConnectionInternal started with timeout: \(timeout)")
302
+ var connectionStatus = false
303
+
304
+ if let fula = self.fula {
305
+ let semaphore = DispatchSemaphore(value: 0)
306
+ let queue = DispatchQueue(label: "com.yourapp.checkConnection", attributes: .concurrent)
307
+
308
+ queue.async {
309
+ do {
310
+ NSLog("ReactNative connectToBlox started")
311
+ try fula.connectToBlox()
312
+ connectionStatus = true
313
+ NSLog("ReactNative checkConnectionInternal succeeded")
314
+ semaphore.signal()
315
+ } catch let error {
316
+ NSLog("ReactNative checkConnectionInternal failed with Error: \(error.localizedDescription)")
317
+ semaphore.signal()
318
+ }
319
+ }
320
+
321
+ let timeoutResult = semaphore.wait(timeout: .now() + .seconds(timeout))
322
+ switch timeoutResult {
323
+ case .timedOut:
324
+ NSLog("ReactNative checkConnectionInternal timed out")
325
+ return false
326
+ case .success:
327
+ return connectionStatus
328
+ }
329
+ } else {
330
+ NSLog("ReactNative checkConnectionInternal failed because fula is not initialized")
331
+ return false
332
+ }
333
+ }
334
+
335
+ @objc(checkFailedActions:withTimeout:withResolver:withRejecter:)
336
+ func checkFailedActions(retry: Bool, timeout: Int, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
337
+ do {
338
+ guard let fula = self.fula else {
339
+ throw NSError(domain: "ERR_FULA", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula is not initialized"])
340
+ }
341
+
342
+ if !retry {
343
+ NSLog("ReactNative checkFailedActions without retry")
344
+ let failedLinks = try fula.listFailedPushes()
345
+
346
+ let nextFailedLink = try failedLinks.next()
347
+ if nextFailedLink != nil {
348
+ // Assuming nextFailedLink is of type Data; replace `toHex()` with an appropriate method to convert Data to a hex string
349
+ NSLog("ReactNative checkFailedActions found")
350
+ resolve(true)
351
+ } else {
352
+ resolve(false)
353
+ }
354
+ } else {
355
+ NSLog("ReactNative checkFailedActions with retry")
356
+ let retryResults = try retryFailedActionsInternal(timeout: timeout) // Ensure retryFailedActionsInternal accepts a timeout parameter
357
+ resolve(!retryResults)
358
+ }
359
+ } catch let error {
360
+ NSLog("ReactNative checkFailedActions failed with Error: \(error.localizedDescription)")
361
+ reject("ERR_FULA", "CheckFailedActions failed", error)
362
+ }
363
+ }
364
+
365
+
366
+
367
+ func retryFailedActionsInternal(timeout: Int) throws -> Bool {
368
+ NSLog("ReactNative retryFailedActionsInternal started")
369
+
370
+ guard let fula = self.fula else {
371
+ NSLog("ReactNative retryFailedActionsInternal failed because fula is not initialized")
372
+ return false
373
+ }
374
+
375
+ do {
376
+ let connectionCheck = try checkConnectionInternal(timeout: timeout)
377
+
378
+ if connectionCheck {
379
+ do {
380
+ NSLog("ReactNative retryFailedPushes started")
381
+ try fula.retryFailedPushes()
382
+ NSLog("ReactNative flush started")
383
+ try fula.flush()
384
+ return true
385
+ } catch let error {
386
+ try fula.flush()
387
+ NSLog("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
388
+ return false
389
+ }
390
+ } else {
391
+ NSLog("ReactNative retryFailedActionsInternal failed because blox is offline")
392
+ return false
393
+ }
394
+ } catch let error {
395
+ NSLog("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
396
+ return false
397
+ }
398
+ }
399
+
400
+ func createPeerIdentity(privateKey: Data) throws -> Data {
401
+ let secretKey = try Cryptography.generateKey(privateKey)
402
+
403
+ var encryptedKey: String? = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
404
+ NSLog("ReactNative createPeerIdentity encryptedKey=\(encryptedKey ?? "nil")")
405
+ if encryptedKey == nil {
406
+ let privateKeyString = String(data: privateKey, encoding: .utf8) ?? ""
407
+
408
+ guard !privateKeyString.isEmpty else {
409
+ throw NSError(domain: "KeyGenerationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Private key string conversion failed"])
410
+ }
411
+ var error: NSError?
412
+ guard let autoGeneratedIdentity = FulamobileGenerateEd25519KeyFromString(privateKeyString, &error)?.toUint8Array() else {
413
+ throw error ?? NSError(domain: "KeyGenerationError", code: -1, userInfo: nil)
414
+ }
415
+ encryptedKey = try Cryptography.encryptMsg([UInt8](autoGeneratedIdentity), [UInt8](secretKey))
416
+ NSLog("ReactNative createPeerIdentity encryptedKey2=\(encryptedKey)")
417
+ userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey ?? "")
418
+ }
419
+
420
+ // Assuming decryptMsg returns Data or throws an error if decryption fails
421
+ guard let encryptedKeyData = encryptedKey, !encryptedKeyData.isEmpty else {
422
+ throw NSError(domain: "DecryptionError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Encrypted key is empty"])
423
+ }
424
+ let decryptedData = try Cryptography.decryptMsg(encryptedKeyData, Array(secretKey))
425
+ let hexString = decryptedData.map { String(format: "%02hhx", $0) }.joined()
426
+ NSLog("ReactNative createPeerIdentity decryptedData=\(hexString)")
427
+ return Data(decryptedData)
428
+ }
429
+
430
+ func decryptLibp2pIdentity(_ encryptedLibp2pId: String, with encryptionSecretKey: Data) throws -> String {
431
+ // Convert Data to [UInt8]
432
+ let secretKeyBytes = [UInt8](encryptionSecretKey)
433
+
434
+ // Attempt decryption
435
+ guard let decryptedBytes = try? Cryptography.decryptMsg(encryptedLibp2pId, secretKeyBytes) else {
436
+ throw NSError(domain: "DecryptionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to decrypt Libp2p ID"])
437
+ }
438
+
439
+ // Assuming decryptedBytes is an array of UInt8
440
+ return String(decoding: decryptedBytes, as: UTF8.self)
441
+ }
442
+
443
+ func createEncryptedLibp2pId(from identity: Data, with encryptionSecretKey: Data) throws -> String {
444
+ var error: NSError?
445
+ guard let autoGeneratedIdentity = FulamobileGenerateEd25519Key(&error) else {
446
+ throw error ?? NSError(domain: "KeyGenerationError", code: -1, userInfo: nil)
447
+ }
448
+ let encryptedLibp2pId = try Cryptography.encryptMsg(Array(autoGeneratedIdentity), Array(encryptionSecretKey))
449
+ userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedLibp2pId)
450
+ return encryptedLibp2pId
451
+ }
452
+
453
+ func createNewrootCid(identity: Data) throws -> Void {
454
+ let hash32 = identity.sha256()
455
+ NSLog("ReactNative createNewrootCid wnfsKey= \(identity.toHex()) , hash32 = \(hash32.toHex())")
456
+ if (self.fula != nil) {
457
+ NSLog("ReactNative createNewrootCid self.fula not null")
458
+ try self.fula?.flush()
459
+ }
460
+ NSLog("ReactNative fula flushed")
461
+ rootCid = try self.wnfs?.Init(wnfsKey: hash32)
462
+ NSLog("ReactNative privateForest is created: \(rootCid!)")
463
+ wnfsKey = identity
464
+ NSLog("ReactNative rootCid is created: cid= \(rootCid!) wnfs_key= \(wnfsKey!.toHex()), hash32=\(hash32)")
465
+ try encryptAndStoreConfig()
466
+ }
467
+
468
+ func loadWnfs(_ _wnfsKey: Data , _ _rootCid: String) throws {
469
+ NSLog("ReactNative loadWnfs called: _rootCid=\(_rootCid)")
470
+ let hash32 = _wnfsKey.sha256()
471
+ NSLog("ReactNative wnfsKey= \(_wnfsKey.toHex()) ; hash32 = \(hash32.toHex())")
472
+ try self.wnfs?.LoadWithWNFSKey(wnfsKey: hash32, cid: _rootCid)
473
+ NSLog("ReactNative loadWnfs LoadWithWNFSKey")
474
+ rootCid = _rootCid
475
+ wnfsKey = _wnfsKey
476
+ NSLog("ReactNative loadWnfs called: rootCid=\(rootCid)")
477
+ if (self.fula != nil) {
478
+ try self.fula?.flush()
479
+ }
480
+ NSLog("ReactNative loadWnfs completed")
481
+ try encryptAndStoreConfig()
482
+ }
483
+
484
+
485
+ func encryptAndStoreConfig() throws {
486
+ do {
487
+ if let identityEncryptedGlobalUnwrapped = identityEncryptedGlobal {
488
+ NSLog("ReactNative encryptAndStoreConfig started")
489
+
490
+ if let rootCidUnwrapped = rootCid, let wnfsKeyUnwrapped = wnfsKey, let secretKeyGlobalUnwrapped = secretKeyGlobal {
491
+ NSLog("ReactNative encryptAndStoreConfig started with rootCid: \(rootCidUnwrapped.toUint8Array()) and wnfsKey:\(wnfsKeyUnwrapped)")
492
+
493
+ let cid_encrypted = try Cryptography.encryptMsg(rootCidUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
494
+ NSLog("ReactNative encryptAndStoreConfig cid_encrypted: \(cid_encrypted)")
495
+
496
+ let wnfs_key_encrypted = try Cryptography.encryptMsg(wnfsKeyUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
497
+ NSLog("ReactNative encryptAndStoreConfig wnfs_key_encrypted: \(wnfs_key_encrypted)")
498
+
499
+ userDataHelper.add("cid_encrypted_" + identityEncryptedGlobalUnwrapped, cid_encrypted)
500
+ userDataHelper.add("wnfs_key_encrypted_" + identityEncryptedGlobalUnwrapped, wnfs_key_encrypted)
501
+ } else {
502
+ // Handle the case where rootCid, wnfsKey, or secretKeyGlobal is nil
503
+ NSLog("ReactNative encryptAndStoreConfig failed because one of the values is nil")
504
+ }
505
+ }
506
+ } catch let error {
507
+ NSLog("ReactNative encryptAndStoreConfig failed with Error: \(error.localizedDescription)")
508
+ throw error
509
+ }
510
+ }
511
+
512
+ func logoutInternal(identity: Data, _storePath: String?) throws {
513
+ do {
514
+ if (self.fula != nil) {
515
+ try self.fula?.flush()
516
+ }
517
+ let secretKey = try Cryptography.generateKey(identity)
518
+ let identity_encrypted: String = try Cryptography.encryptMsg(identity.toUint8Array(), [UInt8](secretKey))
519
+
520
+ userDataHelper.remove("cid_encrypted_"+identity_encrypted)
521
+
522
+ //TODO: Should also remove peerid @Mahdi
523
+
524
+ userDataHelper.remove("cid_encrypted_"+identity_encrypted)
525
+
526
+ rootCid = nil
527
+ secretKeyGlobal = nil
528
+ identityEncryptedGlobal = nil
529
+ var storePath = _storePath
530
+ if (storePath == nil || storePath!.isEmpty) {
531
+ storePath = fulaStorePath
532
+ }
533
+
534
+ do{
535
+ try FileManager.default.removeItem(atPath: fulaStorePath)
536
+ }catch let error{
537
+ print("Deleting fula store path", error.localizedDescription)
538
+ throw error
539
+ }
540
+ } catch let error {
541
+ print("ReactNative", "logout internal failed with Error: ", error.localizedDescription)
542
+ throw error
543
+ }
544
+ }
545
+
546
+ func getFulaClient() -> FulamobileClient? {
547
+ return self.fula
548
+ }
549
+
550
+ func newClientInternal(identity: Data, storePath: String?, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool) throws -> Void {
551
+ do {
552
+ NSLog("ReactNative fula newClientInternal refresh=\(refresh)")
553
+ fulaConfig = FulamobileConfig()
554
+ NSLog("ReactNative: cofig is set")
555
+ if (storePath == nil || storePath!.isEmpty) {
556
+ fulaConfig!.storePath = fulaStorePath
557
+ } else {
558
+ fulaConfig!.storePath = storePath!
559
+ }
560
+ NSLog("ReactNative storePath is set: \(fulaConfig!.storePath)")
561
+
562
+ let peerIdentity = try createPeerIdentity(privateKey: identity)
563
+ fulaConfig!.identity = peerIdentity
564
+ NSLog("ReactNative peerIdentity is set: \(fulaConfig!.identity!.toHex())")
565
+ fulaConfig!.bloxAddr = bloxAddr
566
+ NSLog("ReactNative bloxAddr is set: \(fulaConfig!.bloxAddr)")
567
+ fulaConfig!.exchange = exchange
568
+ fulaConfig!.syncWrites = autoFlush
569
+ if (useRelay) {
570
+ fulaConfig!.allowTransientConnection = true
571
+ fulaConfig!.forceReachabilityPrivate = true
572
+ }
573
+ if (self.fula == nil || refresh) {
574
+ NSLog("ReactNative Creating a new Fula instance")
575
+ do {
576
+ try shutdownInternal()
577
+ NSLog("ReactNative Creating a new Fula instance shutdown done")
578
+ var error: NSError?
579
+ let client = FulamobileNewClient(fulaConfig, &error)
580
+ if let error = error {
581
+ throw error
582
+ }
583
+
584
+ self.fula = client
585
+ NSLog("ReactNative FulamobileClient created")
586
+ if (self.fula != nil) {
587
+ NSLog("ReactNative Creating a new Fula instance fula is not null, flushing")
588
+ try self.fula?.flush()
589
+ } else {
590
+ NSLog("** ReactNative Creating a new Fula instance fula failed **")
591
+ }
592
+ } catch let error {
593
+ NSLog("ReactNative Failed to create new Fula instance: \(error.localizedDescription)")
594
+ throw MyError.runtimeError("ReactNative Failed to create new Fula instance")
595
+ }
596
+ }
597
+ NSLog("ReactNative peerIdentity returned: \(peerIdentity)")
598
+ } catch let error {
599
+ NSLog("ReactNative newclientInternal failed with Error: \(error.localizedDescription)")
600
+ throw error
601
+ }
602
+ }
603
+
604
+ func initInternal(identity: Data, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, _rootCid: String, useRelay: Bool, refresh: Bool) throws -> [String] {
605
+
606
+ do {
607
+ os_log("ReactNative: This is an info message.", log: OSLog.viewCycle, type: .info)
608
+ NSLog("ReactNative: This is a simple log message.")
609
+
610
+ NSLog("ReactNative fula initInternal=\(refresh)")
611
+ if (self.fula == nil || refresh) {
612
+ NSLog("ReactNative fula self.fula is null or refresh is set")
613
+ try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
614
+ NSLog("ReactNative fula initialized")
615
+ if (self.fula == nil) {
616
+ NSLog("ReactNative: fula is not initialized")
617
+ }
618
+ guard let fulaId = self.fula?.id_() else {
619
+ NSLog("ReactNative error: fula is not initialized")
620
+ throw MyError.runtimeError("ReactNative: fula client not ready")
621
+ }
622
+ NSLog("ReactNative fula initialized: \(fulaId)")
623
+ }
624
+ if(self.client == nil || self.wnfs == nil || refresh) {
625
+ NSLog("ReactNative fula self.client is null or refresh is set")
626
+ self.client = Client(clientInput: self.fula!)
627
+ self.wnfs = Wnfs(putFn: { cid, data in
628
+ guard let c = self.client else {
629
+ NSLog("ReactNative wnfs put: fula client not ready")
630
+ throw MyError.runtimeError("ReactNative wnfs: fula client not ready")
631
+ }
632
+ try c.put(cid, data)
633
+ }, getFn: { cid in
634
+ guard let c = self.client else {
635
+ NSLog("ReactNative wnfs get: fula client not ready")
636
+ throw MyError.runtimeError("ReactNative wnfs: fula client not ready")
637
+ }
638
+ return try c.get(cid)
639
+ })
640
+ NSLog("ReactNative wnfs initialized")
641
+ }
642
+
643
+ let secretKey = try Cryptography.generateKey(identity)
644
+ let identity_encrypted = try Cryptography.encryptMsg(identity.toUint8Array(), [UInt8](secretKey))
645
+
646
+ identityEncryptedGlobal = identity_encrypted
647
+ secretKeyGlobal = [UInt8](secretKey)
648
+
649
+ if (rootCid == nil || rootCid!.isEmpty) {
650
+ NSLog("ReactNative rootCid is empty.")
651
+ //Load from keystore
652
+
653
+ let cid_encrypted_fetched = userDataHelper.getValue("cid_encrypted_"+identity_encrypted)
654
+ NSLog("ReactNative Here1")
655
+ var cid: Array<UInt8>? = nil
656
+ if(cid_encrypted_fetched != nil && !cid_encrypted_fetched!.isEmpty) {
657
+ NSLog("ReactNative decrypting cid= \(cid_encrypted_fetched!) with secret \(secretKey.toHex())")
658
+ cid = try Cryptography.decryptMsg(cid_encrypted_fetched!, [UInt8](secretKey))
659
+
660
+ }
661
+ NSLog("ReactNative Here2")
662
+ //print("ReactNative", "Attempted to fetch cid from keystore cid="+cid+" & wnfs_key="+wnfs_key)
663
+ if(cid == nil || cid!.isEmpty){
664
+ NSLog("ReactNative cid or wnfs key was not found")
665
+ if(!_rootCid.isEmpty){
666
+ NSLog("ReactNative Re-setting cid from input: \(_rootCid)")
667
+ cid = _rootCid.toUint8Array()
668
+ }
669
+
670
+ }
671
+ if(cid == nil || cid!.isEmpty){
672
+ NSLog("ReactNative Tried to recover cid but was not successful. Creating ones")
673
+ try createNewrootCid(identity: identity)
674
+ } else {
675
+ NSLog("ReactNative Found cid and wnfs key in keychain store")
676
+ NSLog("ReactNative Recovered cid and private ref from keychain store. cid=\(cid!) & wnfs_key=\(identity)")
677
+ try loadWnfs(identity, cid!.toData().toUTF8String()!)
678
+ }
679
+ NSLog("ReactNative creating/reloading rootCid completed")
680
+
681
+ /*
682
+ byte[] testbyte = convertStringToByte("-104,40,24,-93,24,100,24,114,24,111,24,111,24,116,24,-126,24,-126,0,0,24,-128,24,103,24,118,24,101,24,114,24,115,24,105,24,111,24,110,24,101,24,48,24,46,24,49,24,46,24,48,24,105,24,115,24,116,24,114,24,117,24,99,24,116,24,117,24,114,24,101,24,100,24,104,24,97,24,109,24,116")
683
+ long testcodec = 85
684
+ byte[] testputcid = client!.put(testbyte, testcodec)
685
+ print("ReactNative", "client!.put test done"+ Arrays.toString(testputcid))
686
+ byte[] testfetchedcid = convertStringToByte("1,113,18,32,-6,-63,-128,79,-102,-89,57,77,-8,67,-98,8,-81,40,-87,123,122,29,-52,-124,-60,-53,100,105,125,123,-5,-99,41,106,-124,-64")
687
+ byte[] testfetchedbytes = client!.get(testfetchedcid)
688
+ print("ReactNative", "client!.get test done"+ Arrays.toString(testfetchedbytes))
689
+ */
690
+
691
+
692
+ NSLog("ReactNative rootCid is created: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
693
+ } else {
694
+ NSLog("ReactNative rootCid existed: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
695
+ }
696
+ guard let peerId = self.fula?.id_() else {
697
+ NSLog("ReactNative error: fula is not initialized")
698
+ throw MyError.runtimeError("ReactNative: fula client not ready")
699
+ }
700
+ NSLog("ReactNative fula peerId initialized: \(peerId)")
701
+ var obj = [String]()
702
+ obj.append(peerId)
703
+ obj.append(rootCid!)
704
+ obj.append(wnfsKey!.toHex())
705
+ NSLog("ReactNative initInternal is completed successfully")
706
+ if (self.fula != nil) {
707
+ try self.fula?.flush()
708
+ }
709
+ return obj
710
+ } catch let error {
711
+ NSLog("ReactNative init internal failed with Error: \(error.localizedDescription)")
712
+ throw error
713
+ }
714
+ }
715
+
716
+
717
+ @objc(mkdir:withResolver:withRejecter:)
718
+ func mkdir(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
719
+ NSLog("ReactNative mkdir: path = \(path)")
720
+ do {
721
+ let cid = try self.wnfs?.MkDir(cid: rootCid!, remotePath: path)
722
+ if(cid != nil) {
723
+ rootCid = cid
724
+ try encryptAndStoreConfig()
725
+ if (self.fula != nil) {
726
+ try self.fula?.flush()
727
+ }
728
+ resolve(rootCid)
729
+ } else {
730
+ NSLog("ReactNative mkdir Error: config is nil")
731
+ reject("ERR_WNFS", "Can't make dir", nil)
732
+ }
733
+
734
+ } catch let error{
735
+ print("mkdir", error.localizedDescription)
736
+ reject("ERR_WNFS", "mkdir", error)
737
+ }
738
+ }
739
+
740
+ @objc(writeFile:withLocalFilename:withResolver:withRejecter:)
741
+ func writeFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
742
+ /*
743
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
744
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
745
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
746
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
747
+ // Returns: new cid of the root after this file is placed in the tree
748
+ */
749
+ NSLog("ReactNative writeFile to : path = \(fulaTargetFilename) + from: \(localFilename)")
750
+ do {
751
+ let cid = try self.wnfs?.WriteFileFromPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
752
+ if(cid != nil) {
753
+ rootCid = cid
754
+ try encryptAndStoreConfig()
755
+ if (self.fula != nil) {
756
+ try self.fula?.flush()
757
+ }
758
+ resolve(rootCid)
759
+ } else {
760
+ NSLog("ReactNative writeFile Error: config is nil")
761
+ reject("ERR_WNFS", "writeFile Error: config is nil", nil)
762
+ }
763
+ } catch let error {
764
+ print("writeFile", error.localizedDescription)
765
+ reject("ERR_WNFS", "writeFile", error)
766
+ }
767
+ }
768
+
769
+ @objc(writeFileContent:withContentString:withResolver:withRejecter:)
770
+ func writeFileContent(path: String, contentString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
771
+
772
+ NSLog("ReactNative writeFile: contentString = \(contentString)")
773
+ NSLog("ReactNative writeFile: path = \(path)")
774
+ do {
775
+ let content = convertStringToByte(contentString)
776
+ let cid = try self.wnfs?.WriteFile(cid: rootCid!, remotePath: path, data: content.toData())
777
+ rootCid = cid
778
+ try encryptAndStoreConfig()
779
+ if (self.fula != nil) {
780
+ try self.fula?.flush()
781
+ }
782
+ resolve(rootCid)
783
+ } catch let error {
784
+ print("writeFileContent", error.localizedDescription)
785
+ reject("ERR_WNFS", "writeFileContent", error)
786
+ }
787
+
788
+ }
789
+
790
+ @objc(ls:withResolver:withRejecter:)
791
+ func ls(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
792
+ NSLog("ReactNative ls: path = \(path)")
793
+ do {
794
+ let res = try self.wnfs?.Ls(cid: rootCid!, remotePath: path)
795
+
796
+ //JSONArray jsonArray = new JSONArray(res)
797
+ guard let s = res?.toUTF8String() else {
798
+ throw MyError.runtimeError("ReactNative converting bytes to utf8 string")
799
+ }
800
+ NSLog("ReactNative ls: res = \(s)")
801
+ resolve(s)
802
+ } catch let error {
803
+ print("ls", error.localizedDescription)
804
+ reject("ERR_WNFS", "ls", error)
805
+ }
806
+
807
+ }
808
+
809
+ @objc(rm:withResolver:withRejecter:)
810
+ func rm(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
811
+
812
+ print("ReactNative", "rm: path = " + path)
813
+ do {
814
+ let cid = try self.wnfs?.Rm(cid: rootCid!, remotePath: path)
815
+ if(cid != nil) {
816
+ rootCid = cid
817
+ try encryptAndStoreConfig()
818
+ if (self.fula != nil) {
819
+ try self.fula?.flush()
820
+ }
821
+ resolve(rootCid)
822
+ } else {
823
+ print("ReactNative", "rm Error: config is nil")
824
+ reject("ERR_WNFS", "rm Error: config is nil", nil)
825
+ }
826
+ } catch let error {
827
+ print("rm", error.localizedDescription)
828
+ reject("ERR_WNFS", "rm", error)
829
+ }
830
+
831
+ }
832
+
833
+ @objc(cp:withTargetPath:withResolver:withRejecter:)
834
+ func cp(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
835
+
836
+ print("ReactNative", "rm: sourcePath = " + sourcePath)
837
+ do {
838
+ let cid = try self.wnfs?.Cp(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
839
+ if(cid != nil) {
840
+ rootCid = cid
841
+ try encryptAndStoreConfig()
842
+ if (self.fula != nil) {
843
+ try self.fula?.flush()
844
+ }
845
+ resolve(rootCid)
846
+ } else {
847
+ print("ReactNative", "cp Error: config is nil")
848
+ reject("ERR_WNFS", "cp Error: config is nil", nil)
849
+ }
850
+ } catch let error {
851
+ print("cp", error.localizedDescription)
852
+ reject("ERR_WNFS", "cp", error)
853
+ }
854
+
855
+ }
856
+
857
+ @objc(mv:withTargetPath:withResolver:withRejecter:)
858
+ func mv(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
859
+ print("ReactNative", "rm: sourcePath = " + sourcePath)
860
+ do {
861
+ let cid = try self.wnfs?.Mv(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
862
+ if(cid != nil) {
863
+ rootCid = cid
864
+ try encryptAndStoreConfig()
865
+ if (self.fula != nil) {
866
+ try self.fula?.flush()
867
+ }
868
+ resolve(rootCid)
869
+ } else {
870
+ print("ReactNative", "mv Error: config is nil")
871
+ reject("ERR_WNFS", "mv Error: config is nil", nil)
872
+ }
873
+ } catch let error {
874
+ print("mv", error.localizedDescription)
875
+ reject("ERR_WNFS", "mv", error)
876
+ }
877
+
878
+ }
879
+
880
+ @objc(readFile:withLocalFilename:withResolver:withRejecter:)
881
+ func readFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
882
+ /*
883
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
884
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
885
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
886
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
887
+ // Returns: new cid of the root after this file is placed in the tree
888
+ */
889
+ print("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename)
890
+ do {
891
+ let path = try self.wnfs?.ReadFileToPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
892
+ resolve(path)
893
+ } catch let error {
894
+ print("readFile", error.localizedDescription)
895
+ reject("ERR_WNFS", "readFile", error)
896
+ }
897
+
898
+ }
899
+
900
+ @objc(readFileContent:withResolver:withRejecter:)
901
+ func readFileContent(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
902
+
903
+ print("ReactNative", "readFileContent: path = " + path)
904
+ do {
905
+ // FIXME: dhouldn't we output an NSData object instead?
906
+ let res = try self.wnfs?.ReadFile(cid: rootCid!, remotePath: path)
907
+ guard let resString = res?.toUTF8String() else{
908
+ throw MyError.runtimeError(" ReactNative converting bytes to utf8 string")
909
+ }
910
+ resolve(resString)
911
+ } catch let error {
912
+ print("readFileContent", error.localizedDescription)
913
+ reject("ERR_WNFS", "readFileContent", error)
914
+ }
915
+
916
+ }
917
+
918
+ @objc(get:withResolver:withRejecter:)
919
+ func get(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
920
+
921
+ print("ReactNative", "get: keyString = " + keyString)
922
+ do {
923
+ let key: Data = convertStringToByte(keyString).toData()
924
+ let value = try getInternal(key)
925
+ let valueString: String = value.toUTF8String()!
926
+ resolve(valueString)
927
+ } catch let error {
928
+ print("get", error.localizedDescription)
929
+ reject("ERR_FULA", "get", error)
930
+ }
931
+
932
+ }
933
+
934
+ func getInternal(_ key: Data) throws -> Data {
935
+ do {
936
+ print("ReactNative", "getInternal: key.toUTF8String() = " , key.toUTF8String()!)
937
+ print("ReactNative", "getInternal: key.toHex().bytes = " , key.toHex())
938
+ let value = try self.fula!.get(key)
939
+ print("ReactNative", "getInternal: value.toHex() = " , value.toHex())
940
+ return value
941
+ } catch let error {
942
+ print("ReactNative", "getInternal: error = " + error.localizedDescription)
943
+ print("getInternal", error.localizedDescription)
944
+ throw error
945
+ }
946
+ }
947
+
948
+ @objc(has:withResolver:withRejecter:)
949
+ func has(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
950
+
951
+ print("ReactNative", "has: keyString = " + keyString)
952
+ do {
953
+ let key: Data = convertStringToByte(keyString).toData()
954
+ let result = try hasInternal(key)
955
+ resolve(result)
956
+ } catch let error {
957
+ print("has", error.localizedDescription)
958
+ reject("ERR_FULA", "has", error)
959
+ }
960
+
961
+ }
962
+
963
+ func hasInternal(_ key: Data) throws -> Bool {
964
+ do {
965
+ let ret = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
966
+ try self.fula?.has(key, ret0_: ret)
967
+ let res = ret.pointee.boolValue
968
+ ret.deallocate()
969
+ return res
970
+ } catch let error {
971
+ print("hasInternal", error.localizedDescription)
972
+ throw error
973
+ }
974
+ }
975
+
976
+ func pullInternal(key: Data) throws -> Void {
977
+ do {
978
+ try self.fula!.pull(key)
979
+ } catch let error {
980
+ print("pullInternal", error.localizedDescription)
981
+ throw error
982
+ }
983
+ }
984
+
985
+ @objc(push:withRejecter:)
986
+ func push(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
987
+ print("ReactNative", "push started")
988
+ do {
989
+ try pushInternal(key: convertStringToByte(rootCid!).toData())
990
+ resolve(rootCid)
991
+ } catch let error {
992
+ print("get", error.localizedDescription)
993
+ reject("ERR_FULA", "push", error)
994
+ }
995
+ }
996
+
997
+ func pushInternal(key: Data) throws -> Void {
998
+ do {
999
+ let hasIt = try hasInternal(key)
1000
+ if (self.fula != nil && hasIt) {
1001
+ try self.fula?.push(key)
1002
+ try self.fula?.flush()
1003
+ } else {
1004
+ print("ReactNative", "pushInternal error: key wasn't found or fula is not initialized")
1005
+ throw MyError.runtimeError("ReactNative pushInternal error: key wasn't found or fula is not initialized")
1006
+ }
1007
+ } catch let error {
1008
+ print("ReactNative", "pushInternal", error.localizedDescription)
1009
+ throw error
1010
+ }
1011
+ }
1012
+
1013
+ // FIXME: unused codecString arg
1014
+ @objc(put:withCodecString:withResolver:withRejecter:)
1015
+ func put(valueString: String, codecString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1016
+
1017
+ print("ReactNative", "put: codecString = " + codecString)
1018
+ print("ReactNative", "put: valueString = " + valueString)
1019
+ do {
1020
+ //byte[] codec = convertStringToByte(CodecString)
1021
+ let codec = FulaModule.CODEC_DAG_CBOR
1022
+
1023
+
1024
+ print("ReactNative", "put: codec = ", codec)
1025
+ let value = valueString.toData()
1026
+
1027
+ print("ReactNative", "put: value.toHex() = " , value.toHex())
1028
+ let key = try putInternal(value: value, codec: codec)
1029
+ print("ReactNative", "put: key.toHex() = " , key.toUTF8String()!)
1030
+ resolve(key.toUTF8String()!)
1031
+ } catch let error {
1032
+ print("ReactNative", "put: error = ", error.localizedDescription)
1033
+ reject("ERR_FULA", "put", error)
1034
+ }
1035
+
1036
+ }
1037
+
1038
+ // FIXME: unused codec arg
1039
+ func putInternal(value: Data, codec: Int) throws -> Data {
1040
+ do {
1041
+ if(self.fula != nil) {
1042
+ let key: Data = try self.fula!.put(value, codec: Int64(FulaModule.CODEC_DAG_CBOR))
1043
+ try self.fula?.flush()
1044
+ return key
1045
+ } else {
1046
+ print("ReactNative", "putInternal Error: fula is not initialized")
1047
+ throw MyError.runtimeError("ReactNative putInternal Error: fula is not initialized")
1048
+ }
1049
+ } catch let error {
1050
+ print("ReactNative", "putInternal", error.localizedDescription)
1051
+ throw error
1052
+ }
1053
+ }
1054
+
1055
+ @objc(setAuth:withAllow:withResolver:withRejecter:)
1056
+ func setAuth(peerIdString: String, allow: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1057
+
1058
+ print("ReactNative", "setAuth: peerIdString = " + peerIdString)
1059
+ do {
1060
+ if (self.fula != nil && !(self.fula?.id_().isEmpty)! && fulaConfig != nil && !fulaConfig!.bloxAddr.isEmpty) {
1061
+ let bloxAddr = fulaConfig!.bloxAddr
1062
+ print("ReactNative", "setAuth: bloxAddr = '",bloxAddr,"'"," peerIdString = '",peerIdString,"'")
1063
+ let parts = bloxAddr.split(separator: "/").map(String.init)
1064
+ try self.fula?.setAuth(parts.last, subject: peerIdString, allow: allow)
1065
+ resolve(true)
1066
+ } else {
1067
+ print("ReactNative", "setAuth error: fula is not initialized")
1068
+ throw MyError.runtimeError("ReactNative fula is not initialized")
1069
+ }
1070
+ resolve(false)
1071
+ } catch let error {
1072
+ print("get", error.localizedDescription)
1073
+ reject("ERR_FULA", "setAuth", error)
1074
+ }
1075
+
1076
+ }
1077
+
1078
+ @objc(clearCidsFromRecent:withResolver:withRejecter:)
1079
+ func clearCidsFromRecent(cidArray: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1080
+ DispatchQueue.global(qos: .default).async {
1081
+ do {
1082
+ guard let fulaClient = self.fula else {
1083
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
1084
+ }
1085
+
1086
+ // Concatenate all CID strings into a single string separated by "|"
1087
+ let concatenatedCids = (cidArray as? [String])?.joined(separator: "|")
1088
+
1089
+ guard let cidsData = concatenatedCids?.data(using: .utf8) else {
1090
+ throw MyError.runtimeError("ReactNative Unable to encode CIDs as data")
1091
+ }
1092
+
1093
+ try fulaClient.clearCids(fromRecent: cidsData)
1094
+ resolve(true)
1095
+ } catch let error {
1096
+ print("ReactNative", "clearCidsFromRecent failed with Error: \(error.localizedDescription)")
1097
+ reject("ERR_FULA_CLEAR_CIDS", "Failed to clear CIDs from recent", error)
1098
+ }
1099
+ }
1100
+ }
1101
+
1102
+
1103
+ @objc(listRecentCidsAsString:withRejecter:)
1104
+ func listRecentCidsAsString(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1105
+ DispatchQueue.global(qos: .default).async {
1106
+ do {
1107
+ guard let fulaClient = self.fula else {
1108
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
1109
+ }
1110
+
1111
+ let recentLinksIterator = try fulaClient.listRecentCidsAsString()
1112
+ var recentLinksList = [String]()
1113
+
1114
+ while recentLinksIterator.hasNext() {
1115
+ var error: NSError?
1116
+ let nextLink = try recentLinksIterator.next(&error)
1117
+
1118
+ if let error = error {
1119
+ throw error
1120
+ }
1121
+
1122
+ recentLinksList.append(nextLink)
1123
+ }
1124
+
1125
+ if !recentLinksList.isEmpty {
1126
+ // Return the whole list
1127
+ resolve(recentLinksList)
1128
+ } else {
1129
+ resolve(false)
1130
+ }
1131
+ } catch let error {
1132
+ print("ReactNative", "listRecentCidsAsString failed with Error: \(error.localizedDescription)")
1133
+ reject("ERR_FULA_LIST_RECENT_CIDS", "Failed to list recent CIDs as string", error)
1134
+ }
1135
+ }
1136
+ }
1137
+
1138
+ @objc(listRecentCidsAsStringWithChildren:withRejecter:)
1139
+ func listRecentCidsAsStringWithChildren(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1140
+ DispatchQueue.global(qos: .default).async {
1141
+ do {
1142
+ guard let fulaClient = self.fula else {
1143
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
1144
+ }
1145
+
1146
+ let recentLinksIterator = try fulaClient.listRecentCidsAsStringWithChildren()
1147
+ var recentLinksList = [String]()
1148
+
1149
+ while recentLinksIterator.hasNext() {
1150
+ var error: NSError?
1151
+ let nextLink = try recentLinksIterator.next(&error)
1152
+
1153
+ if let error = error {
1154
+ throw error
1155
+ }
1156
+
1157
+ recentLinksList.append(nextLink)
1158
+ }
1159
+
1160
+ if !recentLinksList.isEmpty {
1161
+ // Return the whole list
1162
+ resolve(recentLinksList)
1163
+ } else {
1164
+ resolve(false)
1165
+ }
1166
+ } catch let error {
1167
+ print("ReactNative", "listRecentCidsAsStringWithChildren failed with Error: \(error.localizedDescription)")
1168
+ reject("ERR_FULA_LIST_RECENT_CIDS_WITH_CHILDREN", "Failed to list recent CIDs as string with children", error)
1169
+ }
1170
+ }
1171
+ }
1172
+
1173
+ @objc(batchUploadManifest:withPoolId:withReplicationFactor:withResolver:withRejecter:)
1174
+ func batchUploadManifest(cidArray: NSArray, poolIDStr: String, replicationFactorStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1175
+ DispatchQueue.global(qos: .default).async {
1176
+ do {
1177
+ guard let fulaClient = self.fula else {
1178
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
1179
+ }
1180
+ guard let poolID = Int64(poolIDStr) else {
1181
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Invalid poolID - not a valid number: \(poolIDStr)"])
1182
+ reject("ERR_FULA", "Invalid poolID - not a valid number: \(poolIDStr)", error)
1183
+ return
1184
+ }
1185
+ guard let replicationFactor = Int64(replicationFactorStr) else {
1186
+ let error = NSError(domain: "FULAErrorDomain",
1187
+ code: 1002,
1188
+ userInfo: [NSLocalizedDescriptionKey: "Invalid replicationFactor - not a valid number: \(replicationFactorStr)"])
1189
+ reject("ERR_FULA", "Invalid replicationFactorStr - not a valid number: \(replicationFactorStr)", error)
1190
+ return
1191
+ }
1192
+
1193
+ // Concatenate all CID strings into a single string separated by "|"
1194
+ let concatenatedCids = (cidArray as? [String])?.joined(separator: "|")
1195
+
1196
+ guard let cidsData = concatenatedCids?.data(using: .utf8) else {
1197
+ throw MyError.runtimeError("ReactNative Unable to encode CIDs as data")
1198
+ }
1199
+
1200
+ // Adjusted call to match the expected method signature and argument types
1201
+ try fulaClient.batchUploadManifest(cidsData, poolID: Int(poolID), replicationFactor: Int(replicationFactor))
1202
+ resolve(true)
1203
+ } catch let error {
1204
+ print("ReactNative", "batchUploadManifest failed with Error: \(error.localizedDescription)")
1205
+ reject("ERR_FULA_BATCH_UPLOAD_MANIFEST", "Failed to batch upload CIDs", error)
1206
+ }
1207
+ }
1208
+ }
1209
+
1210
+
1211
+
1212
+ @objc(shutdown:withRejecter:)
1213
+ func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
1214
+ do {
1215
+ try shutdownInternal()
1216
+ resolve(true)
1217
+ } catch let error {
1218
+ NSLog("ReactNative shutdown \(error.localizedDescription)")
1219
+ reject("ERR_FULA", "shutdown", error)
1220
+ }
1221
+
1222
+ }
1223
+
1224
+ @objc
1225
+ func deleteDsLock(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1226
+ let lockFilePath = (fulaConfig!.storePath as NSString).appendingPathComponent("LOCK")
1227
+ let fileManager = FileManager.default
1228
+
1229
+ do {
1230
+ if fileManager.fileExists(atPath: lockFilePath) {
1231
+ try fileManager.removeItem(atPath: lockFilePath)
1232
+ NSLog("ReactNative: Lock file deleted successfully.")
1233
+ resolve(true)
1234
+ } else {
1235
+ NSLog("ReactNative: Lock file does not exist.")
1236
+ resolve(false) // Resolve with false if the file doesn't exist
1237
+ }
1238
+ } catch let error as NSError {
1239
+ NSLog("ReactNative: Failed to delete lock file: \(error.localizedDescription)")
1240
+ reject("delete_error", "Failed to delete lock file", error)
1241
+ }
1242
+ }
1243
+
1244
+ func shutdownInternal() {
1245
+ NSLog("ReactNative shutdownInternal")
1246
+ if self.fula != nil {
1247
+ NSLog("ReactNative shutdownInternal fula is not null")
1248
+ do {
1249
+ try self.fula?.shutdown()
1250
+ NSLog("ReactNative shutdownInternal fula.shutdown called")
1251
+ } catch {
1252
+ // Handle specific errors if needed or log them
1253
+ NSLog("ReactNative shutdownInternal error: \(error.localizedDescription)")
1254
+ }
1255
+ // Ensure resources are cleaned up regardless of errors
1256
+ self.fula = nil
1257
+ self.client = nil
1258
+ self.wnfs = nil
1259
+ }
1260
+ }
1261
+
1262
+ ///////////////////////////////////////////////////////////
1263
+ ///////////////////////////////////////////////////////////
1264
+ ///////////////////////////////////////////////////////////
1265
+ ///////////////////////////////////////////////////////////
1266
+ //////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
1267
+ ///////////////////////////////////////////////////////////
1268
+
1269
+ @objc(checkAccountExists:withResolver:withRejecter:)
1270
+ func checkAccountExists(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1271
+ print("ReactNative", "checkAccountExists: accountString = ", accountString)
1272
+ do {
1273
+ let result = try self.fula!.accountExists(accountString)
1274
+ let resultString = result.toUTF8String()!
1275
+ resolve(resultString)
1276
+ } catch let error {
1277
+ print("checkAccountExists", error.localizedDescription)
1278
+ reject("ERR_FULA", "checkAccountExists", error)
1279
+ }
1280
+
1281
+ }
1282
+
1283
+ @objc(accountFund:withResolver:withRejecter:)
1284
+ func accountFund(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1285
+ print("ReactNative", "accountFund: accountString = ", accountString)
1286
+ do {
1287
+ let result = try self.fula!.accountFund(accountString)
1288
+ let resultString = result.toUTF8String()!
1289
+ resolve(resultString)
1290
+ } catch let error {
1291
+ print("accountFund", error.localizedDescription)
1292
+ reject("ERR_FULA", "accountFund", error)
1293
+ }
1294
+
1295
+ }
1296
+
1297
+ @objc(listPools:withRejecter:)
1298
+ func listPools( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1299
+ print("ReactNative", "listPools")
1300
+ do {
1301
+ let result = try self.fula!.poolList()
1302
+ let resultString = result.toUTF8String()!
1303
+ resolve(resultString)
1304
+ } catch let error {
1305
+ print("listPools", error.localizedDescription)
1306
+ reject("ERR_FULA", "listPools", error)
1307
+ }
1308
+
1309
+ }
1310
+
1311
+ @objc(listPoolJoinRequests:withResolver:withRejecter:)
1312
+ func listPoolJoinRequests(poolIDStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1313
+ print("ReactNative", "listPoolJoinRequests: poolIDStr = ", poolIDStr)
1314
+ do {
1315
+ if let poolID = Int64(poolIDStr), let intPoolID = Int(exactly: poolID) {
1316
+ // Conversion to Int successful - use intPoolID
1317
+ let result = try self.fula!.poolRequests(intPoolID)
1318
+ let resultString = result.toUTF8String()!
1319
+ resolve(resultString)
1320
+ } else {
1321
+ // Handle invalid input or Int64 not convertible to Int
1322
+ let error = NSError(domain: "FULAErrorDomain",
1323
+ code: 1003,
1324
+ userInfo: [NSLocalizedDescriptionKey: "Invalid poolIDStr - not a valid number or too large: \(poolIDStr)"])
1325
+ reject("ERR_FULA", "Invalid poolIDStr - not a valid number or too large: \(poolIDStr)", error)
1326
+ }
1327
+ } catch let error {
1328
+ print("listPoolJoinRequests", error.localizedDescription)
1329
+ let nsError = error as NSError
1330
+ reject("ERR_FULA", "Failed listPoolJoinRequests due to error", nsError)
1331
+ }
1332
+ }
1333
+
1334
+ @objc(listAvailableReplicationRequests:withResolver:withRejecter:)
1335
+ func listAvailableReplicationRequests(poolIDStr: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1336
+ print("ReactNative", "listAvailableReplicationRequests: poolIDStr = ", poolIDStr)
1337
+ do {
1338
+ guard let poolID = Int64(poolIDStr), let intPoolID = Int(exactly: poolID) else {
1339
+ let error = NSError(domain: "FULAErrorDomain",
1340
+ code: 1004, // Use appropriate error code
1341
+ userInfo: [NSLocalizedDescriptionKey: "Invalid poolID - not a valid number: \(poolIDStr)"])
1342
+ reject("ERR_FULA", "Invalid poolID - not a valid number: \(poolIDStr)", error)
1343
+ return
1344
+ }
1345
+ let result = try self.fula!.manifestAvailable(intPoolID)
1346
+ guard let resultString = result.toUTF8String() else {
1347
+ let error = NSError(domain: "FULAErrorDomain",
1348
+ code: 1005, // Use appropriate error code
1349
+ userInfo: [NSLocalizedDescriptionKey: "Failed to convert result to UTF8 String"])
1350
+ reject("ERR_FULA", "Conversion Error", error)
1351
+ return
1352
+ }
1353
+ resolve(resultString)
1354
+ } catch let error as NSError {
1355
+ print("listAvailableReplicationRequests", error.localizedDescription)
1356
+ reject("ERR_FULA", "listAvailableReplicationRequests failed with error: \(error.localizedDescription)", error)
1357
+ }
1358
+ }
1359
+
1360
+
1361
+ @objc(bloxFreeSpace:withRejecter:)
1362
+ func bloxFreeSpace(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
1363
+ print("ReactNative", "bloxFreeSpace")
1364
+ guard let fulaClient = self.fula else {
1365
+ print("bloxFreeSpace", "fula client is nil")
1366
+ let error = NSError(domain: "FulaModuleError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1367
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1368
+ return
1369
+ }
1370
+
1371
+ do {
1372
+ let result = try fulaClient.bloxFreeSpace()
1373
+ guard let resultString = result.toUTF8String() else {
1374
+ let error = NSError(domain: "FulaModuleError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to convert result to String"])
1375
+ reject("ERR_FULA_RESULT_CONVERSION", "Failed to convert result to String", error)
1376
+ return
1377
+ }
1378
+ resolve(resultString)
1379
+ } catch let error {
1380
+ print("bloxFreeSpace", error.localizedDescription)
1381
+ reject("ERR_FULA", "bloxFreeSpace", error)
1382
+ }
1383
+ }
1384
+
1385
+ @objc(transferToFula:wallet:chain:withResolver:withRejecter:)
1386
+ func transferToFula(amount: String, wallet: String, chain: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1387
+ DispatchQueue.global(qos: .default).async {
1388
+ do {
1389
+ print("ReactNative", "transferToFula called")
1390
+ let result = try self.fula!.transfer(toFula: amount, walletAccount: wallet, chain: chain)
1391
+ let resultString = String(data: result, encoding: .utf8)
1392
+ resolve(resultString)
1393
+ } catch let error {
1394
+ print("ReactNative", "transferToFula failed with Error: \(error.localizedDescription)")
1395
+ reject("ERR_FULA_TRANSFER", "transferToFula failed", error)
1396
+ }
1397
+ }
1398
+ }
1399
+
1400
+
1401
+ @objc(getAccount:withRejecter:)
1402
+ func getAccount(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1403
+ guard let fulaClient = self.fula else {
1404
+ let error = NSError(domain: "FulaModuleError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1405
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1406
+ return
1407
+ }
1408
+
1409
+ do {
1410
+ let account = try fulaClient.getAccount()
1411
+ guard let accountString = String(data: account, encoding: .utf8) else {
1412
+ let conversionError = NSError(domain: "FulaModuleError", code: -2, userInfo: [NSLocalizedDescriptionKey: "Unable to convert account data to String"])
1413
+ reject("ERR_FULA_CONVERSION_FAILED", "Unable to convert account data to String", conversionError)
1414
+ return
1415
+ }
1416
+ resolve(accountString)
1417
+ } catch let error {
1418
+ reject("ERR_FULA", "getAccount: \(error.localizedDescription)", error)
1419
+ }
1420
+ }
1421
+
1422
+ @objc(assetsBalance:withAssetId:withClassId:withResolver:withRejecter:)
1423
+ func assetsBalance(account: String, assetId: String, classId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1424
+ guard let assetIdInt = Int(assetId), let classIdInt = Int(classId) else {
1425
+ reject("ERR_FULA", "Invalid assetId or classId", nil)
1426
+ return
1427
+ }
1428
+
1429
+ do {
1430
+ let balance = try self.fula!.assetsBalance(account, assetId: assetIdInt, classId: classIdInt)
1431
+ let balanceString = String(data: balance, encoding: .utf8)
1432
+ resolve(balanceString)
1433
+ } catch let error {
1434
+ reject("ERR_FULA", "assetsBalance: \(error.localizedDescription)", error)
1435
+ }
1436
+ }
1437
+
1438
+ @objc(joinPool:withResolver:withRejecter:)
1439
+ func joinPool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1440
+ print("ReactNative", "joinPool: poolID = ", poolID)
1441
+ do {
1442
+ guard let poolIdInt = Int(poolID) else {
1443
+ throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1444
+ }
1445
+ let result = try self.fula!.poolJoin(poolIdInt)
1446
+ let resultString = String(data: result, encoding: .utf8)
1447
+ resolve(resultString)
1448
+ } catch let error {
1449
+ reject("ERR_FULA", "joinPool: \(error.localizedDescription)", error)
1450
+ }
1451
+ }
1452
+
1453
+ @objc(cancelPoolJoin:withResolver:withRejecter:)
1454
+ func cancelPoolJoin(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1455
+ do {
1456
+ guard let poolIdInt = Int(poolID) else {
1457
+ throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1458
+ }
1459
+ let result = try self.fula!.poolCancelJoin(poolIdInt)
1460
+ let resultString = String(data: result, encoding: .utf8)
1461
+ resolve(resultString)
1462
+ } catch let error {
1463
+ reject("ERR_FULA", "cancelPoolJoin: \(error.localizedDescription)", error)
1464
+ }
1465
+ }
1466
+
1467
+ @objc(leavePool:withResolver:withRejecter:)
1468
+ func leavePool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1469
+ print("ReactNative", "leavePool: poolID = ", poolID)
1470
+ do {
1471
+ guard let poolIdInt = Int(poolID) else {
1472
+ throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
1473
+ }
1474
+ let result = try self.fula!.poolLeave(poolIdInt)
1475
+ let resultString = String(data: result, encoding: .utf8)
1476
+ resolve(resultString)
1477
+ } catch let error {
1478
+ reject("ERR_FULA", "leavePool: \(error.localizedDescription)", error)
1479
+ }
1480
+ }
1481
+
1482
+ @objc(joinPoolWithChain:withChainName:withResolver:withRejecter:)
1483
+ func joinPoolWithChain(poolID: String, chainName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1484
+ print("ReactNative", "joinPoolWithChain: poolID = ", poolID, ", chainName = ", chainName)
1485
+ do {
1486
+ // Validate inputs
1487
+ guard !poolID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
1488
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Pool ID cannot be null or empty"])
1489
+ reject("INVALID_POOL_ID", "Pool ID cannot be null or empty", error)
1490
+ return
1491
+ }
1492
+
1493
+ guard !chainName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
1494
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Chain name cannot be null or empty"])
1495
+ reject("INVALID_CHAIN_NAME", "Chain name cannot be null or empty", error)
1496
+ return
1497
+ }
1498
+
1499
+ guard let fula = self.fula else {
1500
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1501
+ reject("FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1502
+ return
1503
+ }
1504
+
1505
+ guard let poolIdInt = Int(poolID) else {
1506
+ let error = NSError(domain: "FULAErrorDomain", code: 1004, userInfo: [NSLocalizedDescriptionKey: "Pool ID must be a valid number: \(poolID)"])
1507
+ reject("INVALID_POOL_ID_FORMAT", "Pool ID must be a valid number: \(poolID)", error)
1508
+ return
1509
+ }
1510
+
1511
+ let result = try fula.poolJoinWithChain(poolIdInt, chainName: chainName)
1512
+ let resultString = String(data: result, encoding: .utf8)
1513
+ resolve(resultString)
1514
+ } catch let error {
1515
+ reject("ERR_FULA", "joinPoolWithChain: \(error.localizedDescription)", error)
1516
+ }
1517
+ }
1518
+
1519
+ @objc(leavePoolWithChain:withChainName:withResolver:withRejecter:)
1520
+ func leavePoolWithChain(poolID: String, chainName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1521
+ print("ReactNative", "leavePoolWithChain: poolID = ", poolID, ", chainName = ", chainName)
1522
+ do {
1523
+ // Validate inputs
1524
+ guard !poolID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
1525
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Pool ID cannot be null or empty"])
1526
+ reject("INVALID_POOL_ID", "Pool ID cannot be null or empty", error)
1527
+ return
1528
+ }
1529
+
1530
+ guard !chainName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
1531
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Chain name cannot be null or empty"])
1532
+ reject("INVALID_CHAIN_NAME", "Chain name cannot be null or empty", error)
1533
+ return
1534
+ }
1535
+
1536
+ guard let fula = self.fula else {
1537
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1538
+ reject("FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1539
+ return
1540
+ }
1541
+
1542
+ guard let poolIdInt = Int(poolID) else {
1543
+ let error = NSError(domain: "FULAErrorDomain", code: 1004, userInfo: [NSLocalizedDescriptionKey: "Pool ID must be a valid number: \(poolID)"])
1544
+ reject("INVALID_POOL_ID_FORMAT", "Pool ID must be a valid number: \(poolID)", error)
1545
+ return
1546
+ }
1547
+
1548
+ let result = try fula.poolLeaveWithChain(poolIdInt, chainName: chainName)
1549
+ let resultString = String(data: result, encoding: .utf8)
1550
+ resolve(resultString)
1551
+ } catch let error {
1552
+ reject("ERR_FULA", "leavePoolWithChain: \(error.localizedDescription)", error)
1553
+ }
1554
+ }
1555
+
1556
+
1557
+ @objc(eraseBlData:withRejecter:)
1558
+ func eraseBlData(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1559
+ do {
1560
+ let result = try self.fula!.eraseBlData()
1561
+ let resultString = String(data: result, encoding: .utf8)
1562
+ resolve(resultString)
1563
+ } catch let error {
1564
+ reject("ERR_FULA", "eraseBlData: \(error.localizedDescription)", error)
1565
+ }
1566
+ }
1567
+
1568
+ @objc(reboot:withRejecter:)
1569
+ func reboot(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1570
+ do {
1571
+ let result = try self.fula!.reboot()
1572
+ let resultString = result.toUTF8String()!
1573
+ resolve(resultString)
1574
+ } catch let error {
1575
+ print("reboot", error.localizedDescription)
1576
+ reject("ERR_FULA", "reboot", error)
1577
+ }
1578
+ }
1579
+
1580
+ @objc(partition:withRejecter:)
1581
+ func partition(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1582
+ do {
1583
+ let result = try self.fula!.partition()
1584
+ let resultString = result.toUTF8String()!
1585
+ resolve(resultString)
1586
+ } catch let error {
1587
+ print("partition", error.localizedDescription)
1588
+ reject("ERR_FULA", "partition", error)
1589
+ }
1590
+ }
1591
+
1592
+ @objc(wifiRemoveall:withRejecter:)
1593
+ func wifiRemoveall(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1594
+ do {
1595
+ let result = try self.fula!.wifiRemoveall()
1596
+ let resultString = result.toUTF8String()!
1597
+ resolve(resultString)
1598
+ } catch let error {
1599
+ print("wifiRemoveall", error.localizedDescription)
1600
+ reject("ERR_FULA", "wifiRemoveall", error)
1601
+ }
1602
+ }
1603
+
1604
+ @objc(fetchContainerLogs:withTailCount:withResolver:withRejecter:)
1605
+ func fetchContainerLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1606
+ do {
1607
+ // Since fetchContainerLogs expects a String for tailCount, pass it directly
1608
+ let result = try self.fula!.fetchContainerLogs(containerName, tailCount: tailCount)
1609
+ guard let resultString = result.toUTF8String() else {
1610
+ // Handle the case where result.toUTF8String() returns nil
1611
+ let error = NSError(domain: "FULAErrorDomain",
1612
+ code: 1007, // Choose a suitable error code
1613
+ userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."])
1614
+ reject("ERR_FULA", "Log Conversion Error", error)
1615
+ return
1616
+ }
1617
+ resolve(resultString)
1618
+ } catch let error as NSError {
1619
+ print("fetchContainerLogs", error.localizedDescription)
1620
+ reject("ERR_FULA", "fetchContainerLogs failed", error)
1621
+ }
1622
+ }
1623
+ @objc(findBestAndTargetInLogs:withTailCount:withResolver:withRejecter:)
1624
+ func findBestAndTargetInLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1625
+ do {
1626
+ // Since fetchContainerLogs expects a String for tailCount, pass it directly
1627
+ let result = try self.fula!.findBestAndTarget(inLogs: containerName, tailCount: tailCount)
1628
+ guard let resultString = result.toUTF8String() else {
1629
+ // Handle the case where result.toUTF8String() returns nil
1630
+ let error = NSError(domain: "FULAErrorDomain",
1631
+ code: 1007, // Choose a suitable error code
1632
+ userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."])
1633
+ reject("ERR_FULA", "Log Conversion Error", error)
1634
+ return
1635
+ }
1636
+ resolve(resultString)
1637
+ } catch let error as NSError {
1638
+ print("fetchContainerLogs", error.localizedDescription)
1639
+ reject("ERR_FULA", "fetchContainerLogs failed", error)
1640
+ }
1641
+ }
1642
+
1643
+
1644
+
1645
+ @objc(getFolderSize:withResolver:withRejecter:)
1646
+ func getFolderSize(folderPath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
1647
+ // Validate the folder path
1648
+ guard !folderPath.isEmpty else {
1649
+ reject("ERR_INVALID_PATH", "The folder path is empty.", nil)
1650
+ return
1651
+ }
1652
+
1653
+ do {
1654
+ // Ensure fula is not nil before calling getFolderSize
1655
+ guard let fula = self.fula else {
1656
+ reject("ERR_FULA", "Fula instance is not initialized.", nil)
1657
+ return
1658
+ }
1659
+
1660
+ // Attempt to get the folder size
1661
+ let result = try fula.getFolderSize(folderPath)
1662
+
1663
+ // Convert the result to a UTF-8 string safely
1664
+ if let resultString = result.toUTF8String() {
1665
+ resolve(resultString)
1666
+ } else {
1667
+ reject("ERR_CONVERSION", "Failed to convert result to UTF-8 string.", nil)
1668
+ }
1669
+ } catch let error {
1670
+ // Log and reject with detailed error information
1671
+ print("getFolderSize error:", error.localizedDescription)
1672
+ reject("ERR_FULA", "Failed to get folder size: \(error.localizedDescription)", error)
1673
+ }
1674
+ }
1675
+
1676
+ @objc
1677
+ func getDatastoreSize(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
1678
+ DispatchQueue.global(qos: .background).async {
1679
+ // Safely unwrap `self.fula` using `guard let`
1680
+ guard let fulaClient = self.fula else {
1681
+ let error = NSError(domain: "FulaModuleError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Fula client is not initialized"])
1682
+ DispatchQueue.main.async {
1683
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula client is not initialized", error)
1684
+ }
1685
+ return
1686
+ }
1687
+
1688
+ do {
1689
+ let result = try fulaClient.getDatastoreSize()
1690
+ let resultString = String(decoding: result, as: UTF8.self)
1691
+ DispatchQueue.main.async {
1692
+ resolve(resultString)
1693
+ }
1694
+ } catch let error {
1695
+ DispatchQueue.main.async {
1696
+ reject("ERR_FULA", "Failed to get datastore size: \(error.localizedDescription)", error)
1697
+ }
1698
+ }
1699
+ }
1700
+ }
1701
+
1702
+ @objc(listPlugins:withRejecter:)
1703
+ func listPlugins(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1704
+ DispatchQueue.global(qos: .background).async {
1705
+ do {
1706
+ guard let fula = self.fula else {
1707
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1708
+ DispatchQueue.main.async {
1709
+ reject("ERR_FULA", "Fula not initialized", error)
1710
+ }
1711
+ return
1712
+ }
1713
+
1714
+ let result = try fula.listPlugins()
1715
+ guard let resultString = result.toUTF8String() else {
1716
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert plugin list to string"])
1717
+ DispatchQueue.main.async {
1718
+ reject("ERR_FULA", "Plugin List Conversion Error", error)
1719
+ }
1720
+ return
1721
+ }
1722
+
1723
+ DispatchQueue.main.async {
1724
+ resolve(resultString)
1725
+ }
1726
+ } catch let error {
1727
+ print("listPlugins", error.localizedDescription)
1728
+ DispatchQueue.main.async {
1729
+ reject("ERR_FULA", "listPlugins failed", error)
1730
+ }
1731
+ }
1732
+ }
1733
+ }
1734
+
1735
+ @objc(listActivePlugins:withRejecter:)
1736
+ func listActivePlugins(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1737
+ DispatchQueue.global(qos: .background).async {
1738
+ do {
1739
+ guard let fula = self.fula else {
1740
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1741
+ DispatchQueue.main.async {
1742
+ reject("ERR_FULA", "Fula not initialized", error)
1743
+ }
1744
+ return
1745
+ }
1746
+
1747
+ let result: Data
1748
+ do {
1749
+ result = try fula.listActivePlugins()
1750
+ } catch {
1751
+ print("Error listing active plugins: \(error)")
1752
+ DispatchQueue.main.async {
1753
+ reject("ERR_FULA", "Failed to list active plugins", error)
1754
+ }
1755
+ return
1756
+ }
1757
+
1758
+ guard !result.isEmpty else {
1759
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Active plugin list is empty"])
1760
+ DispatchQueue.main.async {
1761
+ reject("ERR_FULA", "Empty Active Plugin List", error)
1762
+ }
1763
+ return
1764
+ }
1765
+
1766
+ guard let resultString = String(data: result, encoding: .utf8) else {
1767
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert active plugin list to string"])
1768
+ DispatchQueue.main.async {
1769
+ reject("ERR_FULA", "Active Plugin List Conversion Error", error)
1770
+ }
1771
+ return
1772
+ }
1773
+
1774
+ DispatchQueue.main.async {
1775
+ resolve(resultString)
1776
+ }
1777
+ } catch let error {
1778
+ print("listActivePlugins unexpected error:", error.localizedDescription)
1779
+ DispatchQueue.main.async {
1780
+ reject("ERR_FULA", "listActivePlugins failed unexpectedly", error)
1781
+ }
1782
+ }
1783
+ }
1784
+ }
1785
+
1786
+ @objc(installPlugin:withParams:withResolver:withRejecter:)
1787
+ func installPlugin(pluginName: String, params: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1788
+ DispatchQueue.global(qos: .background).async {
1789
+ do {
1790
+ guard let fula = self.fula else {
1791
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1792
+ DispatchQueue.main.async {
1793
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1794
+ }
1795
+ return
1796
+ }
1797
+
1798
+ let result = try fula.installPlugin(pluginName, params: params)
1799
+ guard let resultString = result.toUTF8String() else {
1800
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install result to string"])
1801
+ DispatchQueue.main.async {
1802
+ reject("ERR_FULA_RESULT_CONVERSION", "Install Result Conversion Error", error)
1803
+ }
1804
+ return
1805
+ }
1806
+
1807
+ DispatchQueue.main.async {
1808
+ resolve(resultString)
1809
+ }
1810
+ } catch let error {
1811
+ print("installPlugin error:", error.localizedDescription)
1812
+ DispatchQueue.main.async {
1813
+ reject("ERR_FULA_INSTALL_PLUGIN", "Failed to install plugin: \(pluginName)", error)
1814
+ }
1815
+ }
1816
+ }
1817
+ }
1818
+
1819
+ @objc(uninstallPlugin:withResolver:withRejecter:)
1820
+ func uninstallPlugin(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1821
+ DispatchQueue.global(qos: .background).async {
1822
+ do {
1823
+ guard let fula = self.fula else {
1824
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1825
+ DispatchQueue.main.async {
1826
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1827
+ }
1828
+ return
1829
+ }
1830
+
1831
+ let result = try fula.uninstallPlugin(pluginName)
1832
+ guard let resultString = result.toUTF8String() else {
1833
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Failed to convert uninstall result to string"])
1834
+ DispatchQueue.main.async {
1835
+ reject("ERR_FULA_RESULT_CONVERSION", "Uninstall Result Conversion Error", error)
1836
+ }
1837
+ return
1838
+ }
1839
+
1840
+ DispatchQueue.main.async {
1841
+ resolve(resultString)
1842
+ }
1843
+ } catch let error {
1844
+ print("uninstallPlugin error:", error.localizedDescription)
1845
+ DispatchQueue.main.async {
1846
+ reject("ERR_FULA_UNINSTALL_PLUGIN", "Failed to uninstall plugin: \(pluginName)", error)
1847
+ }
1848
+ }
1849
+ }
1850
+ }
1851
+
1852
+ @objc(showPluginStatus:withLines:withResolver:withRejecter:)
1853
+ func showPluginStatus(pluginName: String, lines: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1854
+ DispatchQueue.global(qos: .background).async {
1855
+ do {
1856
+ guard let fula = self.fula else {
1857
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1858
+ DispatchQueue.main.async {
1859
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1860
+ }
1861
+ return
1862
+ }
1863
+
1864
+ let result: Data
1865
+ do {
1866
+ result = try fula.showPluginStatus(pluginName, lines: lines)
1867
+ } catch {
1868
+ print("Error showing plugin status: \(error)")
1869
+ DispatchQueue.main.async {
1870
+ reject("ERR_FULA_SHOW_PLUGIN_STATUS", "Failed to show plugin status", error)
1871
+ }
1872
+ return
1873
+ }
1874
+
1875
+ guard !result.isEmpty else {
1876
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Plugin status result is empty"])
1877
+ DispatchQueue.main.async {
1878
+ reject("ERR_FULA_EMPTY_RESULT", "Empty Plugin Status Result", error)
1879
+ }
1880
+ return
1881
+ }
1882
+
1883
+ guard let resultString = String(data: result, encoding: .utf8) else {
1884
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert plugin status to string"])
1885
+ DispatchQueue.main.async {
1886
+ reject("ERR_FULA_RESULT_CONVERSION", "Plugin Status Conversion Error", error)
1887
+ }
1888
+ return
1889
+ }
1890
+
1891
+ DispatchQueue.main.async {
1892
+ resolve(resultString)
1893
+ }
1894
+ } catch {
1895
+ print("showPluginStatus unexpected error:", error.localizedDescription)
1896
+ DispatchQueue.main.async {
1897
+ reject("ERR_FULA_SHOW_PLUGIN_STATUS", "showPluginStatus failed unexpectedly", error)
1898
+ }
1899
+ }
1900
+ }
1901
+ }
1902
+
1903
+ @objc(getInstallOutput:withParams:withResolver:withRejecter:)
1904
+ func getInstallOutput(pluginName: String, params: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1905
+ DispatchQueue.global(qos: .background).async {
1906
+ do {
1907
+ guard let fula = self.fula else {
1908
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1909
+ DispatchQueue.main.async {
1910
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1911
+ }
1912
+ return
1913
+ }
1914
+
1915
+ let result: Data
1916
+ do {
1917
+ result = try fula.getInstallOutput(pluginName, params: params)
1918
+ } catch {
1919
+ print("Error getting install output: \(error)")
1920
+ DispatchQueue.main.async {
1921
+ reject("ERR_FULA_GET_INSTALL_OUTPUT", "Failed to get install output", error)
1922
+ }
1923
+ return
1924
+ }
1925
+
1926
+ guard !result.isEmpty else {
1927
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Install output is empty"])
1928
+ DispatchQueue.main.async {
1929
+ reject("ERR_FULA_EMPTY_RESULT", "Empty Install Output", error)
1930
+ }
1931
+ return
1932
+ }
1933
+
1934
+ guard let resultString = String(data: result, encoding: .utf8) else {
1935
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install output to string"])
1936
+ DispatchQueue.main.async {
1937
+ reject("ERR_FULA_RESULT_CONVERSION", "Install Output Conversion Error", error)
1938
+ }
1939
+ return
1940
+ }
1941
+
1942
+ DispatchQueue.main.async {
1943
+ resolve(resultString)
1944
+ }
1945
+ } catch {
1946
+ print("getInstallOutput unexpected error:", error.localizedDescription)
1947
+ DispatchQueue.main.async {
1948
+ reject("ERR_FULA_GET_INSTALL_OUTPUT", "getInstallOutput failed unexpectedly", error)
1949
+ }
1950
+ }
1951
+ }
1952
+ }
1953
+
1954
+ @objc(getInstallStatus:withResolver:withRejecter:)
1955
+ func getInstallStatus(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1956
+ DispatchQueue.global(qos: .background).async {
1957
+ do {
1958
+ guard let fula = self.fula else {
1959
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
1960
+ DispatchQueue.main.async {
1961
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
1962
+ }
1963
+ return
1964
+ }
1965
+
1966
+ let result: Data
1967
+ do {
1968
+ result = try fula.getInstallStatus(pluginName)
1969
+ } catch {
1970
+ print("Error getting install status: \(error)")
1971
+ DispatchQueue.main.async {
1972
+ reject("ERR_FULA_GET_INSTALL_STATUS", "Failed to get install status", error)
1973
+ }
1974
+ return
1975
+ }
1976
+
1977
+ guard !result.isEmpty else {
1978
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Install status result is empty"])
1979
+ DispatchQueue.main.async {
1980
+ reject("ERR_FULA_EMPTY_RESULT", "Empty Install Status Result", error)
1981
+ }
1982
+ return
1983
+ }
1984
+
1985
+ guard let resultString = String(data: result, encoding: .utf8) else {
1986
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert install status to string"])
1987
+ DispatchQueue.main.async {
1988
+ reject("ERR_FULA_RESULT_CONVERSION", "Install Status Conversion Error", error)
1989
+ }
1990
+ return
1991
+ }
1992
+
1993
+ DispatchQueue.main.async {
1994
+ resolve(resultString)
1995
+ }
1996
+ } catch {
1997
+ print("getInstallStatus unexpected error:", error.localizedDescription)
1998
+ DispatchQueue.main.async {
1999
+ reject("ERR_FULA_GET_INSTALL_STATUS", "getInstallStatus failed unexpectedly", error)
2000
+ }
2001
+ }
2002
+ }
2003
+ }
2004
+
2005
+ @objc(updatePlugin:withResolver:withRejecter:)
2006
+ func updatePlugin(pluginName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
2007
+ DispatchQueue.global(qos: .background).async {
2008
+ do {
2009
+ guard let fula = self.fula else {
2010
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
2011
+ DispatchQueue.main.async {
2012
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
2013
+ }
2014
+ return
2015
+ }
2016
+
2017
+ let result: Data
2018
+ do {
2019
+ result = try fula.updatePlugin(pluginName)
2020
+ } catch {
2021
+ print("Error updating plugin: \(error)")
2022
+ DispatchQueue.main.async {
2023
+ reject("ERR_FULA_UPDATE_PLUGIN", "Failed to update plugin: \(pluginName)", error)
2024
+ }
2025
+ return
2026
+ }
2027
+
2028
+ guard !result.isEmpty else {
2029
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Update plugin result is empty"])
2030
+ DispatchQueue.main.async {
2031
+ reject("ERR_FULA_EMPTY_RESULT", "Empty Update Plugin Result", error)
2032
+ }
2033
+ return
2034
+ }
2035
+
2036
+ guard let resultString = String(data: result, encoding: .utf8) else {
2037
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "Failed to convert update plugin result to string"])
2038
+ DispatchQueue.main.async {
2039
+ reject("ERR_FULA_RESULT_CONVERSION", "Update Plugin Result Conversion Error", error)
2040
+ }
2041
+ return
2042
+ }
2043
+
2044
+ DispatchQueue.main.async {
2045
+ resolve(resultString)
2046
+ }
2047
+ } catch {
2048
+ print("updatePlugin unexpected error:", error.localizedDescription)
2049
+ DispatchQueue.main.async {
2050
+ reject("ERR_FULA_UPDATE_PLUGIN", "updatePlugin failed unexpectedly", error)
2051
+ }
2052
+ }
2053
+ }
2054
+ }
2055
+
2056
+ @objc(replicateInPool:withAccount:withPoolID:withResolver:withRejecter:)
2057
+ func replicateInPool(cidArray: [String], account: String, poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
2058
+ DispatchQueue.global(qos: .background).async {
2059
+ do {
2060
+ guard let fula = self.fula else {
2061
+ let error = NSError(domain: "FULAErrorDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula instance is not initialized"])
2062
+ DispatchQueue.main.async {
2063
+ reject("ERR_FULA_NOT_INITIALIZED", "Fula not initialized", error)
2064
+ }
2065
+ return
2066
+ }
2067
+
2068
+ guard let poolIDLong = Int64(poolID) else {
2069
+ let error = NSError(domain: "FULAErrorDomain", code: 1002, userInfo: [NSLocalizedDescriptionKey: "Invalid poolID"])
2070
+ DispatchQueue.main.async {
2071
+ reject("ERR_FULA_INVALID_POOL_ID", "Invalid poolID", error)
2072
+ }
2073
+ return
2074
+ }
2075
+
2076
+ guard let poolIDInt = Int(exactly: poolIDLong) else {
2077
+ let error = NSError(domain: "FULAErrorDomain", code: 1003, userInfo: [NSLocalizedDescriptionKey: "PoolID is too large for Int"])
2078
+ DispatchQueue.main.async {
2079
+ reject("ERR_FULA_POOL_ID_OVERFLOW", "PoolID is too large", error)
2080
+ }
2081
+ return
2082
+ }
2083
+
2084
+ let cidString = cidArray.joined(separator: "|")
2085
+ guard let cidsBytes = cidString.data(using: .utf8) else {
2086
+ let error = NSError(domain: "FULAErrorDomain", code: 1004, userInfo: [NSLocalizedDescriptionKey: "Failed to encode CIDs as data"])
2087
+ DispatchQueue.main.async {
2088
+ reject("ERR_FULA_CID_ENCODING", "Failed to encode CIDs", error)
2089
+ }
2090
+ return
2091
+ }
2092
+
2093
+ let result: Data
2094
+ do {
2095
+ if let replicationResult = fula.replicate(inPool: cidsBytes, account: account, poolID: poolIDInt) {
2096
+ result = replicationResult
2097
+ } else {
2098
+ throw NSError(domain: "FULAErrorDomain", code: 1007, userInfo: [NSLocalizedDescriptionKey: "Replication result is nil"])
2099
+ }
2100
+ } catch {
2101
+ print("Error replicating in pool: \(error)")
2102
+ DispatchQueue.main.async {
2103
+ reject("ERR_FULA_REPLICATION", "Failed to replicate in pool", error)
2104
+ }
2105
+ return
2106
+ }
2107
+
2108
+ guard !result.isEmpty else {
2109
+ let error = NSError(domain: "FULAErrorDomain", code: 1005, userInfo: [NSLocalizedDescriptionKey: "Replication result is empty"])
2110
+ DispatchQueue.main.async {
2111
+ reject("ERR_FULA_EMPTY_RESULT", "Empty replication result", error)
2112
+ }
2113
+ return
2114
+ }
2115
+
2116
+ guard let resultString = String(data: result, encoding: .utf8) else {
2117
+ let error = NSError(domain: "FULAErrorDomain", code: 1006, userInfo: [NSLocalizedDescriptionKey: "Failed to decode result data to string"])
2118
+ DispatchQueue.main.async {
2119
+ reject("ERR_FULA_RESULT_DECODING", "Failed to decode result", error)
2120
+ }
2121
+ return
2122
+ }
2123
+
2124
+ DispatchQueue.main.async {
2125
+ resolve(resultString)
2126
+ }
2127
+ } catch {
2128
+ print("replicateInPool unexpected error:", error.localizedDescription)
2129
+ DispatchQueue.main.async {
2130
+ reject("ERR_FULA_REPLICATE_IN_POOL", "replicateInPool failed unexpectedly", error)
2131
+ }
2132
+ }
2133
+ }
2134
+ }
2135
+
2136
+ // AI Chat methods
2137
+
2138
+ @objc(chatWithAI:withUserMessage:withResolver:withRejecter:)
2139
+ func chatWithAI(aiModel: String, userMessage: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2140
+ NSLog("ReactNative chatWithAI: aiModel = \(aiModel), userMessage = \(userMessage)")
2141
+ DispatchQueue.global(qos: .default).async {
2142
+ do {
2143
+ guard let fula = self.fula else {
2144
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
2145
+ }
2146
+
2147
+ // Call the Go Mobile method, which returns Data
2148
+ let streamIDData = try fula.chatWithAI(aiModel, userMessage: userMessage)
2149
+
2150
+ // Convert Data to String (assuming UTF-8 encoding)
2151
+ guard let streamID = String(data: streamIDData, encoding: .utf8) else {
2152
+ throw MyError.runtimeError("Failed to convert stream ID to string")
2153
+ }
2154
+
2155
+ // Resolve the promise with the stream ID
2156
+ DispatchQueue.main.async {
2157
+ resolve(streamID)
2158
+ }
2159
+ } catch let error {
2160
+ NSLog("ReactNative ERROR in chatWithAI: \(error.localizedDescription)")
2161
+ DispatchQueue.main.async {
2162
+ reject("ERR_CHAT_WITH_AI", "Chat with AI failed", error)
2163
+ }
2164
+ }
2165
+ }
2166
+ }
2167
+
2168
+ @objc(getChatChunk:withResolver:withRejecter:)
2169
+ func getChatChunk(streamID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2170
+ NSLog("ReactNative getChatChunk: streamID = \(streamID)")
2171
+ DispatchQueue.global(qos: .default).async {
2172
+ do {
2173
+ guard let fula = self.fula else {
2174
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
2175
+ }
2176
+
2177
+ // Call the Go Mobile method, which returns a String
2178
+ let chunk = fula.getChatChunk(streamID)
2179
+
2180
+ // Handle null or empty response
2181
+ if chunk.isEmpty {
2182
+ NSLog("ReactNative getChatChunk: No data received for streamID = \(streamID)")
2183
+ DispatchQueue.main.async {
2184
+ resolve("") // Resolve with an empty string
2185
+ }
2186
+ return
2187
+ }
2188
+
2189
+ // Resolve the promise with the chunk of data
2190
+ NSLog("ReactNative getChatChunk: Successfully received chunk for streamID = \(streamID)")
2191
+ DispatchQueue.main.async {
2192
+ resolve(chunk)
2193
+ }
2194
+ } catch let error {
2195
+ // Log and reject the promise with the error
2196
+ NSLog("ReactNative ERROR in getChatChunk: \(error.localizedDescription)")
2197
+ DispatchQueue.main.async {
2198
+ reject("ERR_GET_CHAT_CHUNK", "Get chat chunk failed", error)
2199
+ }
2200
+ }
2201
+ }
2202
+ }
2203
+
2204
+ @objc(streamChunks:withResolver:withRejecter:)
2205
+ func streamChunks(streamID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
2206
+ if streamID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
2207
+ reject("INVALID_ARGUMENT", "streamID cannot be null or empty", nil)
2208
+ return
2209
+ }
2210
+
2211
+ DispatchQueue.global(qos: .default).async {
2212
+ do {
2213
+ guard let fula = self.fula else {
2214
+ throw MyError.runtimeError("ReactNative Fula client is not initialized")
2215
+ }
2216
+
2217
+ let iterator = fula.getStreamIterator(streamID)
2218
+ guard iterator != nil else {
2219
+ throw MyError.runtimeError("Failed to create StreamIterator")
2220
+ }
2221
+
2222
+ // Start listening for chunks on the main thread
2223
+ DispatchQueue.main.async {
2224
+ self.pollIterator(iterator: iterator, resolve: resolve, reject: reject)
2225
+ }
2226
+ } catch let error {
2227
+ NSLog("ReactNative ERROR in streamChunks: \(error.localizedDescription)")
2228
+ DispatchQueue.main.async {
2229
+ reject("STREAM_ERROR", "Stream error", error)
2230
+ }
2231
+ }
2232
+ }
2233
+ }
2234
+
2235
+ private func pollIterator(iterator: FulamobileStreamIterator, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
2236
+ do {
2237
+ let chunk = iterator.next()
2238
+ if !chunk.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
2239
+ self.emitEvent(eventName: "onChunkReceived", data: chunk)
2240
+ }
2241
+
2242
+ if iterator.isComplete() {
2243
+ self.emitEvent(eventName: "onStreamingCompleted", data: nil)
2244
+ resolve(nil)
2245
+ } else {
2246
+ // Schedule another poll with a short delay
2247
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { // 50ms delay for better responsiveness
2248
+ self.pollIterator(iterator: iterator, resolve: resolve, reject: reject)
2249
+ }
2250
+ }
2251
+ } catch let error {
2252
+ let errorMessage = error.localizedDescription
2253
+ if errorMessage.contains("EOF") {
2254
+ self.emitEvent(eventName: "onStreamingCompleted", data: nil)
2255
+ resolve(nil)
2256
+ } else if errorMessage.contains("timeout") {
2257
+ // Retry on timeout
2258
+ DispatchQueue.main.async {
2259
+ self.pollIterator(iterator: iterator!, resolve: resolve, reject: reject)
2260
+ }
2261
+ } else {
2262
+ self.emitEvent(eventName: "onStreamError", data: errorMessage)
2263
+ reject("STREAM_ERROR", errorMessage, error)
2264
+ }
2265
+ }
2266
+ }
2267
+
2268
+ private func emitEvent(eventName: String, data: String?) {
2269
+ self.sendEvent(withName: eventName, body: data)
2270
+ }
2271
+
2272
+ // Required for RCTEventEmitter
2273
+ override func supportedEvents() -> [String]! {
2274
+ return ["onChunkReceived", "onStreamingCompleted", "onStreamError"]
2275
+ }
2276
+
2277
+ // Required for RCTEventEmitter to prevent warnings
2278
+ override static func requiresMainQueueSetup() -> Bool {
2279
+ return false
2280
+ }
2281
+
2282
+ }
2283
+
2284
+