vchain_client 1.0.23 → 1.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92c2df9b16d843b7be9c12ce8cc83f10b643e725
4
- data.tar.gz: c07b86f7d21307c62cc54ccca8975b62c11b0ae0
3
+ metadata.gz: 45eab3023d999ff109e6276094ad539f90b58cd5
4
+ data.tar.gz: 5ba7df0e979f407888885065f52c7b806407411f
5
5
  SHA512:
6
- metadata.gz: 9119d354ff15ad2a38ac4392b5d9a11238a204213c71a854b126b7c9bf7f04dfccab41655d03c8e26bfdbfcf026882f8003215f6f03976d8af0f039dcb99b83f
7
- data.tar.gz: 76e341ee3ca61841b8f270af8abdd331e15c42ce35cd0ba19f85db301c7677d7033bda848cc3c082a0b7269762f8794941743ece67ce886eb14888d8502137ad
6
+ metadata.gz: 0e15a53253f36814cd6c59f7b59109dfcc9f052a23678d686b88be16c6676a8cd4aba8f1537d007bb565febbfdd94b7caae15261be1d540656a7f7f353638feb
7
+ data.tar.gz: 1769eb0e95bfb74529ad4dfe569d3d8b7c91e34efeb492c24b79f193470a3db3ee05f50691343e39ad32bf3e48458234ef9b0730e1bb2db701adc172d66781ee
@@ -13,13 +13,13 @@ module VChainClient
13
13
 
14
14
  @@tx_cache = {}
15
15
 
16
- def initialize(server, port, rpc_username, rpc_password, config)
16
+ def initialize(server, port, rpc_username, rpc_password, adapter_config, app_config)
17
17
  @server = server
18
18
  @port = port
19
19
  @rpc_username = rpc_username
20
20
  @rpc_password = rpc_password
21
21
 
22
- @config = config
22
+ @config = app_config
23
23
 
24
24
  @log = Log4r::Logger["vchain_client"]
25
25
  end
@@ -2,12 +2,12 @@ module VChainClient
2
2
 
3
3
  class BlockchainAdapterFactory
4
4
 
5
- def self.getAdapter(type, config)
5
+ def self.getAdapter(type, config, app_config)
6
6
  if type == "bitcoind"
7
- return VChainClient::BitcoindBlockchainAdapter.new(config["server"], config["port"], config["rpc_username"], config["rpc_password"], config)
7
+ return VChainClient::BitcoindBlockchainAdapter.new(config["server"], config["port"], config["rpc_username"], config["rpc_password"], config, app_config)
8
8
 
9
9
  elsif type == "blockcypher"
10
- return VChainClient::BlockcypherBlockchainAdapter.new(config["api_token"], config)
10
+ return VChainClient::BlockcypherBlockchainAdapter.new(config["api_token"], config, app_config)
11
11
  end
12
12
 
13
13
  raise "No such adapter '#{type}'"
@@ -35,7 +35,7 @@ module VChainClient
35
35
 
36
36
  begin
37
37
 
38
- adapter = VChainClient::BlockchainAdapterFactory.getAdapter(adapter_config["type"], adapter_config)
38
+ adapter = VChainClient::BlockchainAdapterFactory.getAdapter(adapter_config["type"], adapter_config, @config)
39
39
 
40
40
  rescue => e
41
41
  if @log.error?
@@ -8,10 +8,10 @@ module VChainClient
8
8
 
9
9
  @api_token = nil
10
10
 
11
- def initialize(api_token, config)
11
+ def initialize(api_token, adapter_config, app_config)
12
12
  @api_token = api_token
13
13
 
14
- @config = config
14
+ @config = app_config
15
15
 
16
16
  @log = Log4r::Logger["vchain_client"]
17
17
  end
@@ -5,76 +5,203 @@ module VChainClient
5
5
  @config = nil
6
6
  @log = nil
7
7
 
8
+ @@priv_key = nil
9
+ @@ec_priv = nil
10
+
8
11
  def initialize(config)
9
12
  @config = config
10
13
 
11
14
  @log = Log4r::Logger["vchain_client"]
12
15
  end
13
16
 
14
- def signRequest(document, point_type, weight, timestamp)
17
+ def signBatchRequest(batch, timestamp)
15
18
  OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)
