Class: WalletValidator::Bch

Inherits:
Object
  • Object
show all
Defined in:
lib/wallet_validator/bch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, is_testnet) ⇒ Bch

Returns a new instance of Bch.



7
8
9
10
# File 'lib/wallet_validator/bch.rb', line 7

def initialize(address, is_testnet)
  @address = address
  @is_testnet = is_testnet
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/wallet_validator/bch.rb', line 5

def address
  @address
end

#is_testnetObject (readonly)

Returns the value of attribute is_testnet.



5
6
7
# File 'lib/wallet_validator/bch.rb', line 5

def is_testnet
  @is_testnet
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wallet_validator/bch.rb', line 12

def valid?
  match = address.match(/^(.+):(.+)$/)

  if match
    return false unless match[1] == is_testnet ? "bchtest" : "bitcoincash"

    prefix = is_testnet ? [2, 3, 8, 20, 5, 19, 20, 0] : [2, 9, 20, 3, 15, 9, 14, 3, 1, 19, 8, 0]
    raw_payload = match[2].split("").map { |m| Crypto::Bech32::CHARSET.index(m) }
    return false if bch_polymod(prefix + raw_payload) != 0 # raise "Address checksum invalid: #{cashaddress}"

    payload = bch_convert_bits(raw_payload[0..-9], 5, 8, false)
    return false if payload.empty? # raise "Converted payload was empty"

    return true
  else
    Btc.new(@address, @is_testnet).valid?
  end
end