Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 20
Script
shigeyuki azuchi edited this page Mar 18, 2022
·
4 revisions
bitcoinrb support Bitcoin Script and interpreter.
Bitcoin Script is represented by Bitcoin::Script class.
Script instances can be constructed in various ways.
# load from bynary.script=Bitcoin::Script.parse_from_payload('76a91446c2fbfbecc99a63148fa076de58cf29b0bcf0b088ac'.htb)# load from string.script=Bitcoin::Script.from_string('OP_DUP OP_HASH160 46c2fbfbecc99a63148fa076de58cf29b0bcf0b0 OP_EQUALVERIFY OP_CHECKSIG')# create using opcodeincludeBitcoin::Opcodesscript=Bitcoin::Script.new << OP_DUP << OP_HASH160 << '46c2fbfbecc99a63148fa076de58cf29b0bcf0b0' << OP_EQUALVERIFY << OP_CHECKSIGScript can be evaluated with the Bitcoin::Script#run method. it returns true if the Script evaluation is successful, false otherwise.
script=Bitcoin::Script.from_string('6 1 OP_ADD 7 OP_EQUAL')script.run=>trueThis is a simple script execution and the actual evaluation is executed by Bitcoin::ScriptInterpreter.
Bitcoin::Script has some utility method for handling frequently used scripts.
# create P2PKH script.script=Bitcoin::Script.to_p2pkh('46c2fbfbecc99a63148fa076de58cf29b0bcf0b0')# create P2WPKH script.script=Bitcoin::Script.to_p2wpkh('46c2fbfbecc99a63148fa076de58cf29b0bcf0b0')# create P2SH script with the current script as the redeem script.p2sh=script.to_p2sh# create multisig script.k1='021525ca2c0cbd42de7e4f5793c79887fbc8b136b5fe98b279581ef6959307f9e9'k2='032ad705d98318241852ba9394a90e85f6afc8f7b5f445675040318a9d9ea29e35'script=Bitcoin::Script.to_p2sh_multisig_script(1,[k1,k2])# create P2WSH scriptscript=Bitcoin::Script.to_p2wsh(<redeemscript>)There are any other useful methods.
Bitcoin::ScriptInterpreter is a script interpreter that is also used when validating transactions, allowing more detailed script evaluation with flags.
script_pubkey=Bitcoin::Script.to_p2pkh('46c2fbfbecc99a63148fa076de58cf29b0bcf0b0')script_sig=Bitcoin::Script.new << <sig> << <pubkey>
tx_checker=Bitcoin::TxChecker.new(tx: <Bitcoin::Txinstance>,input_index: 0,amount: <valuepersatoshi>)interpreter=Bitcoin::ScriptInterpreter.new(flags: Bitcoin::STANDARD_SCRIPT_VERIFY_FLAGS,checker: tx_checker)interpreter.verify_script(script_sig,script_pubkey)# if success return true, otherwise false# if using segwit transaction, specify witness data.interpreter.verify_script(script_sig,script_pubkey,witness)