Class: CoinOp::Bit::Script

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Encodings

#base58, #decode_base58, #decode_hex, #hex

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(options, network: nil)
  network_name = network || (options[:network] || :testnet3) rescue :testnet3
  @network = Bitcoin::NETWORKS[network_name]

  # literals
  CoinOp.syncbit(network_name) do 
    if options.is_a? String
      @blob = Bitcoin::Script.binary_from_string options
    elsif string = options[:string]
      @blob = Bitcoin::Script.binary_from_string string
    elsif options[:blob]
      @blob = options[:blob]
    elsif options[:hex]
      @blob = decode_hex(options[:hex])
      # arguments for constructing
    else
      if address = options[:address]
        unless Bitcoin::valid_address?(address)
          raise ArgumentError, "Invalid address: #{address}"
        end
        @blob = Bitcoin::Script.to_address_script(address)
      elsif public_key = options[:public_key]
        @blob = Bitcoin::Script.to_pubkey_script(public_key)
      elsif (keys = options[:public_keys]) && (needed = options[:needed])
        @blob = Bitcoin::Script.to_multisig_script(needed, *keys)
      elsif signatures = options[: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

#nativeObject (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

#addressObject



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

#hash160Object



118
119
120
# File 'lib/coin-op/bit/script.rb', line 118

def hash160
  CoinOp.syncbit(@network[:name]) { Bitcoin.hash160(@hex) }
end

#p2sh_addressObject



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_scriptObject

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(options)
  string = Script.new(options).to_s
  CoinOp.syncbit(@network[:name]) do
    Bitcoin::Script.binary_from_string("#{string} #{self.to_hex}")
  end
end

#to_blobObject Also known as: to_binary



76
77
78
# File 'lib/coin-op/bit/script.rb', line 76

def to_blob
  @blob
end

#to_hashObject



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_hexObject



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_sObject



68
69
70
# File 'lib/coin-op/bit/script.rb', line 68

def to_s
  @string
end

#typeObject



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