Class: AdequateCryptoAddress::Bch

Inherits:
Object
  • Object
show all
Includes:
Utils::Bch
Defined in:
lib/adequate_crypto_address/bch.rb

Defined Under Namespace

Classes: InvalidCashAddress, InvalidLegacyAddress

Constant Summary collapse

TYPE_MAP =
{
  legacy: [
    [:p2sh, 5],
    [:p2pkh, 0],
    [:p2shtest, 196],
    [:p2pkhtest, 111]
  ],
  cash: [
    [:p2sh, 8],
    [:p2pkh, 0],
    [:p2shtest, 8],
    [:p2pkhtest, 0]
  ]
}.freeze
DEFAULT_PREFIX =
:bitcoincash

Constants included from Utils::Bch

Utils::Bch::CHARSET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Bch

b32decode, b32encode, calculate_cash_checksum, code_list_to_string, convertbits, expanded_prefix, polymod, verify_cash_checksum

Constructor Details

#initialize(address) ⇒ Bch

Returns a new instance of Bch.



31
32
33
34
# File 'lib/adequate_crypto_address/bch.rb', line 31

def initialize(address)
  @raw_address = address
  normalize
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



29
30
31
# File 'lib/adequate_crypto_address/bch.rb', line 29

def digest
  @digest
end

#payloadObject (readonly)

Returns the value of attribute payload.



29
30
31
# File 'lib/adequate_crypto_address/bch.rb', line 29

def payload
  @payload
end

#prefixObject (readonly)

Returns the value of attribute prefix.



29
30
31
# File 'lib/adequate_crypto_address/bch.rb', line 29

def prefix
  @prefix
end

#raw_addressObject (readonly)

Returns the value of attribute raw_address.



29
30
31
# File 'lib/adequate_crypto_address/bch.rb', line 29

def raw_address
  @raw_address
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/adequate_crypto_address/bch.rb', line 29

def type
  @type
end

Instance Method Details

#address_type(address_code, address_type) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/adequate_crypto_address/bch.rb', line 44

def address_type(address_code, address_type)
  TYPE_MAP[address_code].each do |mapping|
    return mapping if mapping.include?(address_type)
  end

  raise(AdequateCryptoAddress::InvalidAddress, 'Could not determine address type')
end

#cash_addressObject Also known as: address



59
60
61
62
63
64
65
# File 'lib/adequate_crypto_address/bch.rb', line 59

def cash_address
  type_int = address_type(:cash, type)[1]
  p = [type_int] + payload
  p = convertbits(p, 8, 5)
  checksum = calculate_cash_checksum(p)
  "#{prefix}:#{b32encode(p + checksum)}"
end

#legacy_addressObject



52
53
54
55
56
57
# File 'lib/adequate_crypto_address/bch.rb', line 52

def legacy_address
  type_int = address_type(:legacy, type)[1]
  input = code_list_to_string([type_int] + payload + Array(digest))
  input += Digest::SHA256.digest(Digest::SHA256.digest(input))[0..3] unless digest
  Base58.binary_to_base58(input, :bitcoin)
end

#valid?(validated_type = nil) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/adequate_crypto_address/bch.rb', line 36

def valid?(validated_type = nil)
  if validated_type
    type == validated_type.to_sym
  else
    !type.nil?
  end
end