Start your career in bitcoin open source — ₿OSS

APPLY TODAY

Exercise 1: Medium

In this exercise, you'll construct P2PKH (Pay-to-Public-Key-Hash) ScriptPubKeys for given public keys within a partially constructed transaction.

Here's a breakdown of the partially constructed transaction:

FieldValue
Version01000000
Input Count01
Input TXID

97211e92d9590b4111a60f24f2c97eba2884bb813515a3167c634a9dcd113338

Input VOUT01000000
ScriptSig SizeN/A
ScriptSigN/A
Sequenceffffffff
Output Count02
Output1 Amount8023430000000000
Output1 ScriptPubKey Size19
Output1 ScriptPubKeyN/A
Output2 Amount00e1f50500000000
Output2 ScriptPubKey Size19
Output2 ScriptPubKeyN/A
Locktime00000000

The hexadecimal representation of this transaction is:

text
1
201000000
301
497211e92d9590b4111a60f24f2c97eba2884bb813515a3167c634a9dcd113338
501000000
6[scriptSig]
7ffffffff
802
98023430000000000
1019
11[scriptPubKey1]
1200e1f50500000000
1319
14[scriptPubKey2]
1500000000
16

For easier copying, here's the same transaction in a single line:

text
1010000000197211e92d9590b4111a60f24f2c97eba2884bb813515a3167c634a9dcd11333801000000-[scriptSig]-ffffffff028023430000000000-[scriptPubKey1]-00e1f50500000000-[scriptPubKey2]-00000000

Exercise

Now that you understand the concept of P2PKH ScriptPubKey, let's implement it.

Your task is to complete the construct_p2pkh_scriptpubkey function which locks the funds to the following public keys:

  1. 037d91c138583e25b0dbacb2643e5f14a96df6af798b3dcc08d7f7f3b41db614fb
  2. 02f82f8cd17a73ea73538ba86d129254c9ef640babc51b316e089595bb884dbef5

Instructions:

  1. Implement the construct_p2pkh_scriptpubkey function.
  2. The function should take a public key as input and return the complete P2PKH ScriptPubKey.
  3. Remember to hash the public key and include the necessary opcodes.
  4. The provided unit test will check if your implementation is correct for both public keys.

Choose your preferred language below to start coding:

P2PK ScriptPubKey Decoder Exercise
import CryptoJS from 'crypto-js';

export function constructP2PKHScriptPubKey(publicKey) {
  // TODO: Implement this function
  // Hint: Use CryptoJS.SHA256 and CryptoJS.RIPEMD160 for hashing
}

Ready to Code?

Choose your preferred language to begin

For this exercise, we've provided the crypto-js library to help with the hashing operations. You should use this library to implement the P2PKH ScriptPubKey function.

You can look up the hex characters for each op_code in the Bitcoin Script wiki page.

Click to reveal next hint (0/5)
  • Start by converting the public key from hex to bytes.
  • Use CryptoJS.SHA256 to hash the public key, then use CryptoJS.RIPEMD160 on the result.
  • The P2PKH script starts with OP_DUP (0x76) and OP_HASH160 (0xa9).
  • Push the 20-byte public key hash onto the stack (0x14 followed by the hash).
  • End the script with OP_EQUALVERIFY (0x88) and OP_CHECKSIG (0xac).

Inspiration

After completing the exercise, you can check out this testnet transaction to see real P2PKH ScriptPubKeys in action:

Solution code

Suggest Edits