|
| 1 | +import assert from 'assert'; |
| 2 | +import * as should from 'should'; |
| 3 | +import { getBuilderFactory } from '../getBuilderFactory'; |
| 4 | +import * as testData from '../../resources/hbar'; |
| 5 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 6 | + |
| 7 | +describe('HBAR Account Update Builder', () => { |
| 8 | + const factory = getBuilderFactory('thbar'); |
| 9 | + |
| 10 | + const NODE_ID = 3; |
| 11 | + |
| 12 | + const initTxBuilder = () => { |
| 13 | + const txBuilder = factory.getAccountUpdateBuilder(); |
| 14 | + txBuilder.fee({ fee: testData.FEE }); |
| 15 | + txBuilder.source({ address: testData.ACCOUNT_1.accountId }); |
| 16 | + txBuilder.stakedNodeId(NODE_ID); |
| 17 | + return txBuilder; |
| 18 | + }; |
| 19 | + |
| 20 | + describe('should build', () => { |
| 21 | + describe('non serialized transactions', () => { |
| 22 | + it('a stake transaction', async () => { |
| 23 | + const builder = initTxBuilder(); |
| 24 | + builder.validDuration(1000000); |
| 25 | + builder.node({ nodeId: '0.0.2345' }); |
| 26 | + builder.startTime('1596110493.372646570'); |
| 27 | + const tx = await builder.build(); |
| 28 | + const txJson = tx.toJson(); |
| 29 | + txJson.instructionsData.params.accountId.should.deepEqual(testData.ACCOUNT_1.accountId); |
| 30 | + txJson.instructionsData.params.stakedNodeId.should.deepEqual(NODE_ID.toString()); |
| 31 | + should.deepEqual(txJson.from, testData.ACCOUNT_1.accountId); |
| 32 | + should.deepEqual(txJson.fee.toString(), testData.FEE); |
| 33 | + tx.type.should.equal(TransactionType.AccountUpdate); |
| 34 | + tx.inputs.length.should.equal(1); |
| 35 | + tx.inputs[0].should.deepEqual({ |
| 36 | + address: testData.ACCOUNT_1.accountId, |
| 37 | + value: '0', |
| 38 | + coin: 'thbar', |
| 39 | + }); |
| 40 | + tx.outputs.length.should.equal(1); |
| 41 | + tx.outputs[0].should.deepEqual({ |
| 42 | + address: testData.ACCOUNT_1.accountId, |
| 43 | + value: '0', |
| 44 | + coin: 'thbar', |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('an unstake transaction with stakedNodeId -1', async () => { |
| 49 | + const txBuilder = factory.getAccountUpdateBuilder(); |
| 50 | + txBuilder.fee({ fee: testData.FEE }); |
| 51 | + txBuilder.source({ address: testData.ACCOUNT_1.accountId }); |
| 52 | + txBuilder.stakedNodeId(-1); |
| 53 | + txBuilder.validDuration(1000000); |
| 54 | + txBuilder.node({ nodeId: '0.0.2345' }); |
| 55 | + txBuilder.startTime('1596110493.372646570'); |
| 56 | + const tx = await txBuilder.build(); |
| 57 | + const txJson = tx.toJson(); |
| 58 | + txJson.instructionsData.params.stakedNodeId.should.deepEqual('-1'); |
| 59 | + txJson.instructionsData.params.accountId.should.deepEqual(testData.ACCOUNT_1.accountId); |
| 60 | + tx.type.should.equal(TransactionType.AccountUpdate); |
| 61 | + }); |
| 62 | + |
| 63 | + it('a stake transaction with declineReward set to true', async () => { |
| 64 | + const builder = initTxBuilder(); |
| 65 | + builder.declineStakingReward(true); |
| 66 | + builder.node({ nodeId: '0.0.2345' }); |
| 67 | + builder.startTime('1596110493.372646570'); |
| 68 | + const tx = await builder.build(); |
| 69 | + const txJson = tx.toJson(); |
| 70 | + txJson.instructionsData.params.declineReward.should.equal(true); |
| 71 | + txJson.instructionsData.params.stakedNodeId.should.deepEqual(NODE_ID.toString()); |
| 72 | + }); |
| 73 | + |
| 74 | + it('a stake transaction with declineReward set to false', async () => { |
| 75 | + const builder = initTxBuilder(); |
| 76 | + builder.declineStakingReward(false); |
| 77 | + builder.node({ nodeId: '0.0.2345' }); |
| 78 | + builder.startTime('1596110493.372646570'); |
| 79 | + const tx = await builder.build(); |
| 80 | + const txJson = tx.toJson(); |
| 81 | + txJson.instructionsData.params.declineReward.should.equal(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('a signed stake transaction', async () => { |
| 85 | + const builder = initTxBuilder(); |
| 86 | + builder.validDuration(1000000); |
| 87 | + builder.node({ nodeId: '0.0.2345' }); |
| 88 | + builder.startTime('1596110493.372646570'); |
| 89 | + builder.sign({ key: testData.ACCOUNT_1.prvKeyWithPrefix }); |
| 90 | + const tx = await builder.build(); |
| 91 | + should.deepEqual(tx.signature.length, 1); |
| 92 | + tx.type.should.equal(TransactionType.AccountUpdate); |
| 93 | + }); |
| 94 | + |
| 95 | + it('a stake transaction with explicit account id', async () => { |
| 96 | + const txBuilder = factory.getAccountUpdateBuilder(); |
| 97 | + txBuilder.fee({ fee: testData.FEE }); |
| 98 | + txBuilder.source({ address: testData.ACCOUNT_1.accountId }); |
| 99 | + txBuilder.account(testData.ACCOUNT_2.accountId); |
| 100 | + txBuilder.stakedNodeId(NODE_ID); |
| 101 | + txBuilder.node({ nodeId: '0.0.2345' }); |
| 102 | + const tx = await txBuilder.build(); |
| 103 | + const txJson = tx.toJson(); |
| 104 | + txJson.instructionsData.params.accountId.should.deepEqual(testData.ACCOUNT_2.accountId); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('serialized transactions', () => { |
| 109 | + it('a signed account update transaction round-trip', async () => { |
| 110 | + const builder = initTxBuilder(); |
| 111 | + builder.validDuration(1000000); |
| 112 | + builder.node({ nodeId: '0.0.2345' }); |
| 113 | + builder.startTime('1596110493.372646570'); |
| 114 | + builder.sign({ key: testData.ACCOUNT_1.prvKeyWithPrefix }); |
| 115 | + const tx = await builder.build(); |
| 116 | + const serialized = tx.toBroadcastFormat(); |
| 117 | + |
| 118 | + const builder2 = factory.from(serialized); |
| 119 | + builder2.sign({ key: testData.ACCOUNT_2.privateKey }); |
| 120 | + const tx2 = await builder2.build(); |
| 121 | + should.deepEqual(tx2.signature.length, 2); |
| 122 | + tx2.type.should.equal(TransactionType.AccountUpdate); |
| 123 | + tx2.toJson().instructionsData.params.stakedNodeId.should.deepEqual(NODE_ID.toString()); |
| 124 | + }); |
| 125 | + |
| 126 | + it('an unsigned account update transaction round-trip', async () => { |
| 127 | + const builder = initTxBuilder(); |
| 128 | + builder.validDuration(1000000); |
| 129 | + builder.node({ nodeId: '0.0.2345' }); |
| 130 | + builder.startTime('1596110493.372646570'); |
| 131 | + const tx = await builder.build(); |
| 132 | + const serialized = tx.toBroadcastFormat(); |
| 133 | + |
| 134 | + const builder2 = factory.from(serialized); |
| 135 | + const tx2 = await builder2.build(); |
| 136 | + tx2.type.should.equal(TransactionType.AccountUpdate); |
| 137 | + tx2.toJson().instructionsData.params.accountId.should.deepEqual(testData.ACCOUNT_1.accountId); |
| 138 | + tx2.toJson().instructionsData.params.stakedNodeId.should.deepEqual(NODE_ID.toString()); |
| 139 | + }); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('should fail', () => { |
| 144 | + it('a stake transaction without stakedNodeId', async () => { |
| 145 | + const txBuilder = factory.getAccountUpdateBuilder(); |
| 146 | + txBuilder.fee({ fee: testData.FEE }); |
| 147 | + txBuilder.source({ address: testData.ACCOUNT_1.accountId }); |
| 148 | + txBuilder.node({ nodeId: '0.0.2345' }); |
| 149 | + await txBuilder.build().should.be.rejectedWith('Invalid transaction: missing stakedNodeId'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('a stake transaction with an invalid account id', () => { |
| 153 | + const txBuilder = factory.getAccountUpdateBuilder(); |
| 154 | + assert.throws( |
| 155 | + () => txBuilder.account('invalidAccountId'), |
| 156 | + (e: any) => e.message === 'Invalid account address: invalidAccountId' |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('a stake transaction with more signatures than allowed', () => { |
| 161 | + const builder = initTxBuilder(); |
| 162 | + builder.sign({ key: testData.ACCOUNT_1.prvKeyWithPrefix }); |
| 163 | + builder.sign({ key: testData.ACCOUNT_2.privateKey }); |
| 164 | + builder.sign({ key: testData.ACCOUNT_3.privateKey }); |
| 165 | + assert.throws( |
| 166 | + () => builder.sign({ key: '5bb72603f237c0993f7973d37fdade32c71aa94aee686aa79d260acba1882d9a' }), |
| 167 | + (e: any) => e.message === 'A maximum of 3 can sign the transaction.' |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it('a stake transaction with an invalid key', () => { |
| 172 | + const builder = initTxBuilder(); |
| 173 | + assert.throws( |
| 174 | + () => builder.sign({ key: '5bb72603f237c0993f7973d37fdade32c71aa94aee686aa79d260acba1882d90AA' }), |
| 175 | + (e: any) => e.message === 'Invalid private key' |
| 176 | + ); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments