-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtrx.ts
More file actions
745 lines (655 loc) · 29.6 KB
/
trx.ts
File metadata and controls
745 lines (655 loc) · 29.6 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
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
import assert from 'node:assert';
import { afterEach, before, describe, it, mock } from 'node:test';
import { BitGoAPI } from '@bitgo/sdk-api';
import { TestBitGoAPI, TestBitGo } from '@bitgo/sdk-test';
import * as _ from 'lodash';
import { Trx, Ttrx, Utils } from '../../src';
import { signTxOptions, mockTx } from '../fixtures';
import {
baseAddressBalance,
SampleRawTokenSendTxn,
receiveAddressBalance,
TestRecoverData,
creationTransaction,
} from '../resources';
describe('TRON:', function () {
const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' });
bitgo.initializeTestVars();
bitgo.safeRegister('trx', Trx.createInstance);
bitgo.safeRegister('ttrx', Ttrx.createInstance);
let basecoin;
// Test data from wallet 693b011a3ec26986f569b02140c7627e
const testWalletData = {
rootAddress: 'TAf36b36eqoMCzJJm3jwSsP81UvkMxrPbi',
receiveAddress: 'TFaD6DeKFMcBuGuDD7LbbqxTnKunhXfdya',
receiveAddressIndex: 2,
keychains: [
{
id: '693b0110271fc3f5749754097793bb8d',
pub: 'xpub661MyMwAqRbcFsVAdZyN2m8p21WHXg8NRNkqKApyS5gwmFsPdRTrmHYCnzR9vYe8DQ4uWGCBcAAsWE3r97HsFS3K2faZ2ejXNhHxdEoAEWC',
},
{
id: '693b011065b9c4674825ce1f849b7bef',
pub: 'xpub661MyMwAqRbcErKpZr9ztTFJk6fzXWatMFgRnpXfsjybWBfE9847EVGrHHBsGP8fcnzJmJuevAbPUEpjHTjEEWfYUWNMEDahvssQein848o',
},
{
id: '693b01117c41846abb04818815b89b6c',
pub: 'xpub661MyMwAqRbcFqZd7XU9DFW4f29VJzQt7UCA51ypaWa5ymhQ2pRZDTgViw3vZ56PqZ8dj1cracN3fAWhaiG1QKj9mvyt9Cba4nM2tPibNKw',
},
],
};
before(function () {
basecoin = bitgo.coin('ttrx');
});
it('should instantiate the coin', function () {
const basecoin = bitgo.coin('trx');
assert.ok(basecoin instanceof Trx);
});
it('explain a txHex', async function () {
const txHex = JSON.stringify(mockTx);
const explainParams = {
txHex,
feeInfo: { fee: 1 },
txID: mockTx.txID,
};
const explanation = await basecoin.explainTransaction(explainParams);
const toAddress = Utils.getBase58AddressFromHex(mockTx.raw_data.contract[0].parameter.value.to_address);
assert.equal(explanation.id, mockTx.txID);
assert.equal(explanation.outputs.length, 1);
assert.equal(explanation.outputs[0].amount, '10');
assert.equal(explanation.outputs[0].address, toAddress);
assert.equal(explanation.outputAmount, '10');
assert.equal(explanation.changeAmount, '0');
assert.equal(explanation.changeOutputs.length, 0);
assert.equal(explanation.fee.fee, 1);
assert.equal(explanation.expiration, mockTx.raw_data.expiration);
assert.equal(explanation.timestamp, mockTx.raw_data.timestamp);
});
it('should check valid addresses', function () {
const badAddresses = [
'',
null,
'xxxx',
'YZ09fd-',
'412C2BA4A9FF6C53207DC5B686BFECF75EA7B805772',
'412C2BA4A9FF6C53207DC5B686BFECF75EA7B80',
'TBChwKYNaTo4a4N68Me1qEiiKsRDspXqLLZ',
'0x96be113992bdc3be24c11f6017085b605d253649',
'0x341qg3922b1',
'41E0C0F581D7D02D40826C1C6CBEE71F625D6344D0',
'412C2BA4A9FF6C53207DC5B686BFECF75EA7B80577',
'418840E6C55B9ADA326D211D818C34A994AECED808',
'412A2B9F7641D0750C1E822D0E49EF765C8106524B',
'41A614F803B6FD780986A42C78EC9C7F77E6DED13C',
'418840E6C55B9ADA326D211D818C34A994AECED808',
];
const goodAddresses = ['TBChwKYNaTo4a4N68Me1qEiiKsRDspXqLp', 'TPcf5jtYUhCN1X14tN577zF4NepbDZbxT7'];
badAddresses.map((addr) => {
assert.equal(basecoin.isValidAddress(addr), false);
});
goodAddresses.map((addr) => {
assert.equal(basecoin.isValidAddress(addr), true);
});
});
it('should throw if the params object is missing parameters', async function () {
const explainParams = {
feeInfo: { fee: 1 },
txID: mockTx.txID,
txHex: null,
};
await assert.rejects(basecoin.explainTransaction(explainParams), {
message: 'missing explain tx parameters',
});
});
it('explain an half-signed/fully signed transaction', async function () {
const txHex = JSON.stringify(mockTx);
const explainParams = {
halfSigned: { txHex },
feeInfo: { fee: 1 },
txID: mockTx.txID,
};
const explanation = await basecoin.explainTransaction(explainParams);
const toAddress = Utils.getBase58AddressFromHex(mockTx.raw_data.contract[0].parameter.value.to_address);
assert.equal(explanation.id, mockTx.txID);
assert.equal(explanation.outputs.length, 1);
assert.equal(explanation.outputs[0].amount, '10');
assert.equal(explanation.outputs[0].address, toAddress);
assert.equal(explanation.outputAmount, '10');
assert.equal(explanation.changeAmount, '0');
assert.equal(explanation.changeOutputs.length, 0);
assert.equal(explanation.fee.fee, 1);
assert.equal(explanation.expiration, mockTx.raw_data.expiration);
assert.equal(explanation.timestamp, mockTx.raw_data.timestamp);
});
it('should sign a half signed tx', async function () {
const tx = await basecoin.signTransaction(signTxOptions);
const unsignedTxJson = JSON.parse(signTxOptions.txPrebuild.txHex);
const signedTxJson = JSON.parse(tx.halfSigned.txHex);
assert.equal(signedTxJson.txID, unsignedTxJson.txID);
assert.equal(signedTxJson.raw_data_hex, unsignedTxJson.raw_data_hex);
assert.deepStrictEqual(JSON.stringify(signedTxJson.raw_data), JSON.stringify(unsignedTxJson.raw_data));
assert.equal(signedTxJson.signature.length, 1);
assert.equal(
signedTxJson.signature[0],
'0a9944316924ec7fba4895f1ea1e7cc95f9e2b828ae268a48a8dbeddef40c6f5e127170a95aed9f3f5425b13058d0cb6ef1f5c2213190e482e87043691f22e6800'
);
});
it('should sign with an Xprv a half signed tx', async function () {
const p = {
prv: 'xprv9s21ZrQH143K2sg2Cukk5XqLQdrYnMCDah3y1FFVy6Hz9bQfqMSfmUiHPVHKhcUyft3N1emE5FudJVxgFm5N12MAg5o7DTPsDATTkwNgr73',
txPrebuild: {
txHex: signTxOptions.txPrebuild.txHex,
},
};
const tx = await basecoin.signTransaction(p);
const unsignedTxJson = JSON.parse(signTxOptions.txPrebuild.txHex);
const signedTxJson = JSON.parse(tx.halfSigned.txHex);
assert.equal(signedTxJson.txID, unsignedTxJson.txID);
assert.equal(signedTxJson.raw_data_hex, unsignedTxJson.raw_data_hex);
assert.deepStrictEqual(JSON.stringify(signedTxJson.raw_data), JSON.stringify(unsignedTxJson.raw_data));
assert.equal(signedTxJson.signature.length, 1);
assert.equal(
signedTxJson.signature[0],
'65e56f53a458c6f82d1ef39b2cf5be685a906ad22bb02699f907fcb72ef26f1e91cfc2b6a43bf5432faa0b63bdc5aebf1dc2f49a675d28d23fd7e038b3358b0600'
);
});
it('should add feeLimit to recipient', async function () {
const feeLimit = 100;
const buildParams = {
recipients: [{ data: 'test' }],
feeLimit,
};
basecoin.getExtraPrebuildParams(buildParams);
assert.equal((buildParams as any).recipients[0].feeLimit, feeLimit);
});
it('should`t add any new field', async function () {
const buildParams = {
recipients: [{ data: 'test' }],
};
const unmodifiedBuildParams = _.cloneDeep(buildParams);
await basecoin.getExtraPrebuildParams(buildParams);
assert.deepStrictEqual(buildParams, unmodifiedBuildParams);
});
describe('Keypairs:', () => {
it('should generate a keypair from random seed', function () {
const keyPair = basecoin.generateKeyPair();
assert.ok(Object.prototype.hasOwnProperty.call(keyPair, 'pub'));
assert.ok(Object.prototype.hasOwnProperty.call(keyPair, 'prv'));
assert.equal(basecoin.isValidPub(keyPair.pub), true);
});
it('should generate a keypair from a seed', function () {
const seedText =
'80350b4208d381fbfe2276a326603049fe500731c46d3c9936b5ce036b51377f24bab7dd0c2af7f107416ef858ff79b0670c72406dad064e72bb17fc0a9038bb';
const seed = Buffer.from(seedText, 'hex');
const keyPair = basecoin.generateKeyPair(seed);
assert.equal(
keyPair.pub,
'xpub661MyMwAqRbcFAwqvSGbk35kJf7CQqdN1w4CMUBBTqH5e3ivjU6D8ugv9hRSgRbRenC4w3ahXdLVahwjgjXhSuQKMdNdn55Y9TNSagBktws'
);
assert.equal(
keyPair.prv,
'xprv9s21ZrQH143K2gsNpQjbNu91kdGi1NuWei8bZ5mZuVk6mFPnBvmxb7NSJQdbZW3FGpK3Ycn7jorAXcEzMvviGtbyBz5tBrjfnWyQp3g75FK'
);
});
});
describe('Build Unsigned Sweep', () => {
afterEach(() => {
mock.reset();
});
it('should recover trx from base address to recovery address', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(3000000));
}
return undefined;
});
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
}
return undefined;
});
const res = await basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'txHex'));
assert.ok(Object.prototype.hasOwnProperty.call(res, 'feeInfo'));
const rawData = JSON.parse(res.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(value.amount, 900000);
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.baseAddress);
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
});
it('should recover trx from base address to recovery address with scan passed as valid integer string', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(3000000));
}
return undefined;
});
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
}
return undefined;
});
const res = await basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
scan: '10',
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'txHex'));
assert.ok(Object.prototype.hasOwnProperty.call(res, 'feeInfo'));
const rawData = JSON.parse(res.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(value.amount, 900000);
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.baseAddress);
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
});
it('should fail recover trx from base address to recovery address with scan passed as valid integer string', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(3000000));
}
return undefined;
});
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
}
return undefined;
});
await assert.rejects(
basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
scan: 'abc',
}),
{
message: 'Invalid scanning factor',
}
);
});
it('should recover trx from receive address to base address', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(2000000));
}
if (args.length > 0 && args[0] === TestRecoverData.firstReceiveAddress) {
return Promise.resolve(receiveAddressBalance(102100000, TestRecoverData.firstReceiveAddress));
}
return undefined;
});
const firstReceiveAddressHex = Utils.getHexAddressFromBase58Address(TestRecoverData.firstReceiveAddress);
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === baseAddrHex && args[1] === firstReceiveAddressHex && args[2] === 100000000) {
return Promise.resolve(creationTransaction(firstReceiveAddressHex, baseAddrHex, 100000000));
}
return undefined;
});
const res = await basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'txHex'));
assert.ok(Object.prototype.hasOwnProperty.call(res, 'feeInfo'));
const rawData = JSON.parse(res.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(value.amount, 100000000);
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.firstReceiveAddress);
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.baseAddress);
});
it('should recover token from base address to recovery address', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(
baseAddressBalance(100000000, [
{
TSdZwNqpHofzP6BsBKGQUWdBeJphLmF6id: '1000000000',
},
{
TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs: '1100000000',
},
])
);
}
return undefined;
});
mock.method(Trx.prototype as any, 'getTriggerSmartContractTransaction', (...args) => {
return Promise.resolve(SampleRawTokenSendTxn);
});
const res = await basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
tokenContractAddress: 'TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs',
recoveryDestination: TestRecoverData.recoveryDestination,
});
assert.notEqual(res.length, 0);
assert.equal(res.recoveryAmount, 1100000000);
assert.equal(res.feeInfo.fee, '100000000');
const expirationDuration = res.tx.raw_data.expiration - res.tx.raw_data.timestamp;
assert.ok(expirationDuration >= 86400000);
assert.equal(res.addressInfo, undefined);
const rawData = JSON.parse(res.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.baseAddress);
assert.equal(Utils.getBase58AddressFromHex(value.contract_address), 'TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs');
});
it('should throw if trx balance at base address is not sufficient to cover token send', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(
baseAddressBalance(1000000, [
{
TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs: '1100000000',
},
])
);
}
return undefined;
});
mock.method(Trx.prototype as any, 'getTriggerSmartContractTransaction', (...args) => {
return Promise.resolve(SampleRawTokenSendTxn);
});
await assert.rejects(
basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
tokenContractAddress: 'TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs',
recoveryDestination: TestRecoverData.recoveryDestination,
}),
{
message:
"Amount of funds to recover 1000000 is less than 100000000 and wouldn't be able to fund a trc20 send",
}
);
});
});
describe('Build Unsigned Consolidation Recoveries', () => {
afterEach(() => {
mock.reset();
});
it('should throw if startingScanIndex is not ge to 1', async () => {
await assert.rejects(
basecoin.recoverConsolidations({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
startingScanIndex: -1,
}),
{
message:
'Invalid starting or ending index to scan for addresses. startingScanIndex: -1, endingScanIndex: 19.',
}
);
});
it('should throw if scan factor is too high', async () => {
await assert.rejects(
basecoin.recoverConsolidations({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
startingScanIndex: 1,
endingScanIndex: 300,
}),
{
message:
'Invalid starting or ending index to scan for addresses. startingScanIndex: 1, endingScanIndex: 300.',
}
);
});
it('should build consolidate recoveries', async () => {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.firstReceiveAddress) {
return Promise.resolve(receiveAddressBalance(102100000, TestRecoverData.firstReceiveAddress));
}
if (args.length > 0 && args[0] === TestRecoverData.secondReceiveAddress) {
return Promise.resolve(receiveAddressBalance(50000000, TestRecoverData.secondReceiveAddress));
}
return undefined;
});
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === baseAddrHex && args[1] === firstReceiveAddrHex && args[2] === 100000000) {
return Promise.resolve(creationTransaction(firstReceiveAddrHex, baseAddrHex, 100000000));
}
if (args.length > 0 && args[0] === baseAddrHex && args[1] === secondReceiveAddrHex && args[2] === 47900000) {
return Promise.resolve(creationTransaction(secondReceiveAddrHex, baseAddrHex, 47900000));
}
return undefined;
});
const firstReceiveAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.firstReceiveAddress);
const secondReceiveAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.secondReceiveAddress);
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const res = await basecoin.recoverConsolidations({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
startingScanIndex: 1,
endingScanIndex: 3,
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'transactions'));
assert.equal(res.transactions.length, 2);
const txn1 = res.transactions[0];
const rawData1 = JSON.parse(txn1.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData1, 'contract'));
const value1 = rawData1.contract[0].parameter.value;
assert.equal(value1.amount, 100000000);
assert.equal(Utils.getBase58AddressFromHex(value1.owner_address), TestRecoverData.firstReceiveAddress);
assert.equal(Utils.getBase58AddressFromHex(value1.to_address), TestRecoverData.baseAddress);
const txn2 = res.transactions[1];
const rawData2 = JSON.parse(txn2.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData2, 'contract'));
const value2 = rawData2.contract[0].parameter.value;
assert.equal(value2.amount, 47900000);
assert.equal(Utils.getBase58AddressFromHex(value2.owner_address), TestRecoverData.secondReceiveAddress);
assert.equal(Utils.getBase58AddressFromHex(value2.to_address), TestRecoverData.baseAddress);
});
it('should build consolidate token recoveries', async () => {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.firstReceiveAddress) {
return Promise.resolve(
receiveAddressBalance(202100000, TestRecoverData.firstReceiveAddress, [
{
TSdZwNqpHofzP6BsBKGQUWdBeJphLmF6id: '1100000000',
},
])
);
}
if (args.length > 0 && args[0] === TestRecoverData.secondReceiveAddress) {
return Promise.resolve(receiveAddressBalance(500, TestRecoverData.secondReceiveAddress));
}
return undefined;
});
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === baseAddrHex && args[1] === firstReceiveAddrHex && args[2] === 1100000000) {
return Promise.resolve(creationTransaction(firstReceiveAddrHex, baseAddrHex, 1100000000));
}
return undefined;
});
const firstReceiveAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.firstReceiveAddress);
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const res = await basecoin.recoverConsolidations({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
tokenContractAddress: 'TSdZwNqpHofzP6BsBKGQUWdBeJphLmF6id',
startingScanIndex: 1,
endingScanIndex: 3,
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'transactions'));
assert.equal(res.transactions.length, 1);
const txn = res.transactions[0];
const rawData = JSON.parse(txn.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(
value.data,
'a9059cbb000000000000000000000000c25420255c2c5a2dd54ef69f92ef261e6bd4216a000000000000000000000000000000000000000000000000000000004190ab00'
);
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.firstReceiveAddress);
assert.equal(Utils.getBase58AddressFromHex(value.contract_address), 'TSdZwNqpHofzP6BsBKGQUWdBeJphLmF6id');
});
it('should skip building consolidate transaction if balance is lower than reserved fee', async () => {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.firstReceiveAddress) {
return Promise.resolve(receiveAddressBalance(102100000, TestRecoverData.firstReceiveAddress));
}
if (args.length > 0 && args[0] === TestRecoverData.secondReceiveAddress) {
return Promise.resolve(receiveAddressBalance(2000000, TestRecoverData.secondReceiveAddress));
}
return undefined;
});
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === baseAddrHex && args[1] === firstReceiveAddrHex && args[2] === 100000000) {
return Promise.resolve(creationTransaction(firstReceiveAddrHex, baseAddrHex, 100000000));
}
return undefined;
});
const firstReceiveAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.firstReceiveAddress);
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const res = await basecoin.recoverConsolidations({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
startingScanIndex: 1,
endingScanIndex: 3,
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'transactions'));
assert.equal(res.transactions.length, 1);
const txn1 = res.transactions[0];
const rawData1 = JSON.parse(txn1.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData1, 'contract'));
const value1 = rawData1.contract[0].parameter.value;
assert.equal(value1.amount, 100000000);
assert.equal(Utils.getBase58AddressFromHex(value1.owner_address), TestRecoverData.firstReceiveAddress);
assert.equal(Utils.getBase58AddressFromHex(value1.to_address), TestRecoverData.baseAddress);
});
});
describe('isWalletAddress', () => {
it('should verify root address (index 0)', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.rootAddress,
keychains: testWalletData.keychains,
index: 0,
});
assert.equal(result, true);
});
it('should verify receive address (index > 0)', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.receiveAddress,
keychains: testWalletData.keychains,
index: testWalletData.receiveAddressIndex,
chain: 0,
});
assert.equal(result, true);
});
it('should verify address with index in coinSpecific', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.rootAddress,
keychains: testWalletData.keychains,
coinSpecific: { index: 0 },
});
assert.equal(result, true);
});
it('should verify address with string index', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.rootAddress,
keychains: testWalletData.keychains,
index: '0',
});
assert.equal(result, true);
});
it('should return false for wrong address at index 0', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.receiveAddress, // wrong address for index 0
keychains: testWalletData.keychains,
index: 0,
});
assert.equal(result, false);
});
it('should return false for wrong address at receive index', async function () {
const result = await basecoin.isWalletAddress({
address: testWalletData.rootAddress, // wrong address for index 1
keychains: testWalletData.keychains,
index: testWalletData.receiveAddressIndex,
chain: 0,
});
assert.equal(result, false);
});
it('should throw for invalid address', async function () {
await assert.rejects(
basecoin.isWalletAddress({
address: 'invalid-address',
keychains: testWalletData.keychains,
index: 0,
}),
{
message: 'Invalid address: invalid-address',
}
);
});
it('should throw for missing index', async function () {
await assert.rejects(
basecoin.isWalletAddress({
address: testWalletData.rootAddress,
keychains: testWalletData.keychains,
}),
{
message: 'Invalid or missing index for address verification',
}
);
});
it('should throw for missing bitgo key on root address verification', async function () {
await assert.rejects(
basecoin.isWalletAddress({
address: testWalletData.rootAddress,
keychains: testWalletData.keychains.slice(0, 2), // only user and backup keys
index: 0,
}),
{
message: 'BitGo public key required for root address verification',
}
);
});
it('should throw for missing user key on receive address verification', async function () {
await assert.rejects(
basecoin.isWalletAddress({
address: testWalletData.receiveAddress,
keychains: [], // no keys
index: testWalletData.receiveAddressIndex,
}),
{
message: 'User public key required for receive address verification',
}
);
});
});
});