Exercise 2: Hard

P2PKH ScriptPubKey to ASM Converter

In this exercise, you'll implement a function to convert a P2PK (Pay-to-Public-Key) ScriptPubKey from its hexadecimal representation to ASM format.

  1. Implement the decodeP2PKHScriptPubKey function in the code editor below.
  2. The function should convert the input hex string to a Buffer.
  3. Verify that the script starts with OP_DUP (0x76) and OP_HASH160 (0xA9).
  4. Extract the public key hash (20 bytes).
  5. Verify that the script ends with OP_EQUALVERIFY (0x88) and OP_CHECKSIG (0xAC).
  6. Construct and return the ASM string: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Click to reveal next hint (0/3)
  • Remember, P2PKH scripts always start with OP_DUP (0x76) and OP_HASH160 (0xA9).
  • The public key hash is always 20 bytes long.
  • Don't forget to check for OP_EQUALVERIFY (0x88) and OP_CHECKSIG (0xAC) at the end!

Solution code

Suggest Edits