Class: Bech32::SegwitAddr
- Inherits:
-
Object
- Object
- Bech32::SegwitAddr
- Defined in:
- lib/bech32/segwit_addr.rb,
sig/bech32/segwit_addr.rbs
Constant Summary collapse
- HRP_MAINNET =
'bc'- HRP_TESTNET =
'tb'- HRP_REGTEST =
'bcrt'
Instance Attribute Summary collapse
-
#hrp ⇒ String
human-readable part.
-
#prog ⇒ Array[Integer]?
witness program.
-
#ver ⇒ Integer?
witness version.
Instance Method Summary collapse
-
#addr ⇒ String
Generate bech32/bech32m address string.
-
#initialize(addr = nil) ⇒ SegwitAddr
constructor
Initialize SegwitAddr.
-
#parse_addr(addr) ⇒ void
Parse address string into hrp, version and program.
-
#script_pubkey=(script_pubkey) ⇒ void
Parse script pubkey into witness version and program.
-
#to_script_pubkey ⇒ String
Generate script pubkey from witness version and program.
Constructor Details
#initialize(addr = nil) ⇒ SegwitAddr
Initialize SegwitAddr. Optionally parse from address string.
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
#hrp ⇒ String
human-readable part
9 10 11 |
# File 'lib/bech32/segwit_addr.rb', line 9 def hrp @hrp end |
#prog ⇒ Array[Integer]?
witness program
11 12 13 |
# File 'lib/bech32/segwit_addr.rb', line 11 def prog @prog end |
#ver ⇒ Integer?
witness version
10 11 12 |
# File 'lib/bech32/segwit_addr.rb', line 10 def ver @ver end |
Instance Method Details
#addr ⇒ String
Generate bech32/bech32m address 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.
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.
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_pubkey ⇒ String
Generate script pubkey from witness version and program.
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 |