Class: Bech32::SegwitAddr
- Inherits:
-
Object
- Object
- Bech32::SegwitAddr
- Defined in:
- lib/bech32/segwit_addr.rb
Constant Summary collapse
- HRP_MAINNET =
'bc'
- HRP_TESTNET =
'tb'
- HRP_REGTEST =
'bcrt'
Instance Attribute Summary collapse
-
#hrp ⇒ Object
human-readable part.
-
#prog ⇒ Object
witness program.
-
#ver ⇒ Object
witness version.
Instance Method Summary collapse
-
#addr ⇒ Object
Returns segwit address string which generated from hrp, witness version and witness program.
-
#initialize(addr = nil) ⇒ SegwitAddr
constructor
A new instance of SegwitAddr.
-
#script_pubkey=(script_pubkey) ⇒ Object
parse script pubkey into witness version and witness program.
-
#to_script_pubkey ⇒ Object
Returns segwit script pubkey which generated from witness version and witness program.
Constructor Details
#initialize(addr = nil) ⇒ SegwitAddr
Returns a new instance of SegwitAddr.
13 14 15 16 |
# File 'lib/bech32/segwit_addr.rb', line 13 def initialize(addr = nil) @hrp = HRP_MAINNET parse_addr(addr) if addr end |
Instance Attribute Details
#hrp ⇒ Object
human-readable part
9 10 11 |
# File 'lib/bech32/segwit_addr.rb', line 9 def hrp @hrp end |
#prog ⇒ Object
witness program
11 12 13 |
# File 'lib/bech32/segwit_addr.rb', line 11 def prog @prog end |
#ver ⇒ Object
witness version
10 11 12 |
# File 'lib/bech32/segwit_addr.rb', line 10 def ver @ver end |
Instance Method Details
#addr ⇒ Object
Returns segwit address string which generated from hrp, witness version and witness program.
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 |
#script_pubkey=(script_pubkey) ⇒ Object
parse script pubkey into witness version and witness 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 ⇒ Object
Returns segwit script pubkey which generated from witness version and witness 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 |