<p>If I want to recreate the command <code>decodescript</code> of bitcoin core? how is the process for decode the script P2PK?</p>

<p>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.</p>

<p>In my example, I get the scriptPubKey of the codebase block, so the data are</p>

<p>The hex of the script: <code>4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac</code></p>

<p>So the decoded script is</p>

<pre><code>04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac OP_CHECKSIG
</code></pre>

<p>The result of the command decodescript of bitcoin core</p>

<pre><code>{
  "asm": "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG",
  "reqSigs": 1,
  "type": "pubkey",
  "addresses": [
    "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
  ],
  "p2sh": "3DjjKyU38gSfuVxajV43MUy4vHkg1JVL7T"
}
</code></pre>

<p>So the my process decode is</p>

<ol>
<li><p>I get the type of transaction with this code</p>

<pre><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-&gt;second;
//Not are all type script
if (optCode == "OP_HASH160") {
    cout &lt;&lt; "Finded the P2SHA";
} else if (optCode == "OP_DUP") {
    cout &lt;&lt; "Finded the P2PKH";
} else {
    cout &lt;&lt; "Finded the old style script\n";
}
</code></pre></li>
</ol>

<hr>

<ol start="2">
<li><p>The script is a script old style so I decode the hex script with this code</p>

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

<p>The result is <code>04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG</code></p>

<hr>

<ol start="3">
<li>Find the key to this hex string <code>04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f</code></li>
</ol>

<p>I try with this code</p>

<pre><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 &lt;&lt; "The script public key is: " &lt;&lt; address  &lt;&lt; " " &lt;&lt; opcode &lt;&lt; endl;
</code></pre>

<p>But the result is wrong <code>1NXtFSm6h4QAVBQ7ezaDc3ZvSKFj9GTfoV</code></p>

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

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

CC BY-SA 4.0
2019-09-22T17:09:35.517Z
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
CC BY-SA 4.0
2019-09-22T17:48:25.240Z
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
CC BY-SA 4.0
2019-09-22T18:00:36.473Z
Yes, 1... addresses should refer purely to P2PKH addresses to avoid ambiguity. P2PK outputs simply don't have a corresponding address.
Pieter Wuille
CC BY-SA 4.0
2019-09-25T16:16:01.113Z
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
CC BY-SA 4.0
2019-09-25T16:25:44.730Z
Addresses are shorthand notations for specific scripts. But not every script has a corresponding address. P2PK scripts don't have one. P2PKH ones do.
Pieter Wuille
CC BY-SA 4.0
2019-09-25T16:35:29.340Z
So, if in my example `1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa` not is the Key but the result of the process command decode, so sha256 -> Ripemd160 -> Base58Check, What data is entered in the script to unlock the transaction?
vincenzopalazzo
CC BY-SA 4.0
2019-09-25T16:42:13.170Z
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
<bitcoin-core><hash><bitcoin-core-development>
How get the public key to hex script? like command decodescript of bitcoin core
vincenzopalazzo
# How get the public key to hex script? like command decodescript of bitcoin core

(Asked by: username_0 on 2019-09-22)

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

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";
   }

   ```

---

2. 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`

---

3. 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

**Tags:** bitcoin-core, hash, bitcoin-core-development

> **Comments:**
> **username_1**: 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.  
> **username_0**: Thanks, @username_1, 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?  
> **username_1**: Yes, 1... addresses should refer purely to P2PKH addresses to avoid ambiguity. P2PK outputs simply don't have a corresponding address.  
> **username_0**: Sorry @username_1 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?_  
> **username_1**: Addresses are shorthand notations for specific scripts. But not every script has a corresponding address. P2PK scripts don't have one. P2PKH ones do.  
> **username_0**: So, if in my example `1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa` not is the Key but the result of the process command decode, so sha256 -> Ripemd160 -> Base58Check, What data is entered in the script to unlock the transaction?  
> **username_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.  

---

## 0 answers

