$ bitcoin-cli help decodescript
decodescript "hex"

解码一个 16 进制编码的脚本

参数:
1. "hex"          (字符串)16 进制编码的脚本

结果:
{
  "asm":"asm",    (字符串)脚本公钥
  "hex":"hex",    (字符串)16 进制编码的公钥
  "type":"type",  (字符串)输出类型
  "reqSigs": n,   (数字)所需的签名
  "addresses": [  (字符串的 json 数组)
     "address"    (字符串)比特币地址
     ,...
  ],
  "p2sh","address"(字符串)脚本地址
}

例子:
> bitcoin-cli decodescript "hexstring"
> curl --user myusername:mypassword --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "decodescript", "params": ["hexstring"] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/

源码剖析

decodescript 对应的函数在文件 rpcserver.h 中被引用。

extern UniValue decodescript(const UniValue& params, bool fHelp);

实现在文件 rpcrawtransaction.cpp 中。

UniValue decodescript(const UniValue& params, bool fHelp)
{
    if (fHelp || params.size() != 1)
        throw runtime_error(
            "decodescript \"hex\"\n"
            "\nDecode a hex-encoded script.\n"
            "\nArguments:\n"
            "1. \"hex\"     (string) the hex encoded script\n"
            "\nResult:\n"
            "{\n"
            "  \"asm\":\"asm\",   (string) Script public key\n"
            "  \"hex\":\"hex\",   (string) hex encoded public key\n"
            "  \"type\":\"type\", (string) The output type\n"
            "  \"reqSigs\": n,    (numeric) The required signatures\n"
            "  \"addresses\": [   (json array of string)\n"
            "     \"address\"     (string) bitcoin address\n"
            "     ,...\n"
            "  ],\n"
            "  \"p2sh\",\"address\" (string) script address\n"
            "}\n"
            "\nExamples:\n"
            + HelpExampleCli("decodescript", "\"hexstring\"")
            + HelpExampleRpc("decodescript", "\"hexstring\"")
        ); // 1. 帮助内容

    RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR)); // 2. RPC 类型检测

    UniValue r(UniValue::VOBJ);
    CScript script;
    if (params[0].get_str().size() > 0){
        vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
        script = CScript(scriptData.begin(), scriptData.end());
    } else {
        // Empty scripts are valid
    }
    ScriptPubKeyToJSON(script, r, false); // 3. 把脚本公钥转换到 JSON 格式

    r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
    return r;
}

1. 帮助内容

参考比特币 RPC 命令「getbestblockhash」1. 帮助内容

2. RPC 类型检测

3. 把脚本公钥转换到 JSON 格式

函数 ScriptPubKeyToJSON(script, r, false) 实现在文件 rpcrawtransaction.cpp 中。

void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex)
{
    txnouttype type;
    vector<CTxDestination> addresses;
    int nRequired;

    out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey)));
    if (fIncludeHex)
        out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));

    if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
        out.push_back(Pair("type", GetTxnOutputType(type)));
        return;
    }

    out.push_back(Pair("reqSigs", nRequired));
    out.push_back(Pair("type", GetTxnOutputType(type)));

    UniValue a(UniValue::VARR);
    BOOST_FOREACH(const CTxDestination& addr, addresses)
        a.push_back(CBitcoinAddress(addr).ToString());
    out.push_back(Pair("addresses", a));
}

参考链接