16
19
 
17
20
  priv_key_path = @config["private_key_location"]
18
21
 
19
22
  if @log.debug?
20
- @log.debug("[Signatures.signRequest] input:")
21
- @log.debug("-> point_type: "+ point_type)
22
- @log.debug("-> weight: "+ weight.to_s)
23
+ @log.debug("[Signatures.signBatchRequest] input:")
23
24
  @log.debug("-> timestamp: "+ timestamp.to_s)
24
25
  @log.debug("-> key path: #{priv_key_path}")
25
26
  @log.debug("-> input:")
26
- @log.debug(document)
27
+ @log.debug(batch)
27
28
  end
28
29
 
29
- priv_key = nil
30
+ if @@priv_key == nil
30
31
 
31
- begin
32
+ begin
32
33
 
33
- priv_key = File.read(priv_key_path)
34
+ @@priv_key = File.read(priv_key_path)
34
35
 
35
- rescue => e
36
+ rescue => e
37
+ if @log.error?
38
+ @log.error("[Signatures.signBatchRequest] File.read raised exception:")
39
+ @log.error("#{e.class}, #{e.message}")
40
+ @log.error("-> timestamp: "+ timestamp.to_s)
41
+ @log.error("--> priv_key_path: #{priv_key_path}")
42
+ @log.error("-> input:")
43
+ @log.error(batch)
44
+ end
45
+
46
+ raise e
47
+ end
48
+
49
+ if @log.debug?
50
+ @log.debug("[Signatures.signBatchRequest] priv key is loaded")
51
+ end
52
+
53
+ end
54
+
55
+ if @@priv_key == nil
36
56
  if @log.error?
37
- @log.error("[Signatures.signRequest] File.read raised exception:")
38
- @log.error("#{e.class}, #{e.message}")
39
- @log.error("-> point_type: "+ point_type)
40
- @log.error("-> weight: "+ weight.to_s)
57
+ @log.error("[Signatures.signBatchRequest] failed to load private key")
41
58
  @log.error("-> timestamp: "+ timestamp.to_s)
42
- @log.error("-> input:")
43
- @log.error(document)
44
59
  @log.error("--> priv_key_path: #{priv_key_path}")
60
+ @log.error("-> input:")
61
+ @log.error(batch)
62
+ end
63
+
64
+ return nil
65
+ end
66
+
67
+ if @@ec_priv == nil
68
+
69
+ begin
70
+
71
+ @@ec_priv = OpenSSL::PKey::EC.new(@@priv_key)
72
+
73
+ rescue => e
74
+ if @log.error?
75
+ @log.error("[Signatures.signBatchRequest] OpenSSL::PKey::EC.new raised exception:")
76
+ @log.error("#{e.class}, #{e.message}")
77
+ @log.error("-> timestamp: "+ timestamp.to_s)
78
+ @log.error("--> priv_key_path: #{priv_key_path}")
79
+ @log.error("-> input:")
80
+ @log.error(batch)
81
+ end
82
+
83
+ raise e
84
+ end
85
+
86
+ if @log.debug?
87
+ @log.debug("[Signatures.signBatchRequest] key initialized")
45
88
  end
46
89
 
47
- raise e
48
90
  end
49
91
 
50
- if priv_key == nil
92
+ if @@ec_priv == nil
51
93
  if @log.error?
52
- @log.error("[Signatures.signRequest] failed to load private key")
53
- @log.error("-> point_type: "+ point_type)
54
- @log.error("-> weight: "+ weight.to_s)
94
+ @log.error("[Signatures.signBatchRequest] failed init EC key")
55
95
  @log.error("-> timestamp: "+ timestamp.to_s)
56
- @log.error("-> input:")
57
- @log.error(document)
58
96
  @log.error("--> priv_key_path: #{priv_key_path}")
97
+ @log.error("-> input:")
98
+ @log.error(batch)
59
99
  end
60
100
 
61
101
  return nil
62
102
  end
63
103
 
