Class: Bech32::SegwitAddr

Inherits:
Object
  • Object
show all
Defined in:
lib/bech32/segwit_addr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr = nil) ⇒ SegwitAddr

Returns a new instance of SegwitAddr.



8
9
10
11
# File 'lib/bech32/segwit_addr.rb', line 8

def initialize(addr = nil)
  @hrp = 'bc'
  parse_addr(addr) if addr
end

Instance Attribute Details

#hrpObject

human-readable part



4
5
6
# File 'lib/bech32/segwit_addr.rb', line 4

def hrp
  @hrp
end

#progObject

witness program



6
7
8
# File 'lib/bech32/segwit_addr.rb', line 6

def prog
  @prog
end

#verObject

witness version



5
6
7
# File 'lib/bech32/segwit_addr.rb', line 5

def ver
  @ver
end

Instance Method Details

#addrObject

Returns segwit address string which generated from hrp, witness version and witness program.



27
28
29
# File 'lib/bech32/segwit_addr.rb', line 27

def addr
  Bech32.encode(hrp, [ver] + convert_bits(prog, 8, 5))
end

#script_pubkey=(script_pubkey) ⇒ Object

parse script pubkey into witness version and witness program



20
21
22
23
24
# File 'lib/bech32/segwit_addr.rb', line 20

def script_pubkey=(script_pubkey)
  values = [script_pubkey].pack('H*').unpack("C*")
  @ver = values[0]
  @prog = values[2..-1]
end

#to_script_pubkeyObject

Returns segwit script pubkey which generated from witness version and witness program.



14
15
16
17
# File 'lib/bech32/segwit_addr.rb', line 14

def to_script_pubkey
  v = ver == 0 ? ver : ver + 0x80
  ([v, prog.length].pack("CC") + prog.map{|p|[p].pack("C")}.join).unpack('H*').first
end