Start your career in bitcoin open source — ₿OSS

APPLY TODAY

Exercise 1: Medium

In this exercise, you'll construct a P2MS (Pay-to-Multisig) ScriptPubKey within a P2SH (Pay-to-Script-Hash) 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 Size17
Output1 ScriptPubKeyN/A
Output2 Amount00e1f50500000000
Output2 ScriptPubKey Size17
Output2 ScriptPubKey

a91482b736e28a94f42164db1fe448086f74725ae46487

Locktime00000000

The hexadecimal representation of this transaction is:

text
1010000000197211e92d9590b4111a60f24f2c97eba2884bb813515a3167c634a9dcd11333801000000-[signature]-ffffffff028023430000000000-[scriptPubKey1]-00e1f5050000000017a91482b736e28a94f42164db1fe448086f74725ae4648700000000

Exercise

Your task is to complete the constructP2MSP2SHScriptPubKey function which creates a P2SH ScriptPubKey for a 2-of-2 multisig redeem script using the following public keys:

  1. 02632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d6
  2. 03a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d4
P2PK ScriptPubKey Decoder Exercise
import CryptoJS from 'crypto-js';

export function constructP2MSP2SHScriptPubKey(pubkey1, pubkey2) {
  // TODO: Implement this function
  // Hint: Create a 2-of-2 multisig redeem script, then wrap it in P2SH
  // Use CryptoJS.SHA256 and CryptoJS.RIPEMD160 for hashing
}

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/5)
  • Start by creating the multisig redeem script: OP_2 <pubkey1> <pubkey2> OP_2 OP_CHECKMULTISIG
  • Convert the redeem script to bytes
  • Hash the redeem script using SHA256 and then RIPEMD160
  • Create the P2SH script: OP_HASH160 <20-byte-hash> OP_EQUAL
  • Return the final script as a hexadecimal string

Solution code

Inspiration

After completing the exercise, you can check out these testnet transactions to see real P2MS-in-P2SH ScriptPubKeys in action:

These transactions demonstrate how P2MS-in-P2SH ScriptPubKeys are used in actual Bitcoin transactions on the testnet.

Suggest Edits