104
+ digest = OpenSSL::Digest::SHA256.new
105
+
106
+ whole_sign = ""
107
+
108
+ batch.each { |batch_element|
109
+ whole_sign += batch_element["data"].to_json
110
+ whole_sign += batch_element["point_type"]
111
+ whole_sign += batch_element["weight"].to_s
112
+ whole_sign += timestamp.to_s
113
+ }
114
+
64
115
  if @log.debug?
65
- @log.debug("[Signatures.signRequest] priv key is loaded")
116
+ @log.debug("[Signatures.signBatchRequest] whole_to_sign: "+ whole_sign)
66
117
  end
67
118
 
68
- ec = nil
119
+ whole_signature = nil
69
120
 
70
121
  begin
71
122
 
72
- ec = OpenSSL::PKey::EC.new(priv_key)
123
+ whole_signature = @@ec_priv.sign(digest, whole_sign)
73
124
 
74
125
  rescue => e
75
126
  if @log.error?
76
- @log.error("[Signatures.signRequest] OpenSSL::PKey::EC.new raised exception:")
127
+ @log.error("[Signatures.signBatchRequest] ec.sign raised exception:")
77
128
  @log.error("#{e.class}, #{e.message}")
129
+ @log.error("-> timestamp: "+ timestamp.to_s)
130
+ @log.error("--> priv_key_path: #{priv_key_path}")
131
+ @log.error("--> whole_sign: #{whole_sign}")
132
+ @log.error("-> input:")
133
+ @log.error(batch)
134
+ end
135
+
136
+ raise e
137
+ end
138
+
139
+ if whole_signature == nil
140
+ if @log.error?
141
+ @log.error("[Signatures.signBatchRequest] failed to sign")
142
+ @log.error("-> timestamp: "+ timestamp.to_s)
143
+ @log.error("--> priv_key_path: #{priv_key_path}")
144
+ @log.error("--> whole_sign: #{whole_sign}")
145
+ @log.error("-> input:")
146
+ @log.error(batch)
147
+ end
148
+
149
+ return nil
150
+ end
151
+
152
+ if @log.debug?
153
+ @log.debug("[Signatures.signBatchRequest] whole_signature raw: "+ Base64.encode64(whole_signature))
154
+ end
155
+
156
+ return Base64.encode64(whole_signature).gsub(/\n/, "")
157
+ end
158
+
159
+ def signRequest(document, point_type, weight, timestamp)
160
+
161
+ OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)
162
+
163
+ priv_key_path = @config["private_key_location"]
164
+
165
+ if @log.debug?
166
+ @log.debug("[Signatures.signRequest] input:")
167
+ @log.debug("-> point_type: "+ point_type)
168
+ @log.debug("-> weight: "+ weight.to_s)
169
+ @log.debug("-> timestamp: "+ timestamp.to_s)
170
+ @log.debug("-> key path: #{priv_key_path}")
171
+ @log.debug("-> input:")
172
+ @log.debug(document)
173
+ end
174
+
175
+ if @@priv_key == nil
176
+
177
+ begin
178
+
179
+ @@priv_key = File.read(priv_key_path)
180
+
181
+ rescue => e
182
+ if @log.error?
183
+ @log.error("[Signatures.signRequest] File.read raised exception:")
184
+ @log.error("#{e.class}, #{e.message}")
185
+ @log.error("-> point_type: "+ point_type)
186
+ @log.error("-> weight: "+ weight.to_s)
187
+ @log.error("-> timestamp: "+ timestamp.to_s)
188
+ @log.error("-> input:")
189
+ @log.error(document)
190
+ @log.error("--> priv_key_path: #{priv_key_path}")
191
+ end
192
+
193
+ raise e
194
+ end
195
+
196
+ if @log.debug?
197
+ @log.debug("[Signatures.signRequest] priv key is loaded")
198
+ end
199
+
200
+ end
201
+
202
+ if @@priv_key == nil
203
+ if @log.error?
204
+ @log.error("[Signatures.signRequest] failed to load private key")
78
205
  @log.error("-> point_type: "+ point_type)
79
206
  @log.error("-> weight: "+ weight.to_s)
80
207
  @log.error("-> timestamp: "+ timestamp.to_s)
@@ -83,10 +210,37 @@ module VChainClient
83
210
  @log.error("--> priv_key_path: #{priv_key_path}")
84
211
  end
85
212
 
86
- raise e
213
+ return nil
87
214
  end
