Skip to content

Instantly share code, notes, and snippets.

@t4sk
Last active Sep 13, 2021
Embed
What would you like to do?
How to compute OP_HASH160 of public key

How to compute OP_HASH160 of public key

OP_HASH160 of public key = RIPEMD160(SHA256(ECDSA_publicKey))

1. Generate address

> getnewaddress
mpLwhoaRWxfgdQAz8JnnH7NUuhS157DM61

2. Get public key

> validateaddress mpLwhoaRWxfgdQAz8JnnH7NUuhS157DM61
{
  ...
  "pubkey": "033e8c5dcb8e35670de496b57c1fa4366800ce3be5fded9b8800759881bc8da2e5",
  ...
}

> export PUB_KEY=033e8c5dcb8e35670de496b57c1fa4366800ce3be5fded9b8800759881bc8da2e5

3. Compute OP_HASH160

> echo -n $PUB_KEY | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
60d47ac02b129c08f94232ea506d1826424fe7be

4. Verify OP_HASH160 (Optional)

> validateaddress mpLwhoaRWxfgdQAz8JnnH7NUuhS157DM61
{
  ...
  "scriptPubKey": "76a91460d47ac02b129c08f94232ea506d1826424fe7be88ac",
  ...
}

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

Check that public key hash in

OP_DUP OP_HASH160 60d47ac02b129c08f94232ea506d1826424fe7be OP_EQUALVERIFY OP_CHECKSIG

is equal to OP_HASH160 computed in step 3

References

How do you get the OP_HASH160 value from a bitcoin address?

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