@functionland/react-native-fula 1.14.2 → 1.14.4

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 (135) hide show
  1. package/LICENSE +20 -21
  2. package/android/build.gradle +90 -50
  3. package/android/gradle.properties +5 -1
  4. package/android/src/main/AndroidManifest.xml +3 -4
  5. package/android/src/main/AndroidManifestNew.xml +2 -0
  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 +1466 -1466
  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 +36 -0
  14. package/ios/Fula-Bridging-Header.h +3 -0
  15. package/ios/Fula.mm +199 -0
  16. package/ios/Fula.swift +1152 -0
  17. package/ios/{FulaModule.xcodeproj → Fula.xcodeproj}/project.pbxproj +38 -16
  18. package/ios/Fula.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  19. package/ios/Fula.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  20. package/ios/Fula.xcodeproj/project.xcworkspace/xcuserdata/user246549.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  21. package/ios/Fula.xcodeproj/xcuserdata/user246549.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  22. package/ios/UserDataHelper.swift +143 -0
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/interfaces/api-lookup.js +266 -266
  25. package/lib/commonjs/interfaces/api-lookup.js.map +1 -1
  26. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  27. package/lib/commonjs/protocols/blockchain.js +64 -64
  28. package/lib/commonjs/protocols/blockchain.js.map +1 -1
  29. package/lib/commonjs/protocols/chain-api.js +10 -0
  30. package/lib/commonjs/protocols/chain-api.js.map +1 -1
  31. package/lib/commonjs/protocols/fula.js +99 -99
  32. package/lib/commonjs/protocols/fula.js.map +1 -1
  33. package/lib/commonjs/protocols/fxblox.js +3 -3
  34. package/lib/commonjs/protocols/fxblox.js.map +1 -1
  35. package/lib/commonjs/types/blockchain.js.map +1 -1
  36. package/lib/commonjs/types/fxblox.js.map +1 -1
  37. package/lib/module/index.js +4 -8
  38. package/lib/module/index.js.map +1 -1
  39. package/lib/module/interfaces/api-lookup.js +266 -266
  40. package/lib/module/interfaces/api-lookup.js.map +1 -1
  41. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  42. package/lib/module/protocols/blockchain.js +64 -64
  43. package/lib/module/protocols/blockchain.js.map +1 -1
  44. package/lib/module/protocols/chain-api.js +8 -0
  45. package/lib/module/protocols/chain-api.js.map +1 -1
  46. package/lib/module/protocols/fula.js +99 -99
  47. package/lib/module/protocols/fula.js.map +1 -1
  48. package/lib/module/protocols/fxblox.js +3 -3
  49. package/lib/module/protocols/fxblox.js.map +1 -1
  50. package/lib/module/types/blockchain.js.map +1 -1
  51. package/lib/module/types/fxblox.js.map +1 -1
  52. package/lib/typescript/index.d.ts +5 -4
  53. package/lib/typescript/index.d.ts.map +1 -0
  54. package/lib/typescript/interfaces/api-lookup.d.ts +1644 -1643
  55. package/lib/typescript/interfaces/api-lookup.d.ts.map +1 -0
  56. package/lib/typescript/interfaces/fulaNativeModule.d.ts +62 -61
  57. package/lib/typescript/interfaces/fulaNativeModule.d.ts.map +1 -0
  58. package/lib/typescript/protocols/blockchain.d.ts +18 -17
  59. package/lib/typescript/protocols/blockchain.d.ts.map +1 -0
  60. package/lib/typescript/protocols/chain-api.d.ts +7 -6
  61. package/lib/typescript/protocols/chain-api.d.ts.map +1 -0
  62. package/lib/typescript/protocols/fula.d.ts +132 -131
  63. package/lib/typescript/protocols/fula.d.ts.map +1 -0
  64. package/lib/typescript/protocols/fxblox.d.ts +8 -7
  65. package/lib/typescript/protocols/fxblox.d.ts.map +1 -0
  66. package/lib/typescript/types/blockchain.d.ts +69 -68
  67. package/lib/typescript/types/blockchain.d.ts.map +1 -0
  68. package/lib/typescript/types/fxblox.d.ts +9 -8
  69. package/lib/typescript/types/fxblox.d.ts.map +1 -0
  70. package/package.json +43 -28
  71. package/react-native-fula.podspec +47 -19
  72. package/src/index.tsx +4 -4
  73. package/src/interfaces/api-lookup.ts +1647 -1647
  74. package/src/interfaces/fulaNativeModule.ts +122 -122
  75. package/src/protocols/blockchain.ts +504 -504
  76. package/src/protocols/chain-api.ts +8 -0
  77. package/src/protocols/fula.ts +314 -314
  78. package/src/protocols/fxblox.ts +49 -49
  79. package/src/types/blockchain.ts +80 -80
  80. package/src/types/fxblox.ts +8 -8
  81. package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
  82. package/android/.gradle/7.5.1/checksums/md5-checksums.bin +0 -0
  83. package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
  84. package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  85. package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
  86. package/android/.gradle/7.5.1/executionHistory/executionHistory.bin +0 -0
  87. package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +0 -0
  88. package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
  89. package/android/.gradle/7.5.1/fileHashes/fileHashes.bin +0 -0
  90. package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
  91. package/android/.gradle/7.5.1/fileHashes/resourceHashesCache.bin +0 -0
  92. package/android/.gradle/7.5.1/gc.properties +0 -0
  93. package/android/.gradle/7.6/checksums/checksums.lock +0 -0
  94. package/android/.gradle/7.6/checksums/md5-checksums.bin +0 -0
  95. package/android/.gradle/7.6/checksums/sha1-checksums.bin +0 -0
  96. package/android/.gradle/7.6/dependencies-accessors/dependencies-accessors.lock +0 -0
  97. package/android/.gradle/7.6/dependencies-accessors/gc.properties +0 -0
  98. package/android/.gradle/7.6/executionHistory/executionHistory.bin +0 -0
  99. package/android/.gradle/7.6/executionHistory/executionHistory.lock +0 -0
  100. package/android/.gradle/7.6/fileChanges/last-build.bin +0 -0
  101. package/android/.gradle/7.6/fileHashes/fileHashes.bin +0 -0
  102. package/android/.gradle/7.6/fileHashes/fileHashes.lock +0 -0
  103. package/android/.gradle/7.6/fileHashes/resourceHashesCache.bin +0 -0
  104. package/android/.gradle/7.6/gc.properties +0 -0
  105. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  106. package/android/.gradle/buildOutputCleanup/cache.properties +0 -2
  107. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  108. package/android/.gradle/file-system.probe +0 -0
  109. package/android/.gradle/vcs-1/gc.properties +0 -0
  110. package/android/.idea/compiler.xml +0 -6
  111. package/android/.idea/gradle.xml +0 -20
  112. package/android/.idea/jarRepositories.xml +0 -50
  113. package/android/.idea/misc.xml +0 -10
  114. package/android/.idea/sonarlint/issuestore/2/a/2afbb999f001938c88fa43fc2ef52abf0f8213e4 +0 -0
  115. package/android/.idea/sonarlint/issuestore/2/e/2e09eb0f0df666781fdff057a0af6101364ebe2f +0 -0
  116. package/android/.idea/sonarlint/issuestore/6/2/62149c3f7c3a14d7a77778e558d274e8c00a5585 +0 -0
  117. package/android/.idea/sonarlint/issuestore/8/0/80a78d662b0659428eb880336106c3c5fc3f0714 +0 -0
  118. package/android/.idea/sonarlint/issuestore/d/2/d2865ef8fc01c40e9fd82977fde2c8378fad0b12 +0 -0
  119. package/android/.idea/sonarlint/issuestore/f/b/fbe448ebfc3eb2d4e308f6b8b043666f5b57235e +0 -0
  120. package/android/.idea/sonarlint/issuestore/index.pb +0 -13
  121. package/android/.idea/sonarlint/securityhotspotstore/2/a/2afbb999f001938c88fa43fc2ef52abf0f8213e4 +0 -0
  122. package/android/.idea/sonarlint/securityhotspotstore/2/e/2e09eb0f0df666781fdff057a0af6101364ebe2f +0 -0
  123. package/android/.idea/sonarlint/securityhotspotstore/6/2/62149c3f7c3a14d7a77778e558d274e8c00a5585 +0 -0
  124. package/android/.idea/sonarlint/securityhotspotstore/8/0/80a78d662b0659428eb880336106c3c5fc3f0714 +0 -0
  125. package/android/.idea/sonarlint/securityhotspotstore/d/2/d2865ef8fc01c40e9fd82977fde2c8378fad0b12 +0 -0
  126. package/android/.idea/sonarlint/securityhotspotstore/f/b/fbe448ebfc3eb2d4e308f6b8b043666f5b57235e +0 -0
  127. package/android/.idea/sonarlint/securityhotspotstore/index.pb +0 -13
  128. package/android/.idea/vcs.xml +0 -6
  129. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  130. package/android/gradle/wrapper/gradle-wrapper.properties +0 -7
  131. package/android/gradlew +0 -291
  132. package/android/gradlew.bat +0 -91
  133. package/android/local.properties +0 -8
  134. package/ios/FulaModule.h +0 -10
  135. package/ios/FulaModule.m +0 -149