88
215
 
89
- if ec == nil
216
+ if @@ec_priv == nil
217
+
218
+ begin
219
+
220
+ @@ec_priv = OpenSSL::PKey::EC.new(@@priv_key)
221
+
222
+ rescue => e
223
+ if @log.error?
224
+ @log.error("[Signatures.signRequest] OpenSSL::PKey::EC.new raised exception:")
225
+ @log.error("#{e.class}, #{e.message}")
226
+ @log.error("-> point_type: "+ point_type)
227
+ @log.error("-> weight: "+ weight.to_s)
228
+ @log.error("-> timestamp: "+ timestamp.to_s)
229
+ @log.error("-> input:")
230
+ @log.error(document)
231
+ @log.error("--> priv_key_path: #{priv_key_path}")
232
+ end
233
+
234
+ raise e
235
+ end
236
+
237
+ if @log.debug?
238
+ @log.debug("[Signatures.signRequest] key initialized")
239
+ end
240
+
241
+ end
242
+
243
+ if @@ec_priv == nil
90
244
  if @log.error?
91
245
  @log.error("[Signatures.signRequest] failed init EC key")
92
246
  @log.error("-> point_type: "+ point_type)
@@ -100,10 +254,6 @@ module VChainClient
100
254
  return nil
101
255
  end
102
256
 
103
- if @log.debug?
104
- @log.debug("[Signatures.signRequest] key initialized")
105
- end
106
-
107
257
  digest = OpenSSL::Digest::SHA256.new
108
258
 
109
259
  whole_sign = document.to_json + point_type + weight.to_s + timestamp.to_s
@@ -116,7 +266,7 @@ module VChainClient
116
266
 
117
267
  begin
118
268
 
119
- whole_signature = ec.sign(digest, whole_sign)
269
+ whole_signature = @@ec_priv.sign(digest, whole_sign)
120
270
 
121
271
  rescue => e
122
272
  if @log.error?
@@ -235,16 +385,39 @@ module VChainClient
235
385
  @log.debug(data)
236
386
  end
237
387
 
238
- priv_key = nil
388
+ if @@priv_key == nil
389
+
390
+ begin
391
+
392
+ @@priv_key = File.read(priv_key_path)
393
+
394
+ rescue => e
395
+ if @log.error?
396
+ @log.error("[Signatures.signDataPoint] File.read raised exception:")
397
+ @log.error("#{e.class}, #{e.message}")
398
+ @log.error("-> this_client_id: #{this_client_id}")
399
+ @log.error("-> doc_hash: #{doc_hash}")
400
+ @log.error("-> credentials_hash: #{credentials_hash}")
401
+ @log.error("-> weight: "+ weight.to_s)
402
+ @log.error("-> timestamp: "+ timestamp.to_s)
403
+ @log.error("-> point_type: #{point_type}")
404
+ @log.error("-> key path: #{priv_key_path}")
405
+ @log.error("-> data:")
406
+ @log.error(data)
407
+ end
239
408
 
240
- begin
409
+ raise e
410
+ end
241
411
 
242
- priv_key = File.read(priv_key_path)
412
+ if @log.debug?
413
+ @log.debug("[Signatures.signDataPoint] priv key loaded")
414
+ end
243
415
 
244
- rescue => e
416
+ end
417
+
418
+ if @@priv_key == nil
245
419
  if @log.error?
246
- @log.error("[Signatures.signDataPoint] File.read raised exception:")
247
- @log.error("#{e.class}, #{e.message}")
420
+ @log.error("[Signatures.signDataPoint] failed to load private key")
248
421
  @log.error("-> this_client_id: #{this_client_id}")
249
422
  @log.error("-> doc_hash: #{doc_hash}")
250
423
  @log.error("-> credentials_hash: #{credentials_hash}")
@@ -254,15 +427,45 @@ module VChainClient
254
427
  @log.error("-> key path: #{priv_key_path}")
255
428
  @log.error("-> data:")
256
429
  @log.error(data)
257
- @log.error("--> priv_key_path: #{priv_key_path}")
258
430
  end
259
431
 
260
- raise e
432
+ return nil
261
433
  end
262
434
 
