-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathblockchain.ts
More file actions
521 lines (498 loc) · 16.4 KB
/
blockchain.ts
File metadata and controls
521 lines (498 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import Fula from '../interfaces/fulaNativeModule';
import type * as BType from '../types/blockchain';
/*
createAccount: This function takes a seed argument, which is used to create an account. The seed must start with "/". The function returns a promise of an object that contains the seed and the account that was created.
*/
export const createAccount = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string //seed that is used to create the account. It must start with "/"
): Promise<BType.SeededResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log('createAccount in react-native started', seed);
let res = Fula.createAccount(seed)
.then((res) => {
try {
let jsonRes: BType.SeededResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
checkAccountExists: This function takes an account argument, and returns a promise of an object that contains the account and a boolean exists flag. If exists is true, it means the account exists, otherwise, the account does not exist
*/
export const checkAccountExists = (
account: string
): Promise<BType.AccountExistsResponse> => {
console.log('checkAccountExists in react-native started', account);
let res = Fula.checkAccountExists(account)
.then((res) => {
try {
let jsonRes: BType.AccountExistsResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
createPool: This function takes two arguments: seed and poolName. The seed is used to identify the account that is creating the pool, and the poolName is the name of the pool being created. The function returns a promise of an object that contains the owner of the pool and the poolID of the created pool.
*/
export const createPool = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolName: string
): Promise<BType.PoolCreateResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log('createPool in react-native started', seed, poolName);
let res = Fula.createPool(seed, poolName)
.then((res) => {
try {
let jsonRes: BType.PoolCreateResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
listPools: This function takes no arguments and returns a promise of an object that contains a list of pools. Each pool in the list contains the poolID, owner, poolName, parent, and participants of the pool
*/
export const listPools = (): Promise<BType.PoolListResponse> => {
console.log('listPools in react-native started');
let res = Fula.listPools()
.then((res) => {
try {
let jsonRes: BType.PoolListResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
joinPool: This function takes two arguments: seed and poolID. The seed is used to identify the account that is joining the pool, and the poolID is the ID of the pool the account is joining. The function returns a promise of an object that contains the account joining the pool and the poolID of the joined pool.
*/
export const joinPool = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number
): Promise<BType.PoolJoinResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log('joinPool in react-native started', seed, poolID);
let res = Fula.joinPool(seed, poolID)
.then((res) => {
try {
let jsonRes: BType.PoolJoinResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
leavePool: This function takes two arguments: seed and poolID. The seed is used to identify the account that is leaving the pool, and the poolID is the ID of the pool the account is leaving. The function returns a promise of an object that contains the `
*/
export const leavePool = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number
): Promise<BType.PoolLeaveResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log('leavePool in react-native started', seed, poolID);
let res = Fula.leavePool(seed, poolID)
.then((res) => {
try {
let jsonRes: BType.PoolLeaveResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
export const cancelPoolJoin = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number
): Promise<BType.PoolCancelJoinResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log('cancelPoolJoin in react-native started', seed, poolID);
let res = Fula.cancelPoolJoin(seed, poolID)
.then((res) => {
try {
let jsonRes: BType.PoolCancelJoinResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
export const listPoolJoinRequests = (
poolID: number
): Promise<BType.PoolRequestsResponse> => {
console.log('listPoolJoinRequests in react-native started', poolID);
let res = Fula.listPoolJoinRequests(poolID)
.then((res) => {
try {
let jsonRes: BType.PoolRequestsResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
seed is used to authorize the request.
poolID is the ID of the pool in which the account is requesting to join.
account is the account that is requesting to join the pool.
accept is a boolean value that indicates whether to accept or reject the join request.
It returns a promise of BType.PoolVoteResponse which includes the account and poolID
*/
export const votePoolJoinRequest = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number,
account: string,
accept: boolean
): Promise<BType.PoolVoteResponse> => {
// SBP-M1 review: seed phrase exposed in logs, remove this.
console.log(
'votePoolJoinRequest in react-native started',
seed,
poolID,
account,
accept
);
let res = Fula.votePoolJoinRequest(seed, poolID, account, accept)
.then((res) => {
try {
let jsonRes: BType.PoolVoteResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes four arguments:
seed is used to authorize the request.
poolID is the ID of the pool in which the replication request is made.
replicationFactor is the number of copies of the content to be stored.
cid is the content identifier of the content to be replicated.
It returns a promise of BType.ManifestUploadResponse which includes the uploader, storage, ManifestMetadata, and poolID
*/
export const newReplicationRequest = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number,
replicationFactor: number,
cid: string
): Promise<BType.ManifestUploadResponse> => {
console.log(
'newReplicationRequest in react-native started',
seed,
poolID,
replicationFactor,
cid
);
let res = Fula.newReplicationRequest(seed, poolID, replicationFactor, cid)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes four arguments:
seed is used to authorize the request.
poolID is the ID of the pool in which the replication request is made.
uploader is the account of the user making the request
cid is the content identifier of the content to be stored.
It returns a promise of BType.ManifestUploadResponse which includes the uploader, storage, ManifestMetadata, and poolID
*/
export const newStoreRequest = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number,
uploader: string,
cid: string
): Promise<BType.ManifestUploadResponse> => {
console.log(
'newStoreRequest in react-native started',
seed,
poolID,
uploader,
cid
);
let res = Fula.newStoreRequest(seed, poolID, uploader, cid)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes one argument:
poolID is the ID of the pool for which the replication requests are listed
It returns a promise of BType.ManifestUploadResponse[] which is an array of the replication requests, including the uploader, storage, ManifestMetadata, and poolID
*/
export const listAvailableReplicationRequests = (
poolID: number
): Promise<BType.ManifestUploadResponse[]> => {
console.log(
'listAvailableReplicationRequests in react-native started',
poolID
);
let res = Fula.listAvailableReplicationRequests(poolID)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse[] = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes three arguments:
seed is the seed of the account that is removing the replication request
poolID is the ID of the pool for which the replication request is being removed
cid is the content ID of the replication request being removed
It returns a promise of BType.ManifestUploadResponse which is the removed replication request, including the uploader, storage, ManifestMetadata, and poolID
*/
export const removeReplicationRequest = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
poolID: number,
cid: string
): Promise<BType.ManifestUploadResponse> => {
console.log(
'removeReplicationRequest in react-native started',
seed,
poolID,
cid
);
let res = Fula.removeReplicationRequest(seed, poolID, cid)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes four arguments:
seed is the seed of the account that is removing the storer
storer is the address of the storer that is being removed
poolID is the ID of the pool for which the storer is being removed
cid is the content ID of the replication request for which the storer is being removed
It returns a promise of BType.ManifestUploadResponse which is the replication request, including the uploader, storage, ManifestMetadata, and poolID after the storer has been removed.
*/
export const removeStorer = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
storer: string,
poolID: number,
cid: string
): Promise<BType.ManifestUploadResponse> => {
console.log(
'removeStorer in react-native started',
seed,
storer,
poolID,
cid
);
let res = Fula.removeStorer(seed, storer, poolID, cid)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
It takes four arguments:
seed is the seed of the account that is removing the stored replication
uploader is the address of the uploader that is being removed
poolID is the ID of the pool for which the stored replication is being removed
cid is the content ID of the replication request for which the stored replication is being removed
It returns a promise of BType.ManifestUploadResponse which is the replication request, including the uploader, storage, ManifestMetadata, and poolID after the stored replication has been removed.
*/
export const removeStoredReplication = (
// SBP-M1 review: create seed on device and store securely, then use PolkadotJS API to sign an extrinsic which can then be submitted to the node/api. The seed should never leave the device. Remove the seed from here.
seed: string,
uploader: string,
poolID: number,
cid: string
): Promise<BType.ManifestUploadResponse> => {
console.log(
'removeStoredReplication in react-native started',
seed,
uploader,
poolID,
cid
);
let res = Fula.removeStoredReplication(seed, uploader, poolID, cid)
.then((res) => {
try {
let jsonRes: BType.ManifestUploadResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};
/*
bloxFreeSpace: This function takes no arguments and returns a promise of an object that contains the blox free space information.
*/
export const bloxFreeSpace = (): Promise<BType.BloxFreeSpaceResponse> => {
console.log('bloxFreeSpace in react-native started');
let res = Fula.bloxFreeSpace()
.then((res) => {
try {
let jsonRes: BType.BloxFreeSpaceResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e) {
return res;
}
}
})
.catch((err) => {
return err;
});
return res;
};