|
1 | 1 | import * as assert from "node:assert"; |
2 | 2 |
|
3 | | -import { Descriptor, Psbt, testutils } from "../js/index.js"; |
| 3 | +import { Descriptor, Psbt, address } from "../js/index.js"; |
| 4 | +import * as dt from "../js/testutils/descriptor/index.js"; |
| 5 | +import * as testutils from "../js/testutils/index.js"; |
4 | 6 |
|
5 | 7 | describe("testutils.descriptor", () => { |
6 | 8 | describe("getDefaultXPubs", () => { |
@@ -95,6 +97,67 @@ describe("testutils.descriptor.mockPsbt", () => { |
95 | 97 | }); |
96 | 98 | }); |
97 | 99 |
|
| 100 | +describe("Psbt.getOutputsWithAddress", () => { |
| 101 | + it("returns outputs with address strings for btc", () => { |
| 102 | + const psbt = dt.mockPsbtDefault(); |
| 103 | + const outputs = psbt.getOutputsWithAddress("btc"); |
| 104 | + assert.strictEqual(outputs.length, 2); |
| 105 | + for (const output of outputs) { |
| 106 | + assert.ok(typeof output.address === "string", "address should be a string"); |
| 107 | + assert.ok(output.address.length > 0, "address should not be empty"); |
| 108 | + assert.ok(output.script instanceof Uint8Array, "script should be Uint8Array"); |
| 109 | + assert.ok(typeof output.value === "bigint", "value should be bigint"); |
| 110 | + assert.ok(Array.isArray(output.bip32Derivation), "bip32Derivation should be array"); |
| 111 | + assert.ok(Array.isArray(output.tapBip32Derivation), "tapBip32Derivation should be array"); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it("returns consistent addresses with address.fromOutputScriptWithCoin", () => { |
| 116 | + const psbt = dt.mockPsbtDefault(); |
| 117 | + const outputsWithAddr = psbt.getOutputsWithAddress("btc"); |
| 118 | + const rawOutputs = psbt.getOutputs(); |
| 119 | + for (let i = 0; i < rawOutputs.length; i++) { |
| 120 | + const expected = address.fromOutputScriptWithCoin(rawOutputs[i].script, "btc"); |
| 121 | + assert.strictEqual(outputsWithAddr[i].address, expected); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it("returns btc mainnet addresses starting with expected prefixes", () => { |
| 126 | + const psbt = dt.mockPsbtDefaultWithDescriptorTemplate("Wsh2Of3"); |
| 127 | + const outputs = psbt.getOutputsWithAddress("btc"); |
| 128 | + for (const output of outputs) { |
| 129 | + // p2wsh addresses start with bc1 |
| 130 | + assert.ok(output.address.startsWith("bc1"), `expected bc1 prefix, got ${output.address}`); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it("returns tbtc testnet addresses starting with expected prefixes", () => { |
| 135 | + const psbt = dt.mockPsbtDefaultWithDescriptorTemplate("Wsh2Of3"); |
| 136 | + const outputs = psbt.getOutputsWithAddress("tbtc"); |
| 137 | + for (const output of outputs) { |
| 138 | + // p2wsh testnet addresses start with tb1 |
| 139 | + assert.ok(output.address.startsWith("tb1"), `expected tb1 prefix, got ${output.address}`); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it("works for all wsh/tr templates", () => { |
| 144 | + for (const t of [ |
| 145 | + "Wsh2Of3", |
| 146 | + "Wsh2Of2", |
| 147 | + "Wsh2Of3CltvDrop", |
| 148 | + "Tr2Of3-NoKeyPath", |
| 149 | + "Tr1Of3-NoKeyPath-Tree", |
| 150 | + ] as dt.DescriptorTemplate[]) { |
| 151 | + const psbt = dt.mockPsbtDefaultWithDescriptorTemplate(t, dt.getPsbtParams(t)); |
| 152 | + const outputs = psbt.getOutputsWithAddress("btc"); |
| 153 | + assert.strictEqual(outputs.length, 2, `${t}: expected 2 outputs`); |
| 154 | + for (const output of outputs) { |
| 155 | + assert.ok(output.address.length > 0, `${t}: address should not be empty`); |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
98 | 161 | describe("testutils.fixtures", () => { |
99 | 162 | describe("jsonNormalize", () => { |
100 | 163 | it("round-trips a simple object", () => { |
|
0 commit comments