<p>this is the partial answer, on the underlying logic. I cannot help with java script. The critical part would be the base58decoding, but it seems there are solutions already: <a href="https://www.npmjs.com/package/base58check" rel="nofollow noreferrer">https://www.npmjs.com/package/base58check</a></p>

<p><strong>Underlying logic:</strong></p>

<p>the scriptpubkey contains the RipeMD160 Hash, surrounded by some codes:
a9 14  87. This is a P2SH address. </p>

<p>the meaning of the codes is:</p>

<pre><code>a9 -&gt; do a RipeMD160 on the top stack item
14 -&gt; push hex 14 (decimal 20) bytes on stack
87 -&gt; check if the top two stack elements are equal
</code></pre>

<p>From your address, you need four steps, to come to this goal:</p>

<pre><code>1.) base58decode the address
2.) remove last 4 bytes (this is a checksum)
3.) remove the first two bytes (the network prefix
4.) surround the codes to it
</code></pre>

<p>effectivly the procedure is the revers of the standard "base58check encoding": which answers, how to come from a pubkey in hexformat to
 an address:</p>

<pre><code>0: have a Private ECDSA Key
1: derive the Public ECDSA Key
2: do a SHA-256 of 1
3: RIPEMD-160 Hash of 2
4: add address prefix 0x00 for P2PKH, 0x05 for P2SH
5. sha256
6. another sha256
7. take first four Bytes from step 6 as checksum
8. append checksum from step 7 to the result from step4
9. encode Base58
</code></pre>

<p>--> your bitcoin address !</p>

<p>help while testing: <a href="http://gobittest.appspot.com/Address" rel="nofollow noreferrer">http://gobittest.appspot.com/Address</a>
(hint: never put your production private keys into a webpage - you might/will loose the funds on it!)</p>

CC BY-SA 3.0
2018-01-08T03:10:32.113Z
scriptPubKey is static or dynamic ?
Слава Гесто
CC BY-SA 3.0
2018-01-08T07:30:33.390Z
I am not sure what you mean by static or dynamic. The pubkey is derived from the private key via the ECDSA logic, then hashed, concatenated with a prefix and chcksummed, re-assembled and bas58encoded. There are other pubkey-scripts, which can be assembled dynamically, but not keys...
pebwindkraft
CC BY-SA 3.0
2018-01-07T16:26:22.467Z
2018-01-07T16:26:22.467Z
pebwindkraft
<p>My address is 389CLh5Ect2i7U5116gqjBSLykYHRmdcgZ. In request from site i see this
scriptPubKey, how i can generate this scriptPubKey from my Address
{scriptPubKey: "a91446c443cc449d9e9a54a9e07a30dfd2ab1960a18f87", value: 60000} ?</p>

<p>If you have examples in javascript it will be good.</p>

<public-key><address-generation>
How to generate scriptPubKey?
Слава Гесто
# How to generate scriptPubKey?

(Asked by: username_0 on 2018-01-07)

My address is 389CLh5Ect2i7U5116gqjBSLykYHRmdcgZ. In request from site i see this
scriptPubKey, how i can generate this scriptPubKey from my Address
{scriptPubKey: "a91446c443cc449d9e9a54a9e07a30dfd2ab1960a18f87", value: 60000} ?

If you have examples in javascript it will be good.

**Tags:** public-key, address-generation


---

## 1 answers

### Answer by username_1 on 2018-01-07. Score: 2 | Accepted answer

this is the partial answer, on the underlying logic. I cannot help with java script. The critical part would be the base58decoding, but it seems there are solutions already: https://www.npmjs.com/package/base58check
Underlying logic:
the scriptpubkey contains the RipeMD160 Hash, surrounded by some codes:
a9 14  87. This is a P2SH address. 
the meaning of the codes is:
a9 -> do a RipeMD160 on the top stack item
14 -> push hex 14 (decimal 20) bytes on stack
87 -> check if the top two stack elements are equal

From your address, you need four steps, to come to this goal:
1.) base58decode the address
2.) remove last 4 bytes (this is a checksum)
3.) remove the first two bytes (the network prefix
4.) surround the codes to it

effectivly the procedure is the revers of the standard "base58check encoding": which answers, how to come from a pubkey in hexformat to
 an address:
0: have a Private ECDSA Key
1: derive the Public ECDSA Key
2: do a SHA-256 of 1
3: RIPEMD-160 Hash of 2
4: add address prefix 0x00 for P2PKH, 0x05 for P2SH
5. sha256
6. another sha256
7. take first four Bytes from step 6 as checksum
8. append checksum from step 7 to the result from step4
9. encode Base58

--> your bitcoin address !
help while testing: http://gobittest.appspot.com/Address
(hint: never put your production private keys into a webpage - you might/will loose the funds on it!)

> **Comments:**
> **username_0**: scriptPubKey is static or dynamic ?  
> **username_1**: I am not sure what you mean by static or dynamic. The pubkey is derived from the private key via the ECDSA logic, then hashed, concatenated with a prefix and chcksummed, re-assembled and bas58encoded. There are other pubkey-scripts, which can be assembled dynamically, but not keys...  

---

