Changeset View
Changeset View
Standalone View
Standalone View
modules/ecash-lib/tests/txBuilder.test.ts
| // Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| import { expect } from 'chai'; | import { expect } from 'chai'; | ||||
| import { ChronikClient } from 'chronik-client'; | import { ChronikClient } from 'chronik-client'; | ||||
| import { Ecc } from '../src/ecc.js'; | import { Ecc, EccDummy } from '../src/ecc.js'; | ||||
| import { sha256d, shaRmd160 } from '../src/hash.js'; | import { sha256d, shaRmd160 } from '../src/hash.js'; | ||||
| import { initWasm } from '../src/initNodeJs.js'; | import { initWasm } from '../src/initNodeJs.js'; | ||||
| import { fromHex, toHex } from '../src/io/hex.js'; | import { fromHex, toHex } from '../src/io/hex.js'; | ||||
| import { pushBytesOp } from '../src/op.js'; | import { pushBytesOp } from '../src/op.js'; | ||||
| import { | import { | ||||
| OP_CHECKSIG, | OP_CHECKSIG, | ||||
| OP_CHECKSIGVERIFY, | OP_CHECKSIGVERIFY, | ||||
| OP_CODESEPARATOR, | OP_CODESEPARATOR, | ||||
| ▲ Show 20 Lines • Show All 615 Lines • ▼ Show 20 Lines | it('TxBuilder leftover with 0xFD outputs', async () => { | ||||
| // Adding 1 extra sat -> fails -> showing that the previous tx was exact | // Adding 1 extra sat -> fails -> showing that the previous tx was exact | ||||
| extraOutput.value += 1; | extraOutput.value += 1; | ||||
| expect(() => txBuild.sign(ecc, 1000, 546)).to.throw( | expect(() => txBuild.sign(ecc, 1000, 546)).to.throw( | ||||
| `Insufficient input value (180000): Can only pay for ` + | `Insufficient input value (180000): Can only pay for ` + | ||||
| `${smallerSize - 1} fees, but ${smallerSize} required`, | `${smallerSize - 1} fees, but ${smallerSize} required`, | ||||
| ); | ); | ||||
| }); | }); | ||||
| it('TxBuilder signatory dependent on outputs', async () => { | |||||
| // Size of a tx with 1 input with empty scriptSig | |||||
| const expectedSize = | |||||
| 4 + // nVersion | |||||
| 1 + // num inputs | |||||
| 36 + // prevOut | |||||
| 1 + // script len | |||||
| 4 + // nSequence | |||||
| 1 + // num outputs | |||||
| 4; // locktime | |||||
| const txBuild = new TxBuilder({ | |||||
| inputs: [ | |||||
| { | |||||
| input: { | |||||
| prevOut: { | |||||
| txid: toHex(new Uint8Array(32)), | |||||
| outIdx: 0, | |||||
| }, | |||||
| signData: { | |||||
| value: expectedSize, | |||||
| }, | |||||
| }, | |||||
| signatory: (_, input) => { | |||||
| // returns OP_0 * number outputs | |||||
| // -> estimation will start this with 1 leftover output | |||||
| // and then with no outputs. | |||||
| return new Script( | |||||
| new Uint8Array(input.unsignedTx.tx.outputs.length), | |||||
| ); | |||||
| }, | |||||
| }, | |||||
| ], | |||||
| // Leftover script, but will be spliced out again | |||||
| outputs: [new Script()], | |||||
| }); | |||||
| const tx = txBuild.sign(new EccDummy(), 1000, 9999); | |||||
| expect(tx.serSize()).to.equal(expectedSize); | |||||
| }); | |||||
| it('TxBuilder leftover failure', async () => { | it('TxBuilder leftover failure', async () => { | ||||
| const txBuild = new TxBuilder({ | const txBuild = new TxBuilder({ | ||||
| inputs: [ | inputs: [ | ||||
| { | { | ||||
| input: { | input: { | ||||
| prevOut: { | prevOut: { | ||||
| txid: new Uint8Array(32), | txid: new Uint8Array(32), | ||||
| outIdx: 0, | outIdx: 0, | ||||
| Show All 21 Lines | |||||