If I want to recreate the command decodescript of bitcoin core? how is the process for decode the script P2PK?

So I'm writing for personal exercise the Bitcoin script decompiler, form moment I have a version alpha of the process decodifier of the script P2PK script.

In my example, I get the scriptPubKey of the codebase block, so the data are

The hex of the script: 4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac

So the decoded script is

04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac OP_CHECKSIG

The result of the command decodescript of bitcoin core

  "asm": "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG",
  "reqSigs": 1,
  "type": "pubkey",
  "addresses": [
  "p2sh": "3DjjKyU38gSfuVxajV43MUy4vHkg1JVL7T"

So the my process decode is

  1. I get the type of transaction with this code

    string hex(argv[1]);
    BitcoinOPCode bitcoinOpCode;
    string opcode = hex.substr(0, 2);
    int32_t optValue = std::stoul(opcode, nullptr, 16);
    auto optMap = bitcoinOpCode.opCodeList.find(optValue);
    string optCode = optMap->second;
    //Not are all type script
    if (optCode == "OP_HASH160") {
        cout << "Finded the P2SHA";
    } else if (optCode == "OP_DUP") {
        cout << "Finded the P2PKH";
    } else {
        cout << "Finded the old style script\n";

  1. The script is a script old style so I decode the hex script with this code

    string key = hex.substr(2, optValue * 2);
    opcode = hex.substr(2 + optValue * 2, hex.length());
    cout << "Hex key is: " << key << " op code: " << opcode << endl;
    optValue = std::stoul(opcode, nullptr, 16);
    optMap = bitcoinOpCode.opCodeList.find(optValue);
    opcode = optMap->second;

The result is 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG

  1. Find the key to this hex string 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f

I try with this code

Bytes bytes = hexBytes(key.c_str());

 uint8_t result[Ripemd160::HASH_LEN];
 Ripemd160::getHash(bytes.data(), key.length(), result);
 char address[36];
 Base58Check::pubkeyHashToBase58Check(result, 0x00, address);
 cout << "The script public key is: " << address  << " " << opcode << endl;

But the result is wrong 1NXtFSm6h4QAVBQ7ezaDc3ZvSKFj9GTfoV

I looked the code bitcoin, so I think the bitcoin core decode the script with this code GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0].begin(), solutions_data[0].end())));

but in the my logic exist an bug, can you help me to find it? thanks

  • Two comments: 1) the fact that Bitcoin Core shows a 1... address for P2PK output is considered a historical mistake. There is no need to perpetuate it. 2) You should convert everything from hex to bytes, and then work on the byte data. If you feed the hex encoded form to hash functions you'll get the wrong result. – Pieter Wuille Sep 22 at 17:09
  • Thanks, @PieterWuille, on your 1 comment, the historical mistake of the P2PK is because inside the script is insert the pure public key? on the 2 comments, to the function Base58Check::pubkeyHashToBase58Check I must pass the bytes, right? – vincenzopalazzo Sep 22 at 17:48
  • Yes, 1... addresses should refer purely to P2PKH addresses to avoid ambiguity. P2PK outputs simply don't have a corresponding address. – Pieter Wuille Sep 22 at 18:00
  • Sorry @PieterWuille if I reopen this question, can you help me to understand the problem with the P2PK script? what does it mean P2PK outputs simply don't have a corresponding address? – vincenzopalazzo Sep 25 at 16:16
  • 1
    That's a completely unrelated question. I think you should go through some reference material (the bitcoin.org developer documentation, maybe a book on Bitcoin, ...) and if you have specific questions, ask them here. – Pieter Wuille Sep 25 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.