Exercise 2: Hard
In this exercise, you will be working with a Bitcoin P2MS (Pay-to-Multisig) ScriptPubKey.
Your task is to complete the decompileP2MSScriptPubKey
function, which decompiles a hex-encoded ScriptPubKey and extracts the number of required signatures and the associated public keys.
Follow the hints provided if you get stuck, and test your code by entering a ScriptPubKey hex string in the input field below.
Once you've completed the function, you can compare your solution with the provided answer.
- Start by iterating over each byte in the `scriptPubKeyHex` string. You’ll need to determine whether the byte represents an opcode or data.
- For opcodes between 0x01 and 0x4b, you should treat the byte as the length of the data that follows.
- Extract the data by slicing the scriptPubKey buffer and increment the index `i` appropriately.
- Check if the last byte in the script is the `OP_CHECKMULTISIG` opcode (0xae). If it's not, throw an error.
- Subtract 0x50 from the first byte to get the number of required signatures.
- The remaining data elements between the first and last opcodes are the public keys. Convert them to hex strings.