Class: Cryptos::Script
- Inherits:
-
Object
- Object
- Cryptos::Script
- Includes:
- Utils::Hashes, Utils::Hexas
- Defined in:
- lib/cryptos/script.rb
Constant Summary collapse
- OPCODES =
{ 'OP_0' => 0x00, 'OP_PUSHDATA1' => 0x4c, 'OP_1' => 0x51, 'OP_2' => 0x52, 'OP_IF' => 0x63, 'OP_ELSE' => 0x67, 'OP_ENDIF' => 0x68, 'OP_DROP' => 0x75, 'OP_DUP' => 0x76, 'OP_EQUAL' => 0x87, 'OP_EQUALVERIFY' => 0x88, 'OP_RIPEMD160' => 0xA6, 'OP_HASH160' => 0xA9, 'OP_CHECKSIG' => 0xAC, 'OP_CHECKMULTISIG' => 0xAE, 'OP_CHECKLOCKTIMEVERIFY' => 0xB1, 'OP_CHECKSEQUENCEVERIFY' => 0xB2, }.freeze
Instance Attribute Summary collapse
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Class Method Summary collapse
- .bare(script) ⇒ Object
-
.multisig(address1, address2) ⇒ Object
multisign redeem script.
- .p2pkh(address_or_hex) ⇒ Object
- .p2sh(script) ⇒ Object
-
.sig_multisig(der1, der2, redeem_script) ⇒ Object
scriptSig for pay-to-multisig-hash outputs.
-
.sig_pubkey(der, public_key) ⇒ Object
ScriptSig for pay-to-pubkey-hash outputs.
-
.sig_swap(der, public_key, secret, redeem_script) ⇒ Object
scriptSig for atomic swaps.
- .swap(secret_hash, to_address, locktime, from_address) ⇒ Object
Instance Method Summary collapse
-
#initialize(script) ⇒ Script
constructor
A new instance of Script.
- #size ⇒ Object
- #to_asm ⇒ Object
- #to_hash160 ⇒ Object
- #to_hex ⇒ Object (also: #serialize)
- #to_s ⇒ Object
Methods included from Utils::Hexas
#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex
Methods included from Utils::Bytes
#bignum_to_bytes, #bytes_to_bignum
Methods included from Utils::Hashes
#hash160, #hash256, #ripemd160, #sha256
Constructor Details
#initialize(script) ⇒ Script
Returns a new instance of Script.
81 82 83 |
# File 'lib/cryptos/script.rb', line 81 def initialize(script) @script = script.strip end |
Instance Attribute Details
#script ⇒ Object (readonly)
Returns the value of attribute script.
25 26 27 |
# File 'lib/cryptos/script.rb', line 25 def script @script end |
Class Method Details
.bare(script) ⇒ Object
77 78 79 |
# File 'lib/cryptos/script.rb', line 77 def self.(script) new script end |
.multisig(address1, address2) ⇒ Object
multisign redeem script
60 61 62 |
# File 'lib/cryptos/script.rb', line 60 def self.multisig(address1, address2) new "OP_2 #{address1.public_key.to_sec} #{address2.public_key.to_sec} OP_2 OP_CHECKMULTISIG" end |
.p2pkh(address_or_hex) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/cryptos/script.rb', line 46 def self.p2pkh(address_or_hex) hash160 = if address_or_hex.is_a? String Address.to_hash160 address_or_hex else address_or_hex.to_hash160 end new "OP_DUP OP_HASH160 #{hash160} OP_EQUALVERIFY OP_CHECKSIG" end |
.p2sh(script) ⇒ Object
55 56 57 |
# File 'lib/cryptos/script.rb', line 55 def self.p2sh(script) new "OP_HASH160 #{script.to_hash160} OP_EQUAL" end |
.sig_multisig(der1, der2, redeem_script) ⇒ Object
scriptSig for pay-to-multisig-hash outputs
37 38 39 |
# File 'lib/cryptos/script.rb', line 37 def self.sig_multisig(der1, der2, redeem_script) new "OP_0 #{der1.serialize} #{der2.serialize} #{redeem_script.to_hex}" end |
.sig_pubkey(der, public_key) ⇒ Object
ScriptSig for pay-to-pubkey-hash outputs
32 33 34 |
# File 'lib/cryptos/script.rb', line 32 def self.sig_pubkey(der, public_key) new "#{der.serialize} #{public_key.to_sec}" end |
.sig_swap(der, public_key, secret, redeem_script) ⇒ Object
scriptSig for atomic swaps
42 43 44 |
# File 'lib/cryptos/script.rb', line 42 def self.sig_swap(der, public_key, secret, redeem_script) new "#{der.serialize} #{public_key.to_sec} #{secret} OP_1 #{redeem_script.to_hex}" end |
.swap(secret_hash, to_address, locktime, from_address) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cryptos/script.rb', line 64 def self.swap(secret_hash, to_address, locktime, from_address) new %{ OP_IF OP_RIPEMD160 #{secret_hash} OP_EQUALVERIFY OP_DUP OP_HASH160 #{to_address.to_hash160} OP_ELSE #{locktime} OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 #{from_address.to_hash160} OP_ENDIF OP_EQUALVERIFY OP_CHECKSIG }.delete("\n").squeeze ' ' end |
Instance Method Details
#size ⇒ Object
102 103 104 |
# File 'lib/cryptos/script.rb', line 102 def size [to_hex].pack('H*').size end |
#to_asm ⇒ Object
94 95 96 |
# File 'lib/cryptos/script.rb', line 94 def to_asm script.to_s end |
#to_hash160 ⇒ Object
85 86 87 |
# File 'lib/cryptos/script.rb', line 85 def to_hash160 hash160 to_hex end |
#to_hex ⇒ Object Also known as: serialize
89 90 91 |
# File 'lib/cryptos/script.rb', line 89 def to_hex @hex ||= to_asm.split.map { |token| token.start_with?('OP') ? opcode(token) : data(token) }.join end |
#to_s ⇒ Object
98 99 100 |
# File 'lib/cryptos/script.rb', line 98 def to_s to_asm end |