Skip to content

Instantly share code, notes, and snippets.

@t4sk
Last active March 29, 2022 17:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t4sk/e251e6f298b533039f0f276cf6f5fb28 to your computer and use it in GitHub Desktop.
Save t4sk/e251e6f298b533039f0f276cf6f5fb28 to your computer and use it in GitHub Desktop.
How to create scriptPubKey for P2PKH

How to create scriptPubKey for P2PKH

1. Get public key hash

Get the public key hash by computing OP_HASH160 of public key

In this example:

  • public key: 033e8c5dcb8e35670de496b57c1fa4366800ce3be5fded9b8800759881bc8da2e5
  • public key hash: 60d47ac02b129c08f94232ea506d1826424fe7be
> export PUB_KEY_HASH=60d47ac02b129c08f94232ea506d1826424fe7be

2. Create script

Script

OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

OP_DUP OP_HASH160 bytes to push pubKeyHash OP_EQUALVERIFY OP_CHECKSIG
76 a9 14 60d47a... 88 ac
Output
> echo 76a914${PUB_KEY_HASH}88ac
76a91460d47ac02b129c08f94232ea506d1826424fe7be88ac

3. Verify (Optional)

In bitcoin-qt debug console

> decodescript 76a91460d47ac02b129c08f94232ea506d1826424fe7be88ac
{
  "asm": "OP_DUP OP_HASH160 60d47ac02b129c08f94232ea506d1826424fe7be OP_EQUALVERIFY OP_CHECKSIG",
  ...
}

References

Script - Bitcoin Wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment