Class: Bech32::SegwitAddr

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

Constant Summary collapse

HRP_MAINNET =

Returns:

  • (String)
'bc'
HRP_TESTNET =

Returns:

  • (String)
'tb'
HRP_REGTEST =

Returns:

  • (String)
'bcrt'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr = nil) ⇒ SegwitAddr

Initialize SegwitAddr. Optionally parse from address string.

Parameters:

  • addr (String, nil) (defaults to: nil)


12
13
14
15
# File 'sig/bech32/segwit_addr.rbs', line 12

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

Instance Attribute Details

#hrpString

human-readable part

Returns:

  • (String)


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

def hrp
  @hrp
end

#progArray[Integer]?

witness program

Returns:

  • (Array[Integer], nil)


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

def prog
  @prog
end

#verInteger?

witness version

Returns:

  • (Integer, nil)


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

def ver
  @ver
end

Instance Method Details

#addrString

Generate bech32/bech32m address string.

Returns:

  • (String)


32
33
34
35
# File 'lib/bech32/segwit_addr.rb', line 32

def addr
  spec = (ver == 0 ? Bech32::Encoding::BECH32 : Bech32::Encoding::BECH32M)
  Bech32.encode(hrp, [ver] + Bech32.convert_bits(prog, 8, 5), spec)
end

#parse_addr(addr) ⇒ void

This method returns an undefined value.

Parse address string into hrp, version and program.

Parameters:

  • addr (String)


26
27
28
29
30
31
32
33
34
35
# File 'sig/bech32/segwit_addr.rbs', line 26

def parse_addr(addr)
  @hrp, data, spec = Bech32.decode(addr)
  raise 'Invalid address.' if hrp.nil? || data[0].nil? || ![HRP_MAINNET, HRP_TESTNET, HRP_REGTEST].include?(hrp)
  @ver = data[0]
  raise 'Invalid witness version' if @ver > 16
  @prog = Bech32.convert_bits(data[1..-1], 5, 8, false)
  raise 'Invalid witness program' if @prog.nil? || @prog.length < 2 || @prog.length > 40
  raise 'Invalid witness program with version 0' if @ver == 0 && (@prog.length != 20 && @prog.length != 32)
  raise 'Witness version and encoding spec do not match' if (@ver == 0 && spec != Bech32::Encoding::BECH32) || (@ver != 0 && spec != Bech32::Encoding::BECH32M)
end

#script_pubkey=(script_pubkey) ⇒ void

This method returns an undefined value.

Parse script pubkey into witness version and program.

Parameters:

  • script_pubkey (String)


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

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

#to_script_pubkeyString

Generate script pubkey from witness version and program.

Returns:

  • (String)


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

def to_script_pubkey
  v = ver == 0 ? ver : ver + 0x50
  ([v, prog.length] + prog).pack('C*').unpack("H*").first
end