Class: CoinOp::Bit::Script
- Inherits:
-
Object
- Object
- CoinOp::Bit::Script
- Includes:
- Encodings
- Defined in:
- lib/coin-op/bit/script.rb
Overview
A wrapper class to make it easier to read and write Bitcoin scripts. Provides a sane #to_json method.
Instance Attribute Summary collapse
-
#native ⇒ Object
readonly
Accessor for the “native” ::Bitcoin::Script script instance.
Instance Method Summary collapse
- #address ⇒ Object
- #hash160 ⇒ Object
-
#initialize(options, network: nil) ⇒ Script
constructor
Takes either a String or a Hash as its argument.
- #p2sh_address ⇒ Object
-
#p2sh_script ⇒ Object
Generate the script that uses a P2SH address.
-
#p2sh_sig(options) ⇒ Object
Generate a P2SH script_sig for the current script, using the supplied options, which will, in the case of a multisig input, be => array_of_signatures.
- #to_blob ⇒ Object (also: #to_binary)
- #to_hash ⇒ Object
- #to_hex ⇒ Object
- #to_json(*a) ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
Methods included from Encodings
#base58, #decode_base58, #decode_hex, #hex, #int_to_byte_array
Constructor Details
#initialize(options, network: nil) ⇒ Script
Takes either a String or a Hash as its argument.
A String argument will be parsed as a human readable script.
A Hash argument specifies a script using one of several possible keys:
-
:string
-
:blob
-
:hex
-
:address
-
:public_key
-
:public_keys, :needed
-
:signatures
The name of the crypto-currency network may also be specified. It defaults to :testnet3. Names supplied in this manner must correspond to the names in the ::Bitcoin::NETWORKS Hash. TODO: PLEASE refactor this. should not accept either string or hash
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/coin-op/bit/script.rb', line 31 def initialize(, network: nil) network_name = network || ([:network] || :testnet3) rescue :testnet3 @network = Bitcoin::NETWORKS[network_name] # literals CoinOp.syncbit(network_name) do if .is_a? String @blob = Bitcoin::Script.binary_from_string elsif string = [:string] @blob = Bitcoin::Script.binary_from_string string elsif [:blob] @blob = [:blob] elsif [:hex] @blob = decode_hex([:hex]) # arguments for constructing else if address = [:address] unless Bitcoin::valid_address?(address) raise ArgumentError, "Invalid address: #{address}" end @blob = Bitcoin::Script.to_address_script(address) elsif public_key = [:public_key] @blob = Bitcoin::Script.to_pubkey_script(public_key) elsif (keys = [:public_keys]) && (needed = [:needed]) @blob = Bitcoin::Script.to_multisig_script(needed, *keys) elsif signatures = [:signatures] @blob = Bitcoin::Script.to_multisig_script_sig(*signatures) else raise ArgumentError end end @native = Bitcoin::Script.new @blob @hex = hex(@blob) @string = @native.to_string end end |
Instance Attribute Details
#native ⇒ Object (readonly)
Accessor for the “native” ::Bitcoin::Script script instance
10 11 12 |
# File 'lib/coin-op/bit/script.rb', line 10 def native @native end |
Instance Method Details
#address ⇒ Object
126 127 128 129 130 |
# File 'lib/coin-op/bit/script.rb', line 126 def address CoinOp.syncbit(@network[:name]) do @native.get_address end end |
#hash160 ⇒ Object
118 119 120 |
# File 'lib/coin-op/bit/script.rb', line 118 def hash160 CoinOp.syncbit(@network[:name]) { Bitcoin.hash160(@hex) } end |
#p2sh_address ⇒ Object
122 123 124 |
# File 'lib/coin-op/bit/script.rb', line 122 def p2sh_address Bitcoin.encode_address(self.hash160, Bitcoin::NETWORKS[@network[:name]][:p2sh_version]) end |
#p2sh_script ⇒ Object
Generate the script that uses a P2SH address. Used for an Output’s scriptPubKey value. Not much used, and can probably be removed, as I think it is equivalent to Script.new :address => some.address
111 112 113 114 115 116 |
# File 'lib/coin-op/bit/script.rb', line 111 def p2sh_script h160 = CoinOp.syncbit(@network[:name]) do @native.get_hash160 end self.class.new(blob: Bitcoin::Script.to_p2sh_script(h160), network: @network[:name]) end |
#p2sh_sig(options) ⇒ Object
Generate a P2SH script_sig for the current script, using the supplied options, which will, in the case of a multisig input, be => array_of_signatures.
135 136 137 138 139 140 |
# File 'lib/coin-op/bit/script.rb', line 135 def p2sh_sig() string = Script.new().to_s CoinOp.syncbit(@network[:name]) do Bitcoin::Script.binary_from_string("#{string} #{self.to_hex}") end end |
#to_blob ⇒ Object Also known as: to_binary
76 77 78 |
# File 'lib/coin-op/bit/script.rb', line 76 def to_blob @blob end |
#to_hash ⇒ Object
96 97 98 99 100 101 |
# File 'lib/coin-op/bit/script.rb', line 96 def to_hash { :type => self.type, :string => self.to_s } end |
#to_hex ⇒ Object
72 73 74 |
# File 'lib/coin-op/bit/script.rb', line 72 def to_hex @hex end |
#to_json(*a) ⇒ Object
103 104 105 |
# File 'lib/coin-op/bit/script.rb', line 103 def to_json(*a) self.to_hash.to_json(*a) end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/coin-op/bit/script.rb', line 68 def to_s @string end |
#type ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/coin-op/bit/script.rb', line 82 def type case self.native.type when :hash160 # Pay to address, because an "address" is really just the hash # of a public key. :pubkey_hash when :p2sh # Pay to Script Hash :script_hash else self.native.type end end |