263
- if priv_key == nil
435
+ if @@ec_priv == nil
436
+
437
+ begin
438
+
439
+ @@ec_priv = OpenSSL::PKey::EC.new(@@priv_key)
440
+
441
+ rescue => e
442
+ if @log.error?
443
+ @log.error("[Signatures.signDataPoint] OpenSSL::PKey::EC.new raised exception:")
444
+ @log.error("#{e.class}, #{e.message}")
445
+ @log.error("-> this_client_id: #{this_client_id}")
446
+ @log.error("-> doc_hash: #{doc_hash}")
447
+ @log.error("-> credentials_hash: #{credentials_hash}")
448
+ @log.error("-> weight: "+ weight.to_s)
449
+ @log.error("-> timestamp: "+ timestamp.to_s)
450
+ @log.error("-> point_type: #{point_type}")
451
+ @log.error("-> key path: #{priv_key_path}")
452
+ @log.error("-> data:")
453
+ @log.error(data)
454
+ @log.error("--> priv_key_path: #{priv_key_path}")
455
+ end
456
+
457
+ raise e
458
+ end
459
+
460
+ if @log.debug?
461
+ @log.debug("[Signatures.signDataPoint] key created")
462
+ end
463
+
464
+ end
465
+
466
+ if @@ec_priv == nil
264
467
  if @log.error?
265
- @log.error("[Signatures.signDataPoint] failed to load private key")
468
+ @log.error("[Signatures.signDataPoint] failed init EC key")
266
469
  @log.error("-> this_client_id: #{this_client_id}")
267
470
  @log.error("-> doc_hash: #{doc_hash}")
268
471
  @log.error("-> credentials_hash: #{credentials_hash}")
@@ -278,9 +481,7 @@ module VChainClient
278
481
  return nil
279
482
  end
280
483
 
281
- if @log.debug?
282
- @log.debug("[Signatures.signDataPoint] priv key loaded")
283
- end
484
+ digest = OpenSSL::Digest::SHA256.new
284
485
 
285
486
  output = {}
286
487
 
@@ -313,60 +514,11 @@ module VChainClient
313
514
  @log.debug("[Signatures.signDataPoint] value_hash: #{value_hash}")
314
515
  end
315
516
 
316
- ec = nil
317
-
318
- begin
319
-
320
- ec = OpenSSL::PKey::EC.new(priv_key)
321
-
322
- rescue => e
323
- if @log.error?
324
- @log.error("[Signatures.signDataPoint] OpenSSL::PKey::EC.new raised exception:")
325
- @log.error("#{e.class}, #{e.message}")
326
- @log.error("-> this_client_id: #{this_client_id}")
327
- @log.error("-> doc_hash: #{doc_hash}")
328
- @log.error("-> credentials_hash: #{credentials_hash}")
329
- @log.error("-> weight: "+ weight.to_s)
330
- @log.error("-> timestamp: "+ timestamp.to_s)
331
- @log.error("-> point_type: #{point_type}")
332
- @log.error("-> key path: #{priv_key_path}")
333
- @log.error("-> data:")
334
- @log.error(data)
335
- @log.error("--> priv_key_path: #{priv_key_path}")
336
- end
337
-
338
- raise e
339
- end
340
-
341
- if ec == nil
342
- if @log.error?
343
- @log.error("[Signatures.signDataPoint] failed init EC key")
344
- @log.error("-> this_client_id: #{this_client_id}")
345
- @log.error("-> doc_hash: #{doc_hash}")
346
- @log.error("-> credentials_hash: #{credentials_hash}")
347
- @log.error("-> weight: "+ weight.to_s)
348
- @log.error("-> timestamp: "+ timestamp.to_s)
349
- @log.error("-> point_type: #{point_type}")
350
- @log.error("-> key path: #{priv_key_path}")
351
- @log.error("-> data:")
352
- @log.error(data)
353
- @log.error("--> priv_key_path: #{priv_key_path}")
354
- end
355
-
356
- return nil
357
- end
358
-
359
- if @log.debug?
360
- @log.debug("[Signatures.signDataPoint] key created")
361
- end
362
-
363
- digest = OpenSSL::Digest::SHA256.new
364
-
365
517
  signature = nil
366
518
 
367
519
  begin
368
520
 
