Class: BinaryCodec::Currency

Inherits:
Hash160 show all
Defined in:
lib/binary-codec/types/currency.rb

Constant Summary collapse

XRP_HEX_REGEX =
/^0{40}$/
ISO_REGEX =
/^[A-Z0-9a-z?!@#$%^&*(){}\[\]\|]{3}$/
HEX_REGEX =
/^[A-F0-9]{40}$/
STANDARD_FORMAT_HEX_REGEX =
/^0{24}[\x00-\x7F]{6}0{10}$/

Instance Attribute Summary collapse

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#compare_to, from_parser, #nibblet, width

Methods inherited from ComparableSerializedType

#compare_to, #eq, #gt, #gte, #lt, #lte

Methods inherited from SerializedType

from_bytes, from_hex, from_json, from_parser, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #value_of

Constructor Details

#initialize(byte_buf = nil) ⇒ Currency

Returns a new instance of Currency.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/binary-codec/types/currency.rb', line 17

def initialize(byte_buf = nil)
  super(byte_buf)
  hex = to_hex

  if XRP_HEX_REGEX.match?(hex)
    @_iso = 'XRP'
  elsif STANDARD_FORMAT_HEX_REGEX.match?(hex)
    @_iso = iso_code_from_hex(@bytes[12..14])
  else
    @_iso = nil
  end
end

Instance Attribute Details

#isoString? (readonly)

Returns the ISO code of the currency, if applicable.

Returns:

  • (String, nil)

    The ISO code.



32
33
34
# File 'lib/binary-codec/types/currency.rb', line 32

def iso
  @iso
end

Class Method Details

.bytes_from_representation(input) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/binary-codec/types/currency.rb', line 76

def self.bytes_from_representation(input)
  unless is_valid_representation(input)
    raise StandardError, "Unsupported Currency representation: #{input}"
  end

  input.length == 3 ? self.iso_to_bytes(input) : hex_to_bytes(input)
end

.from(value) ⇒ Currency

Creates a new Currency instance from a value.

Parameters:

  • value (Currency, String, Array<Integer>)

    The value to convert.

Returns:

Raises:

  • (StandardError)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/binary-codec/types/currency.rb', line 39

def self.from(value)
  return value if value.is_a?(Currency)

  if value.is_a?(String)
    return new(bytes_from_representation(value))
  end

  if value.is_a?(Array)
    return new(value)
  end

  raise StandardError, "Cannot construct Currency from #{value.class}"
end

.is_bytes_array(bytes) ⇒ Object



106
107
108
# File 'lib/binary-codec/types/currency.rb', line 106

def self.is_bytes_array(bytes)
  bytes.length == 20
end

.is_hex(hex) ⇒ Object



110
111
112
# File 'lib/binary-codec/types/currency.rb', line 110

def self.is_hex(hex)
  !!(HEX_REGEX.match(hex)) # Special case for Vurrency type, do not conflate with valid_hex? function
end

.is_string_representation(input) ⇒ Object



102
103
104
# File 'lib/binary-codec/types/currency.rb', line 102

def self.is_string_representation(input)
  input.length == 3 || is_hex(input)
end

.is_valid_representation(input) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/binary-codec/types/currency.rb', line 94

def self.is_valid_representation(input)
  if input.is_a?(Array)
    self.is_bytes_array(input)
  else
    self.is_string_representation(input)
  end
end

.iso_to_bytes(iso) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/binary-codec/types/currency.rb', line 84

def self.iso_to_bytes(iso)
  bytes = Array.new(20, 0) # Equivalent to Uint8Array(20)
  if iso != 'XRP'
    iso_bytes = iso.chars.map(&:ord)
    # Insert iso_bytes at index 12
    bytes[12, iso_bytes.length] = iso_bytes
  end
  bytes
end

Instance Method Details

#to_jsonString

Returns the JSON representation of the currency.

Returns:

  • (String)

    The ISO code or hex string.



55
56
57
58
59
60
# File 'lib/binary-codec/types/currency.rb', line 55

def to_json
  iso = self.iso
  return iso unless iso.nil?

  bytes_to_hex(@bytes)
end