|
| 1 | +import assert from "node:assert"; |
| 2 | +import { Transaction } from "../js/transaction.js"; |
| 3 | +import { fixedScriptWallet } from "../js/index.js"; |
| 4 | + |
| 5 | +describe("Transaction builder", function () { |
| 6 | + it("should create an empty transaction", function () { |
| 7 | + const tx = Transaction.create(); |
| 8 | + const bytes = tx.toBytes(); |
| 9 | + assert.ok(bytes.length > 0, "serialized transaction should not be empty"); |
| 10 | + |
| 11 | + // Round-trip: the deserialized transaction should produce the same bytes |
| 12 | + const tx2 = Transaction.fromBytes(bytes); |
| 13 | + assert.deepStrictEqual(tx2.toBytes(), bytes); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should add an input and return index 0", function () { |
| 17 | + const tx = Transaction.create(); |
| 18 | + const txid = "a".repeat(64); |
| 19 | + const idx = tx.addInput(txid, 0); |
| 20 | + assert.strictEqual(idx, 0); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should add multiple inputs with incrementing indices", function () { |
| 24 | + const tx = Transaction.create(); |
| 25 | + const txid = "b".repeat(64); |
| 26 | + assert.strictEqual(tx.addInput(txid, 0), 0); |
| 27 | + assert.strictEqual(tx.addInput(txid, 1), 1); |
| 28 | + assert.strictEqual(tx.addInput(txid, 2), 2); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should add an output and return index 0", function () { |
| 32 | + const tx = Transaction.create(); |
| 33 | + // OP_RETURN script |
| 34 | + const script = fixedScriptWallet.createOpReturnScript(); |
| 35 | + const idx = tx.addOutput(script, 0n); |
| 36 | + assert.strictEqual(idx, 0); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should add multiple outputs with incrementing indices", function () { |
| 40 | + const tx = Transaction.create(); |
| 41 | + const script = fixedScriptWallet.createOpReturnScript(); |
| 42 | + assert.strictEqual(tx.addOutput(script, 1000n), 0); |
| 43 | + assert.strictEqual(tx.addOutput(script, 2000n), 1); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should round-trip a transaction with inputs and outputs", function () { |
| 47 | + const tx = Transaction.create(); |
| 48 | + const txid = "c".repeat(64); |
| 49 | + tx.addInput(txid, 0); |
| 50 | + tx.addInput(txid, 1, 0xfffffffe); |
| 51 | + |
| 52 | + const script = fixedScriptWallet.createOpReturnScript(new Uint8Array([0xde, 0xad])); |
| 53 | + tx.addOutput(script, 50000n); |
| 54 | + |
| 55 | + const bytes = tx.toBytes(); |
| 56 | + const tx2 = Transaction.fromBytes(bytes); |
| 57 | + assert.deepStrictEqual(tx2.toBytes(), bytes); |
| 58 | + assert.strictEqual(tx2.getId(), tx.getId()); |
| 59 | + assert.strictEqual(tx2.getVSize(), tx.getVSize()); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should produce a valid txid", function () { |
| 63 | + const tx = Transaction.create(); |
| 64 | + tx.addInput("a".repeat(64), 0); |
| 65 | + tx.addOutput(fixedScriptWallet.createOpReturnScript(), 0n); |
| 66 | + const txid = tx.getId(); |
| 67 | + assert.strictEqual(txid.length, 64); |
| 68 | + assert.match(txid, /^[0-9a-f]{64}$/); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should reject an invalid txid", function () { |
| 72 | + const tx = Transaction.create(); |
| 73 | + assert.throws(() => tx.addInput("not-a-valid-txid", 0)); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should accept custom sequence number", function () { |
| 77 | + const tx = Transaction.create(); |
| 78 | + const txid = "d".repeat(64); |
| 79 | + tx.addInput(txid, 0, 0); |
| 80 | + // If we can round-trip it, the sequence was accepted |
| 81 | + const bytes = tx.toBytes(); |
| 82 | + const tx2 = Transaction.fromBytes(bytes); |
| 83 | + assert.deepStrictEqual(tx2.toBytes(), bytes); |
| 84 | + }); |
| 85 | +}); |
0 commit comments