Module: Tapyrus::Util

Overview

tapyrus utility. following methods can be used as follows.

Tapyrus.pack_var_int(5)

Constant Summary collapse

DIGEST_NAME_SHA256 =
"sha256"

Instance Method Summary collapse

Instance Method Details

#byte_to_bit(byte) ⇒ Object

byte convert to the sequence of bits packed eight in a byte with the least significant bit first.



79
80
81
# File 'lib/tapyrus/util.rb', line 79

def byte_to_bit(byte)
  byte.unpack("b*").first
end

#calc_checksum(hex) ⇒ Object



126
127
128
# File 'lib/tapyrus/util.rb', line 126

def calc_checksum(hex)
  double_sha256(hex.htb).bth[0..7]
end

#decode_base58_address(addr) ⇒ Array

decode Base58 check encoding address.

Parameters:

Returns:

  • (Array)

    hex and address version



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tapyrus/util.rb', line 109

def decode_base58_address(addr)
  hex = Base58.decode(addr)
  if hex.size == 50 && calc_checksum(hex[0...-8]) == hex[-8..-1]
    unless [Tapyrus.chain_params.address_version, Tapyrus.chain_params.p2sh_version].include?(hex[0..1])
      raise "Invalid version bytes."
    end
    [hex[2...-8], hex[0..1]]
  elsif hex.size == 116 && calc_checksum(hex[0...-8]) == hex[-8..-1]
    unless [Tapyrus.chain_params.cp2pkh_version, Tapyrus.chain_params.cp2sh_version].include?(hex[0..1])
      raise "Invalid version bytes."
    end
    [hex[2...-8], hex[0..1]]
  else
    raise "Invalid address."
  end
end

#double_sha256(payload) ⇒ Object



74
75
76
# File 'lib/tapyrus/util.rb', line 74

def double_sha256(payload)
  sha256(sha256(payload))
end

#encode_base58_address(hex, addr_version) ⇒ String

encode Base58 check address.

Parameters:

  • hex (String)

    the address payload.

  • addr_version (String)

    the address version for P2PKH and P2SH.

Returns:

  • (String)

    Base58 check encoding address.



101
102
103
104
# File 'lib/tapyrus/util.rb', line 101

def encode_base58_address(hex, addr_version)
  base = addr_version + hex
  Base58.encode(base + calc_checksum(base))
end

#hash160(hex) ⇒ Object

generate sha256-ripemd160 hash for value



93
94
95
# File 'lib/tapyrus/util.rb', line 93

def hash160(hex)
  Digest::RMD160.hexdigest(Digest::SHA256.digest(hex.htb))
end

#hmac_sha256(key, data) ⇒ Object



132
133
134
# File 'lib/tapyrus/util.rb', line 132

def hmac_sha256(key, data)
  OpenSSL::HMAC.digest(DIGEST_NAME_SHA256, key, data)
end

#pack_boolean(b) ⇒ Object



61
62
63
# File 'lib/tapyrus/util.rb', line 61

def pack_boolean(b)
  b ? [0x01].pack("C") : [0x00].pack("C")
end

#pack_var_int(i) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tapyrus/util.rb', line 18

def pack_var_int(i)
  if i < 0xfd
    [i].pack("C")
  elsif i <= 0xffff
    [0xfd, i].pack("Cv")
  elsif i <= 0xffffffff
    [0xfe, i].pack("CV")
  elsif i <= 0xffffffffffffffff
    [0xff, i].pack("CQ")
  else
    raise "int(#{i}) too large!"
  end
end

#pack_var_string(payload) ⇒ Object



9
10
11
# File 'lib/tapyrus/util.rb', line 9

def pack_var_string(payload)
  pack_var_int(payload.bytesize) + payload
end

#padding_zero(binary, bytesize) ⇒ String

padding zero to the left of binary string until bytesize.

Parameters:

  • binary (String)

    string

  • bytesize (Integer)

    total bytesize.

Returns:

  • (String)

    padded binary string.



87
88
89
90
# File 'lib/tapyrus/util.rb', line 87

def padding_zero(binary, bytesize)
  return binary unless binary.bytesize < bytesize
  ("00" * (bytesize - binary.bytesize)).htb + binary
end

#sha256(payload) ⇒ Object



70
71
72
# File 'lib/tapyrus/util.rb', line 70

def sha256(payload)
  Digest::SHA256.digest(payload)
end

#unpack_boolean(payload) ⇒ Object



65
66
67
68
# File 'lib/tapyrus/util.rb', line 65

def unpack_boolean(payload)
  data, payload = payload.unpack("Ca*")
  [(data.zero? ? false : true), payload]
end

#unpack_var_int(payload) ⇒ Object

Returns an integer for a valid payload, otherwise nil.

Returns:

  • an integer for a valid payload, otherwise nil



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tapyrus/util.rb', line 33

def unpack_var_int(payload)
  case payload.unpack("C").first
  when 0xfd
    payload.unpack("xva*")
  when 0xfe
    payload.unpack("xVa*")
  when 0xff
    payload.unpack("xQa*")
  else
    payload.unpack("Ca*")
  end
end

#unpack_var_int_from_io(buf) ⇒ Object

Returns an integer for a valid payload, otherwise nil.

Returns:

  • an integer for a valid payload, otherwise nil



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tapyrus/util.rb', line 47

def unpack_var_int_from_io(buf)
  uchar = buf.read(1)&.unpack("C")&.first
  case uchar
  when 0xfd
    buf.read(2)&.unpack("v")&.first
  when 0xfe
    buf.read(4)&.unpack("V")&.first
  when 0xff
    buf.read(8)&.unpack("Q")&.first
  else
    uchar
  end
end

#unpack_var_string(payload) ⇒ Object



13
14
15
16
# File 'lib/tapyrus/util.rb', line 13

def unpack_var_string(payload)
  size, payload = unpack_var_int(payload)
  size > 0 ? payload.unpack("a#{size}a*") : [nil, payload]
end

#valid_address?(addr) ⇒ Boolean

check whether addr is valid address.

Parameters:

  • addr (String)

    an address

Returns:

  • (Boolean)

    if valid address return true, otherwise false.



139
140
141
142
143
144
145
146
# File 'lib/tapyrus/util.rb', line 139

def valid_address?(addr)
  begin
    Tapyrus::Script.parse_from_addr(addr)
    true
  rescue Exception => e
    false
  end
end