Class: BinaryCodec::Issue

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/issue.rb

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SerializedType

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

Constructor Details

#initialize(bytes = nil) ⇒ Issue

Returns a new instance of Issue.



5
6
7
# File 'lib/binary-codec/types/issue.rb', line 5

def initialize(bytes = nil)
  super(bytes || [])
end

Class Method Details

.from(value) ⇒ Object

Raises:

  • (StandardError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/binary-codec/types/issue.rb', line 9

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

  if value.is_a?(String)
    return Issue.new(hex_to_bytes(value))
  end

  if value.is_a?(Hash) || value.is_a?(::Hash)
    bytes = []
    bytes.concat(Currency.from(value['currency']).to_bytes)
    bytes.concat(AccountId.from(value['issuer']).to_bytes) if value['issuer']
    return Issue.new(bytes)
  end

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

.from_parser(parser, _hint = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/binary-codec/types/issue.rb', line 26

def self.from_parser(parser, _hint = nil)
  bytes = []
  bytes.concat(parser.read(20)) # Currency
  # If there are more bytes in this field, it might have an issuer?
  # Actually Issue is often fixed length 20 or 40.
  # For XChainBridge it uses Issue.
  # Let's see how much we should read. 
  # Usually if it's an Issue in a field, we might know the size.
  # For now, let's assume it can be 20 or 40.
  # But wait, how does the parser know?
  # If it's not variable length, it must have a fixed width or be the rest of the object.
  # Definitions.json says Issue is type 24.
  bytes.concat(parser.read(20)) unless parser.end? # Try reading issuer
  Issue.new(bytes)
end

Instance Method Details

#to_json(_definitions = nil, _field_name = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/binary-codec/types/issue.rb', line 42

def to_json(_definitions = nil, _field_name = nil)
  parser = BinaryParser.new(to_hex)
  result = {}
  result['currency'] = Currency.from_parser(parser).to_json
  result['issuer'] = AccountId.from_parser(parser).to_json unless parser.end?
  result
end