forked from BananoCoin/bananojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1561 lines (1475 loc) · 50.5 KB
/
index.js
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// STARTED TOP nodejs/browser hack
(function() {
// FINISHED TOP nodejs/browser hack
const bananoUtil = require('./app/scripts/banano-util.js');
const realBananodeApi = require('./app/scripts/bananode-api.js');
const camoUtil = require('./app/scripts/camo-util.js');
const depositUtil = require('./app/scripts/deposit-util.js');
const withdrawUtil = require('./app/scripts/withdraw-util.js');
const loggingUtil = require('./app/scripts/logging-util.js');
/**
* @constant {string}
* @memberof Main
*/
const BANANO_PREFIX = 'ban_';
const NANO_PREFIX = 'nano_';
let bananodeApi = realBananodeApi;
/**
* Sets the Bananode Api (useful for overriding some methods)
* @memberof Main
* @param {string} _bananodeApi the new bananodeApi
* @return {undefined} returns nothing.
*/
const setBananodeApi = (_bananodeApi) => {
bananodeApi = _bananodeApi;
};
/**
* Sets the Bananode Api Authorization
* @memberof Main
* @param {string} auth the new authorization
* @return {undefined} returns nothing.
*/
const setAuth = (auth) => {
if (bananodeApi !== undefined) {
bananodeApi.setAuth(auth);
}
};
/**
* Sets the Bananode Api Headers
* @memberof Main
* @param {string} headers the new headers
* @return {undefined} returns nothing.
*/
const setHeaders = (headers) => {
if (bananodeApi !== undefined) {
bananodeApi.setHeaders(headers);
}
};
/**
* Sets the Bananode Api Proxy (http pr https proxy)
* @memberof Main
* @param {Object} proxy the new proxy
* @return {undefined} returns nothing.
*/
const setBananodeApiProxy = (proxy) => {
bananodeApi.setModuleRef(proxy);
};
/**
* Gets the Bananode Api Proxy (http pr https proxy)
* @memberof Main
* @return {Object} returns the module.
*/
const getBananodeApiProxy = () => {
return bananodeApi.getModuleRef();
};
/**
* converts amount from decimal to bananoParts.
* @memberof BananoUtil
* @param {string} decimalAmount the decimal amount of bananos.
* @return {BananoParts} returns the banano parts of the decimal amount.
*/
const getBananoPartsFromDecimal = (decimalAmount) => {
const raw = getBananoDecimalAmountAsRaw(decimalAmount);
const bananoParts = getBananoPartsFromRaw(raw);
return bananoParts;
};
/**
* converts amount from bananoParts to decimal.
* @memberof BananoUtil
* @param {BananoParts} bananoParts the banano parts to describe.
* @return {string} returns the decimal amount of bananos.
*/
const getBananoPartsAsDecimal = (bananoParts) => {
let bananoDecimal = '';
const banano = bananoParts[bananoParts.majorName];
if (banano !== undefined) {
bananoDecimal += banano;
} else {
bananoDecimal += '0';
}
const banoshi = bananoParts[bananoParts.minorName];
if (banoshi !== undefined || bananoParts.raw !== undefined) {
bananoDecimal += '.';
}
if (banoshi !== undefined) {
if (banoshi.length == 1) {
bananoDecimal += '0';
}
bananoDecimal += banoshi;
}
if (bananoParts.raw !== undefined) {
if (banoshi === undefined) {
bananoDecimal += '00';
}
const count = 27 - bananoParts.raw.length;
if (count < 0) {
throw Error(
`too many numbers in bananoParts.raw '${
bananoParts.raw
}', remove ${-count} of them.`,
);
}
bananoDecimal += '0'.repeat(count);
bananoDecimal += bananoParts.raw;
}
return bananoDecimal;
};
/**
* converts amount from decimal to raw.
* @memberof BananoUtil
* @param {string} amount the decimal amount.
* @return {string} returns amount in raw.
*/
const getBananoDecimalAmountAsRaw = (amount) => {
const amountStr = amount.toString();
const decimal = amountStr.indexOf('.');
let bananoBigInt;
if (decimal < 0) {
bananoBigInt = BigInt(getRawStrFromBananoStr(amountStr));
} else {
bananoBigInt = BigInt(
getRawStrFromBananoStr(amountStr.substring(0, decimal)),
);
}
let banoshiBigInt;
if (decimal < 0) {
banoshiBigInt = BigInt(0);
} else {
let banoshiRaw = amountStr.substring(decimal + 1);
// console.log('banoshiRaw', banoshiRaw);
// console.log('banoshiRaw.length', banoshiRaw.length);
const count = 29 - banoshiRaw.length;
if (count < 0) {
throw Error(
`too many numbers past the decimal in '${amountStr}', remove ${-count} of them.`,
);
}
banoshiRaw += '0'.repeat(count);
banoshiBigInt = BigInt(banoshiRaw);
}
const rawBigInt = banoshiBigInt + bananoBigInt;
const rawStr = rawBigInt.toString(10);
return rawStr;
};
/**
* describes the banano parts in an english description.
* @memberof BananoUtil
* @param {BananoParts} bananoParts the banano parts to describe.
* @return {string} returns the description of the banano parts.
*/
const getBananoPartsDescription = (bananoParts) => {
const numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
let bananoAmountDesc = '';
if (bananoParts[bananoParts.majorName] !== '0') {
bananoAmountDesc += numberWithCommas(bananoParts[bananoParts.majorName]);
bananoAmountDesc += ' ';
bananoAmountDesc += bananoParts.majorName;
}
if (bananoParts[bananoParts.minorName] !== '0') {
if (bananoAmountDesc.length > 0) {
bananoAmountDesc += ' ';
}
bananoAmountDesc += bananoParts[bananoParts.minorName];
bananoAmountDesc += ' ';
bananoAmountDesc += bananoParts.minorName;
}
if (bananoParts.raw !== '0') {
if (bananoAmountDesc.length > 0) {
bananoAmountDesc += ' ';
}
bananoAmountDesc += numberWithCommas(bananoParts.raw);
bananoAmountDesc += ' raw';
}
if (bananoAmountDesc.length === 0) {
bananoAmountDesc = '0 ';
bananoAmountDesc += bananoParts.majorName;
}
return bananoAmountDesc;
};
/**
* Sends the amount to the account with an optional representative and
* previous block hash.
* If the representative is not sent, it will be pulled from the api.
* If the previous is not sent, it will be pulled from the api.
* Be very careful with previous, as setting it incorrectly
* can cause an incorrect amount of funds to be sent.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} destAccount the destination account.
* @param {string} amountRaw the amount to send, in raw.
* @param {string} representative the representative (optional).
* @param {string} previousHash the previous hash (optional).
* @return {Promise<string>} returns the hash returned by the send.
*/
const sendAmountToBananoAccountWithRepresentativeAndPrevious = async (
seed,
seedIx,
destAccount,
amountRaw,
representative,
previousHash,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const hash =
await bananoUtil.sendFromPrivateKeyWithRepresentativeAndPrevious(
bananodeApi,
privateKey,
destAccount,
amountRaw,
representative,
previousHash,
BANANO_PREFIX,
);
return hash;
};
/**
* Sends the amount to the account with an optional representative and
* previous block hash.
* If the representative is not sent, it will be pulled from the api.
* If the previous is not sent, it will be pulled from the api.
* Be very careful with previous, as setting it incorrectly
* can cause an incorrect amount of funds to be sent.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} destAccount the destination account.
* @param {string} amountRaw the amount to send, in raw.
* @param {string} representative the representative (optional).
* @param {string} previousHash the previous hash (optional).
* @return {Promise<string>} returns the hash returned by the send.
*/
const sendAmountToNanoAccountWithRepresentativeAndPrevious = async (
seed,
seedIx,
destAccount,
amountRaw,
representative,
previousHash,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const hash =
await bananoUtil.sendFromPrivateKeyWithRepresentativeAndPrevious(
bananodeApi,
privateKey,
destAccount,
amountRaw,
representative,
previousHash,
NANO_PREFIX,
);
return hash;
};
/**
* Sends the amount to the banano account with a callback for success and failure.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} destAccount the destination account.
* @param {string} amountRaw the amount to send, in raw.
* @param {string} successCallback the callback to call upon success.
* @param {string} failureCallback the callback to call upon failure.
* @return {Promise<string>} returns the hash returned by the send.
*/
const sendAmountToBananoAccount = async (
seed,
seedIx,
destAccount,
amountRaw,
successCallback,
failureCallback,
) => {
return await bananoUtil
.send(
bananodeApi,
seed,
seedIx,
destAccount,
amountRaw,
successCallback,
failureCallback,
BANANO_PREFIX,
)
.catch((error) => {
// console.trace(error);
throw Error(error);
});
};
/**
* Sends the amount to the nano account with a callback for success and failure.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} destAccount the destination account.
* @param {string} amountRaw the amount to send, in raw.
* @param {string} successCallback the callback to call upon success.
* @param {string} failureCallback the callback to call upon failure.
* @return {Promise<string>} returns the hash returned by the send.
*/
const sendAmountToNanoAccount = async (
seed,
seedIx,
destAccount,
amountRaw,
successCallback,
failureCallback,
) => {
return await bananoUtil
.send(
bananodeApi,
seed,
seedIx,
destAccount,
amountRaw,
successCallback,
failureCallback,
NANO_PREFIX,
)
.catch((error) => {
// console.trace(error);
throw Error(error);
});
};
/**
* Sets the rep for an account with a given seed.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @return {Promise<string>} returns the hash returned by the change.
*/
const changeBananoRepresentativeForSeed = async (
seed,
seedIx,
representative,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const response = await bananoUtil.change(
bananodeApi,
privateKey,
representative,
BANANO_PREFIX,
);
return response;
};
/**
* Sets the rep for an account with a given seed.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @return {Promise<string>} returns the hash returned by the change.
*/
const changeNanoRepresentativeForSeed = async (
seed,
seedIx,
representative,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const response = await bananoUtil.change(
bananodeApi,
privateKey,
representative,
NANO_PREFIX,
);
return response;
};
/**
* Recieve deposits for a nano account with a given seed.
* @memberof DepositUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @param {string} specificPendingBlockHash a specific block hash to receive (optional).
* @return {Promise<object>} returns the response returned by the receive.
*/
const receiveNanoDepositsForSeed = async (
seed,
seedIx,
representative,
specificPendingBlockHash,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
const account = bananoUtil.getAccount(publicKey, NANO_PREFIX);
const response = await depositUtil.receive(
loggingUtil,
bananodeApi,
account,
privateKey,
representative,
specificPendingBlockHash,
NANO_PREFIX,
);
return response;
};
/**
* Recieve deposits for a banano account with a given seed.
* @memberof DepositUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @param {string} specificPendingBlockHash a specific block hash to receive (optional).
* @return {Promise<object>} returns the response returned by the receive.
*/
const receiveBananoDepositsForSeed = async (
seed,
seedIx,
representative,
specificPendingBlockHash,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
const account = bananoUtil.getAccount(publicKey, BANANO_PREFIX);
const response = await depositUtil.receive(
loggingUtil,
bananodeApi,
account,
privateKey,
representative,
specificPendingBlockHash,
BANANO_PREFIX,
);
return response;
};
/**
* Send a withdrawal from a banano account with a given seed.
* @memberof WithdrawUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} toAccount the account to send to.
* @param {string} amountBananos the amount of bananos.
* @param {string} representative the new representative (optional).
* @param {string} previous the new previous (optional).
* @return {Promise<object>} returns the response returned by the withdraw.
*/
const sendBananoWithdrawalFromSeed = async (
seed,
seedIx,
toAccount,
amountBananos,
representative,
previous,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const response = withdrawUtil.withdraw(
loggingUtil,
bananodeApi,
privateKey,
toAccount,
amountBananos,
BANANO_PREFIX,
representative,
previous,
);
return response;
};
/**
* Send a withdrawal from a nano account with a given seed.
* @memberof WithdrawUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} toAccount the account to send to.
* @param {string} amountBananos the amount of bananos.
* @param {string} representative the new representative (optional).
* @param {string} previous the new previous (optional).
* @return {Promise<object>} returns the response returned by the withdraw.
*/
const sendNanoWithdrawalFromSeed = async (
seed,
seedIx,
toAccount,
amountBananos,
representative,
previous,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const response = withdrawUtil.withdraw(
loggingUtil,
bananodeApi,
privateKey,
toAccount,
amountBananos,
NANO_PREFIX,
representative,
previous,
);
return response;
};
/**
* Get the balance, in raw, for an account.
*
* (use other methods like getBananoPartsFromRaw to convert to banano or banoshi)
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#accounts_balances}
* @memberof BananodeApi
* @param {string} account the account to use.
* @return {Promise<string>} the account's balance, in raw.
*/
const getAccountBalanceRaw = async (account) => {
return await bananodeApi.getAccountBalanceRaw(account);
};
/**
* Get the balance and pending values, in raw, as an object like this one:
* { balance: '123', pending: '123' } for an account.
*
* (use other methods like getBananoPartsFromRaw to convert to banano or banoshi)
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#accounts_balances}
* @memberof BananodeApi
* @param {string} account the account to use.
* @return {Promise<object>} the account's balances, in raw.
*/
const getAccountBalanceAndPendingRaw = async (account) => {
return await bananodeApi.getAccountBalanceAndPendingRaw(account);
};
/**
* Get the balances and pending values, in raw, as an object for all given account. Returns the Node object without transformation.
*
* (use other methods like getBananoPartsFromRaw to convert to banano or banoshi)
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#accounts_balances}
* @memberof BananodeApi
* @param {string[]} accounts the account to use.
* @return {Promise<object>} the account's balances, in raw.
*/
const getAccountsBalances = async (accounts) => {
return await bananodeApi.getAccountsBalances(accounts);
};
/**
* Get the history for an account.
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#account_history}
* @memberof BananodeApi
* @param {string} account the account to use.
* @param {number} count the count to use (use -1 for all).
* @param {string} head the head to start at (optional).
* @param {string} raw if true, return raw history (optional).
* @return {Promise<object>} the account's history.
*/
const getAccountHistory = async (account, count, head, raw) => {
return await bananodeApi.getAccountHistory(account, count, head, raw);
};
/**
* Get the banano account with a given seed and index.
*
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @return {Promise<string>} the account.
*/
const getBananoAccountFromSeed = async (seed, seedIx) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
const account = bananoUtil.getAccount(publicKey, BANANO_PREFIX);
return account;
};
/**
* Get the banano account with a given seed and index.
*
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @return {Promise<string>} the account.
*/
const getNanoAccountFromSeed = async (seed, seedIx) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
const account = bananoUtil.getAccount(publicKey, NANO_PREFIX);
return account;
};
/**
* Sets the URL to use for the node behind the Bananode Api
* @memberof Main
* @param {string} url the new url
* @return {undefined} returns nothing.
*/
const setBananodeApiUrl = (url) => {
bananodeApi.setUrl(url);
};
/**
* Get the account info for an account.
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#account_info}
* @memberof BananodeApi
* @param {string} account the account to use.
* @param {boolean} representativeFlag the representativeFlag to use (optional).
* @return {Promise<object>} the account's info.
*/
const getAccountInfo = async (account, representativeFlag) => {
return await bananodeApi.getAccountInfo(account, representativeFlag);
};
/**
* Get the network block count.
*
* Calls {@link https://docs.nano.org/commands/rpc-protocol/#block_count}
* @memberof BananodeApi
* @return {Promise<object>} the block count.
*/
const getBlockCount = async () => {
return await bananodeApi.getBlockCount();
};
/**
* Enables rate limiting, which looks for the rate limiting headers in the response.
*
* @memberof BananodeApi
* @param {string} flag the flag to use.
* @return {undefined} returns nothing.
*/
const setUseRateLimit = async (flag) => {
bananodeApi.setUseRateLimit(flag);
};
/**
* Open a banano account with a given seed.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @param {string} pendingBlockHash the pending block hash.
* @param {string} pendingValueRaw the pending block hash.
* @return {Promise<string>} returns the hash returned by the open.
*/
const openBananoAccountFromSeed = async (
seed,
seedIx,
representative,
pendingBlockHash,
pendingValueRaw,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
return await bananoUtil.open(
bananodeApi,
privateKey,
publicKey,
representative,
pendingBlockHash,
pendingValueRaw,
BANANO_PREFIX,
);
};
/**
* Open a nano account with a given seed.
* @memberof BananoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} representative the representative.
* @param {string} pendingBlockHash the pending block hash.
* @param {string} pendingValueRaw the pending block hash.
* @return {Promise<string>} returns the hash returned by the open.
*/
const openNanoAccountFromSeed = async (
seed,
seedIx,
representative,
pendingBlockHash,
pendingValueRaw,
) => {
const privateKey = bananoUtil.getPrivateKey(seed, seedIx);
const publicKey = await bananoUtil.getPublicKey(privateKey);
return await bananoUtil.open(
bananodeApi,
privateKey,
publicKey,
representative,
pendingBlockHash,
pendingValueRaw,
NANO_PREFIX,
);
};
/**
* Get the hash for a given block.
*
* @memberof BananoUtil
* @param {string} block the seed to use to find the account.
* @return {string} the block's hash.
*/
const getBlockHash = (block) => {
return bananoUtil.hash(block);
};
/**
* signs a dummy block with a hash of the utf-8 message using private key.
*
* @memberof BananoUtil
* @param {string} privateKeyOrSigner the private key to use to sign.
* @param {string} message the utf-8 message to sign.
* @return {string} the message's signature.
*/
const signMessage = (privateKeyOrSigner, message) => {
return bananoUtil.signMessage(privateKeyOrSigner, message);
};
/**
* signs a utf-8 message with private key. Only used internally and for testing.
*
* @memberof BananoUtil
* @param {string} message the utf-8 message to sign.
* @return {Uint8Array} hashed message's bytes.
*/
const hashMessageToBytes = (message) => {
return bananoUtil.hashMessageToBytes(message);
};
/**
* generates a dummy block hash that is used for message signing.
*
* @memberof BananoUtil
* @param {string} privateKey the private key to use to sign.
* @param {string} message the utf-8 message to sign.
* @return {Uint8Array} hashed dummy block's bytes.
*/
const messageDummyBlockHashBytes = (privateKey, message) => {
return bananoUtil.messageDummyBlockHashBytes(privateKey, message);
};
/**
* generates a dummy block that is used for message signing.
*
* @memberof BananoUtil
* @param {string} privateKey the private key to use to sign.
* @param {string} message the utf-8 message to sign.
* @return {string} the message's block.
*/
const messageDummyBlock = (privateKey, message) => {
return bananoUtil.messageDummyBlock(privateKey, message);
};
/**
* verifies a utf-8 message with public key from a dummy block signature.
*
* @memberof BananoUtil
* @param {string} publicKey the public key to use to sign.
* @param {string} message the utf-8 message to verify.
* @param {string} signature hex of signature.
* @return {boolean} whether the signature was verified.
*/
const verifyMessage = (publicKey, message, signature) => {
return bananoUtil.verifyMessage(publicKey, message, signature);
};
/**
* signs a hash.
*
* @memberof BananoUtil
* @param {string} privateKey the private key to use to sign.
* @param {string} hash the hash to sign.
* @return {string} the block's hash.
*/
const signHash = (privateKey, hash) => {
return bananoUtil.signHash(privateKey, hash);
};
/**
* verifys a hash.
*
* @memberof BananoUtil
* @param {string} hash the hash to verify.
* @param {string} signature the signature to verify.
* @param {string} publicKey the public key to use to sign.
* @return {string} true if verification passed.
*/
const verify = (hash, signature, publicKey) => {
return bananoUtil.verify(hash, signature, publicKey);
};
/**
* Get the signature for a given block (gets the hash of the block, and signs the hash).
*
* @memberof BananoUtil
* @param {string} privateKey the private key used to sign the block.
* @param {string} block the block to sign.
* @return {string} the block's signature.
*/
const getSignature = (privateKey, block) => {
return bananoUtil.sign(privateKey, block);
};
/**
* Converts a hex string to bytes in a Uint8Array.
*
* @memberof BananoUtil
* @param {string} hex the hex string to use.
* @return {Uint8Array} the bytes in a Uint8Array.
*/
const getBytesFromHex = (hex) => {
return bananoUtil.hexToBytes(hex);
};
/**
* Converts bytes in a Uint8Array to a hex string.
*
* @memberof BananoUtil
* @param {Uint8Array} bytes the bytes to use.
* @return {string} the hex string.
*/
const getHexFromBytes = (bytes) => {
return bananoUtil.bytesToHex(bytes);
};
/**
* gets work bytes using the CPU.
*
* @memberof BananoUtil
* @param {string} hash the hash to use to calculate work bytes.
* @param {Uint8Array} workBytes the Uint8Array(8) used to store temporary calculations.
* @return {string} the work bytes as a hex string.
*/
const getWorkUsingCpu = (hash, workBytes) => {
return bananoUtil.getHashCPUWorker(hash, workBytes);
};
/**
* receives banano funds at a camo address.
*
* @memberof CamoUtil
* @param {string} toPrivateKey the private key that receives the funds.
* @param {string} fromPublicKey the public key that sent the funds.
* @return {Promise<string[]>} the received hashes in an array.
*/
const camoBananoReceive = async (toPrivateKey, fromPublicKey) => {
return await camoUtil.receive(
bananodeApi,
toPrivateKey,
fromPublicKey,
BANANO_PREFIX,
);
};
/**
* receives nano funds at a camo address.
*
* @memberof CamoUtil
* @param {string} toPrivateKey the private key that receives the funds.
* @param {string} fromPublicKey the public key that sent the funds.
* @return {Promise<string[]>} the received hashes in an array.
*/
const camoNanoReceive = async (toPrivateKey, fromPublicKey) => {
return await camoUtil.receive(
bananodeApi,
toPrivateKey,
fromPublicKey,
NANO_PREFIX,
);
};
/**
* finds a new private key to recieve more banano funds. the key would have no history.
*
* @memberof CamoUtil
* @param {string} seed the seed to use to find the account.
* @return {Promise<string>} the private key to use.
*/
const getCamoBananoNextPrivateKeyForReceive = async (seed) => {
return await camoUtil.getFirstUnopenedPrivateKey(
bananodeApi,
seed,
BANANO_PREFIX,
);
};
/**
* finds a new private key to recieve more banano funds. the key would have no history.
*
* @memberof CamoUtil
* @param {string} seed the seed to use to find the account.
* @return {Promise<string>} the private key to use.
*/
const getCamoNanoNextPrivateKeyForReceive = async (seed) => {
return await camoUtil.getFirstUnopenedPrivateKey(
bananodeApi,
seed,
NANO_PREFIX,
);
};
/**
* sends banano funds to a camo address.
*
* @memberof CamoUtil
* @param {string} fundingPrivateKey the private key that sends the funds.
* @param {string} fromCamoPrivateKey the private key used to generate the shared seed.
* @param {string} toCamoPublicKey the public key that receives the funds.
* @param {string} amountBananos the amount of bananos.
* @return {Promise<string[]>} the sent hashes in an array.
*/
const camoBananoSend = async (
fundingPrivateKey,
fromCamoPrivateKey,
toCamoPublicKey,
amountBananos,
) => {
const amountRaw = getRawStrFromBananoStr(amountBananos);
return await camoUtil.send(
bananodeApi,
fundingPrivateKey,
fromCamoPrivateKey,
toCamoPublicKey,
amountRaw,
BANANO_PREFIX,
);
};
/**
* sends camo funds to a camo address.
*
* @memberof CamoUtil
* @param {string} fundingPrivateKey the private key that sends the funds.
* @param {string} fromCamoPrivateKey the private key used to generate the shared seed.
* @param {string} toCamoPublicKey the public key that receives the funds.
* @param {string} amountBananos the amount of bananos.
* @return {Promise<string[]>} the sent hashes in an array.
*/
const camoNanoSend = async (
fundingPrivateKey,
fromCamoPrivateKey,
toCamoPublicKey,
amountBananos,
) => {
const amountRaw = getRawStrFromNanoStr(amountBananos);
return await camoUtil.send(
bananodeApi,
fundingPrivateKey,
fromCamoPrivateKey,
toCamoPublicKey,
amountRaw,
NANO_PREFIX,
);
};
/**
* sends banano funds to a camo account.
* This function uses seed index 0 to generate the shared secret,
* and seed index "seedIx" to get the private key that contains funds to send.
*
* @memberof CamoUtil
* @param {string} seed the seed to use to find the account.
* @param {string} seedIx the index to use with the seed.
* @param {string} toAccount the account to send to.
* @param {string} amountBananos the amount of bananos.
* @return {Promise<string[]>} the sent hashes in an array.
*/
const camoBananoSendWithdrawalFromSeed = async (
seed,