Module: Nis::Util::Serializer

Defined in:
lib/nis/util/serializer.rb

Class Method Summary collapse

Class Method Details

.serialize_importance_transfer(entity) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/nis/util/serializer.rb', line 40

def self.serialize_importance_transfer(entity)
  a = []
  a += serialize_common(entity)
  a += serialize_int(entity[:mode])
  temp = hex2ua(entity[:remoteAccount])
  a += serialize_int(temp.size)
  a += temp
  a
end

.serialize_mosaic_definition_creation(entity) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/nis/util/serializer.rb', line 121

def self.serialize_mosaic_definition_creation(entity)
  a = []
  a += serialize_common(entity)
  a += serialize_mosaic_definition(entity[:mosaicDefinition])
  a += serialize_safe_string(entity[:creationFeeSink])
  a += serialize_long(entity[:creationFee])
  a
end

.serialize_mosaic_supply_change(entity) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nis/util/serializer.rb', line 130

def self.serialize_mosaic_supply_change(entity)
  a = []
  a += serialize_common(entity)
  mo_id = entity[:mosaicId]
  a_ns = hex2ua(utf8_to_hex(mo_id[:namespaceId]))
  a_ns_len = serialize_int(a_ns.size)
  a_mo = hex2ua(utf8_to_hex(mo_id[:name]))
  a_mo_len = serialize_int(a_mo.size)
  a += serialize_int((a_ns_len + a_ns + a_mo_len + a_mo).size)
  a += a_ns_len
  a += a_ns
  a += a_mo_len
  a += a_mo
  a += serialize_int(entity[:supplyType])
  a += serialize_long(entity[:delta])
  a
end

.serialize_multisig(entity) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nis/util/serializer.rb', line 88

def self.serialize_multisig(entity)
  a = []
  a += serialize_common(entity)
  other_tx = entity[:otherTrans]
  tx = case other_tx[:type]
       when 0x0101 then serialize_transfer(other_tx)
       when 0x0801 then serialize_importance_transfer(other_tx)
       when 0x1001 then serialize_multisig_aggregate_modification(other_tx)
    else raise "Unexpected type #{other_tx[:type]}"
  end
  a += serialize_int(tx.size)
  a += tx
  a
end

.serialize_multisig_aggregate_modification(entity) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nis/util/serializer.rb', line 50

def self.serialize_multisig_aggregate_modification(entity)
  a = []
  a += serialize_common(entity)
  a += serialize_int(entity[:modifications].size)
  a += entity[:modifications].map do |mod|
    b = []
    b += serialize_int(40)
    b += serialize_int(mod[:modificationType])
    b += serialize_int(32)
    b += hex2ua(mod[:cosignatoryAccount])
    b
  end.flatten

  # The following part describes the minimum cosignatories modification.
  # The part is optional. Version 1 aggregate modification transactions should omit this part.
  # Version 2 aggregate modification transactions with no minimum cosignatories modification
  # should only write the length field with value 0x00, 0x00, 0x00, 0x00.
  if true # only version2
    if entity[:minCosignatories][:relativeChange] > 0
      a += serialize_int(4)
      a += serialize_int(entity[:minCosignatories][:relativeChange])
    else
      a += [0, 0, 0, 0]
    end
  end
  a
end

.serialize_multisig_signature(entity) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/nis/util/serializer.rb', line 78

def self.serialize_multisig_signature(entity)
  a = []
  a += serialize_common(entity)
  temp = hex2ua(entity[:otherHash][:data])
  a += serialize_int(4 + temp.size)
  a += serialize_int(temp.size)
  a += temp
  a += serialize_safe_string(entity[:otherAccount])
end

.serialize_provision_namespace(entity) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nis/util/serializer.rb', line 103

def self.serialize_provision_namespace(entity)
  a = []
  a += serialize_common(entity)
  a += serialize_safe_string(entity[:rentalFeeSink])
  a += serialize_long(entity[:rentalFee])
  temp = hex2ua(utf8_to_hex(entity[:newPart]))
  a += serialize_int(temp.size)
  a += temp
  if entity[:parent]
    temp = hex2ua(utf8_to_hex(entity[:parent]))
    a += serialize_int(temp.size)
    a += temp
  else
    a += [255, 255, 255, 255]
  end
  a
end

.serialize_transaction(entity) ⇒ Array

Serialize a transaction object

Parameters:

  • entity (Hash)

Returns:

  • (Array)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nis/util/serializer.rb', line 6

def self.serialize_transaction(entity)
  method = case entity[:type]
           when 0x0101 then method(:serialize_transfer)
           when 0x0801 then method(:serialize_importance_transfer)
           when 0x1001 then method(:serialize_multisig_aggregate_modification)
           when 0x1002 then method(:serialize_multisig_signature)
           when 0x1004 then method(:serialize_multisig)
           when 0x2001 then method(:serialize_provision_namespace)
           when 0x4001 then method(:serialize_mosaic_definition_creation)
           when 0x4002 then method(:serialize_mosaic_supply_change)
    else raise "Not implemented entity type: #{entity[:type]}"
  end
  method.call(entity)
end

.serialize_transfer(entity) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nis/util/serializer.rb', line 21

def self.serialize_transfer(entity)
  a = []
  # Common transaction part
  a += serialize_common(entity)
  # Transfer transaction part
  a += serialize_safe_string(entity[:recipient])
  a += serialize_long(entity[:amount])
  temp = hex2ua(entity[:message][:payload])
  if temp.size == 0
    a += [0, 0, 0, 0]
  else
    a += serialize_int(temp.size + 8)
    a += serialize_int(entity[:message][:type])
    a += serialize_int(temp.size)
    a += temp
  end
  a
end