nis-ruby 0.0.12 → 0.0.13
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 +4 -4
- data/.travis.yml +1 -0
- data/README.md +12 -9
- data/examples/transactions/importance_transfer.rb +1 -4
- data/examples/transactions/mosaic_definition_creation.rb +1 -4
- data/examples/transactions/mosaic_supply_change.rb +1 -4
- data/examples/transactions/multisig.rb +1 -6
- data/examples/transactions/multisig_add_cosignatory.rb +1 -4
- data/examples/transactions/multisig_aggregate_modification.rb +1 -4
- data/examples/transactions/multisig_signature.rb +2 -5
- data/examples/transactions/provision_namespace.rb +1 -4
- data/examples/transactions/transfer.rb +1 -4
- data/examples/transactions/transfer_remote.rb +19 -0
- data/lib/nis/endpoint/account/generate.rb +1 -1
- data/lib/nis/endpoint/account/get.rb +4 -4
- data/lib/nis/endpoint/account/harvests.rb +1 -1
- data/lib/nis/endpoint/account/historical.rb +1 -1
- data/lib/nis/endpoint/account/importances.rb +1 -1
- data/lib/nis/endpoint/account/lock.rb +1 -1
- data/lib/nis/endpoint/account/mosaic.rb +2 -2
- data/lib/nis/endpoint/account/namespace.rb +1 -1
- data/lib/nis/endpoint/account/status.rb +1 -1
- data/lib/nis/endpoint/account/transfers.rb +3 -3
- data/lib/nis/endpoint/account/unconfirmed_transactions.rb +1 -1
- data/lib/nis/endpoint/account/unlock.rb +1 -1
- data/lib/nis/endpoint/account/unlocked.rb +1 -1
- data/lib/nis/endpoint/block/at.rb +1 -1
- data/lib/nis/endpoint/block/get.rb +1 -1
- data/lib/nis/endpoint/chain/height.rb +1 -1
- data/lib/nis/endpoint/chain/last_block.rb +1 -1
- data/lib/nis/endpoint/chain/score.rb +1 -1
- data/lib/nis/endpoint/debug/connections.rb +3 -3
- data/lib/nis/endpoint/debug/time_synchronization.rb +1 -1
- data/lib/nis/endpoint/heartbeat.rb +1 -1
- data/lib/nis/endpoint/local/account/transfers.rb +3 -3
- data/lib/nis/endpoint/local/chain.rb +1 -1
- data/lib/nis/endpoint/namespace.rb +1 -1
- data/lib/nis/endpoint/namespace/mosaic.rb +1 -1
- data/lib/nis/endpoint/namespace/root.rb +1 -1
- data/lib/nis/endpoint/node/active_peers.rb +1 -1
- data/lib/nis/endpoint/node/boot.rb +1 -1
- data/lib/nis/endpoint/node/experiences.rb +1 -1
- data/lib/nis/endpoint/node/extended_info.rb +1 -1
- data/lib/nis/endpoint/node/info.rb +1 -1
- data/lib/nis/endpoint/node/peer_list.rb +3 -3
- data/lib/nis/endpoint/status.rb +1 -1
- data/lib/nis/endpoint/transaction/announce.rb +4 -4
- data/lib/nis/endpoint/transaction/prepareAnnounce.rb +4 -4
- data/lib/nis/keypair.rb +13 -3
- data/lib/nis/request/announce.rb +47 -2
- data/lib/nis/request/prepare_announce.rb +1 -1
- data/lib/nis/struct/account_importance_view_model.rb +1 -1
- data/lib/nis/struct/account_info.rb +1 -1
- data/lib/nis/struct/account_meta_data.rb +1 -1
- data/lib/nis/struct/account_meta_data_pair.rb +1 -1
- data/lib/nis/struct/account_private_key_transactions_page.rb +1 -1
- data/lib/nis/struct/application_meta_data.rb +1 -1
- data/lib/nis/struct/audit_collection.rb +1 -1
- data/lib/nis/struct/audit_info.rb +1 -1
- data/lib/nis/struct/block.rb +1 -1
- data/lib/nis/struct/block_height.rb +1 -1
- data/lib/nis/struct/block_score.rb +1 -1
- data/lib/nis/struct/boot_node_request.rb +1 -1
- data/lib/nis/struct/error.rb +1 -1
- data/lib/nis/struct/explorer_block_view_model.rb +1 -1
- data/lib/nis/struct/explorer_transfer_view_model.rb +1 -1
- data/lib/nis/struct/explorer_view_model_transaction.rb +1 -1
- data/lib/nis/struct/extended_node_experience_pair.rb +1 -1
- data/lib/nis/struct/harvest_info.rb +1 -1
- data/lib/nis/struct/key_pair_view_model.rb +1 -1
- data/lib/nis/struct/mosaic.rb +1 -1
- data/lib/nis/struct/mosaic_definition.rb +1 -1
- data/lib/nis/struct/mosaic_definition_meta_data.rb +1 -1
- data/lib/nis/struct/mosaic_definition_meta_data_pair.rb +1 -1
- data/lib/nis/struct/mosaic_id.rb +1 -1
- data/lib/nis/struct/mosaic_levy.rb +1 -1
- data/lib/nis/struct/mosaic_properties.rb +1 -1
- data/lib/nis/struct/multisig_cosignatory_modification.rb +1 -1
- data/lib/nis/struct/namespace.rb +1 -1
- data/lib/nis/struct/namespace_meta_data.rb +1 -1
- data/lib/nis/struct/namespace_meta_data_pair.rb +1 -1
- data/lib/nis/struct/nem_announce_result.rb +1 -1
- data/lib/nis/struct/nem_async_timer_visitor.rb +1 -1
- data/lib/nis/struct/nem_request_result.rb +1 -1
- data/lib/nis/struct/nis_node_info.rb +1 -1
- data/lib/nis/struct/node.rb +1 -1
- data/lib/nis/struct/node_collection.rb +1 -1
- data/lib/nis/struct/node_experience.rb +1 -1
- data/lib/nis/struct/request_announce.rb +1 -1
- data/lib/nis/struct/request_prepare_announce.rb +1 -1
- data/lib/nis/struct/time_synchronization_result.rb +1 -1
- data/lib/nis/struct/transaction.rb +4 -4
- data/lib/nis/struct/transaction_meta_data.rb +1 -1
- data/lib/nis/struct/transaction_meta_data_pair.rb +1 -1
- data/lib/nis/struct/unconfirmed_transaction_meta_data.rb +1 -1
- data/lib/nis/struct/unconfirmed_transaction_meta_data_pair.rb +1 -1
- data/lib/nis/transaction/importance_transfer.rb +1 -1
- data/lib/nis/transaction/mosaic_definition_creation.rb +1 -1
- data/lib/nis/transaction/mosaic_supply_change.rb +1 -1
- data/lib/nis/transaction/multisig.rb +1 -1
- data/lib/nis/transaction/multisig_aggregate_modification.rb +1 -1
- data/lib/nis/transaction/multisig_signature.rb +1 -1
- data/lib/nis/transaction/provision_namespace.rb +1 -1
- data/lib/nis/transaction/transfer.rb +4 -4
- data/lib/nis/unit/address.rb +19 -0
- data/lib/nis/util/convert.rb +125 -0
- data/lib/nis/util/ed25519.rb +263 -0
- data/lib/nis/util/serializer.rb +273 -0
- data/lib/nis/version.rb +1 -1
- data/nis.gemspec +1 -1
- metadata +9 -5
@@ -0,0 +1,273 @@
|
|
1
|
+
module Nis::Util
|
2
|
+
module Serializer
|
3
|
+
# Serialize a transaction object
|
4
|
+
# @param [Hash] entity
|
5
|
+
# @return [Array]
|
6
|
+
def self.serialize_transaction(entity)
|
7
|
+
method = case entity[:type]
|
8
|
+
when 257 then method(:serialize_transfer)
|
9
|
+
when 4100 then method(:serialize_multisig_transfer)
|
10
|
+
else raise "Not implemented entity type: #{entity[:type]}"
|
11
|
+
end
|
12
|
+
method.call(entity)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.serialize_common(entity)
|
16
|
+
a = []
|
17
|
+
a += serialize_int(entity[:type])
|
18
|
+
a += serialize_int(entity[:version])
|
19
|
+
a += serialize_int(entity[:timeStamp])
|
20
|
+
|
21
|
+
temp = hex2ua(entity[:signer])
|
22
|
+
a += serialize_int(temp.size)
|
23
|
+
a += temp
|
24
|
+
|
25
|
+
a += serialize_long(entity[:fee].to_i)
|
26
|
+
a += serialize_int(entity[:deadline])
|
27
|
+
a
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.serialize_multisig_transfer(entity)
|
31
|
+
a = []
|
32
|
+
|
33
|
+
# Common transaction part
|
34
|
+
a += serialize_common(entity)
|
35
|
+
|
36
|
+
# Transfer transaction part
|
37
|
+
tx = serialize_transfer(entity[:otherTrans])
|
38
|
+
a += serialize_int(tx.size)
|
39
|
+
a += tx
|
40
|
+
a
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.serialize_transfer(entity)
|
44
|
+
a = []
|
45
|
+
|
46
|
+
# Common transaction part
|
47
|
+
a += serialize_common(entity)
|
48
|
+
|
49
|
+
# Transfer transaction part
|
50
|
+
temp = serialize_safe_string(entity[:recipient])
|
51
|
+
a += temp
|
52
|
+
|
53
|
+
a += serialize_long(entity[:amount])
|
54
|
+
|
55
|
+
temp = hex2ua(entity[:message][:payload])
|
56
|
+
if temp.size == 0
|
57
|
+
a += [0,0,0,0]
|
58
|
+
else
|
59
|
+
a += serialize_int(temp.size + 8)
|
60
|
+
a += serialize_int(entity[:message][:type])
|
61
|
+
a += serialize_int(temp.size)
|
62
|
+
a += temp
|
63
|
+
end
|
64
|
+
a
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
# Safe String - Each char is 8 bit
|
70
|
+
# @param [String] str
|
71
|
+
# @return [Array]
|
72
|
+
def self.serialize_safe_string(str)
|
73
|
+
return [].fill(255, 0, 4) if str.nil?
|
74
|
+
return [].fill(0, 0, 4) if str.empty?
|
75
|
+
[str.size, 0, 0, 0] + str.bytes
|
76
|
+
end
|
77
|
+
|
78
|
+
# @param [String] str
|
79
|
+
# @return [Array]
|
80
|
+
def self.serialize_bin_string(str)
|
81
|
+
return [].fill(255, 0, 4) if str.nil?
|
82
|
+
return [].fill(0, 0, 4) if str.empty?
|
83
|
+
chars = str.is_a?(String) ? str.chars : str
|
84
|
+
[chars.size, 0, 0, 0] + chars.map(&:to_i)
|
85
|
+
end
|
86
|
+
|
87
|
+
# @param [Integer] value
|
88
|
+
# @return [Array]
|
89
|
+
def self.serialize_int(value)
|
90
|
+
a = [].fill(0, 0, 4)
|
91
|
+
bin = sprintf('%032b', [value].pack('L').unpack('L')[0])
|
92
|
+
0.upto(bin.size / 8 - 1) do |i|
|
93
|
+
a[i] = 0xFF & (value >> 8 * i)
|
94
|
+
end
|
95
|
+
a
|
96
|
+
end
|
97
|
+
|
98
|
+
# @param [Integer] value
|
99
|
+
# @return [Array]
|
100
|
+
def self.serialize_long(value)
|
101
|
+
a = [].fill(0, 0, 8)
|
102
|
+
bin = sprintf('%040b', [value].pack('L').unpack('L')[0])
|
103
|
+
0.upto(bin.size / 8 - 1) do |i|
|
104
|
+
a[i] = 0xFF & (value >> 8 * i)
|
105
|
+
end
|
106
|
+
a
|
107
|
+
end
|
108
|
+
|
109
|
+
# @param [Nis::Struct::MosaicId] mosaic_id
|
110
|
+
# @return [Array]
|
111
|
+
def self.serialize_mosaic_id(mosaic_id)
|
112
|
+
serialized_namespace_id = serialize_safe_string(mosaic_id.namespace_id)
|
113
|
+
serialized_name = serialize_safe_string(mosaic_id.name)
|
114
|
+
[serialized_namespace_id.size + serialized_name.size, 0, 0, 0] +
|
115
|
+
serialized_namespace_id +
|
116
|
+
serialized_name
|
117
|
+
end
|
118
|
+
|
119
|
+
# @param [Nis::Struct::Mosaic] mosaic
|
120
|
+
# @return [Array]
|
121
|
+
def self.serialize_mosaic_and_quantity(mosaic)
|
122
|
+
a = [].fill(0, 0, 4)
|
123
|
+
serialized_mosaic_id = serialize_mosaic_id(mosaic.mosaic_id)
|
124
|
+
serialized_quantity = serialize_long(mosaic.quantity)
|
125
|
+
a[0] = serialized_mosaic_id.size + serialized_quantity.size
|
126
|
+
a += serialized_mosaic_id + serialized_quantity
|
127
|
+
end
|
128
|
+
|
129
|
+
# @param [Array <Nis::Struct::Mosaic>] entities
|
130
|
+
# @return [Array]
|
131
|
+
def self.serialize_mosaics(entities)
|
132
|
+
a = [].fill(0, 0, 4)
|
133
|
+
a[0] = entities.size
|
134
|
+
a + entities.map do |ent|
|
135
|
+
{ entity: ent,
|
136
|
+
value: [
|
137
|
+
ent.mosaic_id.namespace_id,
|
138
|
+
ent.mosaic_id.name,
|
139
|
+
ent.quantity,
|
140
|
+
].join(':') }
|
141
|
+
end.sort_by do |ent|
|
142
|
+
ent[:value]
|
143
|
+
end.map do |ent|
|
144
|
+
serialize_mosaic_and_quantity(ent[:entity])
|
145
|
+
end.flatten
|
146
|
+
end
|
147
|
+
|
148
|
+
# @param [Hash] entity
|
149
|
+
# @return [Array]
|
150
|
+
def self.serialize_property(entity)
|
151
|
+
a = [].fill(0, 0, 4)
|
152
|
+
serialized_name = serialize_safe_string(entity[:name]);
|
153
|
+
serialized_value = serialize_safe_string(entity[:value]);
|
154
|
+
a[0] = serialized_name.size + serialized_value.size
|
155
|
+
a + serialized_name + serialized_value
|
156
|
+
end
|
157
|
+
|
158
|
+
# @param [Array] entities
|
159
|
+
# @return [Array]
|
160
|
+
def self.serialize_properties(entities)
|
161
|
+
a = [].fill(0, 0, 4)
|
162
|
+
a[0] = entities.size
|
163
|
+
|
164
|
+
helper = {
|
165
|
+
'divisibility' => 1,
|
166
|
+
'initialSupply' => 2,
|
167
|
+
'supplyMutable' => 3,
|
168
|
+
'transferable' => 4
|
169
|
+
}
|
170
|
+
a + entities.sort_by { |ent| helper[ent[:name]] }
|
171
|
+
.map { |ent| serialize_property(ent) }.flatten
|
172
|
+
end
|
173
|
+
|
174
|
+
# @param [Hash] entity
|
175
|
+
# @return [Array]
|
176
|
+
def self.serialize_levy(entity)
|
177
|
+
return [].fill(0, 0, 4) if entity.nil?
|
178
|
+
a = [].fill(0, 0, 8)
|
179
|
+
a[0] = entity[:type]
|
180
|
+
temp = serialize_safe_string(entity[:recipient])
|
181
|
+
serialized_mosaic_id = serialize_mosaic_id(entity[:mosaic_id])
|
182
|
+
serialized_fee = serialize_long(entity[:fee])
|
183
|
+
a += temp + serialized_mosaic_id + serialized_fee
|
184
|
+
a[0] = 4 + temp.size + serialized_mosaic_id.size + 8;
|
185
|
+
a
|
186
|
+
end
|
187
|
+
|
188
|
+
# @param [Hash] entity
|
189
|
+
# @return [Array]
|
190
|
+
def self.serialize_mosaic_definition(entity)
|
191
|
+
a = [].fill(0, 0, 4)
|
192
|
+
temp = hex2ua(entity[:creator])
|
193
|
+
a[0] = temp.size
|
194
|
+
a += temp
|
195
|
+
a += serialize_mosaic_id(entity[:id])
|
196
|
+
utf8_to_bin = hex2ua(utf8_to_hex(entity[:description]));
|
197
|
+
a += serialize_bin_string(utf8_to_bin);
|
198
|
+
a += serialize_properties(entity[:properties]);
|
199
|
+
a += serialize_levy(entity[:levy]);
|
200
|
+
a
|
201
|
+
end
|
202
|
+
|
203
|
+
# @param [String] hex
|
204
|
+
# @return [Array]
|
205
|
+
# def self.hex2ua(hex)
|
206
|
+
# hex.scan(/../).map(&:hex)
|
207
|
+
# end
|
208
|
+
#
|
209
|
+
def self.bin2words(bin, bin_size)
|
210
|
+
# temp = [];
|
211
|
+
# for (let i = 0; i < ua_size; i += 4) {
|
212
|
+
# let x = ua[i] * 0x1000000 + (ua[i + 1] || 0) * 0x10000 + (ua[i + 2] || 0) * 0x100 + (ua[i + 3] || 0);
|
213
|
+
# temp.push((x > 0x7fffffff) ? x - 0x100000000 : x);
|
214
|
+
# }
|
215
|
+
# CryptoJS.lib.WordArray.create(temp, ua_size);
|
216
|
+
end
|
217
|
+
|
218
|
+
# @param [String] hex
|
219
|
+
# @return [Array]
|
220
|
+
def self.hex2ua(hex)
|
221
|
+
hex.scan(/../).map(&:hex)
|
222
|
+
end
|
223
|
+
|
224
|
+
# @param [String] hex
|
225
|
+
# @return [Array]
|
226
|
+
def self.hex2ua_reversed(hex)
|
227
|
+
hex2ua(hex).reverse
|
228
|
+
end
|
229
|
+
|
230
|
+
# @param [Array] bin
|
231
|
+
# @return [String]
|
232
|
+
def self.bin2hex(bin)
|
233
|
+
bin.map { |v| '%02x' % v }.join
|
234
|
+
end
|
235
|
+
|
236
|
+
# @param [String] str
|
237
|
+
# @return [String]
|
238
|
+
def self.utf8_to_hex(str)
|
239
|
+
rstr2utf8(str).each_byte.map { |b| b.to_s(16) }.join
|
240
|
+
end
|
241
|
+
|
242
|
+
# @param [String] str
|
243
|
+
# @return [String]
|
244
|
+
def self.rstr2utf8(str)
|
245
|
+
str.unpack('U*').map do |c|
|
246
|
+
case
|
247
|
+
when c < 128
|
248
|
+
c
|
249
|
+
when 128 < c && c < 2048
|
250
|
+
[((c >> 6) | 192), ((c & 63) | 128)]
|
251
|
+
else
|
252
|
+
[((c >> 12) | 224), (((c >> 6) & 63) | 128), ((c & 63) | 128)]
|
253
|
+
end
|
254
|
+
end.flatten.map(&:chr).join
|
255
|
+
end
|
256
|
+
|
257
|
+
def encode(sender_privkey, recipient_pubkey, message)
|
258
|
+
# sk = hex2ua(sender_privkey)
|
259
|
+
# pk = hex2ua(recipient_pubkey)
|
260
|
+
# let shared = new Uint8Array(32);
|
261
|
+
# let r = key_derive(shared, salt, sk, pk);
|
262
|
+
# let encKey = r;
|
263
|
+
# let encIv = {
|
264
|
+
# iv: convert.bin2words(iv, 16)
|
265
|
+
# };
|
266
|
+
# let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(convert.utf8ToHex(msg)), encKey, encIv);
|
267
|
+
#
|
268
|
+
salt = RbNaCl::Random.random_bytes(32)
|
269
|
+
iv = RbNaCl::Random.random_bytes(16)
|
270
|
+
bin2hex(salt) + bin2hex(iv) # + CryptoJS.enc.Hex.stringify(encrypted.ciphertext)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
data/lib/nis/version.rb
CHANGED
data/nis.gemspec
CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_development_dependency 'pry', '~> 0.10'
|
35
35
|
|
36
36
|
spec.add_dependency 'digest-sha3', '~> 1.1'
|
37
|
-
spec.add_dependency '
|
37
|
+
spec.add_dependency 'base32', '~> 0.3'
|
38
38
|
spec.add_dependency 'faraday', '~> 0.11'
|
39
39
|
spec.add_dependency 'faraday_middleware', '~> 0.11'
|
40
40
|
spec.add_dependency 'thor', '~> 0.19'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nis-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshiyuki Ieyama
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -123,19 +123,19 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '1.1'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
126
|
+
name: base32
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - "~>"
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: '
|
131
|
+
version: '0.3'
|
132
132
|
type: :runtime
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: '
|
138
|
+
version: '0.3'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: faraday
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -213,6 +213,7 @@ files:
|
|
213
213
|
- examples/transactions/multisig_signature.rb
|
214
214
|
- examples/transactions/provision_namespace.rb
|
215
215
|
- examples/transactions/transfer.rb
|
216
|
+
- examples/transactions/transfer_remote.rb
|
216
217
|
- lib/nis.rb
|
217
218
|
- lib/nis/client.rb
|
218
219
|
- lib/nis/endpoint.rb
|
@@ -338,6 +339,9 @@ files:
|
|
338
339
|
- lib/nis/unit/version.rb
|
339
340
|
- lib/nis/util.rb
|
340
341
|
- lib/nis/util/assignable.rb
|
342
|
+
- lib/nis/util/convert.rb
|
343
|
+
- lib/nis/util/ed25519.rb
|
344
|
+
- lib/nis/util/serializer.rb
|
341
345
|
- lib/nis/version.rb
|
342
346
|
- nis.gemspec
|
343
347
|
homepage: https://github.com/44uk/nis-ruby
|