-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtokenWithdrawal.ts
More file actions
835 lines (731 loc) · 28.6 KB
/
tokenWithdrawal.ts
File metadata and controls
835 lines (731 loc) · 28.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
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
import should from 'should';
import { TransactionType } from '@bitgo/sdk-core';
import * as testData from '../resources';
import { TransactionBuilderFactory, AdaToken } from '../../src';
import { coins } from '@bitgo/statics';
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';
import { Transaction } from '../../src/lib/transaction';
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
import { BitGoAPI } from '@bitgo/sdk-api';
describe('ADA Token Operations', async () => {
const factory = new TransactionBuilderFactory(coins.get('tada'));
const receiverAddress = testData.rawTx.outputAddress2.address;
const senderAddress = testData.rawTx.outputAddress1.address;
const policyId = 'e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72';
const policyScriptHash = CardanoWasm.ScriptHash.from_hex(policyId);
const assetName = 'tada:water';
const name = 'WATER';
const asciiEncodedName = Buffer.from(name, 'ascii').toString('hex');
const fingerprint = 'asset1n69xf60d0760xvn8v2ffd5frvsm0cl2r8hfjf6';
const unsupportedPolicyId = '279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f';
const unsupportedPolicyScriptHash = CardanoWasm.ScriptHash.from_hex(unsupportedPolicyId);
const unsupportedName = 'BITGO';
const unsupportedAsciiEncodedName = Buffer.from(unsupportedName, 'ascii').toString('hex');
const unsupportedFingerprint = 'asset1m8h0gk7f6x5j5qg0x5m4q5f5j5qg0x5m4q5f5j';
it(`should build a transaction with token ${assetName} - ada + ${assetName}`, async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const expectedChangeAda = (
totalInput -
1500000 /* min ada for change token utxo */ -
1500000 /* min ada for recipient token utxo*/ -
173597
) /* fee */
.toString();
const expectedChangeToken = '80';
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
should.equal(tx.type, TransactionType.Send);
const txData = tx.toJson();
// Check outputs (should include multi-asset and regular change)
txData.outputs.length.should.equal(3);
txData.inputs.length.should.equal(1);
// One output should have the multi-asset change and the other for the transfer
const assetOutput = txData.outputs.filter((output) => output.multiAssets !== undefined);
assetOutput!.length.should.equal(2);
// Verify inputs and outputs
const input = txData.inputs[0];
input.transaction_id.should.equal('3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21');
input.transaction_index.should.equal(1);
// Validate receiver output
const receiverOutput = txData.outputs.filter((output) => output.address === receiverAddress);
receiverOutput.length.should.equal(1);
receiverOutput[0].amount.should.equal('1500000'); // Minimum ADA for asset output
(receiverOutput[0].multiAssets! as CardanoWasm.MultiAsset)
.get_asset(policyScriptHash, CardanoWasm.AssetName.new(Buffer.from(asciiEncodedName, 'hex')))
.to_str()
.should.equal(quantity);
// Validate change outputs (one with asset and one without)
const changeOutput = txData.outputs.filter((output) => output.address === senderAddress);
changeOutput.length.should.equal(2);
const changeWithAsset = changeOutput.find((output) => output.multiAssets !== undefined);
should.exist(changeWithAsset);
changeWithAsset!.amount.should.equal('1500000'); // Minimum ADA for asset output
(changeWithAsset!.multiAssets! as CardanoWasm.MultiAsset)
.get_asset(policyScriptHash, CardanoWasm.AssetName.new(Buffer.from(asciiEncodedName, 'hex')))
.to_str()
.should.equal(expectedChangeToken);
const changeWithoutAsset = changeOutput.find((output) => output.multiAssets === undefined);
should.exist(changeWithoutAsset);
changeWithoutAsset!.amount.should.equal(expectedChangeAda); // Remaining ADA after fees and min ADAs
});
it(`should build a transaction with token ${assetName} - ada + ${assetName} + unsupported token`, async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
[unsupportedFingerprint]: {
quantity: '10000',
policy_id: unsupportedPolicyId,
asset_name: unsupportedAsciiEncodedName,
},
};
const expectedChangeAda = (
totalInput -
1500000 /* min ada for change token utxo */ -
1500000 /* min ada for recipient token utxo*/ -
1500000 /* min ada for unsupported token change utxo */ -
179889
) /* fee */
.toString();
const expectedChangeToken = '80';
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba22',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
should.equal(tx.type, TransactionType.Send);
const txData = tx.toJson();
// Check outputs (should include multi-asset and regular change)
txData.outputs.length.should.equal(4);
txData.inputs.length.should.equal(2);
// One output should have the multi-asset change and the other for the transfer
const assetOutput = txData.outputs.filter((output) => output.multiAssets !== undefined);
assetOutput!.length.should.equal(3);
// Verify inputs and outputs
txData.inputs[0].transaction_id.should.equal('3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21');
txData.inputs[0].transaction_index.should.equal(1);
txData.inputs[1].transaction_id.should.equal('3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba22');
txData.inputs[1].transaction_index.should.equal(1);
// Validate receiver output
const receiverOutput = txData.outputs.filter((output) => output.address === receiverAddress);
receiverOutput.length.should.equal(1);
receiverOutput[0].amount.should.equal('1500000'); // Minimum ADA for asset output
(receiverOutput[0].multiAssets! as CardanoWasm.MultiAsset)
.get_asset(policyScriptHash, CardanoWasm.AssetName.new(Buffer.from(asciiEncodedName, 'hex')))
.to_str()
.should.equal(quantity);
// Validate change outputs (one with supported token, one with unsupported token and one for pure ada)
const changeOutput = txData.outputs.filter((output) => output.address === senderAddress);
changeOutput.length.should.equal(3);
const changeWithAsset = changeOutput.filter((output) => output.multiAssets !== undefined);
changeWithAsset.length.should.equal(2);
let tokens = 0;
changeWithAsset.forEach((output) => {
output!.amount.should.equal('1500000'); // Minimum ADA for asset output
const supportedTokenChangeQty = (output!.multiAssets! as CardanoWasm.MultiAsset)
.get_asset(policyScriptHash, CardanoWasm.AssetName.new(Buffer.from(asciiEncodedName, 'hex')))
.to_str();
const unsupportedTokenChangeQty = (output!.multiAssets! as CardanoWasm.MultiAsset)
.get_asset(
unsupportedPolicyScriptHash,
CardanoWasm.AssetName.new(Buffer.from(unsupportedAsciiEncodedName, 'hex'))
)
.to_str();
if (supportedTokenChangeQty !== '0') {
supportedTokenChangeQty.should.equal(expectedChangeToken);
tokens++;
} else {
unsupportedTokenChangeQty!.should.equal('10000');
tokens++;
}
});
tokens.should.equal(2);
const changeWithoutAsset = changeOutput.find((output) => output.multiAssets === undefined);
should.exist(changeWithoutAsset);
changeWithoutAsset!.amount.should.equal(expectedChangeAda); // Remaining ADA after fees and min ADAs
});
it(`should fail to build a transaction with ${assetName} token and insufficient minimum ADA`, async () => {
const quantity = '20';
const totalInput = 2000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
await txBuilder
.build()
.should.rejectedWith(
'Insufficient funds: need a minimum of 1500000 lovelace per output to construct token transactions'
);
});
it(`should fail to build a transaction with ${assetName} and insufficient token qty`, async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '5',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
await txBuilder.build().should.rejectedWith('Insufficient qty: not enough token qty to cover receiver output');
});
it(`should build a transaction with ${assetName} when the token qty is exactly the qty in withdrawal`, async () => {
const quantity = '1';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '1',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled in sdk build)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
await txBuilder.build().should.not.be.rejected();
});
it(`should build a sponsored token transaction where fee address sponsors min ADA for receiver`, async () => {
const feeAddress =
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp';
const quantity = '20';
const senderInputBalance = 5000000;
const feeAddressInputBalance = 20000000; // Fee address has enough ADA to sponsor
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
// Sender input (has tokens)
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
// Fee address input (sponsors fees and min ADA)
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba22',
transaction_index: 0,
});
txBuilder.output({
address: receiverAddress,
amount: '0', // Set ADA amount to 0 for token transfer (min ADA is handled by fee address)
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, senderInputBalance.toString(), totalAssetList);
txBuilder.sponsorshipInfo({
feeAddress: feeAddress,
feeAddressInputBalance: feeAddressInputBalance.toString(),
});
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
should.equal(tx.type, TransactionType.Send);
const txData = tx.toJson();
txData.inputs.length.should.equal(2);
txData.outputs.length.should.equal(4);
// Validate receiver output - min ADA should be sponsored by fee address
const receiverOutput = txData.outputs.filter((output) => output.address === receiverAddress);
receiverOutput.length.should.equal(1);
receiverOutput[0].amount.should.equal('1500000'); // Minimum ADA for asset output (sponsored by fee address)
(receiverOutput[0].multiAssets! as CardanoWasm.MultiAsset)
.get_asset(policyScriptHash, CardanoWasm.AssetName.new(Buffer.from(asciiEncodedName, 'hex')))
.to_str()
.should.equal(quantity);
// Validate sender change output - should have full sender balance (tokens only, no ADA deduction for receiver)
const senderChangeOutput = txData.outputs.filter((output) => output.address === senderAddress);
senderChangeOutput.length.should.be.equal(2);
// Validate fee address change output - should have remaining ADA after fees and min ADA for receiver
const feeAddressChangeOutput = txData.outputs.filter((output) => output.address === feeAddress);
feeAddressChangeOutput.length.should.equal(1);
// Fee address change should not have any tokens
should.not.exist(feeAddressChangeOutput[0].multiAssets);
tx.getFee.should.equal('182485'); // Fee with two witnesses
});
describe('AdaToken verifyTransaction', () => {
let bitgo: TestBitGoAPI;
let adaToken;
before(function () {
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' });
bitgo.initializeTestVars();
const tokenConfig = {
type: 'tada:water',
coin: 'tada',
network: 'Testnet' as const,
name: 'WATER',
decimalPlaces: 0,
policyId: policyId,
assetName: name, // ASCII 'WATER', not hex-encoded
contractAddress: `${policyId}:${asciiEncodedName}`,
};
adaToken = new AdaToken(bitgo, tokenConfig);
});
it('should verify a token transaction with correct token amount', async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Verify transaction with correct token amount
const txParams = {
recipients: [
{
address: receiverAddress,
amount: quantity, // Token amount, not ADA amount
},
],
};
const txPrebuild = { txHex };
const mockWallet = { coinSpecific: () => ({ baseAddress: senderAddress }) };
const isVerified = await adaToken.verifyTransaction({ txParams, txPrebuild, wallet: mockWallet as any });
isVerified.should.equal(true);
});
it('should fail to verify a token transaction with incorrect token amount', async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Verify transaction with WRONG token amount (should fail)
const txParams = {
recipients: [
{
address: receiverAddress,
amount: '999', // Wrong amount
},
],
};
const txPrebuild = { txHex };
const mockWallet = { coinSpecific: () => ({ baseAddress: senderAddress }) };
await adaToken
.verifyTransaction({ txParams, txPrebuild, wallet: mockWallet as any })
.should.be.rejectedWith('cannot find recipient in expected output');
});
it('should fail to verify when address does not match', async () => {
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Verify with wrong address (should fail)
const txParams = {
recipients: [
{
address:
'addr_test1qqa86e3d7lfpwu0k2rhjz76ecmfxdr74s9kf9yfcp5hj5vmnh6xccjcclrk8jtaw9jgeuy99p2n8smtdpylmy45qjjfsfmp3g6',
amount: quantity,
},
],
};
const txPrebuild = { txHex };
const mockWallet = { coinSpecific: () => ({ baseAddress: senderAddress }) };
await adaToken
.verifyTransaction({ txParams, txPrebuild, wallet: mockWallet as any })
.should.be.rejectedWith('cannot find recipient in expected output');
});
it('should verify transaction when policyId has concatenated assetName (crypto compare format)', async () => {
// This tests the case where policyId in tokenConfig contains policyId + asciiEncodedAssetName
// which is consistent with crypto compare format
const concatenatedPolicyId = policyId + asciiEncodedName; // e.g., 'e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed725741544552'
const tokenConfigWithConcatenatedPolicyId = {
type: 'tada:water',
coin: 'tada',
network: 'Testnet' as const,
name: 'WATER',
decimalPlaces: 0,
policyId: concatenatedPolicyId, // policyId + assetName hex
assetName: name, // ASCII name 'WATER' (not hex encoded)
contractAddress: `${policyId}:${asciiEncodedName}`,
};
const adaTokenWithConcatenatedPolicyId = new AdaToken(bitgo, tokenConfigWithConcatenatedPolicyId);
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Verify transaction - the verifyTransaction should strip the assetName from policyId
const txParams = {
recipients: [
{
address: receiverAddress,
amount: quantity,
},
],
};
const txPrebuild = { txHex };
const mockWallet = { coinSpecific: () => ({ baseAddress: senderAddress }) };
const isVerified = await adaTokenWithConcatenatedPolicyId.verifyTransaction({
txParams,
txPrebuild,
wallet: mockWallet as any,
});
isVerified.should.equal(true);
});
it('should verify transaction with policyId that does not have concatenated assetName', async () => {
// This tests the case where policyId is just the 28-byte policy ID (no assetName appended)
const tokenConfigWithPlainPolicyId = {
type: 'tada:water',
coin: 'tada',
network: 'Testnet' as const,
name: 'WATER',
decimalPlaces: 0,
policyId: policyId, // Just the policy ID without assetName
assetName: name, // ASCII name 'WATER'
contractAddress: `${policyId}:${asciiEncodedName}`,
};
const adaTokenWithPlainPolicyId = new AdaToken(bitgo, tokenConfigWithPlainPolicyId);
const quantity = '20';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Verify transaction - should work with plain policyId as well
const txParams = {
recipients: [
{
address: receiverAddress,
amount: quantity,
},
],
};
const txPrebuild = { txHex };
const mockWallet = { coinSpecific: () => ({ baseAddress: senderAddress }) };
const isVerified = await adaTokenWithPlainPolicyId.verifyTransaction({
txParams,
txPrebuild,
wallet: mockWallet as any,
});
isVerified.should.equal(true);
});
it('should verify token consolidation transaction when all outputs go to base address', async () => {
const quantity = '100';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
// Build a consolidation transaction - all outputs go to sender (base) address
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
// For consolidation, tokens go back to the sender's base address
txBuilder.output({
address: senderAddress,
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Mock wallet with coinSpecific returning base address
const mockWallet = {
coinSpecific: () => ({
baseAddress: senderAddress,
}),
};
// Verify consolidation transaction - no recipients, but consolidationToBaseAddress is true
const txParams = {
recipients: undefined,
};
const txPrebuild = { txHex };
const verification = { consolidationToBaseAddress: true };
const isVerified = await adaToken.verifyTransaction({
txParams,
txPrebuild,
verification,
wallet: mockWallet as any,
});
isVerified.should.equal(true);
});
it('should fail token consolidation when output address does not match base address', async () => {
const quantity = '100';
const totalInput = 20000000;
const totalAssetList = {
[fingerprint]: {
quantity: '100',
policy_id: policyId,
asset_name: asciiEncodedName,
},
};
// Build a transaction with output to receiver (not base address)
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21',
transaction_index: 1,
});
txBuilder.output({
address: receiverAddress, // Output goes to receiver, not base address
amount: '0',
multiAssets: {
asset_name: asciiEncodedName,
policy_id: policyId,
quantity,
fingerprint,
},
});
txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList);
txBuilder.ttl(800000000);
txBuilder.isTokenTransaction();
const tx = (await txBuilder.build()) as Transaction;
const txHex = tx.toBroadcastFormat();
// Mock wallet with different base address
const mockWallet = {
coinSpecific: () => ({
baseAddress: senderAddress, // Base address is sender, but output goes to receiver
}),
};
const txParams = {
recipients: undefined,
};
const txPrebuild = { txHex };
const verification = { consolidationToBaseAddress: true };
await adaToken
.verifyTransaction({
txParams,
txPrebuild,
verification,
wallet: mockWallet as any,
})
.should.be.rejectedWith('tx outputs does not match with expected address');
});
});
});