Start your career in bitcoin open source — ₿OSS

APPLY TODAY

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:

FieldValue
Version01000000
Input Count01
Input TXID

0000000000000000000000000000000000000000000000000000000000000000

Input VOUTffffffff
ScriptSig SizeN/A
ScriptSigN/A
Sequenceffffffff
Output Count01
Output Amount00f2052a01000000
Output ScriptPubKey Size23
Output ScriptPubKeyN/A
Locktime00000000

The hexadecimal representation of this transaction is:

text
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:

text
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:

  1. Implement the construct_p2pk_scriptpubkey function.
  2. The function should take a public key as input and return the complete P2PK ScriptPubKey.
  3. Remember to include the public key length and OP_CHECKSIG in your implementation.
  4. The provided unit test will check if your implementation is correct.

Choose your preferred language below to start coding:

P2PK ScriptPubKey Decoder Exercise
export function constructP2PKScriptPubKey(publicKey) {
  // TODO: Implement this function
  // Hint: Remember to include the public key length and OP_CHECKSIG
}

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.

Click to reveal next hint (0/3)
  • 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'

Solution code

Suggest Edits