@functionland/react-native-fula 1.41.0 → 1.43.0
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.
- package/LICENSE +20 -20
- package/README.md +226 -226
- package/android/build.gradle +1 -1
- package/android/gradle.properties +5 -5
- package/android/src/main/AndroidManifest.xml +3 -3
- package/android/src/main/AndroidManifestNew.xml +2 -2
- package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
- package/android/src/main/java/land/fx/fula/Cryptography.java +62 -62
- package/android/src/main/java/land/fx/fula/FulaModule.java +15 -0
- package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
- package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
- package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
- package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
- package/ios/Cryptography.swift +49 -49
- package/ios/Fula-Bridging-Header.h +3 -3
- package/ios/Fula.mm +170 -215
- package/ios/Fula.swift +1303 -1334
- package/ios/UserDataHelper.swift +143 -143
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interfaces/augment-api-errors.js.map +1 -1
- package/lib/commonjs/interfaces/augment-api-rpc.js.map +1 -1
- package/lib/commonjs/interfaces/augment-api-runtime.js.map +1 -1
- package/lib/commonjs/interfaces/augment-api.js.map +1 -1
- package/lib/commonjs/interfaces/augment-types.js.map +1 -1
- package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/commonjs/interfaces/index.js.map +1 -1
- package/lib/commonjs/interfaces/lookup.js +278 -278
- package/lib/commonjs/interfaces/lookup.js.map +1 -1
- package/lib/commonjs/interfaces/registry.js.map +1 -1
- package/lib/commonjs/interfaces/types-lookup.js.map +1 -1
- package/lib/commonjs/interfaces/types.js.map +1 -1
- package/lib/commonjs/protocols/blockchain.js +25 -2
- package/lib/commonjs/protocols/blockchain.js.map +1 -1
- package/lib/commonjs/protocols/chain-api.js +34 -13
- package/lib/commonjs/protocols/chain-api.js.map +1 -1
- package/lib/commonjs/protocols/fula.js +106 -106
- package/lib/commonjs/protocols/fula.js.map +1 -1
- package/lib/commonjs/protocols/fxblox.js +3 -3
- package/lib/commonjs/protocols/fxblox.js.map +1 -1
- package/lib/commonjs/types/blockchain.js.map +1 -1
- package/lib/commonjs/types/fxblox.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/interfaces/augment-api-errors.js.map +1 -1
- package/lib/module/interfaces/augment-api-rpc.js.map +1 -1
- package/lib/module/interfaces/augment-api-runtime.js.map +1 -1
- package/lib/module/interfaces/augment-api.js.map +1 -1
- package/lib/module/interfaces/augment-types.js.map +1 -1
- package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/module/interfaces/index.js.map +1 -1
- package/lib/module/interfaces/lookup.js +278 -278
- package/lib/module/interfaces/lookup.js.map +1 -1
- package/lib/module/interfaces/registry.js.map +1 -1
- package/lib/module/interfaces/types-lookup.js.map +1 -1
- package/lib/module/interfaces/types.js.map +1 -1
- package/lib/module/protocols/blockchain.js +22 -0
- package/lib/module/protocols/blockchain.js.map +1 -1
- package/lib/module/protocols/chain-api.js +30 -11
- package/lib/module/protocols/chain-api.js.map +1 -1
- package/lib/module/protocols/fula.js +106 -106
- package/lib/module/protocols/fula.js.map +1 -1
- package/lib/module/protocols/fxblox.js +3 -3
- package/lib/module/protocols/fxblox.js.map +1 -1
- package/lib/module/types/blockchain.js.map +1 -1
- package/lib/module/types/fxblox.js.map +1 -1
- package/lib/typescript/interfaces/fulaNativeModule.d.ts +1 -0
- package/lib/typescript/interfaces/fulaNativeModule.d.ts.map +1 -1
- package/lib/typescript/protocols/blockchain.d.ts +1 -0
- package/lib/typescript/protocols/blockchain.d.ts.map +1 -1
- package/lib/typescript/protocols/chain-api.d.ts +4 -0
- package/lib/typescript/protocols/chain-api.d.ts.map +1 -1
- package/lib/typescript/types/blockchain.d.ts +5 -0
- package/lib/typescript/types/blockchain.d.ts.map +1 -1
- package/package.json +3 -2
- package/react-native-fula.podspec +47 -47
- package/src/index.tsx +4 -4
- package/src/interfaces/augment-api-errors.ts +449 -449
- package/src/interfaces/augment-api-rpc.ts +617 -617
- package/src/interfaces/augment-api-runtime.ts +224 -224
- package/src/interfaces/augment-api.ts +10 -10
- package/src/interfaces/augment-types.ts +1230 -1230
- package/src/interfaces/fulaNativeModule.ts +1 -0
- package/src/interfaces/index.ts +4 -4
- package/src/interfaces/lookup.ts +1889 -1889
- package/src/interfaces/registry.ts +152 -152
- package/src/interfaces/types-lookup.ts +2043 -2043
- package/src/interfaces/types.ts +3 -3
- package/src/protocols/blockchain.ts +26 -0
- package/src/protocols/chain-api.ts +308 -290
- package/src/protocols/fula.ts +427 -427
- package/src/protocols/fxblox.ts +70 -70
- package/src/types/blockchain.ts +6 -0
- package/src/types/fxblox.ts +13 -13
package/ios/Fula.swift
CHANGED
|
@@ -1,1334 +1,1303 @@
|
|
|
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 registerLifecycleListener() {
|
|
139
|
-
NotificationCenter.default.addObserver(
|
|
140
|
-
self,
|
|
141
|
-
selector: #selector(applicationWillResignActive),
|
|
142
|
-
name: UIApplication.willResignActiveNotification,
|
|
143
|
-
object: nil)
|
|
144
|
-
|
|
145
|
-
NotificationCenter.default.addObserver(
|
|
146
|
-
self,
|
|
147
|
-
selector: #selector(applicationDidEnterBackground),
|
|
148
|
-
name: UIApplication.didEnterBackgroundNotification,
|
|
149
|
-
object: nil)
|
|
150
|
-
|
|
151
|
-
NotificationCenter.default.addObserver(
|
|
152
|
-
self,
|
|
153
|
-
selector: #selector(applicationWillTerminate),
|
|
154
|
-
name: UIApplication.willTerminateNotification,
|
|
155
|
-
object: nil)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
deinit {
|
|
159
|
-
NotificationCenter.default.removeObserver(self)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
@objc func applicationWillResignActive() {
|
|
163
|
-
// Handle app will resign active (similar to onHostPause)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
@objc func applicationDidEnterBackground() {
|
|
167
|
-
// Handle app entered background
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
@objc func applicationWillTerminate() {
|
|
171
|
-
// Attempt to shut down Fula cleanly (similar to onHostDestroy)
|
|
172
|
-
do {
|
|
173
|
-
try shutdownInternal()
|
|
174
|
-
} catch {
|
|
175
|
-
print("Error shutting down Fula: \(error)")
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
@objc(checkConnection:withResolver:withRejecter:)
|
|
181
|
-
func checkConnection(timeout: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
182
|
-
OSLog.viewCycle.info("ReactNative checkConnection started with timeout=\(timeout)")
|
|
183
|
-
|
|
184
|
-
if let timeoutInt = timeout as? Int {
|
|
185
|
-
if fula != nil {
|
|
186
|
-
DispatchQueue.global(qos: .default).async {
|
|
187
|
-
do {
|
|
188
|
-
let connectionStatus = try self.checkConnectionInternal(timeout: timeoutInt)
|
|
189
|
-
OSLog.viewCycle.info("ReactNative checkConnection ended \(connectionStatus)")
|
|
190
|
-
resolve(connectionStatus)
|
|
191
|
-
}
|
|
192
|
-
catch let error {
|
|
193
|
-
OSLog.viewCycle.info("ReactNative checkConnection failed with Error: \(error.localizedDescription)")
|
|
194
|
-
resolve(false)
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
} else {
|
|
198
|
-
OSLog.viewCycle.info("ReactNative checkConnection fula is null")
|
|
199
|
-
resolve(false)
|
|
200
|
-
}
|
|
201
|
-
} else {
|
|
202
|
-
OSLog.viewCycle.error("ReactNative checkConnection - invalid timeout value")
|
|
203
|
-
reject("ERR_INVALID_TIMEOUT", "Invalid timeout value", nil)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
@objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:)
|
|
209
|
-
func newClient(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
210
|
-
print("ReactNative", "newClient storePath= " , storePath , " bloxAddr= " , bloxAddr , " exchange= " , exchange , " autoFlush= " , autoFlush , " useRelay= " , useRelay , " refresh= " , refresh);
|
|
211
|
-
do {
|
|
212
|
-
print("ReactNative", "newClient storePath= ", storePath)
|
|
213
|
-
let identity = toByte(identityString)
|
|
214
|
-
print("ReactNative", "newClient identity= ", identityString)
|
|
215
|
-
try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
|
|
216
|
-
let peerId = fula?.id_()
|
|
217
|
-
resolve(peerId)
|
|
218
|
-
} catch let error {
|
|
219
|
-
print("ReactNative", "newClient failed with Error: ", error.localizedDescription)
|
|
220
|
-
reject("ERR_FULA", "Can't create client", error)
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
@objc(isReady:withResolver:withRejecter:)
|
|
226
|
-
func isReady(filesystemCheck: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
|
|
227
|
-
print("ReactNative", "isReady started")
|
|
228
|
-
var initialized = false
|
|
229
|
-
do {
|
|
230
|
-
if (fula != nil && !fula!.id_().isEmpty) {
|
|
231
|
-
if (filesystemCheck) {
|
|
232
|
-
if (client != nil && rootCid != nil && !rootCid!.isEmpty) {
|
|
233
|
-
initialized = true
|
|
234
|
-
}
|
|
235
|
-
} else {
|
|
236
|
-
initialized = true
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
resolve(initialized)
|
|
240
|
-
} catch let error {
|
|
241
|
-
print("ReactNative", "isReady failed with Error: " + error.localizedDescription)
|
|
242
|
-
reject("ERR_FULA", "Check if fula is ready", error)
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
//TODO: we should consider refactor the name of this \
|
|
247
|
-
// function to be compatible with the android version.
|
|
248
|
-
@objc(initFula:withStorePath:withBloxAddr:withExchange:withAutoFlush:withRootConfig:withUseRelay:withRefresh:withResolver:withRejecter:)
|
|
249
|
-
func initFula(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, rootConfig: String, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
250
|
-
|
|
251
|
-
OSLog.viewCycle.info("ReactNative - init started")
|
|
252
|
-
|
|
253
|
-
do {
|
|
254
|
-
|
|
255
|
-
var resultData = Dictionary<String, String>()
|
|
256
|
-
OSLog.viewCycle.info("ReactNative init storePath= \(storePath)")
|
|
257
|
-
let identity = self.toByte(identityString)
|
|
258
|
-
OSLog.viewCycle.info("ReactNative init identity= \(identityString)")
|
|
259
|
-
let obj = try initInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, _rootCid: rootConfig, useRelay: useRelay, refresh: refresh)
|
|
260
|
-
OSLog.viewCycle.info("ReactNative init object created: [ \(obj[0]), \(obj[1]), \(obj[2]) ]")
|
|
261
|
-
resultData["peerId"] = obj[0]
|
|
262
|
-
resultData["rootCid"] = obj[1]
|
|
263
|
-
resultData["wnfs_key"] = obj[2]
|
|
264
|
-
resolve(resultData as NSDictionary)
|
|
265
|
-
|
|
266
|
-
} catch let error {
|
|
267
|
-
OSLog.viewCycle.info("ReactNative init failed with Error: \(error.localizedDescription)")
|
|
268
|
-
reject("ERR_FULA", "init failed", error)
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
@objc(logout:withStorePath:withResolver:withRejecter:)
|
|
274
|
-
func logout(identityString: String, storePath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
|
|
275
|
-
|
|
276
|
-
print("ReactNative", "logout started")
|
|
277
|
-
do {
|
|
278
|
-
let identity = toByte(identityString)
|
|
279
|
-
try logoutInternal(identity: identity, _storePath: storePath)
|
|
280
|
-
print("ReactNative", "logout completed")
|
|
281
|
-
resolve("")
|
|
282
|
-
|
|
283
|
-
} catch let error {
|
|
284
|
-
print("ReactNative", "logout failed with Error: ", error.localizedDescription)
|
|
285
|
-
reject("ERR_FULA", "logout failed", error)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
func checkConnectionInternal(timeout: Int) throws -> Bool {
|
|
291
|
-
OSLog.viewCycle.info("ReactNative checkConnectionInternal started with timeout: \(timeout)")
|
|
292
|
-
var connectionStatus = false
|
|
293
|
-
|
|
294
|
-
if let fula = self.fula {
|
|
295
|
-
let semaphore = DispatchSemaphore(value: 0)
|
|
296
|
-
let queue = DispatchQueue(label: "com.yourapp.checkConnection", attributes: .concurrent)
|
|
297
|
-
|
|
298
|
-
queue.async {
|
|
299
|
-
do {
|
|
300
|
-
OSLog.viewCycle.info("ReactNative connectToBlox started")
|
|
301
|
-
try fula.connectToBlox()
|
|
302
|
-
connectionStatus = true
|
|
303
|
-
OSLog.viewCycle.info("ReactNative checkConnectionInternal succeeded")
|
|
304
|
-
semaphore.signal()
|
|
305
|
-
} catch let error {
|
|
306
|
-
OSLog.viewCycle.info("ReactNative checkConnectionInternal failed with Error: \(error.localizedDescription)")
|
|
307
|
-
semaphore.signal()
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
let timeoutResult = semaphore.wait(timeout: .now() + .seconds(timeout))
|
|
312
|
-
switch timeoutResult {
|
|
313
|
-
case .timedOut:
|
|
314
|
-
OSLog.viewCycle.info("ReactNative checkConnectionInternal timed out")
|
|
315
|
-
return false
|
|
316
|
-
case .success:
|
|
317
|
-
return connectionStatus
|
|
318
|
-
}
|
|
319
|
-
} else {
|
|
320
|
-
OSLog.viewCycle.info("ReactNative checkConnectionInternal failed because fula is not initialized")
|
|
321
|
-
return false
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
@objc(checkFailedActions:withTimeout:withResolver:withRejecter:)
|
|
326
|
-
func checkFailedActions(retry: Bool, timeout: Int, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
|
|
327
|
-
do {
|
|
328
|
-
guard let fula = fula else {
|
|
329
|
-
throw NSError(domain: "ERR_FULA", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula is not initialized"])
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if !retry {
|
|
333
|
-
OSLog.viewCycle.info("ReactNative checkFailedActions without retry")
|
|
334
|
-
let failedLinks = try fula.listFailedPushes()
|
|
335
|
-
|
|
336
|
-
let nextFailedLink = try failedLinks.next()
|
|
337
|
-
if nextFailedLink != nil {
|
|
338
|
-
// Assuming nextFailedLink is of type Data; replace `toHex()` with an appropriate method to convert Data to a hex string
|
|
339
|
-
OSLog.viewCycle.info("ReactNative checkFailedActions found")
|
|
340
|
-
resolve(true)
|
|
341
|
-
} else {
|
|
342
|
-
resolve(false)
|
|
343
|
-
}
|
|
344
|
-
} else {
|
|
345
|
-
OSLog.viewCycle.info("ReactNative checkFailedActions with retry")
|
|
346
|
-
let retryResults = try retryFailedActionsInternal(timeout: timeout) // Ensure retryFailedActionsInternal accepts a timeout parameter
|
|
347
|
-
resolve(!retryResults)
|
|
348
|
-
}
|
|
349
|
-
} catch let error {
|
|
350
|
-
OSLog.viewCycle.info("ReactNative checkFailedActions failed with Error: \(error.localizedDescription)")
|
|
351
|
-
reject("ERR_FULA", "CheckFailedActions failed", error)
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
func retryFailedActionsInternal(timeout: Int) throws -> Bool {
|
|
358
|
-
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal started")
|
|
359
|
-
|
|
360
|
-
guard let fula = fula else {
|
|
361
|
-
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed because fula is not initialized")
|
|
362
|
-
return false
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
do {
|
|
366
|
-
let connectionCheck = try checkConnectionInternal(timeout: timeout)
|
|
367
|
-
|
|
368
|
-
if connectionCheck {
|
|
369
|
-
do {
|
|
370
|
-
OSLog.viewCycle.info("ReactNative retryFailedPushes started")
|
|
371
|
-
try fula.retryFailedPushes()
|
|
372
|
-
OSLog.viewCycle.info("ReactNative flush started")
|
|
373
|
-
try fula.flush()
|
|
374
|
-
return true
|
|
375
|
-
} catch let error {
|
|
376
|
-
try fula.flush()
|
|
377
|
-
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
|
|
378
|
-
return false
|
|
379
|
-
}
|
|
380
|
-
} else {
|
|
381
|
-
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed because blox is offline")
|
|
382
|
-
return false
|
|
383
|
-
}
|
|
384
|
-
} catch let error {
|
|
385
|
-
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
|
|
386
|
-
return false
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
func createPeerIdentity(privateKey: Data) throws -> Data {
|
|
393
|
-
do {
|
|
394
|
-
// 1: First: create public key from provided private key
|
|
395
|
-
// 2: Should read the local keychain store (if it is key-value, key is public key above,
|
|
396
|
-
// 3: if found, decrypt using the private key
|
|
397
|
-
// 4: If not found or decryption not successful, generate an identity
|
|
398
|
-
// 5: then encrypt and store in keychain
|
|
399
|
-
// TODO: recheck error handling
|
|
400
|
-
var encryptedKey = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
|
|
401
|
-
let secretKey = try Cryptography.generateKey(privateKey)
|
|
402
|
-
if (encryptedKey == nil) {
|
|
403
|
-
var error: NSError?
|
|
404
|
-
let autoGeneratedIdentity = FulamobileGenerateEd25519Key(&error)
|
|
405
|
-
if error != nil {
|
|
406
|
-
throw error!
|
|
407
|
-
}
|
|
408
|
-
encryptedKey = try Cryptography.encryptMsg(autoGeneratedIdentity!.toUint8Array(), secretKey)
|
|
409
|
-
userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey!)
|
|
410
|
-
}
|
|
411
|
-
return try Cryptography.decryptMsg(encryptedKey!, secretKey).toData()
|
|
412
|
-
|
|
413
|
-
} catch let error {
|
|
414
|
-
print("ReactNative", "createPeerIdentity failed with Error: ", error.localizedDescription)
|
|
415
|
-
throw error
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
func createNewrootCid(identity: Data) throws -> Void {
|
|
420
|
-
let hash32 = identity.sha256()
|
|
421
|
-
print("ReactNative", "wnfsKey=" , identity.toHex() , "; hash32 = " , hash32.toHex());
|
|
422
|
-
if (fula != nil) {
|
|
423
|
-
try fula?.flush()
|
|
424
|
-
}
|
|
425
|
-
rootCid = try wnfs?.Init(wnfsKey: hash32)
|
|
426
|
-
print("ReactNative", "privateForest is created: ", rootCid!)
|
|
427
|
-
wnfsKey = identity
|
|
428
|
-
print("ReactNative", "rootCid is created: cid=", rootCid!, " ; wnfs_key=", wnfsKey!.toHex(), "; hash32=", hash32)
|
|
429
|
-
try encryptAndStoreConfig()
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
func loadWnfs(_ _wnfsKey: Data , _ _rootCid: String) throws {
|
|
433
|
-
OSLog.viewCycle.info("ReactNative loadWnfs called: rootCid=\(_rootCid)")
|
|
434
|
-
let hash32 = _wnfsKey.sha256()
|
|
435
|
-
OSLog.viewCycle.info("ReactNative wnfsKey= \(_wnfsKey.toHex()) ; hash32 = \(hash32.toHex())");
|
|
436
|
-
try wnfs?.LoadWithWNFSKey(wnfsKey: hash32, cid: _rootCid)
|
|
437
|
-
rootCid = _rootCid
|
|
438
|
-
wnfsKey = _wnfsKey
|
|
439
|
-
if (fula != nil) {
|
|
440
|
-
try fula?.flush()
|
|
441
|
-
}
|
|
442
|
-
OSLog.viewCycle.info("ReactNative loadWnfs completed")
|
|
443
|
-
try encryptAndStoreConfig()
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
func encryptAndStoreConfig() throws {
|
|
448
|
-
do {
|
|
449
|
-
if let identityEncryptedGlobalUnwrapped = identityEncryptedGlobal {
|
|
450
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig started")
|
|
451
|
-
|
|
452
|
-
if let rootCidUnwrapped = rootCid, let wnfsKeyUnwrapped = wnfsKey, let secretKeyGlobalUnwrapped = secretKeyGlobal {
|
|
453
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig started with rootCid: \(rootCidUnwrapped.toUint8Array()) and wnfsKey:\(wnfsKeyUnwrapped)")
|
|
454
|
-
|
|
455
|
-
let cid_encrypted = try Cryptography.encryptMsg(rootCidUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
|
|
456
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig cid_encrypted: \(cid_encrypted)")
|
|
457
|
-
|
|
458
|
-
let wnfs_key_encrypted = try Cryptography.encryptMsg(wnfsKeyUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
|
|
459
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig wnfs_key_encrypted: \(wnfs_key_encrypted)")
|
|
460
|
-
|
|
461
|
-
userDataHelper.add("cid_encrypted_" + identityEncryptedGlobalUnwrapped, cid_encrypted)
|
|
462
|
-
userDataHelper.add("wnfs_key_encrypted_" + identityEncryptedGlobalUnwrapped, wnfs_key_encrypted)
|
|
463
|
-
} else {
|
|
464
|
-
// Handle the case where rootCid, wnfsKey, or secretKeyGlobal is nil
|
|
465
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig failed because one of the values is nil")
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
} catch let error {
|
|
469
|
-
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig failed with Error: \(error.localizedDescription)")
|
|
470
|
-
throw error
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
func logoutInternal(identity: Data, _storePath: String?) throws {
|
|
475
|
-
do {
|
|
476
|
-
if (fula != nil) {
|
|
477
|
-
try fula?.flush()
|
|
478
|
-
}
|
|
479
|
-
let secretKey = try Cryptography.generateKey(identity)
|
|
480
|
-
let identity_encrypted: String = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
|
|
481
|
-
userDataHelper.remove("cid_encrypted_"+identity_encrypted)
|
|
482
|
-
|
|
483
|
-
//TODO: Should also remove peerid @Mahdi
|
|
484
|
-
|
|
485
|
-
userDataHelper.remove("cid_encrypted_"+identity_encrypted)
|
|
486
|
-
|
|
487
|
-
rootCid = nil
|
|
488
|
-
secretKeyGlobal = nil
|
|
489
|
-
identityEncryptedGlobal = nil
|
|
490
|
-
var storePath = _storePath
|
|
491
|
-
if (storePath == nil || storePath!.isEmpty) {
|
|
492
|
-
storePath = fulaStorePath
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
do{
|
|
496
|
-
try FileManager.default.removeItem(atPath: fulaStorePath)
|
|
497
|
-
}catch let error{
|
|
498
|
-
print("Deleting fula store path", error.localizedDescription)
|
|
499
|
-
throw error
|
|
500
|
-
}
|
|
501
|
-
} catch let error {
|
|
502
|
-
print("ReactNative", "logout internal failed with Error: ", error.localizedDescription)
|
|
503
|
-
throw error
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
func getFulaClient() -> FulamobileClient? {
|
|
508
|
-
return fula
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
func newClientInternal(identity: Data, storePath: String?, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool) throws -> Data {
|
|
512
|
-
do {
|
|
513
|
-
fulaConfig = FulamobileConfig()
|
|
514
|
-
if (storePath == nil || storePath!.isEmpty) {
|
|
515
|
-
fulaConfig!.storePath = fulaStorePath
|
|
516
|
-
} else {
|
|
517
|
-
fulaConfig!.storePath = storePath!
|
|
518
|
-
}
|
|
519
|
-
print("ReactNative", "storePath is set: " + fulaConfig!.storePath)
|
|
520
|
-
|
|
521
|
-
let peerIdentity = try createPeerIdentity(privateKey: identity)
|
|
522
|
-
fulaConfig!.identity = peerIdentity
|
|
523
|
-
print("ReactNative", "peerIdentity is set: " + fulaConfig!.identity!.toHex())
|
|
524
|
-
fulaConfig!.bloxAddr = bloxAddr
|
|
525
|
-
print("ReactNative", "bloxAddr is set: " + fulaConfig!.bloxAddr)
|
|
526
|
-
fulaConfig!.exchange = exchange
|
|
527
|
-
fulaConfig!.syncWrites = autoFlush
|
|
528
|
-
if (useRelay) {
|
|
529
|
-
fulaConfig!.allowTransientConnection = true
|
|
530
|
-
fulaConfig!.forceReachabilityPrivate = true
|
|
531
|
-
}
|
|
532
|
-
if (fula == nil || refresh) {
|
|
533
|
-
print("ReactNative", "Creating a new Fula instance");
|
|
534
|
-
do {
|
|
535
|
-
try shutdownInternal();
|
|
536
|
-
fula = FulamobileClient(fulaConfig)
|
|
537
|
-
if (fula != nil) {
|
|
538
|
-
try fula?.flush();
|
|
539
|
-
}
|
|
540
|
-
} catch let error {
|
|
541
|
-
print("ReactNative", "Failed to create new Fula instance: " , error.localizedDescription);
|
|
542
|
-
throw MyError.runtimeError("Failed to create new Fula instance")
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
return peerIdentity
|
|
546
|
-
} catch let error {
|
|
547
|
-
print("ReactNative", "newclientInternal failed with Error: ", error.localizedDescription)
|
|
548
|
-
throw error
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
func initInternal(identity: Data, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, _rootCid: String, useRelay: Bool, refresh: Bool) throws -> [String] {
|
|
553
|
-
|
|
554
|
-
do {
|
|
555
|
-
if (fula == nil || refresh) {
|
|
556
|
-
try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
|
|
557
|
-
OSLog.viewCycle.info("ReactNative fula initialized: \(self.fula!.id_())")
|
|
558
|
-
}
|
|
559
|
-
if(client == nil || refresh) {
|
|
560
|
-
client = Client(clientInput: fula!)
|
|
561
|
-
wnfs = Wnfs(putFn: { cid, data in
|
|
562
|
-
guard let c = self.client else {
|
|
563
|
-
throw MyError.runtimeError("wnfs: fula client not ready")
|
|
564
|
-
}
|
|
565
|
-
try c.put(cid, data)
|
|
566
|
-
}, getFn: { cid in
|
|
567
|
-
guard let c = self.client else {
|
|
568
|
-
throw MyError.runtimeError("wnfs: fula client not ready")
|
|
569
|
-
}
|
|
570
|
-
return try c.get(cid)
|
|
571
|
-
})
|
|
572
|
-
OSLog.viewCycle.info("ReactNative wnfs initialized")
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
let secretKey = try Cryptography.generateKey(identity)
|
|
576
|
-
let identity_encrypted = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
|
|
577
|
-
identityEncryptedGlobal = identity_encrypted
|
|
578
|
-
secretKeyGlobal = secretKey
|
|
579
|
-
|
|
580
|
-
if (rootCid == nil || rootCid!.isEmpty) {
|
|
581
|
-
OSLog.viewCycle.info("ReactNative rootCid is empty.")
|
|
582
|
-
//Load from keystore
|
|
583
|
-
|
|
584
|
-
let cid_encrypted_fetched = userDataHelper.getValue("cid_encrypted_"+identity_encrypted)
|
|
585
|
-
OSLog.viewCycle.info("ReactNative Here1")
|
|
586
|
-
var cid: Array<UInt8>? = nil
|
|
587
|
-
if(cid_encrypted_fetched != nil && !cid_encrypted_fetched!.isEmpty) {
|
|
588
|
-
OSLog.viewCycle.info("ReactNative decrypting cid= \(cid_encrypted_fetched!) with secret \(secretKey.toHex())")
|
|
589
|
-
cid = try Cryptography.decryptMsg(cid_encrypted_fetched!, secretKey)
|
|
590
|
-
}
|
|
591
|
-
print("ReactNative", "Here2")
|
|
592
|
-
//print("ReactNative", "Attempted to fetch cid from keystore cid="+cid+" & wnfs_key="+wnfs_key)
|
|
593
|
-
if(cid == nil || cid!.isEmpty){
|
|
594
|
-
OSLog.viewCycle.info("ReactNative cid or wnfs key was not found")
|
|
595
|
-
if(!_rootCid.isEmpty){
|
|
596
|
-
OSLog.viewCycle.info("ReactNative Re-setting cid from input: \(_rootCid)")
|
|
597
|
-
cid = _rootCid.toUint8Array()
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
}
|
|
601
|
-
if(cid == nil || cid!.isEmpty){
|
|
602
|
-
OSLog.viewCycle.info("ReactNative Tried to recover cid but was not successful. Creating ones")
|
|
603
|
-
try createNewrootCid(identity: identity)
|
|
604
|
-
} else {
|
|
605
|
-
OSLog.viewCycle.info("ReactNative Found cid and wnfs key in keychain store")
|
|
606
|
-
OSLog.viewCycle.info("ReactNative Recovered cid and private ref from keychain store. cid=\(cid!) & wnfs_key=\(identity)")
|
|
607
|
-
try loadWnfs(identity, cid!.toData().toUTF8String()!)
|
|
608
|
-
}
|
|
609
|
-
OSLog.viewCycle.info("ReactNative creating/reloading rootCid completed")
|
|
610
|
-
|
|
611
|
-
/*
|
|
612
|
-
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")
|
|
613
|
-
long testcodec = 85
|
|
614
|
-
byte[] testputcid = client!.put(testbyte, testcodec)
|
|
615
|
-
print("ReactNative", "client!.put test done"+ Arrays.toString(testputcid))
|
|
616
|
-
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")
|
|
617
|
-
byte[] testfetchedbytes = client!.get(testfetchedcid)
|
|
618
|
-
print("ReactNative", "client!.get test done"+ Arrays.toString(testfetchedbytes))
|
|
619
|
-
*/
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
OSLog.viewCycle.info("ReactNative rootCid is created: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
|
|
623
|
-
} else {
|
|
624
|
-
OSLog.viewCycle.info("ReactNative rootCid existed: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
|
|
625
|
-
}
|
|
626
|
-
let peerId = fula!.id_()
|
|
627
|
-
var obj = [String]()
|
|
628
|
-
obj.append(peerId)
|
|
629
|
-
obj.append(rootCid!)
|
|
630
|
-
obj.append(wnfsKey!.toHex())
|
|
631
|
-
OSLog.viewCycle.info("ReactNative initInternal is completed successfully")
|
|
632
|
-
if (fula != nil) {
|
|
633
|
-
try fula?.flush()
|
|
634
|
-
}
|
|
635
|
-
return obj
|
|
636
|
-
} catch let error {
|
|
637
|
-
OSLog.viewCycle.info("ReactNative init internal failed with Error: \(error.localizedDescription)")
|
|
638
|
-
throw error
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
@objc(mkdir:withResolver:withRejecter:)
|
|
644
|
-
func mkdir(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
645
|
-
print("ReactNative", "mkdir: path = " + path)
|
|
646
|
-
do {
|
|
647
|
-
let cid = try wnfs?.MkDir(cid: rootCid!, remotePath: path)
|
|
648
|
-
if(cid != nil) {
|
|
649
|
-
rootCid = cid
|
|
650
|
-
try encryptAndStoreConfig()
|
|
651
|
-
if (fula != nil) {
|
|
652
|
-
try fula?.flush()
|
|
653
|
-
}
|
|
654
|
-
resolve(rootCid)
|
|
655
|
-
} else {
|
|
656
|
-
print("ReactNative", "mkdir Error: config is nil")
|
|
657
|
-
reject("ERR_WNFS", "Can't make dir", nil)
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
} catch let error{
|
|
661
|
-
print("mkdir", error.localizedDescription)
|
|
662
|
-
reject("ERR_WNFS", "mkdir", error)
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
@objc(writeFile:withLocalFilename:withResolver:withRejecter:)
|
|
667
|
-
func writeFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
668
|
-
/*
|
|
669
|
-
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
670
|
-
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
671
|
-
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
672
|
-
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
673
|
-
// Returns: new cid of the root after this file is placed in the tree
|
|
674
|
-
*/
|
|
675
|
-
print("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename)
|
|
676
|
-
do {
|
|
677
|
-
let cid = try wnfs?.WriteFileFromPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
|
|
678
|
-
if(cid != nil) {
|
|
679
|
-
rootCid = cid
|
|
680
|
-
try encryptAndStoreConfig()
|
|
681
|
-
if (fula != nil) {
|
|
682
|
-
try fula?.flush()
|
|
683
|
-
}
|
|
684
|
-
resolve(rootCid)
|
|
685
|
-
} else {
|
|
686
|
-
print("ReactNative", "writeFile Error: config is nil")
|
|
687
|
-
reject("ERR_WNFS", "writeFile Error: config is nil", nil)
|
|
688
|
-
}
|
|
689
|
-
} catch let error {
|
|
690
|
-
print("writeFile", error.localizedDescription)
|
|
691
|
-
reject("ERR_WNFS", "writeFile", error)
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
@objc(writeFileContent:withContentString:withResolver:withRejecter:)
|
|
696
|
-
func writeFileContent(path: String, contentString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
697
|
-
|
|
698
|
-
print("ReactNative", "writeFile: contentString = " + contentString)
|
|
699
|
-
print("ReactNative", "writeFile: path = " + path)
|
|
700
|
-
do {
|
|
701
|
-
let content = convertStringToByte(contentString)
|
|
702
|
-
let cid = try wnfs?.WriteFile(cid: rootCid!, remotePath: path, data: content.toData())
|
|
703
|
-
rootCid = cid
|
|
704
|
-
try encryptAndStoreConfig()
|
|
705
|
-
if (fula != nil) {
|
|
706
|
-
try fula?.flush()
|
|
707
|
-
}
|
|
708
|
-
resolve(rootCid)
|
|
709
|
-
} catch let error {
|
|
710
|
-
print("writeFileContent", error.localizedDescription)
|
|
711
|
-
reject("ERR_WNFS", "writeFileContent", error)
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
@objc(ls:withResolver:withRejecter:)
|
|
717
|
-
func ls(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
718
|
-
print("ReactNative", "ls: path = " + path)
|
|
719
|
-
do {
|
|
720
|
-
let res = try wnfs?.Ls(cid: rootCid!, remotePath: path)
|
|
721
|
-
|
|
722
|
-
//JSONArray jsonArray = new JSONArray(res)
|
|
723
|
-
guard let s = res?.toUTF8String() else {
|
|
724
|
-
throw MyError.runtimeError("converting bytes to utf8 string")
|
|
725
|
-
}
|
|
726
|
-
print("ReactNative", "ls: res = " + s)
|
|
727
|
-
resolve(s)
|
|
728
|
-
} catch let error {
|
|
729
|
-
print("ls", error.localizedDescription)
|
|
730
|
-
reject("ERR_WNFS", "ls", error)
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
@objc(rm:withResolver:withRejecter:)
|
|
736
|
-
func rm(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
737
|
-
|
|
738
|
-
print("ReactNative", "rm: path = " + path)
|
|
739
|
-
do {
|
|
740
|
-
let cid = try wnfs?.Rm(cid: rootCid!, remotePath: path)
|
|
741
|
-
if(cid != nil) {
|
|
742
|
-
rootCid = cid
|
|
743
|
-
try encryptAndStoreConfig()
|
|
744
|
-
if (fula != nil) {
|
|
745
|
-
try fula?.flush()
|
|
746
|
-
}
|
|
747
|
-
resolve(rootCid)
|
|
748
|
-
} else {
|
|
749
|
-
print("ReactNative", "rm Error: config is nil")
|
|
750
|
-
reject("ERR_WNFS", "rm Error: config is nil", nil)
|
|
751
|
-
}
|
|
752
|
-
} catch let error {
|
|
753
|
-
print("rm", error.localizedDescription)
|
|
754
|
-
reject("ERR_WNFS", "rm", error)
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
@objc(cp:withTargetPath:withResolver:withRejecter:)
|
|
760
|
-
func cp(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
761
|
-
|
|
762
|
-
print("ReactNative", "rm: sourcePath = " + sourcePath)
|
|
763
|
-
do {
|
|
764
|
-
let cid = try wnfs?.Cp(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
|
|
765
|
-
if(cid != nil) {
|
|
766
|
-
rootCid = cid
|
|
767
|
-
try encryptAndStoreConfig()
|
|
768
|
-
if (fula != nil) {
|
|
769
|
-
try fula?.flush()
|
|
770
|
-
}
|
|
771
|
-
resolve(rootCid)
|
|
772
|
-
} else {
|
|
773
|
-
print("ReactNative", "cp Error: config is nil")
|
|
774
|
-
reject("ERR_WNFS", "cp Error: config is nil", nil)
|
|
775
|
-
}
|
|
776
|
-
} catch let error {
|
|
777
|
-
print("cp", error.localizedDescription)
|
|
778
|
-
reject("ERR_WNFS", "cp", error)
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
@objc(mv:withTargetPath:withResolver:withRejecter:)
|
|
784
|
-
func mv(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
785
|
-
print("ReactNative", "rm: sourcePath = " + sourcePath)
|
|
786
|
-
do {
|
|
787
|
-
let cid = try wnfs?.Mv(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
|
|
788
|
-
if(cid != nil) {
|
|
789
|
-
rootCid = cid
|
|
790
|
-
try encryptAndStoreConfig()
|
|
791
|
-
if (fula != nil) {
|
|
792
|
-
try fula?.flush()
|
|
793
|
-
}
|
|
794
|
-
resolve(rootCid)
|
|
795
|
-
} else {
|
|
796
|
-
print("ReactNative", "mv Error: config is nil")
|
|
797
|
-
reject("ERR_WNFS", "mv Error: config is nil", nil)
|
|
798
|
-
}
|
|
799
|
-
} catch let error {
|
|
800
|
-
print("mv", error.localizedDescription)
|
|
801
|
-
reject("ERR_WNFS", "mv", error)
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
@objc(readFile:withLocalFilename:withResolver:withRejecter:)
|
|
807
|
-
func readFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
808
|
-
/*
|
|
809
|
-
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
810
|
-
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
811
|
-
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
812
|
-
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
813
|
-
// Returns: new cid of the root after this file is placed in the tree
|
|
814
|
-
*/
|
|
815
|
-
print("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename)
|
|
816
|
-
do {
|
|
817
|
-
let path = try wnfs?.ReadFileToPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
|
|
818
|
-
resolve(path)
|
|
819
|
-
} catch let error {
|
|
820
|
-
print("readFile", error.localizedDescription)
|
|
821
|
-
reject("ERR_WNFS", "readFile", error)
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
@objc(readFileContent:withResolver:withRejecter:)
|
|
827
|
-
func readFileContent(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
828
|
-
|
|
829
|
-
print("ReactNative", "readFileContent: path = " + path)
|
|
830
|
-
do {
|
|
831
|
-
// FIXME: dhouldn't we output an NSData object instead?
|
|
832
|
-
let res = try wnfs?.ReadFile(cid: rootCid!, remotePath: path)
|
|
833
|
-
guard let resString = res?.toUTF8String() else{
|
|
834
|
-
throw MyError.runtimeError("converting bytes to utf8 string")
|
|
835
|
-
}
|
|
836
|
-
resolve(resString)
|
|
837
|
-
} catch let error {
|
|
838
|
-
print("readFileContent", error.localizedDescription)
|
|
839
|
-
reject("ERR_WNFS", "readFileContent", error)
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
@objc(get:withResolver:withRejecter:)
|
|
845
|
-
func get(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
846
|
-
|
|
847
|
-
print("ReactNative", "get: keyString = " + keyString)
|
|
848
|
-
do {
|
|
849
|
-
let key: Data = convertStringToByte(keyString).toData()
|
|
850
|
-
let value = try getInternal(key)
|
|
851
|
-
let valueString: String = value.toUTF8String()!
|
|
852
|
-
resolve(valueString)
|
|
853
|
-
} catch let error {
|
|
854
|
-
print("get", error.localizedDescription)
|
|
855
|
-
reject("ERR_FULA", "get", error)
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
func getInternal(_ key: Data) throws -> Data {
|
|
861
|
-
do {
|
|
862
|
-
print("ReactNative", "getInternal: key.toUTF8String() = " , key.toUTF8String()!)
|
|
863
|
-
print("ReactNative", "getInternal: key.toHex().bytes = " , key.toHex())
|
|
864
|
-
let value = try fula!.get(key)
|
|
865
|
-
print("ReactNative", "getInternal: value.toHex() = " , value.toHex())
|
|
866
|
-
return value
|
|
867
|
-
} catch let error {
|
|
868
|
-
print("ReactNative", "getInternal: error = " + error.localizedDescription)
|
|
869
|
-
print("getInternal", error.localizedDescription)
|
|
870
|
-
throw error
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
@objc(has:withResolver:withRejecter:)
|
|
875
|
-
func has(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
876
|
-
|
|
877
|
-
print("ReactNative", "has: keyString = " + keyString)
|
|
878
|
-
do {
|
|
879
|
-
let key: Data = convertStringToByte(keyString).toData()
|
|
880
|
-
let result = try hasInternal(key)
|
|
881
|
-
resolve(result)
|
|
882
|
-
} catch let error {
|
|
883
|
-
print("has", error.localizedDescription)
|
|
884
|
-
reject("ERR_FULA", "has", error)
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
func hasInternal(_ key: Data) throws -> Bool {
|
|
890
|
-
do {
|
|
891
|
-
let ret = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
|
|
892
|
-
try fula?.has(key, ret0_: ret)
|
|
893
|
-
let res = ret.pointee.boolValue
|
|
894
|
-
ret.deallocate()
|
|
895
|
-
return res
|
|
896
|
-
} catch let error {
|
|
897
|
-
print("hasInternal", error.localizedDescription)
|
|
898
|
-
throw error
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
func pullInternal(key: Data) throws -> Void {
|
|
903
|
-
do {
|
|
904
|
-
try fula!.pull(key)
|
|
905
|
-
} catch let error {
|
|
906
|
-
print("pullInternal", error.localizedDescription)
|
|
907
|
-
throw error
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
@objc(push:withRejecter:)
|
|
912
|
-
func push(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
913
|
-
print("ReactNative", "push started")
|
|
914
|
-
do {
|
|
915
|
-
try pushInternal(key: convertStringToByte(rootCid!).toData())
|
|
916
|
-
resolve(rootCid)
|
|
917
|
-
} catch let error {
|
|
918
|
-
print("get", error.localizedDescription)
|
|
919
|
-
reject("ERR_FULA", "push", error)
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
func pushInternal(key: Data) throws -> Void {
|
|
924
|
-
do {
|
|
925
|
-
let hasIt = try hasInternal(key)
|
|
926
|
-
if (fula != nil && hasIt) {
|
|
927
|
-
try fula?.push(key)
|
|
928
|
-
try fula?.flush()
|
|
929
|
-
} else {
|
|
930
|
-
print("ReactNative", "pushInternal error: key wasn't found or fula is not initialized")
|
|
931
|
-
throw MyError.runtimeError("pushInternal error: key wasn't found or fula is not initialized")
|
|
932
|
-
}
|
|
933
|
-
} catch let error {
|
|
934
|
-
print("ReactNative", "pushInternal", error.localizedDescription)
|
|
935
|
-
throw error
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
// FIXME: unused codecString arg
|
|
940
|
-
@objc(put:withCodecString:withResolver:withRejecter:)
|
|
941
|
-
func put(valueString: String, codecString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
942
|
-
|
|
943
|
-
print("ReactNative", "put: codecString = " + codecString)
|
|
944
|
-
print("ReactNative", "put: valueString = " + valueString)
|
|
945
|
-
do {
|
|
946
|
-
//byte[] codec = convertStringToByte(CodecString)
|
|
947
|
-
let codec = FulaModule.CODEC_DAG_CBOR
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
print("ReactNative", "put: codec = ", codec)
|
|
951
|
-
let value = valueString.toData()
|
|
952
|
-
|
|
953
|
-
print("ReactNative", "put: value.toHex() = " , value.toHex())
|
|
954
|
-
let key = try putInternal(value: value, codec: codec)
|
|
955
|
-
print("ReactNative", "put: key.toHex() = " , key.toUTF8String()!)
|
|
956
|
-
resolve(key.toUTF8String()!)
|
|
957
|
-
} catch let error {
|
|
958
|
-
print("ReactNative", "put: error = ", error.localizedDescription)
|
|
959
|
-
reject("ERR_FULA", "put", error)
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
// FIXME: unused codec arg
|
|
965
|
-
func putInternal(value: Data, codec: Int) throws -> Data {
|
|
966
|
-
do {
|
|
967
|
-
if(fula != nil) {
|
|
968
|
-
let key: Data = try fula!.put(value, codec: Int64(FulaModule.CODEC_DAG_CBOR))
|
|
969
|
-
try fula?.flush()
|
|
970
|
-
return key
|
|
971
|
-
} else {
|
|
972
|
-
print("ReactNative", "putInternal Error: fula is not initialized")
|
|
973
|
-
throw MyError.runtimeError("putInternal Error: fula is not initialized")
|
|
974
|
-
}
|
|
975
|
-
} catch let error {
|
|
976
|
-
print("ReactNative", "putInternal", error.localizedDescription)
|
|
977
|
-
throw error
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
@objc(setAuth:withAllow:withResolver:withRejecter:)
|
|
982
|
-
func setAuth(peerIdString: String, allow: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
983
|
-
|
|
984
|
-
print("ReactNative", "setAuth: peerIdString = " + peerIdString)
|
|
985
|
-
do {
|
|
986
|
-
if (fula != nil && !(fula?.id_().isEmpty)! && fulaConfig != nil && !fulaConfig!.bloxAddr.isEmpty) {
|
|
987
|
-
let bloxAddr = fulaConfig!.bloxAddr
|
|
988
|
-
print("ReactNative", "setAuth: bloxAddr = '",bloxAddr,"'"," peerIdString = '",peerIdString,"'")
|
|
989
|
-
let parts = bloxAddr.split(separator: "/").map(String.init)
|
|
990
|
-
try fula?.setAuth(parts.last, subject: peerIdString, allow: allow)
|
|
991
|
-
resolve(true)
|
|
992
|
-
} else {
|
|
993
|
-
print("ReactNative", "setAuth error: fula is not initialized")
|
|
994
|
-
throw MyError.runtimeError("fula is not initialized")
|
|
995
|
-
}
|
|
996
|
-
resolve(false)
|
|
997
|
-
} catch let error {
|
|
998
|
-
print("get", error.localizedDescription)
|
|
999
|
-
reject("ERR_FULA", "setAuth", error)
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
@objc(
|
|
1005
|
-
func
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
let
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
let
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
let
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
let
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
let
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
let
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
reject("ERR_FULA", "eraseBlData: \(error.localizedDescription)", error)
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
@objc(reboot:withRejecter:)
|
|
1309
|
-
func reboot(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
|
1310
|
-
do {
|
|
1311
|
-
let result = try fula!.reboot()
|
|
1312
|
-
let resultString = result.toUTF8String()!
|
|
1313
|
-
resolve(resultString)
|
|
1314
|
-
} catch let error {
|
|
1315
|
-
print("reboot", error.localizedDescription)
|
|
1316
|
-
reject("ERR_FULA", "reboot", error)
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
|
|
1320
|
-
@objc(wifiRemoveall:withRejecter:)
|
|
1321
|
-
func wifiRemoveall(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
|
1322
|
-
do {
|
|
1323
|
-
let result = try fula!.wifiRemoveall()
|
|
1324
|
-
let resultString = result.toUTF8String()!
|
|
1325
|
-
resolve(resultString)
|
|
1326
|
-
} catch let error {
|
|
1327
|
-
print("wifiRemoveall", error.localizedDescription)
|
|
1328
|
-
reject("ERR_FULA", "wifiRemoveall", error)
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
|
|
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 registerLifecycleListener() {
|
|
139
|
+
NotificationCenter.default.addObserver(
|
|
140
|
+
self,
|
|
141
|
+
selector: #selector(applicationWillResignActive),
|
|
142
|
+
name: UIApplication.willResignActiveNotification,
|
|
143
|
+
object: nil)
|
|
144
|
+
|
|
145
|
+
NotificationCenter.default.addObserver(
|
|
146
|
+
self,
|
|
147
|
+
selector: #selector(applicationDidEnterBackground),
|
|
148
|
+
name: UIApplication.didEnterBackgroundNotification,
|
|
149
|
+
object: nil)
|
|
150
|
+
|
|
151
|
+
NotificationCenter.default.addObserver(
|
|
152
|
+
self,
|
|
153
|
+
selector: #selector(applicationWillTerminate),
|
|
154
|
+
name: UIApplication.willTerminateNotification,
|
|
155
|
+
object: nil)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
deinit {
|
|
159
|
+
NotificationCenter.default.removeObserver(self)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@objc func applicationWillResignActive() {
|
|
163
|
+
// Handle app will resign active (similar to onHostPause)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@objc func applicationDidEnterBackground() {
|
|
167
|
+
// Handle app entered background
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
@objc func applicationWillTerminate() {
|
|
171
|
+
// Attempt to shut down Fula cleanly (similar to onHostDestroy)
|
|
172
|
+
do {
|
|
173
|
+
try shutdownInternal()
|
|
174
|
+
} catch {
|
|
175
|
+
print("Error shutting down Fula: \(error)")
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@objc(checkConnection:withResolver:withRejecter:)
|
|
181
|
+
func checkConnection(timeout: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
182
|
+
OSLog.viewCycle.info("ReactNative checkConnection started with timeout=\(timeout)")
|
|
183
|
+
|
|
184
|
+
if let timeoutInt = timeout as? Int {
|
|
185
|
+
if fula != nil {
|
|
186
|
+
DispatchQueue.global(qos: .default).async {
|
|
187
|
+
do {
|
|
188
|
+
let connectionStatus = try self.checkConnectionInternal(timeout: timeoutInt)
|
|
189
|
+
OSLog.viewCycle.info("ReactNative checkConnection ended \(connectionStatus)")
|
|
190
|
+
resolve(connectionStatus)
|
|
191
|
+
}
|
|
192
|
+
catch let error {
|
|
193
|
+
OSLog.viewCycle.info("ReactNative checkConnection failed with Error: \(error.localizedDescription)")
|
|
194
|
+
resolve(false)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
OSLog.viewCycle.info("ReactNative checkConnection fula is null")
|
|
199
|
+
resolve(false)
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
OSLog.viewCycle.error("ReactNative checkConnection - invalid timeout value")
|
|
203
|
+
reject("ERR_INVALID_TIMEOUT", "Invalid timeout value", nil)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
@objc(newClient:withStorePath:withBloxAddr:withExchange:withAutoFlush:withUseRelay:withRefresh:withResolver:withRejecter:)
|
|
209
|
+
func newClient(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
210
|
+
print("ReactNative", "newClient storePath= " , storePath , " bloxAddr= " , bloxAddr , " exchange= " , exchange , " autoFlush= " , autoFlush , " useRelay= " , useRelay , " refresh= " , refresh);
|
|
211
|
+
do {
|
|
212
|
+
print("ReactNative", "newClient storePath= ", storePath)
|
|
213
|
+
let identity = toByte(identityString)
|
|
214
|
+
print("ReactNative", "newClient identity= ", identityString)
|
|
215
|
+
try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
|
|
216
|
+
let peerId = fula?.id_()
|
|
217
|
+
resolve(peerId)
|
|
218
|
+
} catch let error {
|
|
219
|
+
print("ReactNative", "newClient failed with Error: ", error.localizedDescription)
|
|
220
|
+
reject("ERR_FULA", "Can't create client", error)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
@objc(isReady:withResolver:withRejecter:)
|
|
226
|
+
func isReady(filesystemCheck: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
|
|
227
|
+
print("ReactNative", "isReady started")
|
|
228
|
+
var initialized = false
|
|
229
|
+
do {
|
|
230
|
+
if (fula != nil && !fula!.id_().isEmpty) {
|
|
231
|
+
if (filesystemCheck) {
|
|
232
|
+
if (client != nil && rootCid != nil && !rootCid!.isEmpty) {
|
|
233
|
+
initialized = true
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
initialized = true
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
resolve(initialized)
|
|
240
|
+
} catch let error {
|
|
241
|
+
print("ReactNative", "isReady failed with Error: " + error.localizedDescription)
|
|
242
|
+
reject("ERR_FULA", "Check if fula is ready", error)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
//TODO: we should consider refactor the name of this \
|
|
247
|
+
// function to be compatible with the android version.
|
|
248
|
+
@objc(initFula:withStorePath:withBloxAddr:withExchange:withAutoFlush:withRootConfig:withUseRelay:withRefresh:withResolver:withRejecter:)
|
|
249
|
+
func initFula(identityString: String, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, rootConfig: String, useRelay: Bool, refresh: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
250
|
+
|
|
251
|
+
OSLog.viewCycle.info("ReactNative - init started")
|
|
252
|
+
|
|
253
|
+
do {
|
|
254
|
+
|
|
255
|
+
var resultData = Dictionary<String, String>()
|
|
256
|
+
OSLog.viewCycle.info("ReactNative init storePath= \(storePath)")
|
|
257
|
+
let identity = self.toByte(identityString)
|
|
258
|
+
OSLog.viewCycle.info("ReactNative init identity= \(identityString)")
|
|
259
|
+
let obj = try initInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, _rootCid: rootConfig, useRelay: useRelay, refresh: refresh)
|
|
260
|
+
OSLog.viewCycle.info("ReactNative init object created: [ \(obj[0]), \(obj[1]), \(obj[2]) ]")
|
|
261
|
+
resultData["peerId"] = obj[0]
|
|
262
|
+
resultData["rootCid"] = obj[1]
|
|
263
|
+
resultData["wnfs_key"] = obj[2]
|
|
264
|
+
resolve(resultData as NSDictionary)
|
|
265
|
+
|
|
266
|
+
} catch let error {
|
|
267
|
+
OSLog.viewCycle.info("ReactNative init failed with Error: \(error.localizedDescription)")
|
|
268
|
+
reject("ERR_FULA", "init failed", error)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
@objc(logout:withStorePath:withResolver:withRejecter:)
|
|
274
|
+
func logout(identityString: String, storePath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void{
|
|
275
|
+
|
|
276
|
+
print("ReactNative", "logout started")
|
|
277
|
+
do {
|
|
278
|
+
let identity = toByte(identityString)
|
|
279
|
+
try logoutInternal(identity: identity, _storePath: storePath)
|
|
280
|
+
print("ReactNative", "logout completed")
|
|
281
|
+
resolve("")
|
|
282
|
+
|
|
283
|
+
} catch let error {
|
|
284
|
+
print("ReactNative", "logout failed with Error: ", error.localizedDescription)
|
|
285
|
+
reject("ERR_FULA", "logout failed", error)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
func checkConnectionInternal(timeout: Int) throws -> Bool {
|
|
291
|
+
OSLog.viewCycle.info("ReactNative checkConnectionInternal started with timeout: \(timeout)")
|
|
292
|
+
var connectionStatus = false
|
|
293
|
+
|
|
294
|
+
if let fula = self.fula {
|
|
295
|
+
let semaphore = DispatchSemaphore(value: 0)
|
|
296
|
+
let queue = DispatchQueue(label: "com.yourapp.checkConnection", attributes: .concurrent)
|
|
297
|
+
|
|
298
|
+
queue.async {
|
|
299
|
+
do {
|
|
300
|
+
OSLog.viewCycle.info("ReactNative connectToBlox started")
|
|
301
|
+
try fula.connectToBlox()
|
|
302
|
+
connectionStatus = true
|
|
303
|
+
OSLog.viewCycle.info("ReactNative checkConnectionInternal succeeded")
|
|
304
|
+
semaphore.signal()
|
|
305
|
+
} catch let error {
|
|
306
|
+
OSLog.viewCycle.info("ReactNative checkConnectionInternal failed with Error: \(error.localizedDescription)")
|
|
307
|
+
semaphore.signal()
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
let timeoutResult = semaphore.wait(timeout: .now() + .seconds(timeout))
|
|
312
|
+
switch timeoutResult {
|
|
313
|
+
case .timedOut:
|
|
314
|
+
OSLog.viewCycle.info("ReactNative checkConnectionInternal timed out")
|
|
315
|
+
return false
|
|
316
|
+
case .success:
|
|
317
|
+
return connectionStatus
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
OSLog.viewCycle.info("ReactNative checkConnectionInternal failed because fula is not initialized")
|
|
321
|
+
return false
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
@objc(checkFailedActions:withTimeout:withResolver:withRejecter:)
|
|
326
|
+
func checkFailedActions(retry: Bool, timeout: Int, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
|
|
327
|
+
do {
|
|
328
|
+
guard let fula = fula else {
|
|
329
|
+
throw NSError(domain: "ERR_FULA", code: 1001, userInfo: [NSLocalizedDescriptionKey: "Fula is not initialized"])
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if !retry {
|
|
333
|
+
OSLog.viewCycle.info("ReactNative checkFailedActions without retry")
|
|
334
|
+
let failedLinks = try fula.listFailedPushes()
|
|
335
|
+
|
|
336
|
+
let nextFailedLink = try failedLinks.next()
|
|
337
|
+
if nextFailedLink != nil {
|
|
338
|
+
// Assuming nextFailedLink is of type Data; replace `toHex()` with an appropriate method to convert Data to a hex string
|
|
339
|
+
OSLog.viewCycle.info("ReactNative checkFailedActions found")
|
|
340
|
+
resolve(true)
|
|
341
|
+
} else {
|
|
342
|
+
resolve(false)
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
OSLog.viewCycle.info("ReactNative checkFailedActions with retry")
|
|
346
|
+
let retryResults = try retryFailedActionsInternal(timeout: timeout) // Ensure retryFailedActionsInternal accepts a timeout parameter
|
|
347
|
+
resolve(!retryResults)
|
|
348
|
+
}
|
|
349
|
+
} catch let error {
|
|
350
|
+
OSLog.viewCycle.info("ReactNative checkFailedActions failed with Error: \(error.localizedDescription)")
|
|
351
|
+
reject("ERR_FULA", "CheckFailedActions failed", error)
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
func retryFailedActionsInternal(timeout: Int) throws -> Bool {
|
|
358
|
+
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal started")
|
|
359
|
+
|
|
360
|
+
guard let fula = fula else {
|
|
361
|
+
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed because fula is not initialized")
|
|
362
|
+
return false
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
do {
|
|
366
|
+
let connectionCheck = try checkConnectionInternal(timeout: timeout)
|
|
367
|
+
|
|
368
|
+
if connectionCheck {
|
|
369
|
+
do {
|
|
370
|
+
OSLog.viewCycle.info("ReactNative retryFailedPushes started")
|
|
371
|
+
try fula.retryFailedPushes()
|
|
372
|
+
OSLog.viewCycle.info("ReactNative flush started")
|
|
373
|
+
try fula.flush()
|
|
374
|
+
return true
|
|
375
|
+
} catch let error {
|
|
376
|
+
try fula.flush()
|
|
377
|
+
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
|
|
378
|
+
return false
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed because blox is offline")
|
|
382
|
+
return false
|
|
383
|
+
}
|
|
384
|
+
} catch let error {
|
|
385
|
+
OSLog.viewCycle.info("ReactNative retryFailedActionsInternal failed with Error: \(error.localizedDescription)")
|
|
386
|
+
return false
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
func createPeerIdentity(privateKey: Data) throws -> Data {
|
|
393
|
+
do {
|
|
394
|
+
// 1: First: create public key from provided private key
|
|
395
|
+
// 2: Should read the local keychain store (if it is key-value, key is public key above,
|
|
396
|
+
// 3: if found, decrypt using the private key
|
|
397
|
+
// 4: If not found or decryption not successful, generate an identity
|
|
398
|
+
// 5: then encrypt and store in keychain
|
|
399
|
+
// TODO: recheck error handling
|
|
400
|
+
var encryptedKey = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
|
|
401
|
+
let secretKey = try Cryptography.generateKey(privateKey)
|
|
402
|
+
if (encryptedKey == nil) {
|
|
403
|
+
var error: NSError?
|
|
404
|
+
let autoGeneratedIdentity = FulamobileGenerateEd25519Key(&error)
|
|
405
|
+
if error != nil {
|
|
406
|
+
throw error!
|
|
407
|
+
}
|
|
408
|
+
encryptedKey = try Cryptography.encryptMsg(autoGeneratedIdentity!.toUint8Array(), secretKey)
|
|
409
|
+
userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey!)
|
|
410
|
+
}
|
|
411
|
+
return try Cryptography.decryptMsg(encryptedKey!, secretKey).toData()
|
|
412
|
+
|
|
413
|
+
} catch let error {
|
|
414
|
+
print("ReactNative", "createPeerIdentity failed with Error: ", error.localizedDescription)
|
|
415
|
+
throw error
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
func createNewrootCid(identity: Data) throws -> Void {
|
|
420
|
+
let hash32 = identity.sha256()
|
|
421
|
+
print("ReactNative", "wnfsKey=" , identity.toHex() , "; hash32 = " , hash32.toHex());
|
|
422
|
+
if (fula != nil) {
|
|
423
|
+
try fula?.flush()
|
|
424
|
+
}
|
|
425
|
+
rootCid = try wnfs?.Init(wnfsKey: hash32)
|
|
426
|
+
print("ReactNative", "privateForest is created: ", rootCid!)
|
|
427
|
+
wnfsKey = identity
|
|
428
|
+
print("ReactNative", "rootCid is created: cid=", rootCid!, " ; wnfs_key=", wnfsKey!.toHex(), "; hash32=", hash32)
|
|
429
|
+
try encryptAndStoreConfig()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
func loadWnfs(_ _wnfsKey: Data , _ _rootCid: String) throws {
|
|
433
|
+
OSLog.viewCycle.info("ReactNative loadWnfs called: rootCid=\(_rootCid)")
|
|
434
|
+
let hash32 = _wnfsKey.sha256()
|
|
435
|
+
OSLog.viewCycle.info("ReactNative wnfsKey= \(_wnfsKey.toHex()) ; hash32 = \(hash32.toHex())");
|
|
436
|
+
try wnfs?.LoadWithWNFSKey(wnfsKey: hash32, cid: _rootCid)
|
|
437
|
+
rootCid = _rootCid
|
|
438
|
+
wnfsKey = _wnfsKey
|
|
439
|
+
if (fula != nil) {
|
|
440
|
+
try fula?.flush()
|
|
441
|
+
}
|
|
442
|
+
OSLog.viewCycle.info("ReactNative loadWnfs completed")
|
|
443
|
+
try encryptAndStoreConfig()
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
func encryptAndStoreConfig() throws {
|
|
448
|
+
do {
|
|
449
|
+
if let identityEncryptedGlobalUnwrapped = identityEncryptedGlobal {
|
|
450
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig started")
|
|
451
|
+
|
|
452
|
+
if let rootCidUnwrapped = rootCid, let wnfsKeyUnwrapped = wnfsKey, let secretKeyGlobalUnwrapped = secretKeyGlobal {
|
|
453
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig started with rootCid: \(rootCidUnwrapped.toUint8Array()) and wnfsKey:\(wnfsKeyUnwrapped)")
|
|
454
|
+
|
|
455
|
+
let cid_encrypted = try Cryptography.encryptMsg(rootCidUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
|
|
456
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig cid_encrypted: \(cid_encrypted)")
|
|
457
|
+
|
|
458
|
+
let wnfs_key_encrypted = try Cryptography.encryptMsg(wnfsKeyUnwrapped.toUint8Array(), secretKeyGlobalUnwrapped)
|
|
459
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig wnfs_key_encrypted: \(wnfs_key_encrypted)")
|
|
460
|
+
|
|
461
|
+
userDataHelper.add("cid_encrypted_" + identityEncryptedGlobalUnwrapped, cid_encrypted)
|
|
462
|
+
userDataHelper.add("wnfs_key_encrypted_" + identityEncryptedGlobalUnwrapped, wnfs_key_encrypted)
|
|
463
|
+
} else {
|
|
464
|
+
// Handle the case where rootCid, wnfsKey, or secretKeyGlobal is nil
|
|
465
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig failed because one of the values is nil")
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
} catch let error {
|
|
469
|
+
OSLog.viewCycle.info("ReactNative encryptAndStoreConfig failed with Error: \(error.localizedDescription)")
|
|
470
|
+
throw error
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
func logoutInternal(identity: Data, _storePath: String?) throws {
|
|
475
|
+
do {
|
|
476
|
+
if (fula != nil) {
|
|
477
|
+
try fula?.flush()
|
|
478
|
+
}
|
|
479
|
+
let secretKey = try Cryptography.generateKey(identity)
|
|
480
|
+
let identity_encrypted: String = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
|
|
481
|
+
userDataHelper.remove("cid_encrypted_"+identity_encrypted)
|
|
482
|
+
|
|
483
|
+
//TODO: Should also remove peerid @Mahdi
|
|
484
|
+
|
|
485
|
+
userDataHelper.remove("cid_encrypted_"+identity_encrypted)
|
|
486
|
+
|
|
487
|
+
rootCid = nil
|
|
488
|
+
secretKeyGlobal = nil
|
|
489
|
+
identityEncryptedGlobal = nil
|
|
490
|
+
var storePath = _storePath
|
|
491
|
+
if (storePath == nil || storePath!.isEmpty) {
|
|
492
|
+
storePath = fulaStorePath
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
do{
|
|
496
|
+
try FileManager.default.removeItem(atPath: fulaStorePath)
|
|
497
|
+
}catch let error{
|
|
498
|
+
print("Deleting fula store path", error.localizedDescription)
|
|
499
|
+
throw error
|
|
500
|
+
}
|
|
501
|
+
} catch let error {
|
|
502
|
+
print("ReactNative", "logout internal failed with Error: ", error.localizedDescription)
|
|
503
|
+
throw error
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
func getFulaClient() -> FulamobileClient? {
|
|
508
|
+
return fula
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
func newClientInternal(identity: Data, storePath: String?, bloxAddr: String, exchange: String, autoFlush: Bool, useRelay: Bool, refresh: Bool) throws -> Data {
|
|
512
|
+
do {
|
|
513
|
+
fulaConfig = FulamobileConfig()
|
|
514
|
+
if (storePath == nil || storePath!.isEmpty) {
|
|
515
|
+
fulaConfig!.storePath = fulaStorePath
|
|
516
|
+
} else {
|
|
517
|
+
fulaConfig!.storePath = storePath!
|
|
518
|
+
}
|
|
519
|
+
print("ReactNative", "storePath is set: " + fulaConfig!.storePath)
|
|
520
|
+
|
|
521
|
+
let peerIdentity = try createPeerIdentity(privateKey: identity)
|
|
522
|
+
fulaConfig!.identity = peerIdentity
|
|
523
|
+
print("ReactNative", "peerIdentity is set: " + fulaConfig!.identity!.toHex())
|
|
524
|
+
fulaConfig!.bloxAddr = bloxAddr
|
|
525
|
+
print("ReactNative", "bloxAddr is set: " + fulaConfig!.bloxAddr)
|
|
526
|
+
fulaConfig!.exchange = exchange
|
|
527
|
+
fulaConfig!.syncWrites = autoFlush
|
|
528
|
+
if (useRelay) {
|
|
529
|
+
fulaConfig!.allowTransientConnection = true
|
|
530
|
+
fulaConfig!.forceReachabilityPrivate = true
|
|
531
|
+
}
|
|
532
|
+
if (fula == nil || refresh) {
|
|
533
|
+
print("ReactNative", "Creating a new Fula instance");
|
|
534
|
+
do {
|
|
535
|
+
try shutdownInternal();
|
|
536
|
+
fula = FulamobileClient(fulaConfig)
|
|
537
|
+
if (fula != nil) {
|
|
538
|
+
try fula?.flush();
|
|
539
|
+
}
|
|
540
|
+
} catch let error {
|
|
541
|
+
print("ReactNative", "Failed to create new Fula instance: " , error.localizedDescription);
|
|
542
|
+
throw MyError.runtimeError("Failed to create new Fula instance")
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return peerIdentity
|
|
546
|
+
} catch let error {
|
|
547
|
+
print("ReactNative", "newclientInternal failed with Error: ", error.localizedDescription)
|
|
548
|
+
throw error
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
func initInternal(identity: Data, storePath: String, bloxAddr: String, exchange: String, autoFlush: Bool, _rootCid: String, useRelay: Bool, refresh: Bool) throws -> [String] {
|
|
553
|
+
|
|
554
|
+
do {
|
|
555
|
+
if (fula == nil || refresh) {
|
|
556
|
+
try newClientInternal(identity: identity, storePath: storePath, bloxAddr: bloxAddr, exchange: exchange, autoFlush: autoFlush, useRelay: useRelay, refresh: refresh)
|
|
557
|
+
OSLog.viewCycle.info("ReactNative fula initialized: \(self.fula!.id_())")
|
|
558
|
+
}
|
|
559
|
+
if(client == nil || refresh) {
|
|
560
|
+
client = Client(clientInput: fula!)
|
|
561
|
+
wnfs = Wnfs(putFn: { cid, data in
|
|
562
|
+
guard let c = self.client else {
|
|
563
|
+
throw MyError.runtimeError("wnfs: fula client not ready")
|
|
564
|
+
}
|
|
565
|
+
try c.put(cid, data)
|
|
566
|
+
}, getFn: { cid in
|
|
567
|
+
guard let c = self.client else {
|
|
568
|
+
throw MyError.runtimeError("wnfs: fula client not ready")
|
|
569
|
+
}
|
|
570
|
+
return try c.get(cid)
|
|
571
|
+
})
|
|
572
|
+
OSLog.viewCycle.info("ReactNative wnfs initialized")
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
let secretKey = try Cryptography.generateKey(identity)
|
|
576
|
+
let identity_encrypted = try Cryptography.encryptMsg(identity.toUint8Array(), secretKey)
|
|
577
|
+
identityEncryptedGlobal = identity_encrypted
|
|
578
|
+
secretKeyGlobal = secretKey
|
|
579
|
+
|
|
580
|
+
if (rootCid == nil || rootCid!.isEmpty) {
|
|
581
|
+
OSLog.viewCycle.info("ReactNative rootCid is empty.")
|
|
582
|
+
//Load from keystore
|
|
583
|
+
|
|
584
|
+
let cid_encrypted_fetched = userDataHelper.getValue("cid_encrypted_"+identity_encrypted)
|
|
585
|
+
OSLog.viewCycle.info("ReactNative Here1")
|
|
586
|
+
var cid: Array<UInt8>? = nil
|
|
587
|
+
if(cid_encrypted_fetched != nil && !cid_encrypted_fetched!.isEmpty) {
|
|
588
|
+
OSLog.viewCycle.info("ReactNative decrypting cid= \(cid_encrypted_fetched!) with secret \(secretKey.toHex())")
|
|
589
|
+
cid = try Cryptography.decryptMsg(cid_encrypted_fetched!, secretKey)
|
|
590
|
+
}
|
|
591
|
+
print("ReactNative", "Here2")
|
|
592
|
+
//print("ReactNative", "Attempted to fetch cid from keystore cid="+cid+" & wnfs_key="+wnfs_key)
|
|
593
|
+
if(cid == nil || cid!.isEmpty){
|
|
594
|
+
OSLog.viewCycle.info("ReactNative cid or wnfs key was not found")
|
|
595
|
+
if(!_rootCid.isEmpty){
|
|
596
|
+
OSLog.viewCycle.info("ReactNative Re-setting cid from input: \(_rootCid)")
|
|
597
|
+
cid = _rootCid.toUint8Array()
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
}
|
|
601
|
+
if(cid == nil || cid!.isEmpty){
|
|
602
|
+
OSLog.viewCycle.info("ReactNative Tried to recover cid but was not successful. Creating ones")
|
|
603
|
+
try createNewrootCid(identity: identity)
|
|
604
|
+
} else {
|
|
605
|
+
OSLog.viewCycle.info("ReactNative Found cid and wnfs key in keychain store")
|
|
606
|
+
OSLog.viewCycle.info("ReactNative Recovered cid and private ref from keychain store. cid=\(cid!) & wnfs_key=\(identity)")
|
|
607
|
+
try loadWnfs(identity, cid!.toData().toUTF8String()!)
|
|
608
|
+
}
|
|
609
|
+
OSLog.viewCycle.info("ReactNative creating/reloading rootCid completed")
|
|
610
|
+
|
|
611
|
+
/*
|
|
612
|
+
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")
|
|
613
|
+
long testcodec = 85
|
|
614
|
+
byte[] testputcid = client!.put(testbyte, testcodec)
|
|
615
|
+
print("ReactNative", "client!.put test done"+ Arrays.toString(testputcid))
|
|
616
|
+
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")
|
|
617
|
+
byte[] testfetchedbytes = client!.get(testfetchedcid)
|
|
618
|
+
print("ReactNative", "client!.get test done"+ Arrays.toString(testfetchedbytes))
|
|
619
|
+
*/
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
OSLog.viewCycle.info("ReactNative rootCid is created: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
|
|
623
|
+
} else {
|
|
624
|
+
OSLog.viewCycle.info("ReactNative rootCid existed: cid=\(self.rootCid!) & wnfs_key=\(self.wnfsKey!.toHex())")
|
|
625
|
+
}
|
|
626
|
+
let peerId = fula!.id_()
|
|
627
|
+
var obj = [String]()
|
|
628
|
+
obj.append(peerId)
|
|
629
|
+
obj.append(rootCid!)
|
|
630
|
+
obj.append(wnfsKey!.toHex())
|
|
631
|
+
OSLog.viewCycle.info("ReactNative initInternal is completed successfully")
|
|
632
|
+
if (fula != nil) {
|
|
633
|
+
try fula?.flush()
|
|
634
|
+
}
|
|
635
|
+
return obj
|
|
636
|
+
} catch let error {
|
|
637
|
+
OSLog.viewCycle.info("ReactNative init internal failed with Error: \(error.localizedDescription)")
|
|
638
|
+
throw error
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
@objc(mkdir:withResolver:withRejecter:)
|
|
644
|
+
func mkdir(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
645
|
+
print("ReactNative", "mkdir: path = " + path)
|
|
646
|
+
do {
|
|
647
|
+
let cid = try wnfs?.MkDir(cid: rootCid!, remotePath: path)
|
|
648
|
+
if(cid != nil) {
|
|
649
|
+
rootCid = cid
|
|
650
|
+
try encryptAndStoreConfig()
|
|
651
|
+
if (fula != nil) {
|
|
652
|
+
try fula?.flush()
|
|
653
|
+
}
|
|
654
|
+
resolve(rootCid)
|
|
655
|
+
} else {
|
|
656
|
+
print("ReactNative", "mkdir Error: config is nil")
|
|
657
|
+
reject("ERR_WNFS", "Can't make dir", nil)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
} catch let error{
|
|
661
|
+
print("mkdir", error.localizedDescription)
|
|
662
|
+
reject("ERR_WNFS", "mkdir", error)
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
@objc(writeFile:withLocalFilename:withResolver:withRejecter:)
|
|
667
|
+
func writeFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
668
|
+
/*
|
|
669
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
670
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
671
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
672
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
673
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
674
|
+
*/
|
|
675
|
+
print("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename)
|
|
676
|
+
do {
|
|
677
|
+
let cid = try wnfs?.WriteFileFromPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
|
|
678
|
+
if(cid != nil) {
|
|
679
|
+
rootCid = cid
|
|
680
|
+
try encryptAndStoreConfig()
|
|
681
|
+
if (fula != nil) {
|
|
682
|
+
try fula?.flush()
|
|
683
|
+
}
|
|
684
|
+
resolve(rootCid)
|
|
685
|
+
} else {
|
|
686
|
+
print("ReactNative", "writeFile Error: config is nil")
|
|
687
|
+
reject("ERR_WNFS", "writeFile Error: config is nil", nil)
|
|
688
|
+
}
|
|
689
|
+
} catch let error {
|
|
690
|
+
print("writeFile", error.localizedDescription)
|
|
691
|
+
reject("ERR_WNFS", "writeFile", error)
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
@objc(writeFileContent:withContentString:withResolver:withRejecter:)
|
|
696
|
+
func writeFileContent(path: String, contentString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
697
|
+
|
|
698
|
+
print("ReactNative", "writeFile: contentString = " + contentString)
|
|
699
|
+
print("ReactNative", "writeFile: path = " + path)
|
|
700
|
+
do {
|
|
701
|
+
let content = convertStringToByte(contentString)
|
|
702
|
+
let cid = try wnfs?.WriteFile(cid: rootCid!, remotePath: path, data: content.toData())
|
|
703
|
+
rootCid = cid
|
|
704
|
+
try encryptAndStoreConfig()
|
|
705
|
+
if (fula != nil) {
|
|
706
|
+
try fula?.flush()
|
|
707
|
+
}
|
|
708
|
+
resolve(rootCid)
|
|
709
|
+
} catch let error {
|
|
710
|
+
print("writeFileContent", error.localizedDescription)
|
|
711
|
+
reject("ERR_WNFS", "writeFileContent", error)
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
@objc(ls:withResolver:withRejecter:)
|
|
717
|
+
func ls(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
718
|
+
print("ReactNative", "ls: path = " + path)
|
|
719
|
+
do {
|
|
720
|
+
let res = try wnfs?.Ls(cid: rootCid!, remotePath: path)
|
|
721
|
+
|
|
722
|
+
//JSONArray jsonArray = new JSONArray(res)
|
|
723
|
+
guard let s = res?.toUTF8String() else {
|
|
724
|
+
throw MyError.runtimeError("converting bytes to utf8 string")
|
|
725
|
+
}
|
|
726
|
+
print("ReactNative", "ls: res = " + s)
|
|
727
|
+
resolve(s)
|
|
728
|
+
} catch let error {
|
|
729
|
+
print("ls", error.localizedDescription)
|
|
730
|
+
reject("ERR_WNFS", "ls", error)
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
@objc(rm:withResolver:withRejecter:)
|
|
736
|
+
func rm(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
737
|
+
|
|
738
|
+
print("ReactNative", "rm: path = " + path)
|
|
739
|
+
do {
|
|
740
|
+
let cid = try wnfs?.Rm(cid: rootCid!, remotePath: path)
|
|
741
|
+
if(cid != nil) {
|
|
742
|
+
rootCid = cid
|
|
743
|
+
try encryptAndStoreConfig()
|
|
744
|
+
if (fula != nil) {
|
|
745
|
+
try fula?.flush()
|
|
746
|
+
}
|
|
747
|
+
resolve(rootCid)
|
|
748
|
+
} else {
|
|
749
|
+
print("ReactNative", "rm Error: config is nil")
|
|
750
|
+
reject("ERR_WNFS", "rm Error: config is nil", nil)
|
|
751
|
+
}
|
|
752
|
+
} catch let error {
|
|
753
|
+
print("rm", error.localizedDescription)
|
|
754
|
+
reject("ERR_WNFS", "rm", error)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
@objc(cp:withTargetPath:withResolver:withRejecter:)
|
|
760
|
+
func cp(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
761
|
+
|
|
762
|
+
print("ReactNative", "rm: sourcePath = " + sourcePath)
|
|
763
|
+
do {
|
|
764
|
+
let cid = try wnfs?.Cp(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
|
|
765
|
+
if(cid != nil) {
|
|
766
|
+
rootCid = cid
|
|
767
|
+
try encryptAndStoreConfig()
|
|
768
|
+
if (fula != nil) {
|
|
769
|
+
try fula?.flush()
|
|
770
|
+
}
|
|
771
|
+
resolve(rootCid)
|
|
772
|
+
} else {
|
|
773
|
+
print("ReactNative", "cp Error: config is nil")
|
|
774
|
+
reject("ERR_WNFS", "cp Error: config is nil", nil)
|
|
775
|
+
}
|
|
776
|
+
} catch let error {
|
|
777
|
+
print("cp", error.localizedDescription)
|
|
778
|
+
reject("ERR_WNFS", "cp", error)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
@objc(mv:withTargetPath:withResolver:withRejecter:)
|
|
784
|
+
func mv(sourcePath: String, targetPath: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
785
|
+
print("ReactNative", "rm: sourcePath = " + sourcePath)
|
|
786
|
+
do {
|
|
787
|
+
let cid = try wnfs?.Mv(cid: rootCid!, remotePathFrom: sourcePath, remotePathTo: targetPath)
|
|
788
|
+
if(cid != nil) {
|
|
789
|
+
rootCid = cid
|
|
790
|
+
try encryptAndStoreConfig()
|
|
791
|
+
if (fula != nil) {
|
|
792
|
+
try fula?.flush()
|
|
793
|
+
}
|
|
794
|
+
resolve(rootCid)
|
|
795
|
+
} else {
|
|
796
|
+
print("ReactNative", "mv Error: config is nil")
|
|
797
|
+
reject("ERR_WNFS", "mv Error: config is nil", nil)
|
|
798
|
+
}
|
|
799
|
+
} catch let error {
|
|
800
|
+
print("mv", error.localizedDescription)
|
|
801
|
+
reject("ERR_WNFS", "mv", error)
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
@objc(readFile:withLocalFilename:withResolver:withRejecter:)
|
|
807
|
+
func readFile(fulaTargetFilename: String, localFilename: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
808
|
+
/*
|
|
809
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
810
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
811
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
812
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
813
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
814
|
+
*/
|
|
815
|
+
print("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename)
|
|
816
|
+
do {
|
|
817
|
+
let path = try wnfs?.ReadFileToPath(cid: rootCid!, remotePath: fulaTargetFilename, fileUrl: URL.init(string: localFilename)!)
|
|
818
|
+
resolve(path)
|
|
819
|
+
} catch let error {
|
|
820
|
+
print("readFile", error.localizedDescription)
|
|
821
|
+
reject("ERR_WNFS", "readFile", error)
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
@objc(readFileContent:withResolver:withRejecter:)
|
|
827
|
+
func readFileContent(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
828
|
+
|
|
829
|
+
print("ReactNative", "readFileContent: path = " + path)
|
|
830
|
+
do {
|
|
831
|
+
// FIXME: dhouldn't we output an NSData object instead?
|
|
832
|
+
let res = try wnfs?.ReadFile(cid: rootCid!, remotePath: path)
|
|
833
|
+
guard let resString = res?.toUTF8String() else{
|
|
834
|
+
throw MyError.runtimeError("converting bytes to utf8 string")
|
|
835
|
+
}
|
|
836
|
+
resolve(resString)
|
|
837
|
+
} catch let error {
|
|
838
|
+
print("readFileContent", error.localizedDescription)
|
|
839
|
+
reject("ERR_WNFS", "readFileContent", error)
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
@objc(get:withResolver:withRejecter:)
|
|
845
|
+
func get(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
846
|
+
|
|
847
|
+
print("ReactNative", "get: keyString = " + keyString)
|
|
848
|
+
do {
|
|
849
|
+
let key: Data = convertStringToByte(keyString).toData()
|
|
850
|
+
let value = try getInternal(key)
|
|
851
|
+
let valueString: String = value.toUTF8String()!
|
|
852
|
+
resolve(valueString)
|
|
853
|
+
} catch let error {
|
|
854
|
+
print("get", error.localizedDescription)
|
|
855
|
+
reject("ERR_FULA", "get", error)
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
func getInternal(_ key: Data) throws -> Data {
|
|
861
|
+
do {
|
|
862
|
+
print("ReactNative", "getInternal: key.toUTF8String() = " , key.toUTF8String()!)
|
|
863
|
+
print("ReactNative", "getInternal: key.toHex().bytes = " , key.toHex())
|
|
864
|
+
let value = try fula!.get(key)
|
|
865
|
+
print("ReactNative", "getInternal: value.toHex() = " , value.toHex())
|
|
866
|
+
return value
|
|
867
|
+
} catch let error {
|
|
868
|
+
print("ReactNative", "getInternal: error = " + error.localizedDescription)
|
|
869
|
+
print("getInternal", error.localizedDescription)
|
|
870
|
+
throw error
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
@objc(has:withResolver:withRejecter:)
|
|
875
|
+
func has(keyString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
876
|
+
|
|
877
|
+
print("ReactNative", "has: keyString = " + keyString)
|
|
878
|
+
do {
|
|
879
|
+
let key: Data = convertStringToByte(keyString).toData()
|
|
880
|
+
let result = try hasInternal(key)
|
|
881
|
+
resolve(result)
|
|
882
|
+
} catch let error {
|
|
883
|
+
print("has", error.localizedDescription)
|
|
884
|
+
reject("ERR_FULA", "has", error)
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
func hasInternal(_ key: Data) throws -> Bool {
|
|
890
|
+
do {
|
|
891
|
+
let ret = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
|
|
892
|
+
try fula?.has(key, ret0_: ret)
|
|
893
|
+
let res = ret.pointee.boolValue
|
|
894
|
+
ret.deallocate()
|
|
895
|
+
return res
|
|
896
|
+
} catch let error {
|
|
897
|
+
print("hasInternal", error.localizedDescription)
|
|
898
|
+
throw error
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
func pullInternal(key: Data) throws -> Void {
|
|
903
|
+
do {
|
|
904
|
+
try fula!.pull(key)
|
|
905
|
+
} catch let error {
|
|
906
|
+
print("pullInternal", error.localizedDescription)
|
|
907
|
+
throw error
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
@objc(push:withRejecter:)
|
|
912
|
+
func push(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
913
|
+
print("ReactNative", "push started")
|
|
914
|
+
do {
|
|
915
|
+
try pushInternal(key: convertStringToByte(rootCid!).toData())
|
|
916
|
+
resolve(rootCid)
|
|
917
|
+
} catch let error {
|
|
918
|
+
print("get", error.localizedDescription)
|
|
919
|
+
reject("ERR_FULA", "push", error)
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
func pushInternal(key: Data) throws -> Void {
|
|
924
|
+
do {
|
|
925
|
+
let hasIt = try hasInternal(key)
|
|
926
|
+
if (fula != nil && hasIt) {
|
|
927
|
+
try fula?.push(key)
|
|
928
|
+
try fula?.flush()
|
|
929
|
+
} else {
|
|
930
|
+
print("ReactNative", "pushInternal error: key wasn't found or fula is not initialized")
|
|
931
|
+
throw MyError.runtimeError("pushInternal error: key wasn't found or fula is not initialized")
|
|
932
|
+
}
|
|
933
|
+
} catch let error {
|
|
934
|
+
print("ReactNative", "pushInternal", error.localizedDescription)
|
|
935
|
+
throw error
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// FIXME: unused codecString arg
|
|
940
|
+
@objc(put:withCodecString:withResolver:withRejecter:)
|
|
941
|
+
func put(valueString: String, codecString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
942
|
+
|
|
943
|
+
print("ReactNative", "put: codecString = " + codecString)
|
|
944
|
+
print("ReactNative", "put: valueString = " + valueString)
|
|
945
|
+
do {
|
|
946
|
+
//byte[] codec = convertStringToByte(CodecString)
|
|
947
|
+
let codec = FulaModule.CODEC_DAG_CBOR
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
print("ReactNative", "put: codec = ", codec)
|
|
951
|
+
let value = valueString.toData()
|
|
952
|
+
|
|
953
|
+
print("ReactNative", "put: value.toHex() = " , value.toHex())
|
|
954
|
+
let key = try putInternal(value: value, codec: codec)
|
|
955
|
+
print("ReactNative", "put: key.toHex() = " , key.toUTF8String()!)
|
|
956
|
+
resolve(key.toUTF8String()!)
|
|
957
|
+
} catch let error {
|
|
958
|
+
print("ReactNative", "put: error = ", error.localizedDescription)
|
|
959
|
+
reject("ERR_FULA", "put", error)
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
// FIXME: unused codec arg
|
|
965
|
+
func putInternal(value: Data, codec: Int) throws -> Data {
|
|
966
|
+
do {
|
|
967
|
+
if(fula != nil) {
|
|
968
|
+
let key: Data = try fula!.put(value, codec: Int64(FulaModule.CODEC_DAG_CBOR))
|
|
969
|
+
try fula?.flush()
|
|
970
|
+
return key
|
|
971
|
+
} else {
|
|
972
|
+
print("ReactNative", "putInternal Error: fula is not initialized")
|
|
973
|
+
throw MyError.runtimeError("putInternal Error: fula is not initialized")
|
|
974
|
+
}
|
|
975
|
+
} catch let error {
|
|
976
|
+
print("ReactNative", "putInternal", error.localizedDescription)
|
|
977
|
+
throw error
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
@objc(setAuth:withAllow:withResolver:withRejecter:)
|
|
982
|
+
func setAuth(peerIdString: String, allow: Bool, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
983
|
+
|
|
984
|
+
print("ReactNative", "setAuth: peerIdString = " + peerIdString)
|
|
985
|
+
do {
|
|
986
|
+
if (fula != nil && !(fula?.id_().isEmpty)! && fulaConfig != nil && !fulaConfig!.bloxAddr.isEmpty) {
|
|
987
|
+
let bloxAddr = fulaConfig!.bloxAddr
|
|
988
|
+
print("ReactNative", "setAuth: bloxAddr = '",bloxAddr,"'"," peerIdString = '",peerIdString,"'")
|
|
989
|
+
let parts = bloxAddr.split(separator: "/").map(String.init)
|
|
990
|
+
try fula?.setAuth(parts.last, subject: peerIdString, allow: allow)
|
|
991
|
+
resolve(true)
|
|
992
|
+
} else {
|
|
993
|
+
print("ReactNative", "setAuth error: fula is not initialized")
|
|
994
|
+
throw MyError.runtimeError("fula is not initialized")
|
|
995
|
+
}
|
|
996
|
+
resolve(false)
|
|
997
|
+
} catch let error {
|
|
998
|
+
print("get", error.localizedDescription)
|
|
999
|
+
reject("ERR_FULA", "setAuth", error)
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
@objc(clearCidsFromRecent:withResolver:withRejecter:)
|
|
1005
|
+
func clearCidsFromRecent(cidArray: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1006
|
+
DispatchQueue.global(qos: .default).async {
|
|
1007
|
+
do {
|
|
1008
|
+
guard let fulaClient = self.fula else {
|
|
1009
|
+
throw MyError.runtimeError("Fula client is not initialized")
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
// Concatenate all CID strings into a single string separated by "|"
|
|
1013
|
+
let concatenatedCids = (cidArray as? [String])?.joined(separator: "|")
|
|
1014
|
+
|
|
1015
|
+
guard let cidsData = concatenatedCids?.data(using: .utf8) else {
|
|
1016
|
+
throw MyError.runtimeError("Unable to encode CIDs as data")
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
try fulaClient.clearCids(fromRecent: cidsData)
|
|
1020
|
+
resolve(true)
|
|
1021
|
+
} catch let error {
|
|
1022
|
+
print("ReactNative", "clearCidsFromRecent failed with Error: \(error.localizedDescription)")
|
|
1023
|
+
reject("ERR_FULA_CLEAR_CIDS", "Failed to clear CIDs from recent", error)
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
@objc(listRecentCidsAsString:withRejecter:)
|
|
1030
|
+
func listRecentCidsAsString(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1031
|
+
DispatchQueue.global(qos: .default).async {
|
|
1032
|
+
do {
|
|
1033
|
+
guard let fulaClient = self.fula else {
|
|
1034
|
+
throw MyError.runtimeError("Fula client is not initialized")
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
let recentLinksIterator = try fulaClient.listRecentCidsAsString()
|
|
1038
|
+
var recentLinksList = [String]()
|
|
1039
|
+
|
|
1040
|
+
while recentLinksIterator.hasNext() {
|
|
1041
|
+
var error: NSError?
|
|
1042
|
+
let nextLink = try recentLinksIterator.next(&error)
|
|
1043
|
+
|
|
1044
|
+
if let error = error {
|
|
1045
|
+
throw error
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
recentLinksList.append(nextLink)
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
if !recentLinksList.isEmpty {
|
|
1052
|
+
// Return the whole list
|
|
1053
|
+
resolve(recentLinksList)
|
|
1054
|
+
} else {
|
|
1055
|
+
resolve(false)
|
|
1056
|
+
}
|
|
1057
|
+
} catch let error {
|
|
1058
|
+
print("ReactNative", "listRecentCidsAsString failed with Error: \(error.localizedDescription)")
|
|
1059
|
+
reject("ERR_FULA_LIST_RECENT_CIDS", "Failed to list recent CIDs as string", error)
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
@objc(shutdown:withRejecter:)
|
|
1067
|
+
func shutdown( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
|
|
1068
|
+
do {
|
|
1069
|
+
try shutdownInternal()
|
|
1070
|
+
resolve(true)
|
|
1071
|
+
} catch let error {
|
|
1072
|
+
print("ReactNative", "shutdown", error.localizedDescription)
|
|
1073
|
+
reject("ERR_FULA", "shutdown", error)
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
func shutdownInternal() throws {
|
|
1079
|
+
if(fula != nil) {
|
|
1080
|
+
try fula?.shutdown()
|
|
1081
|
+
fula = nil
|
|
1082
|
+
client = nil
|
|
1083
|
+
wnfs = nil
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
///////////////////////////////////////////////////////////
|
|
1088
|
+
///////////////////////////////////////////////////////////
|
|
1089
|
+
///////////////////////////////////////////////////////////
|
|
1090
|
+
///////////////////////////////////////////////////////////
|
|
1091
|
+
//////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
|
|
1092
|
+
///////////////////////////////////////////////////////////
|
|
1093
|
+
|
|
1094
|
+
@objc(checkAccountExists:withResolver:withRejecter:)
|
|
1095
|
+
func checkAccountExists(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1096
|
+
print("ReactNative", "checkAccountExists: accountString = ", accountString)
|
|
1097
|
+
do {
|
|
1098
|
+
let result = try fula!.accountExists(accountString)
|
|
1099
|
+
let resultString = result.toUTF8String()!
|
|
1100
|
+
resolve(resultString)
|
|
1101
|
+
} catch let error {
|
|
1102
|
+
print("checkAccountExists", error.localizedDescription)
|
|
1103
|
+
reject("ERR_FULA", "checkAccountExists", error)
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
@objc(accountFund:withResolver:withRejecter:)
|
|
1109
|
+
func accountFund(accountString: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1110
|
+
print("ReactNative", "accountFund: accountString = ", accountString)
|
|
1111
|
+
do {
|
|
1112
|
+
let result = try fula!.accountFund(accountString)
|
|
1113
|
+
let resultString = result.toUTF8String()!
|
|
1114
|
+
resolve(resultString)
|
|
1115
|
+
} catch let error {
|
|
1116
|
+
print("accountFund", error.localizedDescription)
|
|
1117
|
+
reject("ERR_FULA", "accountFund", error)
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
@objc(listPools:withRejecter:)
|
|
1123
|
+
func listPools( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1124
|
+
print("ReactNative", "listPools")
|
|
1125
|
+
do {
|
|
1126
|
+
let result = try fula!.poolList()
|
|
1127
|
+
let resultString = result.toUTF8String()!
|
|
1128
|
+
resolve(resultString)
|
|
1129
|
+
} catch let error {
|
|
1130
|
+
print("listPools", error.localizedDescription)
|
|
1131
|
+
reject("ERR_FULA", "listPools", error)
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
@objc(listPoolJoinRequests:withResolver:withRejecter:)
|
|
1137
|
+
func listPoolJoinRequests(poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1138
|
+
print("ReactNative", "listPoolJoinRequests: poolID = ", poolID)
|
|
1139
|
+
do {
|
|
1140
|
+
let result = try fula!.poolRequests(poolID)
|
|
1141
|
+
let resultString = result.toUTF8String()!
|
|
1142
|
+
resolve(resultString)
|
|
1143
|
+
} catch let error {
|
|
1144
|
+
print("listPoolJoinRequests", error.localizedDescription)
|
|
1145
|
+
reject("ERR_FULA", "listPoolJoinRequests", error)
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
@objc(listAvailableReplicationRequests:withResolver:withRejecter:)
|
|
1151
|
+
func listAvailableReplicationRequests(poolID: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1152
|
+
print("ReactNative", "listAvailableReplicationRequests: poolID = ", poolID)
|
|
1153
|
+
do {
|
|
1154
|
+
let result = try fula!.manifestAvailable(poolID)
|
|
1155
|
+
let resultString = result.toUTF8String()!
|
|
1156
|
+
resolve(resultString)
|
|
1157
|
+
} catch let error {
|
|
1158
|
+
print("listAvailableReplicationRequests", error.localizedDescription)
|
|
1159
|
+
reject("ERR_FULA", "listAvailableReplicationRequests", error)
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
@objc(bloxFreeSpace:withRejecter:)
|
|
1165
|
+
func bloxFreeSpace( resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
|
1166
|
+
print("ReactNative", "bloxFreeSpace")
|
|
1167
|
+
do {
|
|
1168
|
+
let result = try fula!.bloxFreeSpace()
|
|
1169
|
+
let resultString = result.toUTF8String()!
|
|
1170
|
+
resolve(resultString)
|
|
1171
|
+
} catch let error {
|
|
1172
|
+
print("bloxFreeSpace", error.localizedDescription)
|
|
1173
|
+
reject("ERR_FULA", "bloxFreeSpace", error)
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
@objc(transferToFula:wallet:chain:withResolver:withRejecter:)
|
|
1179
|
+
func transferToFula(amount: String, wallet: String, chain: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1180
|
+
DispatchQueue.global(qos: .default).async {
|
|
1181
|
+
do {
|
|
1182
|
+
print("ReactNative", "transferToFula called")
|
|
1183
|
+
let result = try self.fula!.transfer(toFula: amount, walletAccount: wallet, chain: chain)
|
|
1184
|
+
let resultString = String(data: result, encoding: .utf8)
|
|
1185
|
+
resolve(resultString)
|
|
1186
|
+
} catch let error {
|
|
1187
|
+
print("ReactNative", "transferToFula failed with Error: \(error.localizedDescription)")
|
|
1188
|
+
reject("ERR_FULA_TRANSFER", "transferToFula failed", error)
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
@objc(getAccount:withRejecter:)
|
|
1195
|
+
func getAccount(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1196
|
+
do {
|
|
1197
|
+
let account = try fula!.getAccount()
|
|
1198
|
+
let accountString = String(data: account, encoding: .utf8)
|
|
1199
|
+
resolve(accountString)
|
|
1200
|
+
} catch let error {
|
|
1201
|
+
reject("ERR_FULA", "getAccount: \(error.localizedDescription)", error)
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
@objc(assetsBalance:assetId:classId:withResolver:withRejecter:)
|
|
1206
|
+
func assetsBalance(account: String, assetId: String, classId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1207
|
+
guard let assetIdInt = Int(assetId), let classIdInt = Int(classId) else {
|
|
1208
|
+
reject("ERR_FULA", "Invalid assetId or classId", nil)
|
|
1209
|
+
return
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
do {
|
|
1213
|
+
let balance = try fula!.assetsBalance(account, assetId: assetIdInt, classId: classIdInt)
|
|
1214
|
+
let balanceString = String(data: balance, encoding: .utf8)
|
|
1215
|
+
resolve(balanceString)
|
|
1216
|
+
} catch let error {
|
|
1217
|
+
reject("ERR_FULA", "assetsBalance: \(error.localizedDescription)", error)
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
@objc(joinPool:withResolver:withRejecter:)
|
|
1222
|
+
func joinPool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1223
|
+
print("ReactNative", "joinPool: poolID = ", poolID)
|
|
1224
|
+
do {
|
|
1225
|
+
guard let poolIdInt = Int(poolID) else {
|
|
1226
|
+
throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
|
|
1227
|
+
}
|
|
1228
|
+
let result = try fula!.poolJoin(poolIdInt)
|
|
1229
|
+
let resultString = String(data: result, encoding: .utf8)
|
|
1230
|
+
resolve(resultString)
|
|
1231
|
+
} catch let error {
|
|
1232
|
+
reject("ERR_FULA", "joinPool: \(error.localizedDescription)", error)
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
@objc(cancelPoolJoin:withResolver:withRejecter:)
|
|
1237
|
+
func cancelPoolJoin(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1238
|
+
do {
|
|
1239
|
+
guard let poolIdInt = Int(poolID) else {
|
|
1240
|
+
throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
|
|
1241
|
+
}
|
|
1242
|
+
let result = try fula!.poolCancelJoin(poolIdInt)
|
|
1243
|
+
let resultString = String(data: result, encoding: .utf8)
|
|
1244
|
+
resolve(resultString)
|
|
1245
|
+
} catch let error {
|
|
1246
|
+
reject("ERR_FULA", "cancelPoolJoin: \(error.localizedDescription)", error)
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
@objc(leavePool:withResolver:withRejecter:)
|
|
1251
|
+
func leavePool(poolID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1252
|
+
print("ReactNative", "leavePool: poolID = ", poolID)
|
|
1253
|
+
do {
|
|
1254
|
+
guard let poolIdInt = Int(poolID) else {
|
|
1255
|
+
throw NSError(domain: "Invalid poolID", code: 0, userInfo: nil)
|
|
1256
|
+
}
|
|
1257
|
+
let result = try fula!.poolLeave(poolIdInt)
|
|
1258
|
+
let resultString = String(data: result, encoding: .utf8)
|
|
1259
|
+
resolve(resultString)
|
|
1260
|
+
} catch let error {
|
|
1261
|
+
reject("ERR_FULA", "leavePool: \(error.localizedDescription)", error)
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
@objc(eraseBlData:withRejecter:)
|
|
1267
|
+
func eraseBlData(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1268
|
+
do {
|
|
1269
|
+
let result = try fula!.eraseBlData()
|
|
1270
|
+
let resultString = String(data: result, encoding: .utf8)
|
|
1271
|
+
resolve(resultString)
|
|
1272
|
+
} catch let error {
|
|
1273
|
+
reject("ERR_FULA", "eraseBlData: \(error.localizedDescription)", error)
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
@objc(reboot:withRejecter:)
|
|
1278
|
+
func reboot(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
|
1279
|
+
do {
|
|
1280
|
+
let result = try fula!.reboot()
|
|
1281
|
+
let resultString = result.toUTF8String()!
|
|
1282
|
+
resolve(resultString)
|
|
1283
|
+
} catch let error {
|
|
1284
|
+
print("reboot", error.localizedDescription)
|
|
1285
|
+
reject("ERR_FULA", "reboot", error)
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
@objc(wifiRemoveall:withRejecter:)
|
|
1290
|
+
func wifiRemoveall(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
|
1291
|
+
do {
|
|
1292
|
+
let result = try fula!.wifiRemoveall()
|
|
1293
|
+
let resultString = result.toUTF8String()!
|
|
1294
|
+
resolve(resultString)
|
|
1295
|
+
} catch let error {
|
|
1296
|
+
print("wifiRemoveall", error.localizedDescription)
|
|
1297
|
+
reject("ERR_FULA", "wifiRemoveall", error)
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
|