From dfc9acbf0170bde6f2abb879b5584dabd1266531 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Fri, 3 Feb 2023 17:44:22 +0100 Subject: [PATCH] rpc: decode Miniscript descriptor when possible in decodescript The descriptor inference logic would previously always use a dummy signing provider and would never analyze the witness script of a P2WSH scriptPubKey. Note even a valid Miniscript might not always be decodable from Script without more contextual information (for instance the key preimage for a pk_h). --- src/core_io.h | 3 ++- src/core_write.cpp | 4 ++-- src/rpc/rawtransaction.cpp | 4 +++- test/functional/data/rpc_decodescript.json | 2 +- test/functional/rpc_decodescript.py | 12 ++++++++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core_io.h b/src/core_io.h index 33e1ad82fc..997f3bfd5b 100644 --- a/src/core_io.h +++ b/src/core_io.h @@ -15,6 +15,7 @@ class CBlockHeader; class CScript; class CTransaction; struct CMutableTransaction; +class SigningProvider; class uint256; class UniValue; class CTxUndo; @@ -52,7 +53,7 @@ UniValue ValueFromAmount(const CAmount amount); std::string FormatScript(const CScript& script); std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0); std::string SighashToStr(unsigned char sighash_type); -void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false); +void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false, const SigningProvider* provider = nullptr); void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS); #endif // BITCOIN_CORE_IO_H diff --git a/src/core_write.cpp b/src/core_write.cpp index 300bd30e43..b0e3b0b3c4 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -147,13 +147,13 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags) return HexStr(ssTx); } -void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address) +void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider) { CTxDestination address; out.pushKV("asm", ScriptToAsmStr(script)); if (include_address) { - out.pushKV("desc", InferDescriptor(script, DUMMY_SIGNING_PROVIDER)->ToString()); + out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString()); } if (include_hex) { out.pushKV("hex", HexStr(script)); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 7704496c10..503099d8a9 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -522,15 +522,17 @@ static RPCHelpMan decodescript() if (can_wrap_P2WSH) { UniValue sr(UniValue::VOBJ); CScript segwitScr; + FillableSigningProvider provider; if (which_type == TxoutType::PUBKEY) { segwitScr = GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0]))); } else if (which_type == TxoutType::PUBKEYHASH) { segwitScr = GetScriptForDestination(WitnessV0KeyHash(uint160{solutions_data[0]})); } else { // Scripts that are not fit for P2WPKH are encoded as P2WSH. + provider.AddCScript(script); segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script)); } - ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true); + ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true, /*provider=*/&provider); sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr))); r.pushKV("segwit", sr); } diff --git a/test/functional/data/rpc_decodescript.json b/test/functional/data/rpc_decodescript.json index 4a15ae8792..5f3e725d4c 100644 --- a/test/functional/data/rpc_decodescript.json +++ b/test/functional/data/rpc_decodescript.json @@ -69,7 +69,7 @@ "p2sh": "2N34iiGoUUkVSPiaaTFpJjB1FR9TXQu3PGM", "segwit": { "asm": "0 96c2368fc30514a438a8bd909f93c49a1549d77198ccbdb792043b666cb24f42", - "desc": "addr(bcrt1qjmprdr7rq522gw9ghkgfly7yng25n4m3nrxtmdujqsakvm9jfapqk795l5)#5akkdska", + "desc": "wsh(raw(02eeee))#gtay4y0z", "hex": "002096c2368fc30514a438a8bd909f93c49a1549d77198ccbdb792043b666cb24f42", "address": "bcrt1qjmprdr7rq522gw9ghkgfly7yng25n4m3nrxtmdujqsakvm9jfapqk795l5", "type": "witness_v0_scripthash", diff --git a/test/functional/rpc_decodescript.py b/test/functional/rpc_decodescript.py index a61710b739..8df3b68785 100755 --- a/test/functional/rpc_decodescript.py +++ b/test/functional/rpc_decodescript.py @@ -263,6 +263,16 @@ class DecodeScriptTest(BitcoinTestFramework): rpc_result = self.nodes[0].decodescript(script) assert_equal(result, rpc_result) + def decodescript_miniscript(self): + """Check that a Miniscript is decoded when possible under P2WSH context.""" + # Sourced from https://github.com/bitcoin/bitcoin/pull/27037#issuecomment-1416151907. + # Miniscript-compatible offered HTLC + res = self.nodes[0].decodescript("82012088a914ffffffffffffffffffffffffffffffffffffffff88210250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0ad51b2") + assert res["segwit"]["desc"] == "wsh(and_v(and_v(v:hash160(ffffffffffffffffffffffffffffffffffffffff),v:pk(0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0)),older(1)))#gm8xz4fl" + # Miniscript-incompatible offered HTLC + res = self.nodes[0].decodescript("82012088a914ffffffffffffffffffffffffffffffffffffffff882102ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffacb2") + assert res["segwit"]["desc"] == "wsh(raw(82012088a914ffffffffffffffffffffffffffffffffffffffff882102ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffacb2))#ra6w2xa7" + def run_test(self): self.log.info("Test decoding of standard input scripts [scriptSig]") self.decodescript_script_sig() @@ -272,6 +282,8 @@ class DecodeScriptTest(BitcoinTestFramework): self.decoderawtransaction_asm_sighashtype() self.log.info("Data-driven tests") self.decodescript_datadriven_tests() + self.log.info("Miniscript descriptor decoding") + self.decodescript_miniscript() if __name__ == '__main__': DecodeScriptTest().main()