refactor: use miniscript interpreter for input sighash detection - #51
refactor: use miniscript interpreter for input sighash detection#51bubb1es71 wants to merge 1 commit into
Conversation
|
While going through the files changed tab, I noticed that From what I understand, Bitcoin Core only supports that kind of lookup if the node is running with Not saying this is wrong, just wanted to flag it in case it's worth mentioning as a requirement somewhere, or double checking what actually happens on a node without txindex. |
|
Good catch. I'll update the README to require this index is enabled. |
Authored by: kimi-k3
fixes #47
Summary
Rewrites ddust transaction detection (
is_ddust_tx) to classify input signatures with the miniscript interpreter instead of witness-length heuristics, fixing detection of taproot script-path spends and resolving both TODOs in the function.Background
A witness/scriptSig stack is untyped: whether an element is a signature, pubkey, hash preimage, or script is defined by the script being spent, not by the stack itself. The old code guessed script type from witness length (1 = taproot key-path, 2 = P2WPKH, 3+ = P2WSH multisig), which could not be made correct in general:
tr()descriptors with script trees.wsh(pk(key))inputs have a 2-item witness<sig> <script>with noOP_0dummy, so the old multisig logic skipped the signature entirely and accepted any sighash.Changes
is_ddust_tx(tx, rpc_client)now fetches each input's prevoutscript_pubkeyvia RPC (cheap OP_RETURN check runs first, so only candidates incur the lookup). Unfetchable prevouts are skipped gracefully rather than panicking.is_all_anyonecanpay_input()feeds prevout script + scriptSig + witness intominiscript::interpreter::Interpreter::from_txdataand iteratesiter_assume_sigs(). The interpreter assigns each stack element its script-defined role — only actual signatures are checked, covering legacy P2PK/P2PKH, P2SH (incl. wrapped segwit), P2WPKH, P2WSH, and taproot key-path and script-path. Signature validity is not verified, only the sighash type, so no prevout amounts or sighash cache are needed.bdk_walletre-exportsminiscript, so thebitcointypes align with existing code.Tests
src/test_sighash.rs: 17 fast, node-less unit tests for the pure classification function:pkleaf,multi_a2-of-3 with empty sig slot),wsh(pk),P2SH-P2WPKHSIGHASH_ALL, 64-byteSIGHASH_DEFAULT, invalid 65-byte0x00sighash, one bad sighash in a multi-sig input, signature-less inputs, empty inputscargo fmt --check,cargo clippy --tests -- -D warningsclean; full suite: 74 passed