Exercise 2: Medium
In this exercise, you'll construct a P2PK (Pay-to-Public-Key) ScriptPubKey for a given public key within a partially constructed transaction.
Here's a breakdown of the partially constructed transaction:
Field | Value |
---|---|
Version | 01000000 |
Input Count | 01 |
Input TXID | 0000000000000000000000000000000000000000000000000000000000000000 |
Input VOUT | ffffffff |
ScriptSig Size | N/A |
ScriptSig | N/A |
Sequence | ffffffff |
Output Count | 01 |
Output Amount | 00f2052a01000000 |
Output ScriptPubKey Size | 23 |
Output ScriptPubKey | N/A |
Locktime | 00000000 |
The hexadecimal representation of this transaction is:
1
201000000
301
40000000000000000000000000000000000000000000000000000000000000000
5ffffffff
6[scriptSig]
7ffffffff
801
900f2052a01000000
1023
11[scriptPubKey]
1200000000
13
For easier copying, here's the same transaction in a single line:
101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff-[scriptSig]-ffffffff0100f2052a0100000023-[scriptPubKey]-00000000
Exercise
Now that you understand the concept of P2PK ScriptPubKey, let's implement it.
Your task is to complete the construct_p2pk_scriptpubkey
function which locks the funds to the following public key: 02802e2df5f9fb88065630e5fa3b6f7b7a89deef2b6a3cb5203cd0b897f66acae4
Instructions:
- Implement the
construct_p2pk_scriptpubkey
function. - The function should take a public key as input and return the complete P2PK ScriptPubKey.
- Remember to include the public key length and OP_CHECKSIG in your implementation.
- The provided unit test will check if your implementation is correct.
Choose your preferred language below to start coding:
Ready to Code?
Choose your preferred language to begin
You can look up the hex characters for each op_code in the Bitcoin Script wiki page.
- The public key length should be calculated dynamically based on the input.
- Remember that the public key is in hexadecimal, so each byte is represented by two characters.
- The OP_CHECKSIG opcode in hexadecimal is 'ac'