|
1 | 1 | import should from 'should'; |
2 | 2 | import { TransactionType } from '@bitgo/sdk-core'; |
3 | 3 | import * as testData from '../resources'; |
4 | | -import { TransactionBuilderFactory } from '../../src'; |
| 4 | +import { TransactionBuilderFactory, AdaToken } from '../../src'; |
5 | 5 | import { coins } from '@bitgo/statics'; |
6 | 6 | import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs'; |
7 | 7 | import { Transaction } from '../../src/lib/transaction'; |
| 8 | +import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; |
| 9 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 10 | + |
| 11 | +// Set test password for BitGoJS tests |
| 12 | +process.env.BITGOJS_TEST_PASSWORD = 't3stSicretly!'; |
8 | 13 |
|
9 | 14 | describe('ADA Token Operations', async () => { |
10 | 15 | const factory = new TransactionBuilderFactory(coins.get('tada')); |
@@ -326,4 +331,175 @@ describe('ADA Token Operations', async () => { |
326 | 331 |
|
327 | 332 | await txBuilder.build().should.not.be.rejected(); |
328 | 333 | }); |
| 334 | + |
| 335 | + describe('AdaToken verifyTransaction', () => { |
| 336 | + let bitgo: TestBitGoAPI; |
| 337 | + let adaToken; |
| 338 | + |
| 339 | + before(function () { |
| 340 | + bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' }); |
| 341 | + bitgo.initializeTestVars(); |
| 342 | + const tokenConfig = { |
| 343 | + type: 'tada:water', |
| 344 | + coin: 'tada', |
| 345 | + network: 'Testnet' as const, |
| 346 | + name: 'WATER', |
| 347 | + decimalPlaces: 0, |
| 348 | + policyId: policyId, |
| 349 | + assetName: asciiEncodedName, |
| 350 | + contractAddress: `${policyId}:${asciiEncodedName}`, |
| 351 | + }; |
| 352 | + adaToken = new AdaToken(bitgo, tokenConfig); |
| 353 | + }); |
| 354 | + |
| 355 | + it('should verify a token transaction with correct token amount', async () => { |
| 356 | + const quantity = '20'; |
| 357 | + const totalInput = 20000000; |
| 358 | + const totalAssetList = { |
| 359 | + [fingerprint]: { |
| 360 | + quantity: '100', |
| 361 | + policy_id: policyId, |
| 362 | + asset_name: asciiEncodedName, |
| 363 | + }, |
| 364 | + }; |
| 365 | + |
| 366 | + const txBuilder = factory.getTransferBuilder(); |
| 367 | + txBuilder.input({ |
| 368 | + transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21', |
| 369 | + transaction_index: 1, |
| 370 | + }); |
| 371 | + |
| 372 | + txBuilder.output({ |
| 373 | + address: receiverAddress, |
| 374 | + amount: '0', |
| 375 | + multiAssets: { |
| 376 | + asset_name: asciiEncodedName, |
| 377 | + policy_id: policyId, |
| 378 | + quantity, |
| 379 | + fingerprint, |
| 380 | + }, |
| 381 | + }); |
| 382 | + |
| 383 | + txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList); |
| 384 | + txBuilder.ttl(800000000); |
| 385 | + txBuilder.isTokenTransaction(); |
| 386 | + const tx = (await txBuilder.build()) as Transaction; |
| 387 | + const txHex = tx.toBroadcastFormat(); |
| 388 | + |
| 389 | + // Verify transaction with correct token amount |
| 390 | + const txParams = { |
| 391 | + recipients: [ |
| 392 | + { |
| 393 | + address: receiverAddress, |
| 394 | + amount: quantity, // Token amount, not ADA amount |
| 395 | + }, |
| 396 | + ], |
| 397 | + }; |
| 398 | + |
| 399 | + const txPrebuild = { txHex }; |
| 400 | + const isVerified = await adaToken.verifyTransaction({ txParams, txPrebuild }); |
| 401 | + isVerified.should.equal(true); |
| 402 | + }); |
| 403 | + |
| 404 | + it('should fail to verify a token transaction with incorrect token amount', async () => { |
| 405 | + const quantity = '20'; |
| 406 | + const totalInput = 20000000; |
| 407 | + const totalAssetList = { |
| 408 | + [fingerprint]: { |
| 409 | + quantity: '100', |
| 410 | + policy_id: policyId, |
| 411 | + asset_name: asciiEncodedName, |
| 412 | + }, |
| 413 | + }; |
| 414 | + |
| 415 | + const txBuilder = factory.getTransferBuilder(); |
| 416 | + txBuilder.input({ |
| 417 | + transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21', |
| 418 | + transaction_index: 1, |
| 419 | + }); |
| 420 | + |
| 421 | + txBuilder.output({ |
| 422 | + address: receiverAddress, |
| 423 | + amount: '0', |
| 424 | + multiAssets: { |
| 425 | + asset_name: asciiEncodedName, |
| 426 | + policy_id: policyId, |
| 427 | + quantity, |
| 428 | + fingerprint, |
| 429 | + }, |
| 430 | + }); |
| 431 | + |
| 432 | + txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList); |
| 433 | + txBuilder.ttl(800000000); |
| 434 | + txBuilder.isTokenTransaction(); |
| 435 | + const tx = (await txBuilder.build()) as Transaction; |
| 436 | + const txHex = tx.toBroadcastFormat(); |
| 437 | + |
| 438 | + // Verify transaction with WRONG token amount (should fail) |
| 439 | + const txParams = { |
| 440 | + recipients: [ |
| 441 | + { |
| 442 | + address: receiverAddress, |
| 443 | + amount: '999', // Wrong amount |
| 444 | + }, |
| 445 | + ], |
| 446 | + }; |
| 447 | + |
| 448 | + const txPrebuild = { txHex }; |
| 449 | + await adaToken |
| 450 | + .verifyTransaction({ txParams, txPrebuild }) |
| 451 | + .should.be.rejectedWith('cannot find recipient in expected output'); |
| 452 | + }); |
| 453 | + |
| 454 | + it('should fail to verify when address does not match', async () => { |
| 455 | + const quantity = '20'; |
| 456 | + const totalInput = 20000000; |
| 457 | + const totalAssetList = { |
| 458 | + [fingerprint]: { |
| 459 | + quantity: '100', |
| 460 | + policy_id: policyId, |
| 461 | + asset_name: asciiEncodedName, |
| 462 | + }, |
| 463 | + }; |
| 464 | + |
| 465 | + const txBuilder = factory.getTransferBuilder(); |
| 466 | + txBuilder.input({ |
| 467 | + transaction_id: '3677e75c7ba699bfdc6cd57d42f246f86f63aefd76025006ac78313fad2bba21', |
| 468 | + transaction_index: 1, |
| 469 | + }); |
| 470 | + |
| 471 | + txBuilder.output({ |
| 472 | + address: receiverAddress, |
| 473 | + amount: '0', |
| 474 | + multiAssets: { |
| 475 | + asset_name: asciiEncodedName, |
| 476 | + policy_id: policyId, |
| 477 | + quantity, |
| 478 | + fingerprint, |
| 479 | + }, |
| 480 | + }); |
| 481 | + |
| 482 | + txBuilder.changeAddress(senderAddress, totalInput.toString(), totalAssetList); |
| 483 | + txBuilder.ttl(800000000); |
| 484 | + txBuilder.isTokenTransaction(); |
| 485 | + const tx = (await txBuilder.build()) as Transaction; |
| 486 | + const txHex = tx.toBroadcastFormat(); |
| 487 | + |
| 488 | + // Verify with wrong address (should fail) |
| 489 | + const txParams = { |
| 490 | + recipients: [ |
| 491 | + { |
| 492 | + address: |
| 493 | + 'addr_test1qqa86e3d7lfpwu0k2rhjz76ecmfxdr74s9kf9yfcp5hj5vmnh6xccjcclrk8jtaw9jgeuy99p2n8smtdpylmy45qjjfsfmp3g6', |
| 494 | + amount: quantity, |
| 495 | + }, |
| 496 | + ], |
| 497 | + }; |
| 498 | + |
| 499 | + const txPrebuild = { txHex }; |
| 500 | + await adaToken |
| 501 | + .verifyTransaction({ txParams, txPrebuild }) |
| 502 | + .should.be.rejectedWith('cannot find recipient in expected output'); |
| 503 | + }); |
| 504 | + }); |
329 | 505 | }); |
0 commit comments