369
- signature = ec.sign(digest, what_to_sign)
521
+ signature = @@ec_priv.sign(digest, what_to_sign)
370
522
 
371
523
  rescue => e
372
524
  if @log.error?
data/lib/vchain_client.rb CHANGED
@@ -24,7 +24,7 @@ module VChainClient
24
24
 
25
25
  DATA_POINT_VERSION = "1"
26
26
 
27
- CLIENT_LIB_VERSION = "1.0.21"
27
+ CLIENT_LIB_VERSION = "1.0.24"
28
28
 
29
29
  @config = nil
30
30
  @log = nil
@@ -221,6 +221,286 @@ module VChainClient
221
221
  return previous_parent
222
222
  end
223
223
 
224
+ def generate_batch_data_points(input_arr)
225
+ output = []
226
+
227
+ input_arr.each { |raw_document|
228
+
229
+ if !raw_document.key?("point_type")
230
+ if @log.error?
231
+ @log.error("CLIENT LIB VERSION: "+ CLIENT_LIB_VERSION)
232
+ @log.error("[generate_batch_data_points] point_type not set for element in input array")
233
+ end
234
+
235
+ return false
236
+ end
237
+ point_type = raw_document["point_type"]
238
+ raw_document.delete("point_type")
239
+
240
+ weight = 1
241
+ if raw_document.key?("weight")
242
+ weight = raw_document["weight"]
243
+ raw_document.delete("weight")
244
+ end
245
+
246
+ surnames_arr = raw_document["surname"].split
247
+ given_names_arr = raw_document["given_names"].split
248
+
249
+ hashed_surname = ""
250
+ sep = ""
251
+ surnames_arr.each { |surname_part|
252
+ hashed_surname += sep
253
+ hashed_surname += Digest::SHA512.hexdigest(surname_part.downcase)
254
+ sep = " "
255
+ }
256
+
257
+ hashed_given_names = ""
258
+ sep = ""
259
+ given_names_arr.each { |given_names_part|
260
+ hashed_given_names += sep
261
+ hashed_given_names += Digest::SHA512.hexdigest(given_names_part.downcase)
262
+ sep = " "
263
+ }
264
+
265
+ raw_document["given_names"] = hashed_given_names
266
+ raw_document["surname"] = hashed_surname
267
+
268
+ out_document = self.hash(raw_document)
269
+
270
+ out_document = self.cut(out_document)
271
+
272
+ doc_hash = self.get_doc_hash(out_document)
273
+ out_document["doc_hash"] = doc_hash
274
+
275
+ credentials_hash = self.get_credentials_hash(out_document)
276
+ out_document["credentials_hash"] = credentials_hash
277
+
278
+ if weight > 1
279
+ weight = 1
280
+ elsif weight < 0
281
+ weight = 0
282
+ end
283
+
284
+ out_document["weight"] = weight
285
+ out_document["point_type"] = point_type
286
+
287
+ output.push(out_document)
288
+ }
289
+
290
+ return output
291
+ end
292
+
293
+ def add_batch_data_points(input_arr)
294
+
295
+ signaturesHelper = VChainClient::Signatures.new(@config)
296
+
297
+ time = Time.now.getutc
298
+ timestamp = time.to_i
299
+
300
+ batch = []
301
+
302
+ index = 0
303
+ input_arr.each { |hashed_document|
304
+
305
+ if !hashed_document.key?("point_type")
306
+ if @log.error?
307
+ @log.error("CLIENT LIB VERSION: "+ CLIENT_LIB_VERSION)
308
+ @log.error("[add_batch_data_points] point_type not set for "+ index +" element in input array")
309
+ end
310
+
311
+ return false
312
+ end
313
+
314
+ if !hashed_document.key?("doc_hash")
315
+ if @log.error?
316
+ @log.error("CLIENT LIB VERSION: "+ CLIENT_LIB_VERSION)
317
+ @log.error("[add_batch_data_points] doc_hash not set for "+ index +" element in input array")
318
+ end
319
+
320
+ return false
321
+ end
322
+
323
+ if !hashed_document.key?("credentials_hash")
324
+ if @log.error?
325
+ @log.error("CLIENT LIB VERSION: "+ CLIENT_LIB_VERSION)
326
+ @log.error("[add_batch_data_points] credentials_hash not set for "+ index +" element in input array")
327
+ end
328
+
329
+ return false
330
+ end
331
+
332
+ point_type = hashed_document["point_type"]
333
+ hashed_document.delete("point_type")
334
+
335
+ doc_hash = hashed_document["doc_hash"]
336
+ hashed_document.delete("doc_hash")
337
+
338
+ credentials_hash = hashed_document["credentials_hash"]
339
+ hashed_document.delete("credentials_hash")
340
+
341
+ weight = 1
342
+ if hashed_document.key?("weight")
343
+ weight = hashed_document["weight"]
344
+ end
345
+ hashed_document.delete("weight")
346
+
347
+ if weight > 1
348
+ weight = 1
349
+ elsif weight < 0
350
+ weight = 0
351
+ end
352
+
353
+ point_signatures = nil
354
+
355
+ begin
356
+
357
+ point_signatures = signaturesHelper.signDataPoint(point_type, hashed_document, doc_hash, credentials_hash, weight, timestamp)
358
+
359
+ rescue => e
360
+ if @log.error?
361
+ @log.error("[add_batch_data_points] Signatures.signDataPoint raised exception")
362
+ @log.error("#{e.class}, #{e.message}")
363
+ @log.error("-> point_type: "+ point_type)
364
+ @log.error("-> weight: "+ weight.to_s)
365
+ @log.error("-> timestamp: "+ timestamp.to_s)
366
+ @log.error("-> document:")
367
+ @log.error(hashed_document)
368
+ end
369
+
370
+ raise e
371
+ end
372
+
373
+ if point_signatures == nil
374
+ if @log.error?
375
+ @log.error("[add_batch_data_points] failed to Signatures.signDataPoint")
376
+ @log.error("-> point_type: "+ point_type)
377
+ @log.error("-> weight: "+ weight.to_s)
378
+ @log.error("-> timestamp: "+ timestamp.to_s)
379
+ @log.error("-> document:")
380
+ @log.error(hashed_document)
381
+ end
382
+
383
+ return false
384
+ end
385
+
386
+ if @log.debug?
387
+ @log.debug("[add_batch_data_points] point_signatures:")
388
+ @log.debug(point_signatures)
389
+ end
390
+
391
+ batch_element = {
392
+ "data" => Hash[hashed_document.sort],
393
+ "point_type" => point_type,
394
+ "weight" => weight,
395
+ "signatures" => point_signatures
396
+ }
397
+
398
+ batch.push(batch_element)
399
+
400
+ index += 1
401
+ }
402
+
403
+ client_id = @config["client_id"]
404
+ api_url = @config["api"]["url"] + "v0.2/batchAddDataPoint/"
405
+
406
+ whole_signature = nil
407
+
408
+ begin
409
+
410
+ whole_signature = signaturesHelper.signBatchRequest(batch, timestamp)
411
+
412
+ rescue => e
413
+
414
+ if @log.error?
415
+ @log.error("[add_batch_data_points] Signatures.signRequest raised exception:")
416
+ @log.error("#{e.class}: #{e.message}")
417
+ end
418
+
419
+ raise e
420
+
421
+ else
422
+
423
+ if whole_signature == nil
424
+
425
+ if @log.error?
426
+ @log.error("[add_batch_data_points] failed to sign request")
427
+ end
428
+
429
+ return false
430
+ end
431
+
432
+ if @log.debug?
433
+ @log.debug("[add_batch_data_points] signaturesHelper.signRequest went well, whole_signature:")
434
+ @log.debug(whole_signature)
435
+ end
436
+
437
+ send_data = {}
438
+ send_data["client_id"] = client_id
439
+ send_data["signature"] = whole_signature
440
+ send_data["timestamp"] = timestamp.to_s
441
+
442
+ send_data["data"] = []
443
+
444
+ batch.each { |doc|
445
+ send_doc = {}
446
+
447
+ send_doc["data"] = doc["data"]
448
+ send_doc["weight"] = doc["weight"].to_s
449
+ send_doc["type"] = doc["point_type"]
450
+ send_doc["point_signatures"] = doc["signatures"]
451
+
452
+ send_data["data"].push(send_doc)
453
+ }
454
+
455
+ if @log.debug?
456
+ @log.debug("[add_batch_data_points] send_data:")
457
+ @log.debug(send_data)
458
+ end
459
+
460
+ begin
461
+ req = RestClient.post(api_url,
462
+ send_data.to_json,
463
+ {'Content-Type' => 'application/json'})
464
+
465
+ if req.code != 200
466
+
467
+ if @log.error?
468
+ @log.error("[add_batch_data_points] response with code "+ req.code.to_s)
469
+ @log.error("-> client_id: #{client_id}")
470
+ @log.error("-> api_url: #{api_url}")
471
+ @log.error("-> timestamp: "+ timestamp.to_s)
472
+ @log.error("-> send_data:")
473
+ @log.error(send_data)
474
+ end
475
+
476
+ return false
477
+ end
478
+
479
+ if @log.debug?
480
+ @log.debug("[add_batch_data_points] response code is 200:")
481
+ @log.debug(req.body)
482
+ end
483
+
484
+ return true
485
+
486
+ rescue => e
487
+ if @log.error?
488
+ @log.error("[add_batch_data_points] RestClient.post raised exception")
489
+ @log.error("#{e.class}, #{e.message}")
490
+ @log.error("-> client_id: #{client_id}")
491
+ @log.error("-> api_url: #{api_url}")
492
+ @log.error("-> timestamp: "+ timestamp.to_s)
493
+ @log.error("-> send_data:")
494
+ @log.error(send_data)
495
+ end
496
+
497
+ raise e
498
+ end
499
+
500
+ end
501
+
502
+ end
503
+
224
504
  def add_data_point(point_type, input, weight = 1)
