@functionland/react-native-fula 1.41.0 → 1.41.1
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/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 +0 -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 +166 -215
- package/ios/Fula.swift +1289 -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.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.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/protocols/chain-api.d.ts +4 -0
- package/lib/typescript/protocols/chain-api.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/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/chain-api.ts +308 -290
- package/src/protocols/fula.ts +427 -427
- package/src/protocols/fxblox.ts +70 -70
- package/src/types/fxblox.ts +13 -13
package/src/protocols/fula.ts
CHANGED
|
@@ -1,427 +1,427 @@
|
|
|
1
|
-
import Fula from '../interfaces/fulaNativeModule';
|
|
2
|
-
import {
|
|
3
|
-
init as chainApiInit,
|
|
4
|
-
batchUploadManifest,
|
|
5
|
-
checkAccountBalance,
|
|
6
|
-
getAccountIdFromSeed,
|
|
7
|
-
} from './chain-api';
|
|
8
|
-
import { ApiPromise } from '@polkadot/api';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Register the app's lifecycle listeners to handle foreground, background, and termination states.
|
|
12
|
-
*/
|
|
13
|
-
export const registerLifecycleListener = (): Promise<void> => {
|
|
14
|
-
console.log('called registerLifecycleListener');
|
|
15
|
-
return Fula.registerLifecycleListener();
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Get gets the value corresponding to the given key from the local datastore.
|
|
20
|
-
// The key must be a valid ipld.Link.
|
|
21
|
-
* @param config
|
|
22
|
-
* @returns boolean
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
export const init = (
|
|
26
|
-
identity: string, //privateKey of did identity
|
|
27
|
-
storePath: string,
|
|
28
|
-
bloxAddr: string,
|
|
29
|
-
exchange: string,
|
|
30
|
-
autoFlush: boolean = false,
|
|
31
|
-
rootCid: string | null = null,
|
|
32
|
-
useRelay: boolean = true,
|
|
33
|
-
refresh: boolean = false
|
|
34
|
-
): Promise<{ peerId: string; rootCid: string }> => {
|
|
35
|
-
console.log(
|
|
36
|
-
'init in react-native started',
|
|
37
|
-
identity,
|
|
38
|
-
storePath,
|
|
39
|
-
bloxAddr,
|
|
40
|
-
exchange,
|
|
41
|
-
autoFlush,
|
|
42
|
-
useRelay
|
|
43
|
-
);
|
|
44
|
-
return Fula.initFula(
|
|
45
|
-
identity,
|
|
46
|
-
storePath,
|
|
47
|
-
bloxAddr,
|
|
48
|
-
exchange,
|
|
49
|
-
autoFlush,
|
|
50
|
-
rootCid,
|
|
51
|
-
useRelay,
|
|
52
|
-
refresh
|
|
53
|
-
);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get gets the value corresponding to the given key from the local datastore.
|
|
58
|
-
// The key must be a valid ipld.Link.
|
|
59
|
-
* @param config
|
|
60
|
-
* @returns boolean
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
export const newClient = (
|
|
64
|
-
identity: string, //privateKey of did identity
|
|
65
|
-
storePath: string,
|
|
66
|
-
bloxAddr: string,
|
|
67
|
-
exchange: string,
|
|
68
|
-
autoFlush: boolean = false,
|
|
69
|
-
useRelay: boolean = true,
|
|
70
|
-
refresh: boolean = false
|
|
71
|
-
): Promise<string> => {
|
|
72
|
-
console.log(
|
|
73
|
-
'newClient in react-native started',
|
|
74
|
-
identity,
|
|
75
|
-
storePath,
|
|
76
|
-
bloxAddr,
|
|
77
|
-
exchange,
|
|
78
|
-
autoFlush,
|
|
79
|
-
useRelay,
|
|
80
|
-
refresh
|
|
81
|
-
);
|
|
82
|
-
return Fula.newClient(
|
|
83
|
-
identity,
|
|
84
|
-
storePath,
|
|
85
|
-
bloxAddr,
|
|
86
|
-
exchange,
|
|
87
|
-
autoFlush,
|
|
88
|
-
useRelay,
|
|
89
|
-
refresh
|
|
90
|
-
);
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* rm removes all data
|
|
95
|
-
* @param path
|
|
96
|
-
* @returns string: new cid of the root
|
|
97
|
-
*/
|
|
98
|
-
export const logout = (
|
|
99
|
-
identity: string,
|
|
100
|
-
storePath: string
|
|
101
|
-
): Promise<boolean> => {
|
|
102
|
-
return Fula.logout(identity, storePath);
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Checks if there are any un-synced changes on the device
|
|
107
|
-
*/
|
|
108
|
-
export const checkFailedActions = (
|
|
109
|
-
retry: boolean = false,
|
|
110
|
-
timeout: number = 20
|
|
111
|
-
): Promise<boolean> => {
|
|
112
|
-
return Fula.checkFailedActions(retry, timeout);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Lists the cids that failed to be sent to backend and are kept only locally
|
|
117
|
-
*/
|
|
118
|
-
export const listFailedActions = (cids: string[] = []): Promise<string[]> => {
|
|
119
|
-
return Fula.listFailedActions(cids);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Lists the cids that are recent
|
|
124
|
-
*/
|
|
125
|
-
export const listRecentCidsAsString = (): Promise<string[]> => {
|
|
126
|
-
return Fula.listRecentCidsAsString();
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Clears the cids that ar recent
|
|
131
|
-
*/
|
|
132
|
-
export const clearCidsFromRecent = (cids: string[] = []): Promise<boolean> => {
|
|
133
|
-
return Fula.clearCidsFromRecent(cids);
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Checks if there are any un-synced changes on the device
|
|
138
|
-
*/
|
|
139
|
-
export const checkConnection = (timeout: number = 20): Promise<boolean> => {
|
|
140
|
-
return Fula.checkConnection(timeout);
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Get gets the value corresponding to the given key from the local datastore.
|
|
145
|
-
// The key must be a valid ipld.Link.
|
|
146
|
-
* @param key
|
|
147
|
-
* @returns value
|
|
148
|
-
*/
|
|
149
|
-
export const get = (key: string): Promise<string> => {
|
|
150
|
-
return Fula.get(key);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Has checks whether the value corresponding to the given key is present in the local datastore.
|
|
155
|
-
// The key must be a valid ipld.Link.
|
|
156
|
-
* @param key
|
|
157
|
-
* @returns boolean
|
|
158
|
-
*/
|
|
159
|
-
export const has = (key: Uint8Array): Promise<boolean> => {
|
|
160
|
-
return Fula.has(key);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Push requests the given addr to download the root cid from this node.
|
|
165
|
-
// The addr must be a valid multiaddr that includes peer ID.
|
|
166
|
-
// this function.
|
|
167
|
-
* @param addr
|
|
168
|
-
* @returns null or error
|
|
169
|
-
*/
|
|
170
|
-
export const push = (): Promise<string> => {
|
|
171
|
-
return Fula.push();
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
//This method sends some test data to backedn
|
|
175
|
-
export const testData = (
|
|
176
|
-
identity: string,
|
|
177
|
-
bloxAddr: string
|
|
178
|
-
): Promise<string> => {
|
|
179
|
-
return Fula.testData(identity, bloxAddr);
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Put stores the given key value onto the local datastore.
|
|
184
|
-
// The key must be a valid ipld.Link and the value must be the valid encoded ipld.Node corresponding
|
|
185
|
-
// to the given key.
|
|
186
|
-
* @param key, value
|
|
187
|
-
* @returns null or string
|
|
188
|
-
*/
|
|
189
|
-
export const put = (value: string, codec: string): Promise<string> => {
|
|
190
|
-
return Fula.put(value, codec);
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* mkdir creates a directory at the given path.
|
|
195
|
-
* @param path
|
|
196
|
-
* @returns string: new cid of the root
|
|
197
|
-
*/
|
|
198
|
-
export const mkdir = (path: string): Promise<string> => {
|
|
199
|
-
return Fula.mkdir(path);
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* writeFileContent writes content at a given path
|
|
204
|
-
* @param path
|
|
205
|
-
* @returns string: new cid of the root
|
|
206
|
-
*/
|
|
207
|
-
export const writeFileContent = (
|
|
208
|
-
path: string,
|
|
209
|
-
content: string
|
|
210
|
-
): Promise<string> => {
|
|
211
|
-
return Fula.writeFileContent(path, content);
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
/*
|
|
215
|
-
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
216
|
-
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
217
|
-
// It keeps the original file modiifcation date
|
|
218
|
-
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
219
|
-
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
220
|
-
// Returns: new cid of the root after this file is placed in the tree
|
|
221
|
-
*/
|
|
222
|
-
export const writeFile = (
|
|
223
|
-
fulaTargetFilename: string,
|
|
224
|
-
localFilename: string
|
|
225
|
-
): Promise<string> => {
|
|
226
|
-
return Fula.writeFile(fulaTargetFilename, localFilename);
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* ls lists the name of files and folders at a given path
|
|
231
|
-
* @param path
|
|
232
|
-
* @returns string: list of items
|
|
233
|
-
* TODO: Findout how is the string and convert to array
|
|
234
|
-
*/
|
|
235
|
-
export const ls = (path: string): Promise<void | JSON> => {
|
|
236
|
-
return Fula.ls(path)
|
|
237
|
-
.then((res) => {
|
|
238
|
-
let lsResult = [];
|
|
239
|
-
let lsRows = res.split('!!!');
|
|
240
|
-
for (const element of lsRows) {
|
|
241
|
-
let rowItems = element.split('???');
|
|
242
|
-
if (rowItems && rowItems[0]) {
|
|
243
|
-
let item = {
|
|
244
|
-
name: '',
|
|
245
|
-
created: '',
|
|
246
|
-
modified: '',
|
|
247
|
-
};
|
|
248
|
-
item.name = rowItems[0];
|
|
249
|
-
if (rowItems[1]) {
|
|
250
|
-
item.created = rowItems[1];
|
|
251
|
-
}
|
|
252
|
-
if (rowItems[2]) {
|
|
253
|
-
item.modified = rowItems[2];
|
|
254
|
-
}
|
|
255
|
-
lsResult.push(item);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
let jsonRes = JSON.parse(JSON.stringify(lsResult));
|
|
259
|
-
return jsonRes;
|
|
260
|
-
})
|
|
261
|
-
.catch((e) => {
|
|
262
|
-
return e;
|
|
263
|
-
});
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* rm removes all files and folders at a given path
|
|
268
|
-
* @param path
|
|
269
|
-
* @returns string: new cid of the root
|
|
270
|
-
*/
|
|
271
|
-
export const rm = (path: string): Promise<string> => {
|
|
272
|
-
return Fula.rm(path);
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* cp copies the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
|
|
277
|
-
* @param sourcePath, targetPath
|
|
278
|
-
* @returns string: new cid of the root
|
|
279
|
-
*/
|
|
280
|
-
export const cp = (sourcePath: string, targetPath: string): Promise<string> => {
|
|
281
|
-
return Fula.cp(sourcePath, targetPath);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* mv moves the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
|
|
286
|
-
* @param sourcePath, targetPath
|
|
287
|
-
* @returns string: new cid of the root
|
|
288
|
-
*/
|
|
289
|
-
export const mv = (sourcePath: string, targetPath: string): Promise<string> => {
|
|
290
|
-
return Fula.mv(sourcePath, targetPath);
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
/*
|
|
294
|
-
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
295
|
-
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
296
|
-
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
297
|
-
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
298
|
-
// Returns: new cid of the root after this file is placed in the tree
|
|
299
|
-
*/
|
|
300
|
-
export const readFile = (
|
|
301
|
-
fulaTargetFilename: string,
|
|
302
|
-
localFilename: string
|
|
303
|
-
): Promise<string> => {
|
|
304
|
-
return Fula.readFile(fulaTargetFilename, localFilename);
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* readFile reads content of a given path
|
|
309
|
-
* @param path
|
|
310
|
-
* @returns string: cotent
|
|
311
|
-
*/
|
|
312
|
-
export const readFileContent = (path: string): Promise<string> => {
|
|
313
|
-
return Fula.readFileContent(path);
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Shutdown closes all resources used by Client.
|
|
318
|
-
// After calling this function Client must be discarded.
|
|
319
|
-
* @param
|
|
320
|
-
* @returns
|
|
321
|
-
*/
|
|
322
|
-
export const shutdown = (): Promise<void> => {
|
|
323
|
-
return Fula.shutdown();
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* setAuth adds or removes a peer from the list of peers that are allowed to push to this node.
|
|
328
|
-
* This can only be called on a peer that is added as an owner of blox by --authorizer parameter
|
|
329
|
-
* @param peerId, allow
|
|
330
|
-
* @returns boolean: true if successful or false if not
|
|
331
|
-
*/
|
|
332
|
-
export const setAuth = (peerId: string, allow: boolean): Promise<boolean> => {
|
|
333
|
-
return Fula.setAuth(peerId, allow);
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* isReady checks if the connection is ready to be used.
|
|
338
|
-
* @param filesystemCheck: also check if the wnfs is ready
|
|
339
|
-
* @returns boolean: true if ready or false if not
|
|
340
|
-
*/
|
|
341
|
-
export const isReady = (filesystemCheck: boolean = true): Promise<boolean> => {
|
|
342
|
-
return Fula.isReady(filesystemCheck);
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* replicate replicates data on the nework
|
|
347
|
-
*/
|
|
348
|
-
export const replicateRecentCids = async (
|
|
349
|
-
api: ApiPromise,
|
|
350
|
-
seed: string,
|
|
351
|
-
poolId: number,
|
|
352
|
-
replicationNo: number = 4
|
|
353
|
-
): Promise<{ status: boolean; msg: string }> => {
|
|
354
|
-
let status = true;
|
|
355
|
-
let msg = '';
|
|
356
|
-
if (!api) {
|
|
357
|
-
api = await chainApiInit();
|
|
358
|
-
}
|
|
359
|
-
if (api) {
|
|
360
|
-
console.log('uploading manifests');
|
|
361
|
-
try {
|
|
362
|
-
let account = await getAccountIdFromSeed(seed);
|
|
363
|
-
console.log('account: ' + account);
|
|
364
|
-
const accountBal = await checkAccountBalance(api, account);
|
|
365
|
-
console.log('account balance: ' + accountBal);
|
|
366
|
-
if (accountBal !== '0') {
|
|
367
|
-
const recentCids = await listRecentCidsAsString();
|
|
368
|
-
console.log(recentCids);
|
|
369
|
-
if (recentCids) {
|
|
370
|
-
console.log({
|
|
371
|
-
api,
|
|
372
|
-
seed,
|
|
373
|
-
recentCids,
|
|
374
|
-
poolId,
|
|
375
|
-
replicationNo,
|
|
376
|
-
});
|
|
377
|
-
const res = await batchUploadManifest(
|
|
378
|
-
api,
|
|
379
|
-
seed,
|
|
380
|
-
recentCids,
|
|
381
|
-
poolId,
|
|
382
|
-
replicationNo
|
|
383
|
-
);
|
|
384
|
-
console.log('batchUploadManifest res received');
|
|
385
|
-
console.log(res);
|
|
386
|
-
if (res && res.hash) {
|
|
387
|
-
const signedBlock = await api.rpc.chain.getBlock(res.hash);
|
|
388
|
-
if (signedBlock?.block?.extrinsics?.length) {
|
|
389
|
-
await clearCidsFromRecent(recentCids);
|
|
390
|
-
msg = res.hash;
|
|
391
|
-
} else {
|
|
392
|
-
status = false;
|
|
393
|
-
msg = 'block data is not found';
|
|
394
|
-
}
|
|
395
|
-
} else {
|
|
396
|
-
status = false;
|
|
397
|
-
msg = 'hash is not returned';
|
|
398
|
-
}
|
|
399
|
-
} else {
|
|
400
|
-
status = false;
|
|
401
|
-
msg = 'No recent Cids found';
|
|
402
|
-
}
|
|
403
|
-
} else {
|
|
404
|
-
status = false;
|
|
405
|
-
msg = 'Account balance is not enough or account does not exists';
|
|
406
|
-
}
|
|
407
|
-
} catch (e: any) {
|
|
408
|
-
console.log('res failed');
|
|
409
|
-
console.log(e);
|
|
410
|
-
let errorMessage = '';
|
|
411
|
-
|
|
412
|
-
if (e instanceof Error) {
|
|
413
|
-
// If it's an Error instance, use the message property
|
|
414
|
-
errorMessage = e.message;
|
|
415
|
-
} else {
|
|
416
|
-
// If it's not an Error instance, convert it to string
|
|
417
|
-
errorMessage = e.toString();
|
|
418
|
-
}
|
|
419
|
-
status = false;
|
|
420
|
-
msg = errorMessage;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// Return a value (true/false) depending on the outcome of the function
|
|
425
|
-
// For example:
|
|
426
|
-
return { status: status, msg: msg }; // or false, depending on your logic
|
|
427
|
-
};
|
|
1
|
+
import Fula from '../interfaces/fulaNativeModule';
|
|
2
|
+
import {
|
|
3
|
+
init as chainApiInit,
|
|
4
|
+
batchUploadManifest,
|
|
5
|
+
checkAccountBalance,
|
|
6
|
+
getAccountIdFromSeed,
|
|
7
|
+
} from './chain-api';
|
|
8
|
+
import { ApiPromise } from '@polkadot/api';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Register the app's lifecycle listeners to handle foreground, background, and termination states.
|
|
12
|
+
*/
|
|
13
|
+
export const registerLifecycleListener = (): Promise<void> => {
|
|
14
|
+
console.log('called registerLifecycleListener');
|
|
15
|
+
return Fula.registerLifecycleListener();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get gets the value corresponding to the given key from the local datastore.
|
|
20
|
+
// The key must be a valid ipld.Link.
|
|
21
|
+
* @param config
|
|
22
|
+
* @returns boolean
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const init = (
|
|
26
|
+
identity: string, //privateKey of did identity
|
|
27
|
+
storePath: string,
|
|
28
|
+
bloxAddr: string,
|
|
29
|
+
exchange: string,
|
|
30
|
+
autoFlush: boolean = false,
|
|
31
|
+
rootCid: string | null = null,
|
|
32
|
+
useRelay: boolean = true,
|
|
33
|
+
refresh: boolean = false
|
|
34
|
+
): Promise<{ peerId: string; rootCid: string }> => {
|
|
35
|
+
console.log(
|
|
36
|
+
'init in react-native started',
|
|
37
|
+
identity,
|
|
38
|
+
storePath,
|
|
39
|
+
bloxAddr,
|
|
40
|
+
exchange,
|
|
41
|
+
autoFlush,
|
|
42
|
+
useRelay
|
|
43
|
+
);
|
|
44
|
+
return Fula.initFula(
|
|
45
|
+
identity,
|
|
46
|
+
storePath,
|
|
47
|
+
bloxAddr,
|
|
48
|
+
exchange,
|
|
49
|
+
autoFlush,
|
|
50
|
+
rootCid,
|
|
51
|
+
useRelay,
|
|
52
|
+
refresh
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get gets the value corresponding to the given key from the local datastore.
|
|
58
|
+
// The key must be a valid ipld.Link.
|
|
59
|
+
* @param config
|
|
60
|
+
* @returns boolean
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
export const newClient = (
|
|
64
|
+
identity: string, //privateKey of did identity
|
|
65
|
+
storePath: string,
|
|
66
|
+
bloxAddr: string,
|
|
67
|
+
exchange: string,
|
|
68
|
+
autoFlush: boolean = false,
|
|
69
|
+
useRelay: boolean = true,
|
|
70
|
+
refresh: boolean = false
|
|
71
|
+
): Promise<string> => {
|
|
72
|
+
console.log(
|
|
73
|
+
'newClient in react-native started',
|
|
74
|
+
identity,
|
|
75
|
+
storePath,
|
|
76
|
+
bloxAddr,
|
|
77
|
+
exchange,
|
|
78
|
+
autoFlush,
|
|
79
|
+
useRelay,
|
|
80
|
+
refresh
|
|
81
|
+
);
|
|
82
|
+
return Fula.newClient(
|
|
83
|
+
identity,
|
|
84
|
+
storePath,
|
|
85
|
+
bloxAddr,
|
|
86
|
+
exchange,
|
|
87
|
+
autoFlush,
|
|
88
|
+
useRelay,
|
|
89
|
+
refresh
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* rm removes all data
|
|
95
|
+
* @param path
|
|
96
|
+
* @returns string: new cid of the root
|
|
97
|
+
*/
|
|
98
|
+
export const logout = (
|
|
99
|
+
identity: string,
|
|
100
|
+
storePath: string
|
|
101
|
+
): Promise<boolean> => {
|
|
102
|
+
return Fula.logout(identity, storePath);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Checks if there are any un-synced changes on the device
|
|
107
|
+
*/
|
|
108
|
+
export const checkFailedActions = (
|
|
109
|
+
retry: boolean = false,
|
|
110
|
+
timeout: number = 20
|
|
111
|
+
): Promise<boolean> => {
|
|
112
|
+
return Fula.checkFailedActions(retry, timeout);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Lists the cids that failed to be sent to backend and are kept only locally
|
|
117
|
+
*/
|
|
118
|
+
export const listFailedActions = (cids: string[] = []): Promise<string[]> => {
|
|
119
|
+
return Fula.listFailedActions(cids);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Lists the cids that are recent
|
|
124
|
+
*/
|
|
125
|
+
export const listRecentCidsAsString = (): Promise<string[]> => {
|
|
126
|
+
return Fula.listRecentCidsAsString();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Clears the cids that ar recent
|
|
131
|
+
*/
|
|
132
|
+
export const clearCidsFromRecent = (cids: string[] = []): Promise<boolean> => {
|
|
133
|
+
return Fula.clearCidsFromRecent(cids);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Checks if there are any un-synced changes on the device
|
|
138
|
+
*/
|
|
139
|
+
export const checkConnection = (timeout: number = 20): Promise<boolean> => {
|
|
140
|
+
return Fula.checkConnection(timeout);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get gets the value corresponding to the given key from the local datastore.
|
|
145
|
+
// The key must be a valid ipld.Link.
|
|
146
|
+
* @param key
|
|
147
|
+
* @returns value
|
|
148
|
+
*/
|
|
149
|
+
export const get = (key: string): Promise<string> => {
|
|
150
|
+
return Fula.get(key);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Has checks whether the value corresponding to the given key is present in the local datastore.
|
|
155
|
+
// The key must be a valid ipld.Link.
|
|
156
|
+
* @param key
|
|
157
|
+
* @returns boolean
|
|
158
|
+
*/
|
|
159
|
+
export const has = (key: Uint8Array): Promise<boolean> => {
|
|
160
|
+
return Fula.has(key);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Push requests the given addr to download the root cid from this node.
|
|
165
|
+
// The addr must be a valid multiaddr that includes peer ID.
|
|
166
|
+
// this function.
|
|
167
|
+
* @param addr
|
|
168
|
+
* @returns null or error
|
|
169
|
+
*/
|
|
170
|
+
export const push = (): Promise<string> => {
|
|
171
|
+
return Fula.push();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
//This method sends some test data to backedn
|
|
175
|
+
export const testData = (
|
|
176
|
+
identity: string,
|
|
177
|
+
bloxAddr: string
|
|
178
|
+
): Promise<string> => {
|
|
179
|
+
return Fula.testData(identity, bloxAddr);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Put stores the given key value onto the local datastore.
|
|
184
|
+
// The key must be a valid ipld.Link and the value must be the valid encoded ipld.Node corresponding
|
|
185
|
+
// to the given key.
|
|
186
|
+
* @param key, value
|
|
187
|
+
* @returns null or string
|
|
188
|
+
*/
|
|
189
|
+
export const put = (value: string, codec: string): Promise<string> => {
|
|
190
|
+
return Fula.put(value, codec);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* mkdir creates a directory at the given path.
|
|
195
|
+
* @param path
|
|
196
|
+
* @returns string: new cid of the root
|
|
197
|
+
*/
|
|
198
|
+
export const mkdir = (path: string): Promise<string> => {
|
|
199
|
+
return Fula.mkdir(path);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* writeFileContent writes content at a given path
|
|
204
|
+
* @param path
|
|
205
|
+
* @returns string: new cid of the root
|
|
206
|
+
*/
|
|
207
|
+
export const writeFileContent = (
|
|
208
|
+
path: string,
|
|
209
|
+
content: string
|
|
210
|
+
): Promise<string> => {
|
|
211
|
+
return Fula.writeFileContent(path, content);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/*
|
|
215
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
216
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
217
|
+
// It keeps the original file modiifcation date
|
|
218
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
219
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
220
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
221
|
+
*/
|
|
222
|
+
export const writeFile = (
|
|
223
|
+
fulaTargetFilename: string,
|
|
224
|
+
localFilename: string
|
|
225
|
+
): Promise<string> => {
|
|
226
|
+
return Fula.writeFile(fulaTargetFilename, localFilename);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* ls lists the name of files and folders at a given path
|
|
231
|
+
* @param path
|
|
232
|
+
* @returns string: list of items
|
|
233
|
+
* TODO: Findout how is the string and convert to array
|
|
234
|
+
*/
|
|
235
|
+
export const ls = (path: string): Promise<void | JSON> => {
|
|
236
|
+
return Fula.ls(path)
|
|
237
|
+
.then((res) => {
|
|
238
|
+
let lsResult = [];
|
|
239
|
+
let lsRows = res.split('!!!');
|
|
240
|
+
for (const element of lsRows) {
|
|
241
|
+
let rowItems = element.split('???');
|
|
242
|
+
if (rowItems && rowItems[0]) {
|
|
243
|
+
let item = {
|
|
244
|
+
name: '',
|
|
245
|
+
created: '',
|
|
246
|
+
modified: '',
|
|
247
|
+
};
|
|
248
|
+
item.name = rowItems[0];
|
|
249
|
+
if (rowItems[1]) {
|
|
250
|
+
item.created = rowItems[1];
|
|
251
|
+
}
|
|
252
|
+
if (rowItems[2]) {
|
|
253
|
+
item.modified = rowItems[2];
|
|
254
|
+
}
|
|
255
|
+
lsResult.push(item);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
let jsonRes = JSON.parse(JSON.stringify(lsResult));
|
|
259
|
+
return jsonRes;
|
|
260
|
+
})
|
|
261
|
+
.catch((e) => {
|
|
262
|
+
return e;
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* rm removes all files and folders at a given path
|
|
268
|
+
* @param path
|
|
269
|
+
* @returns string: new cid of the root
|
|
270
|
+
*/
|
|
271
|
+
export const rm = (path: string): Promise<string> => {
|
|
272
|
+
return Fula.rm(path);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* cp copies the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
|
|
277
|
+
* @param sourcePath, targetPath
|
|
278
|
+
* @returns string: new cid of the root
|
|
279
|
+
*/
|
|
280
|
+
export const cp = (sourcePath: string, targetPath: string): Promise<string> => {
|
|
281
|
+
return Fula.cp(sourcePath, targetPath);
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* mv moves the file or folder at the sourcePath to targetPath. targetPath is a folder that must exist already
|
|
286
|
+
* @param sourcePath, targetPath
|
|
287
|
+
* @returns string: new cid of the root
|
|
288
|
+
*/
|
|
289
|
+
export const mv = (sourcePath: string, targetPath: string): Promise<string> => {
|
|
290
|
+
return Fula.mv(sourcePath, targetPath);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/*
|
|
294
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
295
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
296
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
297
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
298
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
299
|
+
*/
|
|
300
|
+
export const readFile = (
|
|
301
|
+
fulaTargetFilename: string,
|
|
302
|
+
localFilename: string
|
|
303
|
+
): Promise<string> => {
|
|
304
|
+
return Fula.readFile(fulaTargetFilename, localFilename);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* readFile reads content of a given path
|
|
309
|
+
* @param path
|
|
310
|
+
* @returns string: cotent
|
|
311
|
+
*/
|
|
312
|
+
export const readFileContent = (path: string): Promise<string> => {
|
|
313
|
+
return Fula.readFileContent(path);
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Shutdown closes all resources used by Client.
|
|
318
|
+
// After calling this function Client must be discarded.
|
|
319
|
+
* @param
|
|
320
|
+
* @returns
|
|
321
|
+
*/
|
|
322
|
+
export const shutdown = (): Promise<void> => {
|
|
323
|
+
return Fula.shutdown();
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* setAuth adds or removes a peer from the list of peers that are allowed to push to this node.
|
|
328
|
+
* This can only be called on a peer that is added as an owner of blox by --authorizer parameter
|
|
329
|
+
* @param peerId, allow
|
|
330
|
+
* @returns boolean: true if successful or false if not
|
|
331
|
+
*/
|
|
332
|
+
export const setAuth = (peerId: string, allow: boolean): Promise<boolean> => {
|
|
333
|
+
return Fula.setAuth(peerId, allow);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* isReady checks if the connection is ready to be used.
|
|
338
|
+
* @param filesystemCheck: also check if the wnfs is ready
|
|
339
|
+
* @returns boolean: true if ready or false if not
|
|
340
|
+
*/
|
|
341
|
+
export const isReady = (filesystemCheck: boolean = true): Promise<boolean> => {
|
|
342
|
+
return Fula.isReady(filesystemCheck);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* replicate replicates data on the nework
|
|
347
|
+
*/
|
|
348
|
+
export const replicateRecentCids = async (
|
|
349
|
+
api: ApiPromise,
|
|
350
|
+
seed: string,
|
|
351
|
+
poolId: number,
|
|
352
|
+
replicationNo: number = 4
|
|
353
|
+
): Promise<{ status: boolean; msg: string }> => {
|
|
354
|
+
let status = true;
|
|
355
|
+
let msg = '';
|
|
356
|
+
if (!api) {
|
|
357
|
+
api = await chainApiInit();
|
|
358
|
+
}
|
|
359
|
+
if (api) {
|
|
360
|
+
console.log('uploading manifests');
|
|
361
|
+
try {
|
|
362
|
+
let account = await getAccountIdFromSeed(seed);
|
|
363
|
+
console.log('account: ' + account);
|
|
364
|
+
const accountBal = await checkAccountBalance(api, account);
|
|
365
|
+
console.log('account balance: ' + accountBal);
|
|
366
|
+
if (accountBal !== '0') {
|
|
367
|
+
const recentCids = await listRecentCidsAsString();
|
|
368
|
+
console.log(recentCids);
|
|
369
|
+
if (recentCids) {
|
|
370
|
+
console.log({
|
|
371
|
+
api,
|
|
372
|
+
seed,
|
|
373
|
+
recentCids,
|
|
374
|
+
poolId,
|
|
375
|
+
replicationNo,
|
|
376
|
+
});
|
|
377
|
+
const res = await batchUploadManifest(
|
|
378
|
+
api,
|
|
379
|
+
seed,
|
|
380
|
+
recentCids,
|
|
381
|
+
poolId,
|
|
382
|
+
replicationNo
|
|
383
|
+
);
|
|
384
|
+
console.log('batchUploadManifest res received');
|
|
385
|
+
console.log(res);
|
|
386
|
+
if (res && res.hash) {
|
|
387
|
+
const signedBlock = await api.rpc.chain.getBlock(res.hash);
|
|
388
|
+
if (signedBlock?.block?.extrinsics?.length) {
|
|
389
|
+
await clearCidsFromRecent(recentCids);
|
|
390
|
+
msg = res.hash;
|
|
391
|
+
} else {
|
|
392
|
+
status = false;
|
|
393
|
+
msg = 'block data is not found';
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
status = false;
|
|
397
|
+
msg = 'hash is not returned';
|
|
398
|
+
}
|
|
399
|
+
} else {
|
|
400
|
+
status = false;
|
|
401
|
+
msg = 'No recent Cids found';
|
|
402
|
+
}
|
|
403
|
+
} else {
|
|
404
|
+
status = false;
|
|
405
|
+
msg = 'Account balance is not enough or account does not exists';
|
|
406
|
+
}
|
|
407
|
+
} catch (e: any) {
|
|
408
|
+
console.log('res failed');
|
|
409
|
+
console.log(e);
|
|
410
|
+
let errorMessage = '';
|
|
411
|
+
|
|
412
|
+
if (e instanceof Error) {
|
|
413
|
+
// If it's an Error instance, use the message property
|
|
414
|
+
errorMessage = e.message;
|
|
415
|
+
} else {
|
|
416
|
+
// If it's not an Error instance, convert it to string
|
|
417
|
+
errorMessage = e.toString();
|
|
418
|
+
}
|
|
419
|
+
status = false;
|
|
420
|
+
msg = errorMessage;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Return a value (true/false) depending on the outcome of the function
|
|
425
|
+
// For example:
|
|
426
|
+
return { status: status, msg: msg }; // or false, depending on your logic
|
|
427
|
+
};
|