Skip to content

decodescript could infer miniscript from given script in "segwit" context #27007

Closed

Description

@instagibbs

I was playing around with a probably-too-cute script for LN channel smart contract, and was looking for something to decide for me if miniscript can infer the structure of the script.

f.e.

decodescript 82012088a914ffffffffffffffffffffffffffffffffffffffff8820ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffad51b2

{
  "asm": "OP_SIZE 32 OP_EQUALVERIFY OP_HASH160 ffffffffffffffffffffffffffffffffffffffff OP_EQUALVERIFY ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY",
  "desc": "raw(82012088a914ffffffffffffffffffffffffffffffffffffffff8820ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffad51b2)#5guaahuv",
  "type": "nonstandard",
  "p2sh": "33YyjgxsJYwSi8Wbp7TaCSFAuJBoUfYWDQ",
  "segwit": {
    "asm": "0 99f5d0b69f9828fea9363198cf812e95d7fc5e94c7881092b8e3f43984c1bd5c",
    "desc": "addr(bc1qn86apd5lnq50a2fkxxvvlqfwjhtlch55c7yppy4cu06rnpxph4wqrar3md)#wtlf0gwk",
    "hex": "002099f5d0b69f9828fea9363198cf812e95d7fc5e94c7881092b8e3f43984c1bd5c",
    "address": "bc1qn86apd5lnq50a2fkxxvvlqfwjhtlch55c7yppy4cu06rnpxph4wqrar3md",
    "type": "witness_v0_scripthash",
    "p2sh-segwit": "3MRJN2wTnD2BVm31HLcBwueAYpbbximrtk"
  }
}

Since it's evaluated at "top level for desc it cannot get any meaningful structure from it. Perhaps in segwit it could try infering the miniscript in the segwit context

Activity

  1. darosior commented on Jan 31, 2023

    @darosior
    Member

    The issue here is that in decodescript we do try to parse a wsh descriptor from a constructed P2WSH scriptPubKey:

    if (can_wrap_P2WSH) {
    UniValue sr(UniValue::VOBJ);
    CScript segwitScr;
    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.
    segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
    }
    ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true);

    But inferScript won't be able to analyze the witness script itself since we are passing a dummy signature provider:

    bitcoin/src/core_write.cpp

    Lines 150 to 156 in 2b211b4

    void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address)
    {
    CTxDestination address;
    out.pushKV("asm", ScriptToAsmStr(script));
    if (include_address) {
    out.pushKV("desc", InferDescriptor(script, DUMMY_SIGNING_PROVIDER)->ToString());

    Therefore GetCScript won't return anything:

    if (txntype == TxoutType::WITNESS_V0_SCRIPTHASH && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH)) {
    CScriptID scriptid{RIPEMD160(data[0])};
    CScript subscript;
    if (provider.GetCScript(scriptid, subscript)) {
    auto sub = InferScript(subscript, ParseScriptContext::P2WSH, provider);
    if (sub) return std::make_unique<WSHDescriptor>(std::move(sub));
    }
    }

  2. sipa commented on Jan 31, 2023

    @sipa
    Member

    I would think that all we need to do is add the RPC argument script to the signature provider used for inference.

  3. instagibbs commented on Jan 31, 2023

    @instagibbs
    MemberAuthor

    Yes, it's that simple (well, example I gave above was xonly key, so added 0x02 prefix):

    decodescript 82012088a914ffffffffffffffffffffffffffffffffffffffff882102ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffad51b2
    {
      "asm": "OP_SIZE 32 OP_EQUALVERIFY OP_HASH160 ffffffffffffffffffffffffffffffffffffffff OP_EQUALVERIFY 02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY",
      "desc": "raw(82012088a914ffffffffffffffffffffffffffffffffffffffff882102ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffad51b2)#x8xuy2m2",
      "type": "nonstandard",
      "p2sh": "2MzccBLNZbDP7tXZkrKKp5Zi7wDX4XpvAjQ",
      "segwit": {
        "asm": "0 ef65a26f417cee177ce157608612a95ff6a45a052dc440883cf2d069fca8fc82",
        "desc": "wsh(and_v(and_v(v:hash160(ffffffffffffffffffffffffffffffffffffffff),v:pk(02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)),older(1)))#twc36s59",
        "hex": "0020ef65a26f417cee177ce157608612a95ff6a45a052dc440883cf2d069fca8fc82",
        "address": "bcrt1qaaj6ym6p0nhpwl8p2asgvy4ftlm2gks99hzypzpu7tgxnl9gljpq8j9ewh",
        "type": "witness_v0_scripthash",
        "p2sh-segwit": "2MxgwFJU1Ei4eYqbShbVSX2ecYYooEm8zxy"
      }
    }
    
  4. darosior commented on Feb 1, 2023

    @darosior
    Member

    @instagibbs do you intend to PR this? Otherwise i can do it.

  5. instagibbs commented on Feb 1, 2023

    @instagibbs
    MemberAuthor
  6. locked and limited conversation to collaborators on Feb 3, 2024
  7. added a commit that references this issue on Mar 4, 2024
  8. added a commit that references this issue on Jun 28, 2026
  9. added a commit that references this issue on Jun 30, 2026
  10. added a commit that references this issue on Aug 19, 2026
  11. added a commit that references this issue on Sep 18, 2026
  12. added a commit that references this issue on Oct 4, 2026
  13. added a commit that references this issue on Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions