nem-ruby 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: af0488dcad2793275e0bc42b4613730c63c6d11e6c8c9815605137b1c60788ad
4
- data.tar.gz: 93ddce86c953c3b220b2d81b24d83d806c05fbbb3e06ece73dac7ef429fcd396
2
+ SHA1:
3
+ metadata.gz: a3d08ddab8d97a89125da8160e6f7e1495652eab
4
+ data.tar.gz: f58d5a8984b9e9ba2b059e4439b600aa13b3eea2
5
5
  SHA512:
6
- metadata.gz: 6706e148b2ded5c9443ba9e0d5a5bfeac9d472965d167d0611ed6ca6765a63b4319fe3161a1c52da060bce2bc982ded779af2287b3bfe2abeee6f09604975ddc
7
- data.tar.gz: 03afec67c11f6a77a6575072fa39dcbc0d875d7feaf1477bcde766adb59f5e2bae8d62c09abd298991c9eb9a55116b4f94462cb1f66f4e2db85fd535c5f43aa2
6
+ metadata.gz: c2a60386d49ef6345fa6a7cdb421ea28b3b57075d85bc31fcf23a231e76ce63e752fe82ab44e0d43240fee03c4a182aa0d35694439cfbf320db7d21cd7fcc668
7
+ data.tar.gz: bb4a8a197890bfbc11cf413cdaa0cfbcc265bacc4298b94b61e0b1ec18e86136a92ffc26431e81dfff2986351a2958e01918d498a84419eff751a56fd42d5dea
@@ -26,7 +26,7 @@ module Nem
26
26
  end
27
27
 
28
28
  def amount
29
- return quantity * (10**properties.divisibility)
29
+ (quantity * (10**properties.divisibility)).to_i
30
30
  end
31
31
 
32
32
  def self.included(base)
@@ -47,7 +47,7 @@ module Nem
47
47
  # Mosaics need to be sorted by fqn. if not it will occur FAILURE_SIGNATURE_NOT_VERIFIABLE
48
48
  def to_hash
49
49
  tmp = {
50
- amount: amount * 1_000_000,
50
+ amount: (amount * 1_000_000).to_i,
51
51
  recipient: recipient,
52
52
  message: message.to_hash
53
53
  }
@@ -5,71 +5,92 @@ module Nem
5
5
  # @param [String] serialized
6
6
  # @return [Hash]
7
7
  def self.deserialize(serialized)
8
- s = Nem::Util::Convert.hex2ua(serialized)
9
- deserialize_transaction(s)
8
+ s = serialized.scan(/../)
9
+ transaction(s)
10
10
  end
11
11
 
12
12
  private
13
13
 
14
+ def self.transaction(s)
15
+ comm = s[0, 60]
16
+ spec = s[60, s.size]
17
+ type = hexa2int(comm[0, 4])
18
+ method = switch_method(type)
19
+ common(comm).merge(method.call(spec))
20
+ end
21
+
22
+ def self.hexa2int(hexa)
23
+ [hexa.join].pack('H*').unpack('i*').first
24
+ end
25
+
26
+ def self.hexa2ascii(hexa)
27
+ hexa.inject('') { |memo, el| memo << el.hex.chr }
28
+ end
29
+
30
+ def self.hexa2utf8(hexa)
31
+ [hexa.join].pack('H*').force_encoding('UTF-8')
32
+ end
33
+
14
34
  def self.switch_method(type)
15
35
  case type
16
- when 0x0101 then method(:deserialize_transfer)
17
- when 0x0801 then method(:deserialize_importance_transfer)
18
- when 0x1001 then method(:deserialize_multisig_aggregate_modification)
19
- when 0x1002 then method(:deserialize_multisig_signature)
20
- when 0x1004 then method(:deserialize_multisig)
21
- when 0x2001 then method(:deserialize_provision_namespace)
22
- when 0x4001 then method(:deserialize_mosaic_definition_creation)
23
- when 0x4002 then method(:deserialize_mosaic_supply_change)
36
+ when 0x0101 then method(:transfer)
37
+ when 0x0801 then method(:importance_transfer)
38
+ when 0x1001 then method(:multisig_aggregate_modification)
39
+ when 0x1002 then method(:multisig_signature)
40
+ when 0x1004 then method(:multisig)
41
+ when 0x2001 then method(:provision_namespace)
42
+ when 0x4001 then method(:mosaic_definition_creation)
43
+ when 0x4002 then method(:mosaic_supply_change)
24
44
  else raise "Not implemented entity type: #{type}"
25
45
  end
26
46
  end
27
47
 
28
- # Deserialize a transaction object
48
+ # Deserialize transaction common part
29
49
  # @param [String] serialized
30
50
  # @return [Hash]
31
- def self.deserialize_transaction(s)
32
- common = s[0, 60]
33
- specific = s[60, s.size]
34
- type = deserialize_int(common[0, 4])
35
- method = switch_method(type)
36
- # require 'pry'; binding.pry
37
- deserialize_common(common).merge(method.call(specific))
51
+ def self.common(s)
52
+ {
53
+ type: hexa2int(s[0, 4]),
54
+ version: hexa2int(s[4, 4]),
55
+ timeStamp: hexa2int(s[8, 4]),
56
+ signer: s[16, 32].join,
57
+ fee: hexa2int(s[48, 8]),
58
+ deadline: hexa2int(s[56, 4])
59
+ }
38
60
  end
39
61
 
40
62
  # Deserialize a transfer transaction object
41
63
  # @param [String] serialized
42
64
  # @return [Hash]
43
- def self.deserialize_transfer(s)
65
+ def self.transfer(s)
44
66
  tx = {}
45
- tx[:recipient] = deserialize_a(s[4, 40])
46
- tx[:amount] = deserialize_int(s[44, 8])
67
+ tx[:recipient] = hexa2ascii(s[4, 40])
68
+ tx[:amount] = hexa2int(s[44, 8])
47
69
  tx[:message] = {}
48
- msg_len = deserialize_int(s[52, 4])
70
+ msg_len = hexa2int(s[52, 4])
49
71
  if msg_len > 0
50
- payload_len = deserialize_int(s[60, 4])
72
+ payload_len = hexa2int(s[60, 4])
51
73
  tx[:message] = {
52
- type: deserialize_int(s[56, 4]),
53
- payload: Nem::Util::Convert.hex_to_utf8(deserialize_hex(s[64, payload_len]))
74
+ type: hexa2int(s[56, 4]),
75
+ payload: hexa2utf8(s[64, payload_len])
54
76
  }
55
77
  else
56
78
  tx[:message] = { type: 1, payload: '' }
57
79
  end
58
80
 
59
- mosaic_cnt = deserialize_int(s[56 + msg_len, 4])
81
+ mosaic_cnt = hexa2int(s[56 + msg_len, 4])
60
82
  return tx unless mosaic_cnt
61
83
 
62
84
  # mosaic section
63
85
  tx[:mosaics] = []
64
86
  offset = 0
65
87
  mosaic_cnt.times do |i|
66
- mo_len = deserialize_int(s[offset + 60 + msg_len, 4])
67
- # ns_len = deserialize_int(s[offset+64+msg_len, 4])
68
- ns_name_len = deserialize_int(s[offset + 68 + msg_len, 4])
69
- mo_name_len = deserialize_int(s[offset + 72 + msg_len + ns_name_len, 4])
70
- ns = Nem::Util::Convert.hex_to_utf8 deserialize_hex(s[offset + 72 + msg_len, ns_name_len])
71
- name = Nem::Util::Convert.hex_to_utf8 deserialize_hex(s[offset + 76 + msg_len + ns_name_len, mo_name_len])
72
- quantity = deserialize_int(s[offset + 76 + msg_len + ns_name_len + mo_name_len, 8])
88
+ mo_len = hexa2int(s[offset + 60 + msg_len, 4])
89
+ ns_name_len = hexa2int(s[offset + 68 + msg_len, 4])
90
+ mo_name_len = hexa2int(s[offset + 72 + msg_len + ns_name_len, 4])
91
+ ns = hex2ascii(s[offset + 72 + msg_len, ns_name_len])
92
+ name = hex2ascii(s[offset + 76 + msg_len + ns_name_len, mo_name_len])
93
+ quantity = hexa2int(s[offset + 76 + msg_len + ns_name_len + mo_name_len, 8])
73
94
  attachment = {
74
95
  mosaicId: { namespaceId: ns, name: name },
75
96
  quantity: quantity
@@ -83,25 +104,25 @@ module Nem
83
104
  # Deserialize a importance transaction object
84
105
  # @param [String] serialized
85
106
  # @return [Hash]
86
- def self.deserialize_importance_transfer(s)
107
+ def self.importance_transfer(s)
87
108
  {
88
- mode: deserialize_int(s[0, 4]),
89
- remoteAccount: deserialize_hex(s[8, 32])
109
+ mode: hexa2int(s[0, 4]),
110
+ remoteAccount: s[8, 32].join
90
111
  }
91
112
  end
92
113
 
93
114
  # Deserialize a multisig aggregate modification transaction object
94
115
  # @param [String] serialized
95
116
  # @return [Hash]
96
- def self.deserialize_multisig_aggregate_modification(s)
117
+ def self.multisig_aggregate_modification(s)
97
118
  mods = []
98
- mod_count = deserialize_int(s[0, 4])
119
+ mod_count = hexa2int(s[0, 4])
99
120
  offset = 4
100
121
  mod_count.times do |i|
101
- mod_len = deserialize_int(s[offset, 4])
122
+ mod_len = hexa2int(s[offset, 4])
102
123
  mods << {
103
- modificationType: deserialize_int(s[offset + 4, 4]),
104
- cosignatoryAccount: deserialize_hex(s[offset + 12, 32])
124
+ modificationType: hexa2int(s[offset + 4, 4]),
125
+ cosignatoryAccount: s[offset + 12, 32].join
105
126
  }
106
127
  offset += 4 + mod_len
107
128
  end
@@ -112,7 +133,7 @@ module Nem
112
133
  if s[offset + 4, 4]
113
134
  tx.merge(
114
135
  minCosignatories: {
115
- relativeChange: deserialize_int(s[offset + 4, 4])
136
+ relativeChange: hexa2int(s[offset + 4, 4])
116
137
  }
117
138
  )
118
139
  else
@@ -123,36 +144,36 @@ module Nem
123
144
  # Deserialize a multisig signature transaction object
124
145
  # @param [String] serialized
125
146
  # @return [Hash]
126
- def self.deserialize_multisig_signature(s)
147
+ def self.multisig_signature(s)
127
148
  {
128
- otherHash: { data: deserialize_hex(s[8, 32]) },
129
- otherAccount: deserialize_a(s[44, 40])
149
+ otherHash: { data: s[8, 32].join },
150
+ otherAccount: hexa2ascii(s[44, 40])
130
151
  }
131
152
  end
132
153
 
133
154
  # Deserialize a multisig transfer transaction object
134
155
  # @param [String] serialized
135
156
  # @return [Hash]
136
- def self.deserialize_multisig(s)
137
- msig_len = deserialize_int(s[0, 4])
157
+ def self.multisig(s)
158
+ msig_len = hexa2int(s[0, 4])
138
159
  inner = s[4, msig_len]
139
- inner_tx = deserialize_transaction(inner)
160
+ inner_tx = transaction(inner)
140
161
  { otherTrans: inner_tx }
141
162
  end
142
163
 
143
164
  # Deserialize a provision namespace transaction object
144
165
  # @param [String] serialized
145
166
  # @return [Hash]
146
- def self.deserialize_provision_namespace(s)
167
+ def self.provision_namespace(s)
147
168
  tx = {}
148
- tx[:rentalFeeSink] = deserialize_a(s[4, 40])
149
- tx[:rentalFee] = deserialize_int(s[44, 8])
150
- newpart_len = deserialize_int(s[52, 4])
151
- tx[:newPart] = deserialize_a(s[56, newpart_len])
152
- parent_len = deserialize_int(s[56 + newpart_len, 4])
153
- parent = s[56 + newpart_len, parent_len]
154
- unless parent.all? { |val| val == 0xff }
155
- tx[:parent] = deserialize_a(parent)
169
+ tx[:rentalFeeSink] = hexa2ascii(s[4, 40])
170
+ tx[:rentalFee] = hexa2int(s[44, 8])
171
+ newpart_len = hexa2int(s[52, 4])
172
+ tx[:newPart] = hexa2ascii(s[56, newpart_len])
173
+ parent_len = hexa2int(s[56 + newpart_len, 4])
174
+ unless parent_len == -1
175
+ parent = s[56 + newpart_len, parent_len]
176
+ tx[:parent] = hexa2ascii(parent)
156
177
  end
157
178
  tx
158
179
  end
@@ -160,7 +181,7 @@ module Nem
160
181
  # Deserialize a mosaic definition creation transaction object
161
182
  # @param [String] serialized
162
183
  # @return [Hash]
163
- def self.deserialize_mosaic_definition_creation(s)
184
+ def self.mosaic_definition_creation(s)
164
185
  raise 'Not yet implimented.'
165
186
  # TODO: deserializing
166
187
  tx = {}
@@ -170,43 +191,19 @@ module Nem
170
191
  # Deserialize a mosaic supply change transaction object
171
192
  # @param [String] serialized
172
193
  # @return [Hash]
173
- def self.deserialize_mosaic_supply_change(s)
194
+ def self.mosaic_supply_change(s)
174
195
  tx = {}
175
196
  # s[0, 4] # Length of mosaic id structure
176
- ns_len = deserialize_int(s[4, 4])
177
- mo_len = deserialize_int(s[8 + ns_len, 4])
197
+ ns_len = hexa2int(s[4, 4])
198
+ mo_len = hexa2int(s[8 + ns_len, 4])
178
199
  tx[:mosaicId] = {
179
- namespaceId: deserialize_a(s[8, ns_len]),
180
- name: deserialize_a(s[8 + ns_len + mo_len, mo_len])
200
+ namespaceId: hexa2ascii(s[8, ns_len]),
201
+ name: hexa2ascii(s[8 + ns_len + mo_len, mo_len])
181
202
  }
182
- tx[:supplyType] = deserialize_int(s[8 + ns_len + 4 + mo_len, 4])
183
- tx[:delta] = deserialize_int(s[8 + ns_len + 4 + mo_len + 4, 8])
203
+ tx[:supplyType] = hexa2int(s[8 + ns_len + 4 + mo_len, 4])
204
+ tx[:delta] = hexa2int(s[8 + ns_len + 4 + mo_len + 4, 8])
184
205
  tx
185
206
  end
186
-
187
- def self.deserialize_common(s)
188
- {
189
- type: deserialize_int(s[0, 4]),
190
- version: deserialize_int(s[4, 4]),
191
- timeStamp: deserialize_int(s[8, 4]),
192
- # s[12,4] # length of public key,
193
- signer: deserialize_hex(s[16, 32]),
194
- fee: deserialize_int(s[48, 8]),
195
- deadline: deserialize_int(s[56, 4])
196
- }
197
- end
198
-
199
- def self.deserialize_int(ua)
200
- Nem::Util::Convert.ua2hex(ua.reverse).to_i(16)
201
- end
202
-
203
- def self.deserialize_hex(ua)
204
- Nem::Util::Convert.ua2hex(ua)
205
- end
206
-
207
- def self.deserialize_a(ua)
208
- Nem::Util::Convert.hex2a(deserialize_hex(ua))
209
- end
210
207
  end
211
208
  end
212
209
  end
@@ -1,3 +1,3 @@
1
1
  module Nem
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nem-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiyuki Ieyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-10 00:00:00.000000000 Z
11
+ date: 2018-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -380,7 +380,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
380
380
  version: '0'
381
381
  requirements: []
382
382
  rubyforge_project:
383
- rubygems_version: 2.7.3
383
+ rubygems_version: 2.6.11
384
384
  signing_key:
385
385
  specification_version: 4
386
386
  summary: Ruby gem for communicating with the nem