Skip to content
shigeyuki azuchi edited this page Sep 6, 2022 · 2 revisions

tapyrusrb support Tapyrus Script and interpreter.

Tapyrus::Script

Tapyrus Script is represented by Tapyrus::Script class.

Create Script instance

Script instances can be constructed in various ways.

# load from bynary.
script = Tapyrus::Script.parse_from_payload('76a91446c2fbfbecc99a63148fa076de58cf29b0bcf0b088ac'.htb)

# load from string.
script = Tapyrus::Script.from_string('OP_DUP OP_HASH160 46c2fbfbecc99a63148fa076de58cf29b0bcf0b0 OP_EQUALVERIFY OP_CHECKSIG')

# create using opcode
include Tapyrus::Opcodes
script = Tapyrus::Script.new << OP_DUP << OP_HASH160 << '46c2fbfbecc99a63148fa076de58cf29b0bcf0b0' << OP_EQUALVERIFY << OP_CHECKSIG

Eval script

Script can be evaluated with the Bitcoin::Script#run method. it returns true if the Script evaluation is successful, false otherwise.

script = Tapyrus::Script.from_string('6 1 OP_ADD 7 OP_EQUAL')
script.run
=> true

This is a simple script execution and the actual evaluation is executed by Bitcoin::ScriptInterpreter.

Utility

Tapyrus::Script has some utility method for handling frequently used scripts.

# create P2PKH script.
script = Tapyrus::Script.to_p2pkh('46c2fbfbecc99a63148fa076de58cf29b0bcf0b0')

# create P2SH script with the current script as the redeem script.
p2sh = script.to_ps2sh

# create multisig script.
k1 = '021525ca2c0cbd42de7e4f5793c79887fbc8b136b5fe98b279581ef6959307f9e9'
k2 = '032ad705d98318241852ba9394a90e85f6afc8f7b5f445675040318a9d9ea29e35'
script = Tapyrus::Script.to_p2sh_multisig_script(1, [k1, k2])

There are any other useful methods.

Tapyrus::ScriptInterpreter

Tapyrus::ScriptInterpreter is a script interpreter that is also used when validating transactions, allowing more detailed script evaluation with flags.

script_pubkey = Tapyrus::Script.to_p2pkh('46c2fbfbecc99a63148fa076de58cf29b0bcf0b0')
script_sig = Tapyrus::Script.new << <sig> << <pubkey>

tx_checker = Tapyrus::TxChecker.new(tx: <Tapyrus::Tx instance>, input_index: 0)
interpreter = Tapyrus::ScriptInterpreter.new(flags: Tapyrus::STANDARD_SCRIPT_VERIFY_FLAGS, checker: tx_checker)
interpreter.verify_script(script_sig, script_pubkey)
# if success return true, otherwise false

Script Debugger

The tapyrus-script-debugger command is available in v0.3.2 and later. This command allows you to check stack transitions while stepping through scripts. This command is still an experimental feature. If you have a case where it does not work well, please report it in an Issue.

Usage

$ tapyrus-script-debugger
Enter scriptPubkey: <Enter your scriptPubkey with hex format>
Enter scriptSig: <Enter your scriptSig with hex format>
Enter tx: <Enter your tx, if you have a signature verification opcode, like `OP_CHECKSIG`>
Enter index of the input: <Enter your input index>
The Script is ready to be executed; you can step execution it by putting the Enter key.
> 

After entering all of the above, you can step through the scripts in sequence with the Enter key and see the data on the stack as you do so.

Example(P2PKH):

$ tapyrus-script-debugger
Enter scriptPubkey: 76a914d7a7cd2ba99f7319d83fa5cbd249646d8701791e88ac
OP_DUP OP_HASH160 d7a7cd2ba99f7319d83fa5cbd249646d8701791e OP_EQUALVERIFY OP_CHECKSIG
Enter scriptSig: 473044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b012102bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597
3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597
Enter tx: 010000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c30000000000001976a9147dd164c8fde4679f938ea269c96d135524114b2e88ac00000000
Enter index of the input: 0
The Script is ready to be executed; you can step execution it by putting the Enter key.
> 
PUSH 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
PUSH 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
APPLY OP_DUP
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
APPLY OP_HASH160
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| d7a7cd2ba99f7319d83fa5cbd249646d8701791e                                                                                                       |
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
PUSH d7a7cd2ba99f7319d83fa5cbd249646d8701791e
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| d7a7cd2ba99f7319d83fa5cbd249646d8701791e                                                                                                       |
| d7a7cd2ba99f7319d83fa5cbd249646d8701791e                                                                                                       |
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
APPLY OP_EQUALVERIFY
+------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                 Current Stack                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| 02bc64d1b3c5a62ddc2b0f80900db5c2ff707b410126d8e230b1dc19fb6d8fd597                                                                             |
| 3044022011aca871edd55fca561e78803b30877ea21088dc5aa558e3ba19216a7e0b4e260220711f5a1005d9c1899adce2558843f814f10d496e00ead3564fc03ea4f41fb37b01 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
> 
APPLY OP_CHECKSIG
+---------------+
| Current Stack |
+---------------+
| 01            |
+---------------+
> 
Execution finished.
Clone this wiki locally