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
CASH_PREFIXES =
%w[bitcoincash bchtest bchreg].freeze
CASH_HASH_SIZES =

CashAddr size bits (version byte bits 2..0) mapped to the hash length in bytes.

{ 0 => 20, 1 => 24, 2 => 28, 3 => 32, 4 => 40, 5 => 48, 6 => 56, 7 => 64 }.freeze
LEGACY_LENGTH =

1 version byte + 20 hash bytes + 4 checksum bytes

25
MAX_LENGTH =
120

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.



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

def initialize(address)
  @raw_address = address
  normalize
end

Instance Attribute Details

#digest ⇒ Object (readonly)

Returns the value of attribute digest.



35
36
37
# File 'lib/adequate_crypto_address/bch.rb', line 35

def digest
  @digest
end

#payload ⇒ Object (readonly)

Returns the value of attribute payload.



35
36
37
# File 'lib/adequate_crypto_address/bch.rb', line 35

def payload
  @payload
end

#prefix ⇒ Object (readonly)

Returns the value of attribute prefix.



35
36
37
# File 'lib/adequate_crypto_address/bch.rb', line 35

def prefix
  @prefix
end

#raw_address ⇒ Object (readonly)

Returns the value of attribute raw_address.



35
36
37
# File 'lib/adequate_crypto_address/bch.rb', line 35

def raw_address
  @raw_address
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/adequate_crypto_address/bch.rb', line 35

def type
  @type
end

Instance Method Details

#address_type ⇒ Object

Public contract: the detected legacy/cash type Symbol, or nil when invalid.



51
52
53
# File 'lib/adequate_crypto_address/bch.rb', line 51

def address_type
  type
end

#cash_address ⇒ Object Also known as: address



62
63
64
65
66
67
68
# File 'lib/adequate_crypto_address/bch.rb', line 62

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

#legacy_address ⇒ Object



55
56
57
58
59
60
# File 'lib/adequate_crypto_address/bch.rb', line 55

def legacy_address
  type_int = type_mapping(: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)


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

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