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:
Field | Value |
---|---|
Version | 01000000 |
Input Count | 01 |
Input TXID | 97211e92d9590b4111a60f24f2c97eba2884bb813515a3167c634a9dcd113338 |
Input VOUT | 01000000 |
ScriptSig Size | N/A |
ScriptSig | N/A |
Sequence | ffffffff |
Output Count | 02 |
Output1 Amount | 8023430000000000 |
Output1 ScriptPubKey Size | 19 |
Output1 ScriptPubKey | N/A |
Output2 Amount | 00e1f50500000000 |
Output2 ScriptPubKey Size | 19 |
Output2 ScriptPubKey | N/A |
Locktime | 00000000 |
The hexadecimal representation of this transaction is:
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:
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:
037d91c138583e25b0dbacb2643e5f14a96df6af798b3dcc08d7f7f3b41db614fb
02f82f8cd17a73ea73538ba86d129254c9ef640babc51b316e089595bb884dbef5
Instructions:
- Implement the
construct_p2pkh_scriptpubkey
function. - The function should take a public key as input and return the complete P2PKH ScriptPubKey.
- Remember to hash the public key and include the necessary opcodes.
- The provided unit test will check if your implementation is correct for both public keys.
Choose your preferred language below to start coding:
Ready to Code?
Choose your preferred language to begin
For this exercise, we've provided the crypto-js
(JavaScript) and hashlib
(Python) libraries to help with the hashing operations. You should use the relevant library to implement the P2PKH ScriptPubKey function.
You can look up the hex characters for each op_code in the Bitcoin Script wiki page.
- 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: