Class: BinaryCodec::XChainBridge

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/xchain_bridge.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) ⇒ XChainBridge

Returns a new instance of XChainBridge.



5
6
7
# File 'lib/binary-codec/types/xchain_bridge.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
25
26
# File 'lib/binary-codec/types/xchain_bridge.rb', line 9

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

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

  if value.is_a?(Hash)
    bytes = []
    bytes.concat(AccountId.from(value['LockingChainDoor']).to_bytes)
    bytes.concat(Issue.from(value['LockingChainIssue']).to_bytes)
    bytes.concat(AccountId.from(value['IssuingChainDoor']).to_bytes)
    bytes.concat(Issue.from(value['IssuingChainIssue']).to_bytes)
    return XChainBridge.new(bytes)
  end

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

.from_parser(parser, _hint = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/binary-codec/types/xchain_bridge.rb', line 28

def self.from_parser(parser, _hint = nil)
  bytes = []
  bytes.concat(parser.read(20)) # LockingChainDoor
  bytes.concat(Issue.from_parser(parser).to_bytes) # LockingChainIssue
  bytes.concat(parser.read(20)) # IssuingChainDoor
  bytes.concat(Issue.from_parser(parser).to_bytes) # IssuingChainIssue
  XChainBridge.new(bytes)
end

Instance Method Details

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



37
38
39
40
41
42
43
44
45
# File 'lib/binary-codec/types/xchain_bridge.rb', line 37

def to_json(_definitions = nil, _field_name = nil)
  parser = BinaryParser.new(to_hex)
  result = {}
  result['LockingChainDoor'] = AccountId.from_parser(parser).to_json
  result['LockingChainIssue'] = Issue.from_parser(parser).to_json
  result['IssuingChainDoor'] = AccountId.from_parser(parser).to_json
  result['IssuingChainIssue'] = Issue.from_parser(parser).to_json
  result
end