225
505
 
226
506
  client_id = @config["client_id"]
@@ -439,7 +719,49 @@ module VChainClient
439
719
  end
440
720
  end
441
721
 
442
- def check(input, is_already_hashed=false)
722
+ def generate_batch_check(input_arr)
723
+
724
+ if !input_arr.kind_of?(Array)
725
+ if @log.error?
726
+ @log.error("[generate_batch_check] input should be array")
727
+ @log.error("-> input_arr:")
728
+ @log.error(input_arr)
729
+ end
730
+
731
+ return false
732
+ end
733
+
734
+ output = []
735
+
736
+ input_arr.each { |raw_document|
737
+
738
+ names = {}
739
+ if raw_document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT
740
+ names = raw_document["names"]
741
+ raw_document.delete("names")
742
+ end
743
+
744
+ out_document = self.hash(raw_document)
745
+
746
+ out_document = self.cut(out_document)
747
+
748
+ if out_document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT_HASHED
749
+ out_document["names"] = []
750
+
751
+ names.each { |name|
752
+ name_hash = Digest::SHA512.hexdigest(name.downcase)
753
+
754
+ out_document["names"].push(name_hash)
755
+ }
756
+ end
757
+
758
+ output.push(out_document)
759
+ }
760
+
761
+ return output
762
+ end
763
+
764
+ def check(input, is_already_hashed = false)
443
765
 
444
766
  client_id = @config["client_id"]
445
767
 
@@ -448,7 +770,7 @@ module VChainClient
448
770
  document = input
449
771
 
450
772
  names = {}
451
- if document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT
773
+ if (!is_already_hashed && document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT) || (is_already_hashed && document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT_HASHED)
452
774
  names = document["names"]
453
775
  document.delete("names")
454
776
  end
@@ -463,8 +785,14 @@ module VChainClient
463
785
 
464
786
  if document["type"] == FIELD_TYPE_TRAVEL_DOCUMENT_HASHED
465
787
  document["names"] = []
788
+
466
789
  names.each { |name|
467
- name_hash = Digest::SHA512.hexdigest(name.downcase)
790
+ name_hash = name
791
+
792
+ if (!is_already_hashed)
793
+ name_hash = Digest::SHA512.hexdigest(name.downcase)
794
+ end
795
+
468
796
  names_index[name_hash] = name
469
797
  document["names"].push(name_hash)
470
798
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vchain_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.23
4
+ version: 1.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksandr Gorelik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-01 00:00:00.000000000 Z
11
+ date: 2017-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log4r