|
| 1 | +import should from 'should'; |
| 2 | +import { RejectInstructionBuilder } from '../../../src/lib'; |
| 3 | +import { utils } from '../../../src'; |
| 4 | + |
| 5 | +import { accounts, rawTx, chainName, genesisHash, mockTssSignature } from '../../resources'; |
| 6 | +import { buildTestConfig } from './base'; |
| 7 | +import { testnetMaterial } from '../../../src/resources'; |
| 8 | + |
| 9 | +describe('Polyx Reject Instruction Builder - Testnet', () => { |
| 10 | + let builder: RejectInstructionBuilder; |
| 11 | + |
| 12 | + const sender = accounts.account1; |
| 13 | + const instructionId = '14100'; |
| 14 | + const portfolioDID = '0x1208d7851e6698249aea40742701ee1ef6cdcced260a7c49c1cca1a9db836342'; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + const config = buildTestConfig(); |
| 18 | + builder = new RejectInstructionBuilder(config).material(utils.getMaterial(config.network.type)); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('build rejectInstruction transaction', () => { |
| 22 | + it('should build a rejectInstruction transaction', async () => { |
| 23 | + builder |
| 24 | + .instructionId(instructionId) |
| 25 | + .portfolioDID(portfolioDID) |
| 26 | + .sender({ address: sender.address }) |
| 27 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 28 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 29 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 30 | + .fee({ amount: 0, type: 'tip' }); |
| 31 | + builder.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 32 | + const tx = await builder.build(); |
| 33 | + const txJson = tx.toJson(); |
| 34 | + should.deepEqual(txJson.instructionId, instructionId); |
| 35 | + should.deepEqual(txJson.portfolioDID, portfolioDID); |
| 36 | + should.deepEqual(txJson.sender, sender.address); |
| 37 | + should.deepEqual(txJson.blockNumber, 3933); |
| 38 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 39 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 40 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 41 | + should.deepEqual(txJson.nonce, 200); |
| 42 | + should.deepEqual(txJson.tip, 0); |
| 43 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 44 | + should.deepEqual(txJson.chainName, testnetMaterial.chainName); |
| 45 | + should.deepEqual(txJson.eraPeriod, 64); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should build an unsigned rejectInstruction transaction', async () => { |
| 49 | + builder |
| 50 | + .instructionId(instructionId) |
| 51 | + .portfolioDID(portfolioDID) |
| 52 | + .sender({ address: sender.address }) |
| 53 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 54 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 55 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 56 | + .fee({ amount: 0, type: 'tip' }); |
| 57 | + const tx = await builder.build(); |
| 58 | + const txJson = tx.toJson(); |
| 59 | + should.deepEqual(txJson.instructionId, instructionId); |
| 60 | + should.deepEqual(txJson.portfolioDID, portfolioDID); |
| 61 | + should.deepEqual(txJson.sender, sender.address); |
| 62 | + should.deepEqual(txJson.blockNumber, 3933); |
| 63 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 64 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 65 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 66 | + should.deepEqual(txJson.nonce, 200); |
| 67 | + should.deepEqual(txJson.tip, 0); |
| 68 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 69 | + should.deepEqual(txJson.chainName, chainName); |
| 70 | + should.deepEqual(txJson.eraPeriod, 64); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should build from raw signed tx', async () => { |
| 74 | + if (rawTx.rejectInstruction?.signed) { |
| 75 | + builder.from(rawTx.rejectInstruction.signed); |
| 76 | + builder |
| 77 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 78 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 79 | + const tx = await builder.build(); |
| 80 | + const txJson = tx.toJson(); |
| 81 | + should.exist(txJson.instructionId); |
| 82 | + should.deepEqual(txJson.instructionId, instructionId); |
| 83 | + should.exist(txJson.portfolioDID); |
| 84 | + should.deepEqual(txJson.portfolioDID, portfolioDID); |
| 85 | + should.deepEqual(txJson.blockNumber, 3933); |
| 86 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 87 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 88 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 89 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 90 | + should.deepEqual(txJson.chainName, chainName); |
| 91 | + should.deepEqual(txJson.eraPeriod, 64); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('should build from raw unsigned tx', async () => { |
| 96 | + if (rawTx.rejectInstruction?.unsigned) { |
| 97 | + builder.from(rawTx.rejectInstruction.unsigned); |
| 98 | + builder |
| 99 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 100 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 101 | + .sender({ address: sender.address }) |
| 102 | + .addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 103 | + |
| 104 | + const tx = await builder.build(); |
| 105 | + const txJson = tx.toJson(); |
| 106 | + should.exist(txJson.instructionId); |
| 107 | + should.deepEqual(txJson.instructionId, instructionId); |
| 108 | + should.exist(txJson.portfolioDID); |
| 109 | + should.deepEqual(txJson.portfolioDID, portfolioDID); |
| 110 | + should.deepEqual(txJson.sender, sender.address); |
| 111 | + should.deepEqual(txJson.blockNumber, 3933); |
| 112 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 113 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 114 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 115 | + should.deepEqual(txJson.eraPeriod, 64); |
| 116 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 117 | + should.deepEqual(txJson.chainName, chainName); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('should validate instruction ID is set', async () => { |
| 122 | + builder |
| 123 | + .portfolioDID(portfolioDID) |
| 124 | + .sender({ address: sender.address }) |
| 125 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 126 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 127 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 128 | + .fee({ amount: 0, type: 'tip' }); |
| 129 | + |
| 130 | + await builder.build().should.be.rejected(); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments