Class: Platon::SegwitAddr

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

Constant Summary collapse

HRP_MAINNET =

TODO

'lat'
HRP_TESTNET =
'atp'
HRP_REGTEST =
'atx'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr = nil) ⇒ SegwitAddr

Returns a new instance of SegwitAddr.



13
14
15
16
# File 'lib/platon/segwit_addr.rb', line 13

def initialize(addr = nil)
  @hrp = HRP_TESTNET  ## TODO
  parse_addr(addr) if addr
end

Instance Attribute Details

#hrpObject

human-readable part



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

def hrp
  @hrp
end

#progObject

witness program



11
12
13
# File 'lib/platon/segwit_addr.rb', line 11

def prog
  @prog
end

#verObject

witness version



10
11
12
# File 'lib/platon/segwit_addr.rb', line 10

def ver
  @ver
end

Instance Method Details

#addrObject

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



29
30
31
# File 'lib/platon/segwit_addr.rb', line 29

def addr
  Bech32.encode(hrp, convert_bits(prog, 8, 5), 1)
end

#script_pubkey=(script_pubkey) ⇒ Object

parse script pubkey into witness version and witness program



24
25
26
# File 'lib/platon/segwit_addr.rb', line 24

def script_pubkey=(script_pubkey)
  @prog = [script_pubkey].pack('H*').unpack("C*")
end

#to_script_pubkeyObject

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



19
20
21
# File 'lib/platon/segwit_addr.rb', line 19

def to_script_pubkey
  (prog.map{|p|[p].pack("C")}.join).unpack('H*').first
end