Class: BinaryCodec::XChainBridge

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

Constant Summary collapse

DOOR_LENGTH_PREFIX =

An account inside a bridge carries the same length prefix as a standalone AccountID field. Leaving it off produces bytes that decode as something else entirely, which is why every XChain fixture failed to serialise.

0x14

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(door_bytes(value['LockingChainDoor']))
    bytes.concat(Issue.from(value['LockingChainIssue']).to_bytes)
    bytes.concat(door_bytes(value['IssuingChainDoor']))
    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



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

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

Instance Method Details

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binary-codec/types/xchain_bridge.rb', line 50

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