|
| 1 | +/** |
| 2 | + * Previous-transaction inclusion policy for fixed-script wallet PSBT inputs. |
| 3 | + * |
| 4 | + * Decides whether a p2sh input requires the full previous transaction |
| 5 | + * (PSBT_IN_NON_WITNESS_UTXO) or can be signed from witness_utxo-only. |
| 6 | + * |
| 7 | + * This is a pure-JS module (no WASM initialization) so callers can evaluate |
| 8 | + * the policy cheaply without loading the wasm-utxo module. |
| 9 | + */ |
| 10 | +import { type CoinName, getMainnet } from "../coinName.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Whether a p2sh input requires the full previous transaction |
| 14 | + * (PSBT_IN_NON_WITNESS_UTXO). Callers are expected to have already |
| 15 | + * confirmed the input is p2sh (non-segwit) and that the tx format |
| 16 | + * includes prevTx (e.g. "psbt", not "psbt-lite"); this predicate only |
| 17 | + * answers the coin-level question. |
| 18 | + * |
| 19 | + * Returns false for value-committing coins whose sighash commits the |
| 20 | + * input amount, making `non_witness_utxo` (full prevTx) cryptographically |
| 21 | + * pointless for signing p2sh inputs — `witness_utxo` (value + |
| 22 | + * scriptPubKey) suffices: |
| 23 | + * |
| 24 | + * - Zcash (`zec`/`tzec`): ZIP-243 transparent sighash commits the amount. |
| 25 | + * Including prevTx also crashes wasm-utxo, whose consensus::deserialize |
| 26 | + * rejects Zcash overwintered transactions. |
| 27 | + * - BCH family (`bch`/`bcha`/`bsv`/`btg` + testnets): replay-protected |
| 28 | + * BIP-143 sighash (SIGHASH_FORKID, the default for the whole family) |
| 29 | + * commits the 8-byte value as preimage item #6. eCash is `bcha`/`tbcha`. |
| 30 | + * For the BCH family, skipping prevTx is an optimization (no DB fetch) |
| 31 | + * plus defense-in-depth, with the same fee-validation risk that the |
| 32 | + * existing `psbt-lite` path already accepts for all coins. |
| 33 | + * |
| 34 | + * Testnets are normalized via `getMainnet` before the switch. True |
| 35 | + * otherwise. |
| 36 | + */ |
| 37 | +export function requiresPrevTxForP2sh(coinName: CoinName): boolean { |
| 38 | + switch (getMainnet(coinName)) { |
| 39 | + case "zec": // Zcash (ZIP-243) |
| 40 | + case "bch": // Bitcoin Cash (BIP-143/FORKID) |
| 41 | + case "bcha": // eCash (BIP-143/FORKID) |
| 42 | + case "bsv": // Bitcoin SV (BIP-143/FORKID) |
| 43 | + case "btg": // Bitcoin Gold (BIP-143/FORKID) |
| 44 | + return false; |
| 45 | + default: |
| 46 | + return true; |
| 47 | + } |
| 48 | +} |
0 commit comments