Class: Bitcoin::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/segwit/script.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_witness_hash160_script(hash160) ⇒ Object

generate p2wpkh tx for given address. returns a raw binary script of the form: 0 <hash160>



61
62
63
64
65
# File 'lib/segwit/script.rb', line 61

def self.to_witness_hash160_script(hash160)
  return nil  unless hash160
  #  witness ver  length  hash160
  [ ["00",         "14",  hash160].join ].pack("H*")
end

.to_witness_p2sh_script(p2sh) ⇒ Object

generate p2wsh output script for given p2sh sha256. returns a raw binary script of the form: 0 <p2sh>



69
70
71
72
73
# File 'lib/segwit/script.rb', line 69

def self.to_witness_p2sh_script(p2sh)
  return nil  unless p2sh
  #  witness ver  length  sha256
  [ [ "00",        "20",   p2sh].join].pack("H*")
end

Instance Method Details

#codeseparator_countObject



75
76
77
# File 'lib/segwit/script.rb', line 75

def codeseparator_count
  @chunks.select{|c|c == Bitcoin::Script::OP_CODESEPARATOR}.length
end

#get_hash160Object

get the hash160 for this hash160 or pubkey script



51
52
53
54
55
56
57
# File 'lib/segwit/script.rb', line 51

def get_hash160
  return @chunks[2..-3][0].unpack("H*")[0]  if is_hash160?
  return @chunks[-2].unpack("H*")[0]        if is_p2sh?
  return Bitcoin.hash160(get_pubkey)        if is_pubkey?
  return @chunks[1].unpack("H*")[0]         if is_witness_v0_keyhash?
  return @chunks[1].unpack("H*")[0]         if is_witness_v0_scripthash?
end

#is_standard?Boolean

check if script is in one of the recognized standard formats

Returns:

  • (Boolean)


18
19
20
# File 'lib/segwit/script.rb', line 18

def is_standard?
  is_pubkey? || is_hash160? || is_multisig? || is_p2sh?  || is_op_return? || is_witness_v0_keyhash? || is_witness_v0_scripthash?
end

#is_witness?Boolean

is this a witness script(witness_v0_keyhash or witness_v0_scripthash)

Returns:

  • (Boolean)


23
24
25
# File 'lib/segwit/script.rb', line 23

def is_witness?
  is_witness_v0_keyhash? || is_witness_v0_scripthash?
end

#is_witness_v0_keyhash?Boolean

is this a witness pubkey script

Returns:

  • (Boolean)


28
29
30
# File 'lib/segwit/script.rb', line 28

def is_witness_v0_keyhash?
  @chunks.length == 2 &&@chunks[0] == 0 && @chunks[1].is_a?(String) && @chunks[1].bytesize == 20
end

#is_witness_v0_scripthash?Boolean

is this a witness script hash

Returns:

  • (Boolean)


33
34
35
# File 'lib/segwit/script.rb', line 33

def is_witness_v0_scripthash?
  @chunks.length == 2 &&@chunks[0] == 0 && @chunks[1].is_a?(String) && @chunks[1].bytesize == 32
end

#subscript_codeseparator(separator_index) ⇒ Object

Returns a script that deleted the script before the index specified by separator_index.



7
8
9
10
11
12
13
14
15
# File 'lib/segwit/script.rb', line 7

def subscript_codeseparator(separator_index)
  buf = []
  process_separator_index = 0
  (chunks || @chunks).each{|chunk|
    buf << chunk if process_separator_index == separator_index
    process_separator_index += 1 if chunk == OP_CODESEPARATOR and process_separator_index < separator_index
  }
  to_binary(buf)
end

#typeObject

get type of this tx



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/segwit/script.rb', line 38

def type
  if is_hash160?;                 :hash160
  elsif is_pubkey?;               :pubkey
  elsif is_multisig?;             :multisig
  elsif is_p2sh?;                 :p2sh
  elsif is_op_return?;            :op_return
  elsif is_witness_v0_keyhash?;   :witness_v0_keyhash
  elsif is_witness_v0_scripthash?;:witness_v0_scripthash
  else;                           :unknown
  end
end