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": [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
],
"p2sh": "3DjjKyU38gSfuVxajV43MUy4vHkg1JVL7T"
}
So the my process decode is
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"; }
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
- Find the key to this hex string
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f
I try with this code
Bytes bytes = hexBytes(key.c_str());
//Ripemd160
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
Base58Check::pubkeyHashToBase58Check
I must pass the bytes, right? – vincenzopalazzo Sep 22 '19 at 17:48