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.
- Implement the decodeP2PKHScriptPubKey function in the code editor below.
- The function should convert the input hex string to a Buffer.
- Verify that the script starts with OP_DUP (0x76) and OP_HASH160 (0xA9).
- Extract the public key hash (20 bytes).
- Verify that the script ends with OP_EQUALVERIFY (0x88) and OP_CHECKSIG (0xAC).
- Construct and return the ASM string:
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Ready to Code?
Choose your preferred language to begin
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!