package/ios/Fula.swift ADDED
@@ -0,0 +1,1152 @@
1
+ import Foundation
2
+ import Foundation.NSDate // for TimeInterval
3
+ import CommonCrypto
4
+ import Wnfs
5
+ import Fula
6
+
7
+ @objc(FulaModule)
8
+ class FulaModule: NSObject {
9
+ public let NAME: String = "FulaModule"
10
+ var fula: FulamobileClient?
11
+
12
+ var client: Client?
13
+ var wnfs: Wnfs?
14
+ var fulaConfig: FulamobileConfig?
15
+ var appDir: URL
16
+ var fulaStorePath: String
17
+ // Related to the Wnfs library
18
+ var rootCid: Cid?
19
+ var wnfsKey: Data?
20
+ let userDataHelper = UserDataHelper()
21
+ var secretKeyGlobal: Array<UInt8>?
22
+ var identityEncryptedGlobal: String?
23
+ static let PRIVATE_KEY_STORE_ID = "PRIVATE_KEY"
24
+
25
+ // FIXME: Hardcoded ,fula should remove all the codec arguments as the rs-wnfs library removed its usage now.
26
+ static let CODEC_DAG_CBOR = Int(113)
27
+
28
+ enum MyError: Error {
29
+ case runtimeError(String)
30
+ }
31
+
32
+ @objc(Client)
33
+ public class Client: NSObject {
34
+ let internalClient: FulamobileClient
35
+
36
+ init(clientInput: FulamobileClient) {
37
+ internalClient = clientInput
38
+ }
39
+
40
+ func get(_ cid: Data) throws -> Data {
41
+ do {
42
+ print(String(format: "ReactNative get cid: %s", cid.toHex()))
43
+ return try internalClient.get(cid)
44
+ } catch let error {
45
+ throw error
46
+ }
47
+ }
48
+
49
+ func put(_ cid: Data, _ data: Data) throws -> Void {
50
+ do {
51
+ print(String(format: "ReactNative put(%s) data: %s", cid.toHex(), data.toHex()))
52
+ try internalClient.put(data, codec: 0xFF & Int64(cid[1]))
53
+ } catch let error {
54
+ print("ReactNative put: ", error.localizedDescription)
55
+ throw error
56
+ }
57
+ }
58
+ }
59
+ override init() {
60
+
61
+ self.appDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
62
+ let fulaStoreURL = appDir.appendingPathComponent("/fula")
63
+
64
+ self.fulaStorePath = fulaStoreURL.path
65
+ let fileManager = FileManager.default
66
+ var success = true
67
+ if !fileManager.fileExists(atPath: fulaStorePath) {
68
+ do{
69
+ try fileManager.createDirectory(atPath: fulaStorePath, withIntermediateDirectories: true)
70
+ }catch let error{
71
+ print(error.localizedDescription)
72
+ success = false
73
+ }
74
+
75
+ }
76
+ if success {
77
+ print("Fula store folder created")
78
+ } else {
79
+ print("Unable to create fula store folder!")
80
+
81
+ }
82
+ super.init()
83
+
84
+ }
85
+ func convertConfigToJson(config: Cid) -> String {
86
+ return String(format: "{\"cid\": \"%@\"}", config)
87
+ }
88
+
89
+ func getName() -> String {
90
+ return NAME
91
+ }
92
+
93
+ func toByte(_ input: String) -> Data {
94
+ return input.data(using: .utf8)!
95
+ }
96
+
97
+
98
+ func stringArrToIntArr(_ s: Array<String>) -> Array<Int> {
99
+ return s.map { Int($0)!}
100
+ }
101
+
102
+ func convertIntToByte(_ s: Array<Int>) -> Array<UInt8> {
103
+ return s.map { UInt8(exactly: $0)!}
104
+ }
105
+
106
+ func convertStringToByte(_ input: String) -> Array<UInt8> {
107
+ let splitted = input.split(separator: ",").map { String($0) }
108
+ let keyInt = stringArrToIntArr(splitted)
109
+ return convertIntToByte(keyInt)
110
+ }
111
+
112
+ @objc(checkConnection:withResolver:withRejecter:)
113
+ func checkConnection(timeout: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
114
+ print("ReactNative", "checkConnection started")
115
+ if (fula != nil) {
116
+ do {
117
+ // FIXME: run with timeout
118
+ // Task {
119
+ // try await withTimeout(seconds: TimeInterval(timeout)) { [weak self] in
120
+ // guard let weakSelf = self else { return false }
121
+ // try weakSelf.checkConnectionInternal()
122
+ // callback(BOOL(true))
123
+ // }
124
+ // }
125
+ resolve(try self.checkConnectionInternal())
126
+ }
127
+ catch let error {
128
+ print("ReactNative", "checkConnection failed with Error: ", error.localizedDescription)
129
+ // callback(BOOL(false))
130
+ resolve(false)
131
+ }
132
+ }
133
+ }
134
+
135
+ @objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:)
136
+ func newClient(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
137
+ print("ReactNative", "newClient storePath= " , storePath , " bloxAddr= " , bloxAddr , " exchange= " , exchange , " autoFlush= " , autoFlush , " useRelay= " , useRelay , " refresh= " , refresh);
138
+ do {
139
+ print("ReactNative", "newClient storePath= ", storePath)
140
+ let identity = toByte(identityString)
141
+ print("ReactNative", "newClient identity= ", identityString)
142
+ try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
143
+ let peerId = fula?.id_()
144
+ resolve(peerId)
145
+ } catch let error {
146
+ print("ReactNative", "newClient failed with Error: ", error.localizedDescription)
147
+ reject("ERR_FULA", "Can't create client", error)
148
+ }
149
+
150
+ }
151
+
152
+ @objc(isReady:withResolver:withRejecter:)
153
+ func isReady(filesystemCheck: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
154
+ print("ReactNative", "isReady started")
155
+ var initialized = false
156
+ do {
157
+ if (fula != nil && !fula!.id_().isEmpty) {
158
+ if (filesystemCheck) {
159
+ if (client != nil && rootCid != nil && !rootCid!.isEmpty) {
160
+ initialized = true
161
+ }
162
+ } else {
163
+ initialized = true
164
+ }
165
+ }
166
+ resolve(initialized)
167
+ } catch let error {
168
+ print("ReactNative", "isReady failed with Error: " + error.localizedDescription)
169
+ reject("ERR_FULA", "Check if fula is ready", error)
170
+ }
171
+ }
172
+
173
+ //TODO: we should consider refactor the name of this \
174
+ // function to be compatible with the android version.
175
+ @objc(initFula:withStorePath:withBloxAddr:withExchange:withAutoFlush:withRootConfig:withUseRelay:withRefresh:withResolver:withRejecter:)
176
+ func initFula(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, rootConfig: String, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
177
+ print("ReactNative", "init started")
178
+ do {
179
+
180
+ var resultData = Dictionary<String, String>()
181
+ print("ReactNative", "init storePath= ", storePath)
182
+ let identity = self.toByte(identityString)
183
+ print("ReactNative", "init identity= ", identityString)
184
+ let obj = try initInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, _rootCid: rootConfig, useRelay: useRelay, refresh: refresh)
185
+ print("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]")
186
+ resultData["peerId"] = obj[0]
187
+ resultData["rootCid"] = obj[1]
188
+ resultData["wnfs_key"] = obj[2]
189
+ resolve(resultData as NSDictionary)
190
+
191
+ } catch let error {
192
+ print("ReactNative", "init failed with Error: ", error.localizedDescription)
193
+ reject("ERR_FULA", "init failed", error)
194
+ }
195
+
196
+ }
197
+
198
+ @objc(logout:withStorePath:withResolver:withRejecter:)
199
+ func logout(identityString: String, storePath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
200
+
201
+ print("ReactNative", "logout started")
202
+ do {
203
+ let identity = toByte(identityString)
204
+ try logoutInternal(identity: identity, _storePath: storePath)
205
+ print("ReactNative", "logout completed")
206
+ resolve("")
207
+
208
+ } catch let error {
209
+ print("ReactNative", "logout failed with Error: ", error.localizedDescription)
210
+ reject("ERR_FULA", "logout failed", error)
211
+ }
212
+
213
+ }
214
+
215
+ func checkConnectionInternal() throws {
216
+ print("ReactNative", "checkConnectionInternal started")
217
+ if (fula != nil) {
218
+ do {
219
+ print("ReactNative", "connectToBlox started")
220
+ try fula?.connectToBlox()
221
+ } catch let error {
222
+ print("ReactNative", "checkConnectionInternal failed with Error: ", error.localizedDescription)
223
+ throw error
224
+ }
225
+ } else {
226
+ print("ReactNative", "checkConnectionInternal failed because fula is not initialized ")
227
+ }
228
+ }
229
+
230
+ @objc(checkFailedActions:withResolver:withRejecter:)
231
+ func checkFailedActions(retry: Bool, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void{
232
+ do {
233
+ if (fula != nil) {
234
+ if (!retry) {
235
+ print("ReactNative", "checkFailedActions without retry")
236
+ let failedLinks = try fula!.listFailedPushes()
237
+ if (failedLinks.hasNext()) {
238
+ print("ReactNative", "checkFailedActions found: ", try failedLinks.next().toHex())
239
+ resolve(true)
240
+ } else {
241
+ resolve(false)
242
+ }
243
+ } else {
244
+ print("ReactNative", "checkFailedActions with retry")
245
+
246
+ let retryResults = try retryFailedActionsInternal()
247
+ resolve(!retryResults)
248
+
249
+ }
250
+ } else {
251
+ reject("ERR_FULA", "Fula is not initialized", nil)
252
+ }
253
+ } catch let error {
254
+ print("ReactNative", "checkFailedActions failed with Error: ", error.localizedDescription)
255
+ reject("ERR_FULA", "CheckFailedActions failed", error)
256
+ }
257
+ }
258
+
259
+ func retryFailedActionsInternal() throws -> Bool {
260
+ print("ReactNative", "retryFailedActionsInternal started")
261
+ if (fula != nil) {
262
+ //Fula is initialized
263
+ do {
264
+ try checkConnectionInternal()
265
+
266
+ do {
267
+ print("ReactNative", "retryFailedPushes started")
268
+ try fula?.retryFailedPushes()
269
+ print("ReactNative", "flush started")
270
+ try fula?.flush()
271
+ return true
272
+ }
273
+ catch let error {
274
+ try fula?.flush()
275
+ print("ReactNative", "retryFailedActionsInternal failed with Error: ", error.localizedDescription)
276
+ return false
277
+ }
278
+
279
+
280
+ }
281
+ catch let error {
282
+ print("ReactNative", "retryFailedActions failed with Error: ", error.localizedDescription)
283
+ return false
284
+ }
285
+ } else {
286
+ print("ReactNative", "retryFailedActions failed because fula is not initialized")
287
+ //Fula is not initialized
288
+ return false
289
+ }
290
+
291
+ }
292
+
293
+
294
+ func createPeerIdentity(privateKey: Data) throws -> Data {
295
+ do {
296
+ // 1: First: create public key from provided private key
297
+ // 2: Should read the local keychain store (if it is key-value, key is public key above,
298
+ // 3: if found, decrypt using the private key
299
+ // 4: If not found or decryption not successful, generate an identity
300
+ // 5: then encrypt and store in keychain
301
+ // TODO: recheck error handling
302
+ var encryptedKey = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
303
+ let secretKey = try Cryptography.generateKey(privateKey)
304
+ if (encryptedKey == nil) {
305
+ var error: NSError?
306
+ let autoGeneratedIdentity = FulamobileGenerateEd25519Key(&error)
307
+ if error != nil {
308
+ throw error!
309
+ }
310
+ encryptedKey = try Cryptography.encryptMsg(autoGeneratedIdentity!.toUint8Array(), secretKey)
311
+ userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey!)
312
+ }
313
+ return try Cryptography.decryptMsg(encryptedKey!, secretKey).toData()
314
+
315
+ } catch let error {
316
+ print("ReactNative", "createPeerIdentity failed with Error: ", error.localizedDescription)
317
+ throw error
318
+ }
319
+ }
320
+
321
+ func createNewrootCid(identity: Data) throws -> Void {
322
+ let hash32 = identity.sha256()
323
+ print("ReactNative", "wnfsKey=" , identity.toHex() , "; hash32 = " , hash32.toHex());
324
+ if (fula != nil) {
325
+ try fula?.flush()
326
+ }
327
+ rootCid = try wnfs?.Init(wnfsKey: hash32)
328
+ print("ReactNative", "privateForest is created: ", rootCid!)
329
+ wnfsKey = identity
330
+ print("ReactNative", "rootCid is created: cid=", rootCid!, " ; wnfs_key=", wnfsKey!.toHex(), "; hash32=", hash32)
331
+ try encryptAndStoreConfig()
332
+ }
333
+
334
+ func loadWnfs(_ wnfsKey: Data , _ _rootCid: String) throws {
335
+ print("ReactNative", "loadWnfs called: rootCid=", _rootCid)
336
+ let hash32 = wnfsKey.sha256()
337
+ print("ReactNative", "wnfsKey=" , wnfsKey.toHex() , "; hash32 = " , hash32.toHex());
338
+ try wnfs?.LoadWithWNFSKey(wnfsKey: hash32, cid: _rootCid)
339
+ rootCid = _rootCid
340
+ if (fula != nil) {
341
+ try fula?.flush()
342
+ }
343
+ print("ReactNative", "loadWnfs completed")
344
+ try encryptAndStoreConfig()
345
+ }
346
+
347
+
348
+ func encryptAndStoreConfig() throws {
349
+ do {
350
+ if(identityEncryptedGlobal != nil) {
351
+ let cid_encrypted = try Cryptography.encryptMsg(rootCid!.toUint8Array(), secretKeyGlobal!)
352
+ let wnfs_key_encrypted = try Cryptography.encryptMsg(wnfsKey!.toUint8Array(), secretKeyGlobal!)
353
+
354
+ userDataHelper.add("cid_encrypted_" + identityEncryptedGlobal!, cid_encrypted)
355
+ userDataHelper.add("wnfs_key_encrypted_" + identityEncryptedGlobal!, wnfs_key_encrypted)
356
+ }
357
+ } catch let error {
358
+ print("ReactNative", "encryptAndStoreConfig failed with Error: ", error.localizedDescription)
359
+ throw error
360
+ }
361
+ }
362
+
363
+ func logoutInternal(identity: Data, _storePath: String?) throws {
364
+ do {
365
+ if (fula != nil) {
366
+ try fula?.flush()
367
+ }
368
+ let secretKey = try Cryptography.generateKey(identity)
369
+ let identity_encrypted: String = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
370
+ userDataHelper.remove("cid_encrypted_"+identity_encrypted)
371
+
372
+ //TODO: Should also remove peerid @Mahdi
373
+
374
+ userDataHelper.remove("cid_encrypted_"+identity_encrypted)
375
+
376
+ rootCid = nil
377
+ secretKeyGlobal = nil
378
+ identityEncryptedGlobal = nil
379
+ var storePath = _storePath
380
+ if (storePath == nil || storePath!.isEmpty) {
381
+ storePath = fulaStorePath
382
+ }
383
+
384
+ do{
385
+ try FileManager.default.removeItem(atPath: fulaStorePath)
386
+ }catch let error{
387
+ print("Deleting fula store path", error.localizedDescription)
388
+ throw error
389
+ }
390
+ } catch let error {
391
+ print("ReactNative", "logout internal failed with Error: ", error.localizedDescription)
392
+ throw error
393
+ }
394
+ }
395
+
396
+ func getFulaClient() -> FulamobileClient? {
397
+ return fula
398
+ }
399
+
400
+ func newClientInternal(identity: Data, storePath: String?, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool) throws -> Data {
401
+ do {
402
+ fulaConfig = FulamobileConfig()
403
+ if (storePath == nil || storePath!.isEmpty) {
404
+ fulaConfig!.storePath = fulaStorePath
405
+ } else {
406
+ fulaConfig!.storePath = storePath!
407
+ }
408
+ print("ReactNative", "storePath is set: " + fulaConfig!.storePath)
409
+
410
+ let peerIdentity = try createPeerIdentity(privateKey: identity)
411
+ fulaConfig!.identity = peerIdentity
412
+ print("ReactNative", "peerIdentity is set: " + fulaConfig!.identity!.toHex())
413
+ fulaConfig!.bloxAddr = bloxAddr
414
+ print("ReactNative", "bloxAddr is set: " + fulaConfig!.bloxAddr)
415
+ fulaConfig!.exchange = exchange
416
+ fulaConfig!.syncWrites = autoFlush
417
+ if (useRelay) {
418
+ fulaConfig!.allowTransientConnection = true
419
+ fulaConfig!.forceReachabilityPrivate = true
420
+ }
421
+ if (fula == nil || refresh) {
422
+ print("ReactNative", "Creating a new Fula instance");
423
+ do {
424
+ try shutdownInternal();
425
+ fula = FulamobileClient(fulaConfig)
426
+ if (fula != nil) {
427
+ try fula?.flush();
428
+ }
429
+ } catch let error {
430
+ print("ReactNative", "Failed to create new Fula instance: " , error.localizedDescription);
431
+ throw MyError.runtimeError("Failed to create new Fula instance")
432
+ }
433
+ }
434
+ return peerIdentity
435
+ } catch let error {
436
+ print("ReactNative", "newclientInternal failed with Error: ", error.localizedDescription)
437
+ throw error
438
+ }
439
+ }
440
+
441
+ func initInternal(identity: Data, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, _rootCid: String, useRelay: Bool, refresh: Bool) throws -> [String] {
442
+
443
+ do {
444
+ if (fula == nil || refresh) {
445
+ try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
446
+ print("ReactNative", "fula initialized: " + fula!.id_())
447
+ }
448
+ if(client == nil || refresh) {
449
+ client = Client(clientInput: fula!)
450
+ wnfs = Wnfs(putFn: { cid, data in
451
+ guard let c = self.client else {
452
+ throw MyError.runtimeError("wnfs: fula client not ready")
453
+ }
454
+ try c.put(cid, data)
455
+ }, getFn: { cid in
456
+ guard let c = self.client else {
457
+ throw MyError.runtimeError("wnfs: fula client not ready")
458
+ }
459
+ return try c.get(cid)
460
+ })
461
+ print("ReactNative", "wnfs initialized")
462
+ }
463
+
464
+ let secretKey = try Cryptography.generateKey(identity)
465
+ let identity_encrypted = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
466
+ identityEncryptedGlobal = identity_encrypted
467
+ secretKeyGlobal = secretKey
468
+
469
+ if (rootCid == nil || rootCid!.isEmpty) {
470
+ print("ReactNative", "rootCid is empty.")
471
+ //Load from keystore
472
+
473
+ let cid_encrypted_fetched = userDataHelper.getValue("cid_encrypted_"+identity_encrypted)
474
+ print("ReactNative", "Here1")
475
+ var cid: Array<UInt8>? = nil
476
+ if(cid_encrypted_fetched != nil && !cid_encrypted_fetched!.isEmpty) {
477
+ print("ReactNative", "decrypting cid="+cid_encrypted_fetched!+" with secret="+secretKey.toHex())
478
+ cid = try Cryptography.decryptMsg(cid_encrypted_fetched!, secretKey)
479
+ }
480
+ print("ReactNative", "Here2")
481
+ //print("ReactNative", "Attempted to fetch cid from keystore cid="+cid+" & wnfs_key="+wnfs_key)
482
+ if(cid == nil || cid!.isEmpty){
483
+ print("ReactNative", "cid or wnfs key was not found")
484
+ if(!_rootCid.isEmpty){
485
+ print("ReactNative", "Re-setting cid from input: "+_rootCid)
486
+ cid = _rootCid.toUint8Array()
487
+ }
488
+
489
+ }
490
+ if(cid == nil || cid!.isEmpty){
491
+ print("ReactNative", "Tried to recover cid but was not successful. Creating ones")
492
+ try createNewrootCid(identity: identity)
493
+ } else {
494
+ print("ReactNative", "Found cid and wnfs key in keychain store")
495
+ print("ReactNative", "Recovered cid and private ref from keychain store. cid=",cid!," & wnfs_key=",identity)
496
+ try loadWnfs(identity, cid!.toData().toUTF8String()!)
497
+ }
498
+ print("ReactNative", "creating/reloading rootCid completed")
499
+
500
+ /*
501
+ 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")
502
+ long testcodec = 85
503
+ byte[] testputcid = client!.put(testbyte, testcodec)
504
+ print("ReactNative", "client!.put test done"+ Arrays.toString(testputcid))
505
+ 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")
506
+ byte[] testfetchedbytes = client!.get(testfetchedcid)
507
+ print("ReactNative", "client!.get test done"+ Arrays.toString(testfetchedbytes))
508
+ */
509
+
510
+
511
+ print("ReactNative", "rootCid is created: cid=" , rootCid!,"& wnfs_key=",wnfsKey!.toHex())
512
+ } else {
513
+ print("ReactNative", "rootCid existed: cid=" , rootCid!," & wnfs_key=",wnfsKey!.toHex())
514
+ }
515
+ let peerId = fula!.id_()
516
+ var obj = [String]()
517
+ obj.append(peerId)
518
+ obj.append(rootCid!)
519
+ obj.append(wnfsKey!.toHex())
520
+ print("ReactNative", "initInternal is completed successfully")
521
+ if (fula != nil) {
522
+ try fula?.flush()
523
+ }
524
+ return obj
525
+ } catch let error {
526
+ print("ReactNative", "init internal failed with Error: " , error.localizedDescription)
527
+ throw error
528
+ }
529
+ }
530
+
531
+
532
+ @objc(mkdir:withResolver:withRejecter:)
533
+ func mkdir(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
534
+ print("ReactNative", "mkdir: path = " + path)
535
+ do {
536
+ let cid = try wnfs?.MkDir(cid: rootCid!, remotePath: path)
537
+ if(cid != nil) {
538
+ rootCid = cid
539
+ try encryptAndStoreConfig()
540
+ if (fula != nil) {
541
+ try fula?.flush()
542
+ }
543
+ resolve(rootCid)
544
+ } else {
545
+ print("ReactNative", "mkdir Error: config is nil")
546
+ reject("ERR_WNFS", "Can't make dir", nil)
547
+ }
548
+
549
+ } catch let error{
550
+ print("mkdir", error.localizedDescription)
551
+ reject("ERR_WNFS", "mkdir", error)
552
+ }
553
+ }
554
+
555
+ @objc(writeFile:withLocalFilename:withResolver:withRejecter:)
556
+ func writeFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
557
+ /*
558
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
559
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
560
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
561
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
562
+ // Returns: new cid of the root after this file is placed in the tree
563
+ */
564
+ print("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename)
565
+ do {
566
+ let cid = try wnfs?.WriteFileFromPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
567
+ if(cid != nil) {
568
+ rootCid = cid
569
+ try encryptAndStoreConfig()
570
+ if (fula != nil) {
571
+ try fula?.flush()
572
+ }
573
+ resolve(rootCid)
574
+ } else {
575
+ print("ReactNative", "writeFile Error: config is nil")
576
+ reject("ERR_WNFS", "writeFile Error: config is nil", nil)
577
+ }
578
+ } catch let error {
579
+ print("writeFile", error.localizedDescription)
580
+ reject("ERR_WNFS", "writeFile", error)
581
+ }
582
+ }
583
+
584
+ @objc(writeFileContent:withContentString:withResolver:withRejecter:)
585
+ func writeFileContent(path: String, contentString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
586
+
587
+ print("ReactNative", "writeFile: contentString = " + contentString)
588
+ print("ReactNative", "writeFile: path = " + path)
589
+ do {
590
+ let content = convertStringToByte(contentString)
591
+ let cid = try wnfs?.WriteFile(cid: rootCid!, remotePath: path, data: content.toData())
592
+ rootCid = cid
593
+ try encryptAndStoreConfig()
594
+ if (fula != nil) {
595
+ try fula?.flush()
596
+ }
597
+ resolve(rootCid)
598
+ } catch let error {
599
+ print("writeFileContent", error.localizedDescription)
600
+ reject("ERR_WNFS", "writeFileContent", error)
601
+ }
602
+
603
+ }
604
+
605
+ @objc(ls:withResolver:withRejecter:)
606
+ func ls(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
607
+ print("ReactNative", "ls: path = " + path)
608
+ do {
609
+ let res = try wnfs?.Ls(cid: rootCid!, remotePath: path)
610
+
611
+ //JSONArray jsonArray = new JSONArray(res)
612
+ guard let s = res?.toUTF8String() else {
613
+ throw MyError.runtimeError("converting bytes to utf8 string")
614
+ }
615
+ print("ReactNative", "ls: res = " + s)
616
+ resolve(s)
617
+ } catch let error {
618
+ print("ls", error.localizedDescription)
619
+ reject("ERR_WNFS", "ls", error)
620
+ }
621
+
622
+ }
623
+
624
+ @objc(rm:withResolver:withRejecter:)
625
+ func rm(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
626
+
627
+ print("ReactNative", "rm: path = " + path)
628
+ do {
629
+ let cid = try wnfs?.Rm(cid: rootCid!, remotePath: path)
630
+ if(cid != nil) {
631
+ rootCid = cid
632
+ try encryptAndStoreConfig()
633
+ if (fula != nil) {
634
+ try fula?.flush()
635
+ }
636
+ resolve(rootCid)
637
+ } else {
638
+ print("ReactNative", "rm Error: config is nil")
639
+ reject("ERR_WNFS", "rm Error: config is nil", nil)
640
+ }
641
+ } catch let error {
642
+ print("rm", error.localizedDescription)
643
+ reject("ERR_WNFS", "rm", error)
644
+ }
645
+
646
+ }
647
+
648
+ @objc(cp:withTargetPath:withResolver:withRejecter:)
649
+ func cp(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
650
+
651
+ print("ReactNative", "rm: sourcePath = " + sourcePath)
652
+ do {
653
+ let cid = try wnfs?.Cp(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
654
+ if(cid != nil) {
655
+ rootCid = cid
656
+ try encryptAndStoreConfig()
657
+ if (fula != nil) {
658
+ try fula?.flush()
659
+ }
660
+ resolve(rootCid)
661
+ } else {
662
+ print("ReactNative", "cp Error: config is nil")
663
+ reject("ERR_WNFS", "cp Error: config is nil", nil)
664
+ }
665
+ } catch let error {
666
+ print("cp", error.localizedDescription)
667
+ reject("ERR_WNFS", "cp", error)
668
+ }
669
+
670
+ }
671
+
672
+ @objc(mv:withTargetPath:withResolver:withRejecter:)
673
+ func mv(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
674
+ print("ReactNative", "rm: sourcePath = " + sourcePath)
675
+ do {
676
+ let cid = try wnfs?.Mv(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
677
+ if(cid != nil) {
678
+ rootCid = cid
679
+ try encryptAndStoreConfig()
680
+ if (fula != nil) {
681
+ try fula?.flush()
682
+ }
683
+ resolve(rootCid)
684
+ } else {
685
+ print("ReactNative", "mv Error: config is nil")
686
+ reject("ERR_WNFS", "mv Error: config is nil", nil)
687
+ }
688
+ } catch let error {
689
+ print("mv", error.localizedDescription)
690
+ reject("ERR_WNFS", "mv", error)
691
+ }
692
+
693
+ }
694
+
695
+ @objc(readFile:withLocalFilename:withResolver:withRejecter:)
696
+ func readFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
697
+ /*
698
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
699
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
700
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
701
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
702
+ // Returns: new cid of the root after this file is placed in the tree
703
+ */
704
+ print("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename)
705
+ do {
706
+ let path = try wnfs?.ReadFileToPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
707
+ resolve(path)
708
+ } catch let error {
709
+ print("readFile", error.localizedDescription)
710
+ reject("ERR_WNFS", "readFile", error)
711
+ }
712
+
713
+ }
714
+
715
+ @objc(readFileContent:withResolver:withRejecter:)
716
+ func readFileContent(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
717
+
718
+ print("ReactNative", "readFileContent: path = " + path)
719
+ do {
720
+ // FIXME: dhouldn't we output an NSData object instead?
721
+ let res = try wnfs?.ReadFile(cid: rootCid!, remotePath: path)
722
+ guard let resString = res?.toUTF8String() else{
723
+ throw MyError.runtimeError("converting bytes to utf8 string")
724
+ }
725
+ resolve(resString)
726
+ } catch let error {
727
+ print("readFileContent", error.localizedDescription)
728
+ reject("ERR_WNFS", "readFileContent", error)
729
+ }
730
+
731
+ }
732
+
733
+ @objc(get:withResolver:withRejecter:)
734
+ func get(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
735
+
736
+ print("ReactNative", "get: keyString = " + keyString)
737
+ do {
738
+ let key: Data = convertStringToByte(keyString).toData()
739
+ let value = try getInternal(key)
740
+ let valueString: String = value.toUTF8String()!
741
+ resolve(valueString)
742
+ } catch let error {
743
+ print("get", error.localizedDescription)
744
+ reject("ERR_FULA", "get", error)
745
+ }
746
+
747
+ }
748
+
749
+ func getInternal(_ key: Data) throws -> Data {
750
+ do {
751
+ print("ReactNative", "getInternal: key.toUTF8String() = " , key.toUTF8String()!)
752
+ print("ReactNative", "getInternal: key.toHex().bytes = " , key.toHex())
753
+ let value = try fula!.get(key)
754
+ print("ReactNative", "getInternal: value.toHex() = " , value.toHex())
755
+ return value
756
+ } catch let error {
757
+ print("ReactNative", "getInternal: error = " + error.localizedDescription)
758
+ print("getInternal", error.localizedDescription)
759
+ throw error
760
+ }
761
+ }
762
+
763
+ @objc(has:withResolver:withRejecter:)
764
+ func has(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
765
+
766
+ print("ReactNative", "has: keyString = " + keyString)
767
+ do {
768
+ let key: Data = convertStringToByte(keyString).toData()
769
+ let result = try hasInternal(key)
770
+ resolve(result)
771
+ } catch let error {
772
+ print("has", error.localizedDescription)
773
+ reject("ERR_FULA", "has", error)
774
+ }
775
+
776
+ }
777
+
778
+ func hasInternal(_ key: Data) throws -> Bool {
779
+ do {
780
+ let ret = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
781
+ try fula?.has(key, ret0_: ret)
782
+ let res = ret.pointee.boolValue
783
+ ret.deallocate()
784
+ return res
785
+ } catch let error {
786
+ print("hasInternal", error.localizedDescription)
787
+ throw error
788
+ }
789
+ }
790
+
791
+ func pullInternal(key: Data) throws -> Void {
792
+ do {
793
+ try fula!.pull(key)
794
+ } catch let error {
795
+ print("pullInternal", error.localizedDescription)
796
+ throw error
797
+ }
798
+ }
799
+
800
+ @objc(push:withRejecter:)
801
+ func push(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
802
+ print("ReactNative", "push started")
803
+ do {
804
+ try pushInternal(key: convertStringToByte(rootCid!).toData())
805
+ resolve(rootCid)
806
+ } catch let error {
807
+ print("get", error.localizedDescription)
808
+ reject("ERR_FULA", "push", error)
809
+ }
810
+ }
811
+
812
+ func pushInternal(key: Data) throws -> Void {
813
+ do {
814
+ let hasIt = try hasInternal(key)
815
+ if (fula != nil && hasIt) {
816
+ try fula?.push(key)
817
+ try fula?.flush()
818
+ } else {
819
+ print("ReactNative", "pushInternal error: key wasn't found or fula is not initialized")
820
+ throw MyError.runtimeError("pushInternal error: key wasn't found or fula is not initialized")
821
+ }
822
+ } catch let error {
823
+ print("ReactNative", "pushInternal", error.localizedDescription)
824
+ throw error
825
+ }
826
+ }
827
+
828
+ // FIXME: unused codecString arg
829
+ @objc(put:withCodecString:withResolver:withRejecter:)
830
+ func put(valueString: String, codecString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
831
+
832
+ print("ReactNative", "put: codecString = " + codecString)
833
+ print("ReactNative", "put: valueString = " + valueString)
834
+ do {
835
+ //byte[] codec = convertStringToByte(CodecString)
836
+ let codec = FulaModule.CODEC_DAG_CBOR
837
+
838
+
839
+ print("ReactNative", "put: codec = ", codec)
840
+ let value = valueString.toData()
841
+
842
+ print("ReactNative", "put: value.toHex() = " , value.toHex())
843
+ let key = try putInternal(value: value, codec: codec)
844
+ print("ReactNative", "put: key.toHex() = " , key.toUTF8String()!)
845
+ resolve(key.toUTF8String()!)
846
+ } catch let error {
847
+ print("ReactNative", "put: error = ", error.localizedDescription)
848
+ reject("ERR_FULA", "put", error)
849
+ }
850
+
851
+ }
852
+
853
+ // FIXME: unused codec arg
854
+ func putInternal(value: Data, codec: Int) throws -> Data {
855
+ do {
856
+ if(fula != nil) {
857
+ let key: Data = try fula!.put(value, codec: Int64(FulaModule.CODEC_DAG_CBOR))
858
+ try fula?.flush()
859
+ return key
860
+ } else {
861
+ print("ReactNative", "putInternal Error: fula is not initialized")
862
+ throw MyError.runtimeError("putInternal Error: fula is not initialized")
863
+ }
864
+ } catch let error {
865
+ print("ReactNative", "putInternal", error.localizedDescription)
866
+ throw error
867
+ }
868
+ }
869
+
870
+ @objc(setAuth:withAllow:withResolver:withRejecter:)
871
+ func setAuth(peerIdString: String, allow: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
872
+
873
+ print("ReactNative", "setAuth: peerIdString = " + peerIdString)
874
+ do {
875
+ if (fula != nil && !(fula?.id_().isEmpty)! && fulaConfig != nil && !fulaConfig!.bloxAddr.isEmpty) {
876
+ let bloxAddr = fulaConfig!.bloxAddr
877
+ print("ReactNative", "setAuth: bloxAddr = '",bloxAddr,"'"," peerIdString = '",peerIdString,"'")
878
+ let parts = bloxAddr.split(separator: "/").map(String.init)
879
+ try fula?.setAuth(parts.last, subject: peerIdString, allow: allow)
880
+ resolve(true)
881
+ } else {
882
+ print("ReactNative", "setAuth error: fula is not initialized")
883
+ throw MyError.runtimeError("fula is not initialized")
884
+ }
885
+ resolve(false)
886
+ } catch let error {
887
+ print("get", error.localizedDescription)
888
+ reject("ERR_FULA", "setAuth", error)
889
+ }
890
+
891
+ }
892
+
893
+ @objc(shutdown:withRejecter:)
894
+ func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
895
+ do {
896
+ try shutdownInternal()
897
+ resolve(true)
898
+ } catch let error {
899
+ print("ReactNative", "shutdown", error.localizedDescription)
900
+ reject("ERR_FULA", "shutdown", error)
901
+ }
902
+
903
+ }
904
+
905
+ func shutdownInternal() throws {
906
+ if(fula != nil) {
907
+ try fula?.shutdown()
908
+ fula = nil
909
+ client = nil
910
+ wnfs = nil
911
+ }
912
+ }
913
+
914
+ ///////////////////////////////////////////////////////////
915
+ ///////////////////////////////////////////////////////////
916
+ ///////////////////////////////////////////////////////////
917
+ ///////////////////////////////////////////////////////////
918
+ //////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
919
+ ///////////////////////////////////////////////////////////
920
+ @objc(createAccount:withResolver:withRejecter:)
921
+ func createAccount(seedString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
922
+ print("ReactNative", "createAccount: seedString = ", seedString)
923
+ do {
924
+ if (fula == nil || ((fula?.id_().isEmpty) != nil)) {
925
+ reject("ERR_FULA", "createAccount", MyError.runtimeError("Fula client is not initialized"))
926
+ } else {
927
+
928
+ if (!seedString.starts(with: "/")) {
929
+ reject("ERR_FULA", "createAccount", MyError.runtimeError("seed should start with /"))
930
+ }
931
+ let result = try fula!.seeded(seedString)
932
+ let resultString = result.toUTF8String()!
933
+ resolve(resultString)
934
+ }
935
+ } catch let error {
936
+ print("createAccount", error.localizedDescription)
937
+ reject("ERR_FULA", "createAccount", error)
938
+ }
939
+
940
+ }
941
+
942
+ @objc(checkAccountExists:withResolver:withRejecter:)
943
+ func checkAccountExists(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
944
+ print("ReactNative", "checkAccountExists: accountString = ", accountString)
945
+ do {
946
+ let result = try fula!.accountExists(accountString)
947
+ let resultString = result.toUTF8String()!
948
+ resolve(resultString)
949
+ } catch let error {
950
+ print("checkAccountExists", error.localizedDescription)
951
+ reject("ERR_FULA", "checkAccountExists", error)
952
+ }
953
+
954
+ }
955
+
956
+ @objc(createPool:withPoolName:withResolver:withRejecter:)
957
+ func createPool(seedString: String, poolName: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
958
+ print("ReactNative", "createPool: seedString = " + seedString + " poolName = " + poolName)
959
+ do {
960
+ let result = try fula!.poolCreate(seedString, poolName: poolName)
961
+ let resultString = result.toUTF8String()!
962
+ resolve(resultString)
963
+ } catch let error {
964
+ print("createPool", error.localizedDescription)
965
+ reject("ERR_FULA", "createPool", error)
966
+ }
967
+
968
+ }
969
+
970
+ @objc(listPools:withRejecter:)
971
+ func listPools( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
972
+ print("ReactNative", "listPools")
973
+ do {
974
+ let result = try fula!.poolList()
975
+ let resultString = result.toUTF8String()!
976
+ resolve(resultString)
977
+ } catch let error {
978
+ print("listPools", error.localizedDescription)
979
+ reject("ERR_FULA", "listPools", error)
980
+ }
981
+
982
+ }
983
+
984
+ @objc(joinPool:withPoolID:withResolver:withRejecter:)
985
+ func joinPool(seedString: String, poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
986
+ print("ReactNative", "joinPool: seedString = ",seedString," poolID = ",poolID)
987
+ do {
988
+ let result = try fula!.poolJoin(seedString, poolID: poolID)
989
+ let resultString = result.toUTF8String()!
990
+ resolve(resultString)
991
+ } catch let error {
992
+ print("joinPool", error.localizedDescription)
993
+ reject("ERR_FULA", "joinPool", error)
994
+ }
995
+
996
+ }
997
+
998
+ @objc(cancelPoolJoin:withPoolID:withResolver:withRejecter:)
999
+ func cancelPoolJoin(seedString: String, poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1000
+ print("ReactNative", "cancelPoolJoin: seedString = " , seedString , " poolID = " , poolID)
1001
+ do {
1002
+ let result = try fula!.poolCancelJoin(seedString, poolID: poolID)
1003
+ let resultString = result.toUTF8String()!
1004
+ resolve(resultString)
1005
+ } catch let error {
1006
+ print("cancelPoolJoin", error.localizedDescription)
1007
+ reject("ERR_FULA", "cancelPoolJoin", error)
1008
+ }
1009
+
1010
+ }
1011
+
1012
+ @objc(listPoolJoinRequests:withResolver:withRejecter:)
1013
+ func listPoolJoinRequests(poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1014
+ print("ReactNative", "listPoolJoinRequests: poolID = ", poolID)
1015
+ do {
1016
+ let result = try fula!.poolRequests(poolID)
1017
+ let resultString = result.toUTF8String()!
1018
+ resolve(resultString)
1019
+ } catch let error {
1020
+ print("listPoolJoinRequests", error.localizedDescription)
1021
+ reject("ERR_FULA", "listPoolJoinRequests", error)
1022
+ }
1023
+
1024
+ }
1025
+
1026
+ @objc(votePoolJoinRequest:withPoolID:withAccountString:withAccept:withResolver:withRejecter:)
1027
+ func votePoolJoinRequest(seedString: String, poolID: Int, accountString: String, accept: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1028
+ print("ReactNative", "votePoolJoinRequest: seedString = ", seedString ," poolID = ", poolID, " accountString = ", accountString , " accept = ", accept)
1029
+ do {
1030
+ let result = try fula!.poolVote(seedString, poolID: Int(poolID), account: accountString, voteValue: accept)
1031
+ let resultString = result.toUTF8String()!
1032
+ resolve(resultString)
1033
+ } catch let error {
1034
+ print("votePoolJoinRequest", error.localizedDescription)
1035
+ reject("ERR_FULA", "votePoolJoinRequest", error)
1036
+ }
1037
+
1038
+ }
1039
+
1040
+ @objc(leavePool:withPoolID:withResolver:withRejecter:)
1041
+ func leavePool(seedString: String, poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1042
+ print("ReactNative", "leavePool: seedString = " , seedString , " poolID = " , poolID)
1043
+ do {
1044
+ let result = try fula!.poolLeave(seedString, poolID: poolID)
1045
+ let resultString = result.toUTF8String()!
1046
+ resolve(resultString)
1047
+ } catch let error {
1048
+ print("leavePool", error.localizedDescription)
1049
+ reject("ERR_FULA", "leavePool", error)
1050
+ }
1051
+
1052
+ }
1053
+
1054
+ @objc(newReplicationRequest:withPoolID:withReplicationFactor:withCid:withResolver:withRejecter:)
1055
+ func newReplicationRequest(seedString: String, poolID: Int, replicationFactor: Int, cid: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1056
+ print("ReactNative", "newReplicationRequest: seedString = " , seedString , " poolID = " , poolID , " replicationFactor = " , replicationFactor , " cid = " , cid)
1057
+ do {
1058
+ let result = try fula!.manifestUpload(seedString, poolID: poolID, replicationFactor: replicationFactor, uri: cid)
1059
+ let resultString = result.toUTF8String()!
1060
+ resolve(resultString)
1061
+ } catch let error {
1062
+ print("newReplicationRequest", error.localizedDescription)
1063
+ reject("ERR_FULA", "newReplicationRequest", error)
1064
+ }
1065
+
1066
+ }
1067
+
1068
+ @objc(newStoreRequest:withPoolID:withUploader:withCid:withResolver:withRejecter:)
1069
+ func newStoreRequest(seedString: String, poolID: Int, uploader: String, cid: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1070
+ print("ReactNative", "newStoreRequest: seedString = " + seedString + " poolID = " , poolID , " uploader = " , uploader , " cid = " , cid)
1071
+ do {
1072
+ let result = try fula!.manifestStore(seedString, poolID: poolID, uploader: uploader, cid: cid)
1073
+ let resultString = result.toUTF8String()!
1074
+ resolve(resultString)
1075
+ } catch let error {
1076
+ print("newStoreRequest", error.localizedDescription)
1077
+ reject("ERR_FULA", "newStoreRequest", error)
1078
+ }
1079
+
1080
+ }
1081
+
1082
+ @objc(listAvailableReplicationRequests:withResolver:withRejecter:)
1083
+ func listAvailableReplicationRequests(poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1084
+ print("ReactNative", "listAvailableReplicationRequests: poolID = ", poolID)
1085
+ do {
1086
+ let result = try fula!.manifestAvailable(poolID)
1087
+ let resultString = result.toUTF8String()!
1088
+ resolve(resultString)
1089
+ } catch let error {
1090
+ print("listAvailableReplicationRequests", error.localizedDescription)
1091
+ reject("ERR_FULA", "listAvailableReplicationRequests", error)
1092
+ }
1093
+
1094
+ }
1095
+
1096
+ @objc(removeReplicationRequest:withPoolID:withCid:withResolver:withRejecter:)
1097
+ func removeReplicationRequest(seedString: String, poolID: Int, cid: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1098
+ print("ReactNative", "newReplicationRequest: seedString = " , seedString , " poolID = " , poolID , " cid = " , cid)
1099
+ do {
1100
+ let result = try fula!.manifestRemove(seedString, poolID: poolID, cid: cid)
1101
+ let resultString = result.toUTF8String()!
1102
+ resolve(resultString)
1103
+ } catch let error {
1104
+ print("removeReplicationRequest", error.localizedDescription)
1105
+ reject("ERR_FULA", "removeReplicationRequest", error)
1106
+ }
1107
+
1108
+ }
1109
+
1110
+ @objc(removeStorer:withStorage:withPoolID:withCid:withResolver:withRejecter:)
1111
+ func removeStorer(seedString: String, storage: String, poolID: Int, cid: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1112
+ print("ReactNative", "removeStorer: seedString = " , seedString , " storage = " , storage , " poolID = " , poolID , " cid = " , cid)
1113
+ do {
1114
+ let result = try fula!.manifestRemoveStorer(seedString, storage: storage, poolID: poolID, cid: cid)
1115
+ let resultString = result.toUTF8String()!
1116
+ resolve(resultString)
1117
+ } catch let error {
1118
+ print("removeStorer", error.localizedDescription)
1119
+ reject("ERR_FULA", "removeStorer", error)
1120
+ }
1121
+
1122
+ }
1123
+
1124
+ @objc(removeStoredReplication:withUploader:withPoolID:withCid:withResolver:withRejecter:)
1125
+ func removeStoredReplication(seedString: String, uploader: String, poolID: Int, cid: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1126
+ print("ReactNative", "removeStoredReplication: seedString = " , seedString , " uploader = " , uploader , " poolID = " , poolID , " cid = " , cid)
1127
+ do {
1128
+ let result = try fula!.manifestRemoveStored(seedString, uploader: uploader, poolID: poolID, cid: cid)
1129
+ let resultString = result.toUTF8String()!
1130
+ resolve(resultString)
1131
+ } catch let error {
1132
+ print("removeStoredReplication", error.localizedDescription)
1133
+ reject("ERR_FULA", "removeStoredReplication", error)
1134
+ }
1135
+
1136
+ }
1137
+
1138
+ @objc(bloxFreeSpace:withRejecter:)
1139
+ func bloxFreeSpace( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
1140
+ print("ReactNative", "bloxFreeSpace")
1141
+ do {
1142
+ let result = try fula!.bloxFreeSpace()
1143
+ let resultString = result.toUTF8String()!
1144
+ resolve(resultString)
1145
+ } catch let error {
1146
+ print("bloxFreeSpace", error.localizedDescription)
1147
+ reject("ERR_FULA", "bloxFreeSpace", error)
1148
+ }
1149
+
1150
+ }
1151
+
1152
+ }