|
| 1 | +import assert from 'assert'; |
| 2 | +import should from 'should'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import { RegisterDidWithCDDBuilder } from '../../../src/lib'; |
| 5 | +import { utils } from '../../../src'; |
| 6 | + |
| 7 | +import { accounts, rawTx, chainName, genesisHash, mockTssSignature } from '../../resources'; |
| 8 | +import { buildTestConfig } from './base'; |
| 9 | +import { testnetMaterial } from '../../../src/resources'; |
| 10 | + |
| 11 | +describe('Polyx Register DID with CDD builder Builder', () => { |
| 12 | + let builder: RegisterDidWithCDDBuilder; |
| 13 | + |
| 14 | + const sender = accounts.cddProvider; |
| 15 | + const receiver = accounts.account2; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + const config = buildTestConfig(); |
| 19 | + builder = new RegisterDidWithCDDBuilder(config).material(utils.getMaterial(config.network.type)); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('setter validation', () => { |
| 23 | + it('should validate to address', () => { |
| 24 | + const spy = sinon.spy(builder, 'validateAddress'); |
| 25 | + assert.throws( |
| 26 | + () => builder.to({ address: 'asd' }), |
| 27 | + (e: Error) => e.message === `The address 'asd' is not a well-formed dot address` |
| 28 | + ); |
| 29 | + should.doesNotThrow(() => builder.to({ address: sender.address })); |
| 30 | + sinon.assert.calledTwice(spy); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('build transfer transaction', () => { |
| 35 | + it('should build a register did with cdd transaction', async () => { |
| 36 | + builder |
| 37 | + .to({ address: receiver.address }) |
| 38 | + .sender({ address: sender.address }) |
| 39 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 40 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 41 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 42 | + .fee({ amount: 0, type: 'tip' }); |
| 43 | + builder.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 44 | + const tx = await builder.build(); |
| 45 | + const txJson = tx.toJson(); |
| 46 | + should.deepEqual(txJson.to, receiver.address); |
| 47 | + should.deepEqual(txJson.sender, sender.address); |
| 48 | + should.deepEqual(txJson.blockNumber, 3933); |
| 49 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 50 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 51 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 52 | + should.deepEqual(txJson.nonce, 200); |
| 53 | + should.deepEqual(txJson.tip, 0); |
| 54 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 55 | + should.deepEqual(txJson.chainName, testnetMaterial.chainName); |
| 56 | + should.deepEqual(txJson.eraPeriod, 64); |
| 57 | + |
| 58 | + const inputs = tx.inputs[0]; |
| 59 | + should.deepEqual(inputs.address, sender.address); |
| 60 | + should.deepEqual(inputs.value, '0'); |
| 61 | + |
| 62 | + const outputs = tx.outputs[0]; |
| 63 | + should.deepEqual(outputs.address, receiver.address); |
| 64 | + should.deepEqual(outputs.value, '0'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should build a transaction with zero maxDuration (immortal)', async () => { |
| 68 | + builder |
| 69 | + .to({ address: receiver.address }) |
| 70 | + .sender({ address: sender.address }) |
| 71 | + .validity({ firstValid: 3933, maxDuration: 0 }) |
| 72 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 73 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 74 | + .fee({ amount: 0, type: 'tip' }); |
| 75 | + builder.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 76 | + const tx = await builder.build(); |
| 77 | + const txJson = tx.toJson(); |
| 78 | + should.deepEqual(txJson.to, receiver.address); |
| 79 | + should.deepEqual(txJson.sender, sender.address); |
| 80 | + should.deepEqual(txJson.blockNumber, 3933); |
| 81 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 82 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 83 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 84 | + should.deepEqual(txJson.nonce, 200); |
| 85 | + should.deepEqual(txJson.tip, 0); |
| 86 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 87 | + should.deepEqual(txJson.chainName, chainName); |
| 88 | + should.deepEqual(txJson.eraPeriod, 0); |
| 89 | + |
| 90 | + const inputs = tx.inputs[0]; |
| 91 | + should.deepEqual(inputs.address, sender.address); |
| 92 | + should.deepEqual(inputs.value, '0'); |
| 93 | + |
| 94 | + const outputs = tx.outputs[0]; |
| 95 | + should.deepEqual(outputs.address, receiver.address); |
| 96 | + should.deepEqual(outputs.value, '0'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should build an unsigned transfer transaction', async () => { |
| 100 | + builder |
| 101 | + .to({ address: receiver.address }) |
| 102 | + .sender({ address: sender.address }) |
| 103 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 104 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 105 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 106 | + .fee({ amount: 0, type: 'tip' }); |
| 107 | + const tx = await builder.build(); |
| 108 | + const txJson = tx.toJson(); |
| 109 | + should.deepEqual(txJson.to, receiver.address); |
| 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.nonce, 200); |
| 116 | + should.deepEqual(txJson.tip, 0); |
| 117 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 118 | + should.deepEqual(txJson.chainName, chainName); |
| 119 | + should.deepEqual(txJson.eraPeriod, 64); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should build from raw signed tx', async () => { |
| 123 | + builder.from(rawTx.cddTransaction.signed); |
| 124 | + builder |
| 125 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 126 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 127 | + const tx = await builder.build(); |
| 128 | + const txJson = tx.toJson(); |
| 129 | + should.deepEqual(txJson.to, '5F8jxKE81GhFrphyfMFr5UjeAz5wS4AaZFmeFPnf8wTetD72'); |
| 130 | + should.deepEqual(txJson.sender, '5CLYvxwx4PUS678MNuhNJ9EfpUU9utrYCz9WVxovac4u9AYD'); |
| 131 | + should.deepEqual(txJson.blockNumber, 3933); |
| 132 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 133 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 134 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 135 | + should.deepEqual(txJson.nonce, 34); |
| 136 | + should.deepEqual(txJson.tip, 0); |
| 137 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 138 | + should.deepEqual(txJson.chainName, chainName); |
| 139 | + should.deepEqual(txJson.eraPeriod, 64); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should build from raw unsigned tx', async () => { |
| 143 | + builder.from(rawTx.transfer.unsigned); |
| 144 | + builder |
| 145 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 146 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 147 | + .sender({ address: sender.address }) |
| 148 | + .addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 149 | + |
| 150 | + const tx = await builder.build(); |
| 151 | + const txJson = tx.toJson(); |
| 152 | + should.deepEqual(txJson.amount, '2000000000'); |
| 153 | + should.deepEqual(txJson.to, '5F8jxKE81GhFrphyfMFr5UjeAz5wS4AaZFmeFPnf8wTetD72'); |
| 154 | + should.deepEqual(txJson.sender, sender.address); |
| 155 | + should.deepEqual(txJson.blockNumber, 3933); |
| 156 | + should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'); |
| 157 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 158 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 159 | + should.deepEqual(txJson.nonce, 36); |
| 160 | + should.deepEqual(txJson.eraPeriod, 64); |
| 161 | + should.deepEqual(txJson.tip, 0); |
| 162 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 163 | + should.deepEqual(txJson.chainName, chainName); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments