Author

Topic: How do I know the scriptPubKey of an address? (Read 211 times)

full member
Activity: 214
Merit: 278
October 24, 2019, 01:04:32 PM
#6
For example, 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC.

Step 1: Open the address in Blockchair.com, e.g. https://blockchair.com/bitcoin/address/3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC.

Step 2: Click to see more under General info.

Step 3: Click on Hex button against label Script.

The value under label Script, i.e. a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087, is scriptPubKey of 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC.
legendary
Activity: 3472
Merit: 10611
you really shouldn't mess with these things if you don't know what you are doing. just let the wallet do it in the background itself.

(1) easy but not safe or recommended way:
search the address on blockchain.com: https://www.blockchain.com/en/btc/address/3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC
there is a field called "Hash 160" copy that, add a914 to its beginning and 87 to its end:
Code:
a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087

(2) "the right way" to "convert" a given address to its equivalent scriptPubkey is to first check if the given address is valid using either one of two encodings used by bitcoin addresses (base58 and bech32) then based on decoded data you set the appropriate the script:
1. check starting characters of the given string -> if 1 or 3 then use base58, if bc1 then use bech32 (different for testnet)
Code:
3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC
2. it is 3 -> decode with base58, verify and remove the 4 byte checksum
Code:
decoded: 05f815b036d9bbbce5e9f2a00abd1bf3dc91e95510cd003107
data: 05f815b036d9bbbce5e9f2a00abd1bf3dc91e95510
expected checksum: cd003107
double sha256 of data: cd00310784cb5c11ef41396565fa524c0fcefc05eaefa52740a50aaafaaaf9cf
actual checksum: cd003107
expected == actual
return data
3. check version byte, figure out type of script and remove it
Code:
0x05 => P2SH script
4. check validity of the length of remaining data, that's your hash
Code:
f815b036d9bbbce5e9f2a00abd1bf3dc91e95510 => 20 bytes => correct
5. write the appropriate script:
for P2SH this is
Code:
OP_HASH160 OP_EQUAL
a9 14-f815b036d9bbbce5e9f2a00abd1bf3dc91e95510 87

ps. this is for P2SH scripts, the PublicScript for other types is different from this.
sr. member
Activity: 532
Merit: 302
How do I extract scriptPubKey for 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC from it?

Not sure what you're asking. You can find C++ code on github.

Or use an online tool to decode the raw tx, e.g.: https://live.blockcypher.com/btc/decodetx/
sr. member
Activity: 860
Merit: 423
Find a transaction that sends to that address and decode it, for example https://btc.com/e06137426f7876fb12f49d78c848cb3f43c786271892f7b1df6cb788d7af5008
Here is the rawtx...

Code:
02000000000102716799b96076f2e7cbd790485c7b78257f6b1651475252af0192a462880094cc0000000017160014907b1b235b7557fb56d687a0fc02bfa5d93035c8feffffff1ab853e1aba73188029263f85fe22a1000056cfed14cac1ba508299ea608e59700000000171600141c997a293ebf47363db69e032b9b57ae8a76e5a8feffffff03718801000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087d33e0300000000001976a9144413e3095fd0cadd69a41e8a9b630ffbab2aa02688acfd0d05000000000017a9145836bc2dd460251910f021128cc91303e4d16e84870247304402201f3b7adb3cdec76a0b033ac5f45dbf32e765efc1bbc09dccfa240effd990a1b90220649ef86721e587749d76f65720e818b021d159851de7b476d23ffbb3716cf8df0121021699d05ab549ec3cf40d2c96f8e0e2c2a0b0b35b6a67fa2d080dec805b9ccee102473044022037e6fa96ed8e60ca3bffdb930b66971e698d2e00f43f1c62d5687286552c0377022056e444c3d40c87109b04f73fed17caa5502e7765bf28be0358025269d41ec2e201210205902c0cbabb500c1d13b8d7ae1cc1a6ba3d93fb806d5001b255a154cc6ae6565b060900

How do I extract scriptPubKey for 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC from it?
sr. member
Activity: 532
Merit: 302
Find a transaction that sends to that address and decode it, for example https://btc.com/e06137426f7876fb12f49d78c848cb3f43c786271892f7b1df6cb788d7af5008
sr. member
Activity: 860
Merit: 423
For example, 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC.
Jump to: