iota-ruby 1.1.5 → 1.1.6

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
2
  SHA1:
3
- metadata.gz: 7213d4251e2127c9eb86cc3e6962357f9b174a9d
4
- data.tar.gz: 8de076c67c080a09864226c81cb8c9f37d706ba8
3
+ metadata.gz: 001faa9697735af3ccb7dc1b9e853808a09d9ebc
4
+ data.tar.gz: 46f4cb83b7559dad8265ec8b3b4e080467c180db
5
5
  SHA512:
6
- metadata.gz: 59003e99d4eeb6e956e4a6675ebaa1081e8d357dd4f74e1f7823765095c28dcab425ac2e2068826c6f62a9b4219052c9cf400a9be568b44b76430299a83ea72f
7
- data.tar.gz: 88d1cbdeb5958ebf3247e5c971a6d5ca80a24d2a281e49ce30c720cba4cacef9d0b9275bf1771a4fbd436a657d3abc7fdd2f76e7c9452ed6160acba0623a52b5
6
+ metadata.gz: f9057f008b0a7647b49ff7d9a3bd11e5c719f42f39922d4ae819ce214377fd2ff1d3b7f06e63115a6b01b5723966d3f1f4e4e250cd929e5a351d078228593df1
7
+ data.tar.gz: a6209e6d04d1295017d4e738cd8770e0e18036cdf45be69ffc6155227655685fa815f6b4dd60bee1ff1418d881da17f738a35eda07ca58493df2b8c9ae9dc7d1
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## [1.1.6] - 2018-07-22
2
+ ### Added
3
+ - `wasAddressSpentFrom` API
4
+ - Inbuilt batching for following apis: `getTrytes`, `getBalances`, `wereAddressesSpentFrom`, `getInclusionStates` & `findTransactions`
5
+ - adding changelog file to keep track of future changes
6
+
7
+ ### Changed
8
+ - Only returning balance array as reponse for `getBalances` api call (Major)
data/README.md CHANGED
@@ -9,7 +9,7 @@ This is the **unofficial** Ruby gem for the IOTA Core. It implements both the [o
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'iota-ruby', '~> 1.1.2'
12
+ gem 'iota-ruby'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -87,4 +87,4 @@ else
87
87
  end
88
88
  ```
89
89
 
90
- If you'd like to support development, please consider donating to my IOTA address: **LOBYBAFIKJFEGTLCCQSHOPX9ROIRAI99QSTD9BRALHMHUOISF9DIZJYICXAIQMJCPZYTXMRMPOWG9WOICGMMNHQIH9**
90
+ If you'd like to support development, please consider donating to my IOTA address: **SGBYR9DXPBPGTFLILQWAYIMINZ9MQAFKGDBHQSXGLKNIHCQZEDYPVNRMDVWFPXQTAIVKWCVWXVXZVZ9R9HIKTZWUAC**
data/lib/iota.rb CHANGED
@@ -8,6 +8,7 @@ require "iota/utils/broker"
8
8
 
9
9
  require "iota/api/commands"
10
10
  require "iota/api/wrappers"
11
+ require "iota/api/transport"
11
12
  require "iota/api/api"
12
13
 
13
14
  require "iota/crypto/curl"
@@ -31,7 +32,7 @@ require "iota/models/account"
31
32
 
32
33
  module IOTA
33
34
  class Client
34
- attr_reader :version, :host, :port, :provider, :sandbox, :token, :broker, :api, :utils, :validator, :multisig
35
+ attr_reader :version, :host, :port, :provider, :sandbox, :token, :broker, :api, :utils, :validator, :multisig, :batch_size
35
36
 
36
37
  def initialize(settings = {})
37
38
  setSettings(settings)
@@ -56,6 +57,7 @@ module IOTA
56
57
  @sandbox = settings[:sandbox] || false
57
58
  @token = settings[:token] || false
58
59
  @timeout = settings[:timeout] || 120
60
+ @batch_size = settings[:batch_size] || 500
59
61
 
60
62
  if @sandbox
61
63
  @sandbox = @provider.gsub(/\/$/, '')
@@ -63,7 +65,7 @@ module IOTA
63
65
  end
64
66
 
65
67
  @broker = IOTA::Utils::Broker.new(@provider, @token, @timeout)
66
- @api = IOTA::API::Api.new(@broker, @sandbox)
68
+ @api = IOTA::API::Api.new(@broker, @sandbox, @batch_size)
67
69
  end
68
70
 
69
71
  def symbolize_keys(hash)
data/lib/iota/api/api.rb CHANGED
@@ -2,17 +2,15 @@ module IOTA
2
2
  module API
3
3
  class Api
4
4
  include Wrappers
5
+ include Transport
5
6
 
6
- def initialize(broker, sandbox)
7
+ def initialize(broker, sandbox, batch_size = 500)
7
8
  @broker = broker
8
9
  @sandbox = sandbox
9
10
  @commands = Commands.new
10
11
  @utils = IOTA::Utils::Utils.new
11
12
  @validator = @utils.validator
12
- end
13
-
14
- def sendCommand(command, &callback)
15
- @broker.send(command, &callback)
13
+ @batch_size = batch_size
16
14
  end
17
15
 
18
16
  def findTransactions(searchValues, &callback)
@@ -23,7 +21,8 @@ module IOTA
23
21
  searchKeys = searchValues.keys
24
22
  validKeys = ['bundles', 'addresses', 'tags', 'approvees']
25
23
 
26
- error = false;
24
+ error = false
25
+ entry_count = 0
27
26
 
28
27
  searchKeys.each do |key|
29
28
  if !validKeys.include?(key.to_s)
@@ -32,6 +31,7 @@ module IOTA
32
31
  end
33
32
 
34
33
  hashes = searchValues[key]
34
+ entry_count += hashes.count
35
35
 
36
36
  if key.to_s == 'addresses'
37
37
  searchValues[key] = hashes.map do |address|
@@ -66,18 +66,21 @@ module IOTA
66
66
  if error
67
67
  return sendData(false, error, &callback)
68
68
  else
69
- sendCommand(@commands.findTransactions(searchValues), &callback)
69
+ if entry_count <= @batch_size || searchKeys.count > 1
70
+ return sendCommand(@commands.findTransactions(searchValues), &callback)
71
+ else
72
+ return sendBatchedCommand(@commands.findTransactions(searchValues), &callback)
73
+ end
70
74
  end
71
75
  end
72
76
 
73
77
  def getBalances(addresses, threshold, &callback)
74
- # Check if correct transaction hashes
75
78
  if !@validator.isArrayOfHashes(addresses)
76
79
  return sendData(false, "Invalid Trytes provided", &callback)
77
80
  end
78
81
 
79
82
  command = @commands.getBalances(addresses.map{|address| @utils.noChecksum(address)}, threshold)
80
- sendCommand(command, &callback)
83
+ sendBatchedCommand(command, &callback)
81
84
  end
82
85
 
83
86
  def getTrytes(hashes, &callback)
@@ -85,21 +88,15 @@ module IOTA
85
88
  return sendData(false, "Invalid Trytes provided", &callback)
86
89
  end
87
90
 
88
- sendCommand(@commands.getTrytes(hashes), &callback)
91
+ sendBatchedCommand(@commands.getTrytes(hashes), &callback)
89
92
  end
90
93
 
91
94
  def getInclusionStates(transactions, tips, &callback)
92
- # Check if correct transaction hashes
93
- if !@validator.isArrayOfHashes(transactions)
94
- return sendData(false, "Invalid Trytes provided", &callback)
95
- end
96
-
97
- # Check if correct tips
98
- if !@validator.isArrayOfHashes(tips)
95
+ if !@validator.isArrayOfHashes(transactions) || !@validator.isArrayOfHashes(tips)
99
96
  return sendData(false, "Invalid Trytes provided", &callback)
100
97
  end
101
98
 
102
- sendCommand(@commands.getInclusionStates(transactions, tips), &callback)
99
+ sendBatchedCommand(@commands.getInclusionStates(transactions, tips), &callback)
103
100
  end
104
101
 
105
102
  def getNodeInfo(&callback)
@@ -193,6 +190,15 @@ module IOTA
193
190
  sendCommand(@commands.checkConsistency(tails), &callback)
194
191
  end
195
192
 
193
+ def wereAddressesSpentFrom(addresses, &callback)
194
+ if !@validator.isArrayOfHashes(addresses)
195
+ return sendData(false, "Invalid Trytes provided", &callback)
196
+ end
197
+
198
+ command = @commands.wereAddressesSpentFrom(addresses.map{|address| @utils.noChecksum(address)})
199
+ sendBatchedCommand(command, &callback)
200
+ end
201
+
196
202
  private
197
203
  def sendData(status, data, &callback)
198
204
  callback ? callback.call(status, data) : [status, data]
@@ -6,7 +6,7 @@ module IOTA
6
6
  command: 'findTransactions'
7
7
  }
8
8
 
9
- searchValues.each { |k, v| command[k] = v }
9
+ searchValues.each { |k, v| command[k.to_sym] = v }
10
10
 
11
11
  command
12
12
  end
@@ -101,6 +101,13 @@ module IOTA
101
101
  tails: tails
102
102
  }
103
103
  end
104
+
105
+ def wereAddressesSpentFrom(addresses)
106
+ {
107
+ command: 'wereAddressesSpentFrom',
108
+ addresses: addresses
109
+ }
110
+ end
104
111
  end
105
112
  end
106
113
  end
@@ -0,0 +1,43 @@
1
+ module IOTA
2
+ module API
3
+ module Transport
4
+ def sendCommand(command, &callback)
5
+ @broker.send(command, &callback)
6
+ end
7
+
8
+ def sendBatchedCommand(command, &callback)
9
+ # Key and value both should be symbols
10
+ mapping = {
11
+ getTrytes: [ :hashes ],
12
+ getBalances: [ :addresses ],
13
+ wereAddressesSpentFrom: [ :addresses ],
14
+ getInclusionStates: [ :transactions ],
15
+ findTransactions: [ :bundles, :addresses, :tags, :approvees ]
16
+ }
17
+
18
+ command_name = command[:command].to_sym
19
+ if mapping[command_name]
20
+ results = []
21
+
22
+ keys_to_batch = mapping[command_name]
23
+ keys_to_batch.each do |key|
24
+ if command[key]
25
+ command[key].each_slice(@batch_size) do |group|
26
+ batchedCommand = command.clone.merge({ key => group })
27
+ sendCommand(batchedCommand) do |status, response|
28
+ if !status
29
+ return sendData(false, response, &callback)
30
+ end
31
+ results += response
32
+ end
33
+ end
34
+ end
35
+ end
36
+ return sendData(true, results, &callback)
37
+ else
38
+ return sendCommand(command, &callback)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -10,7 +10,7 @@ module IOTA
10
10
  ret_status = false
11
11
  ret_data = nil
12
12
  # get the trytes of the transaction hashes
13
- getTrytesInBatches(hashes) do |status, trytes|
13
+ getTrytes(hashes) do |status, trytes|
14
14
  ret_status = status
15
15
  if status
16
16
  transactionObjects = []
@@ -425,25 +425,6 @@ module IOTA
425
425
  end
426
426
  end
427
427
  end
428
-
429
- def getTrytesInBatches(hashes, batchSize = 500, &callback)
430
- if !@validator.isArrayOfHashes(hashes)
431
- return sendData(false, "Invalid Trytes provided", &callback)
432
- end
433
-
434
- data = []
435
-
436
- hashes.each_slice(batchSize) do |group|
437
- getTrytes(group) do |status, trytes|
438
- if !status
439
- return sendData(false, trytes, &callback)
440
- end
441
- data += trytes
442
- end
443
- end
444
-
445
- sendData(true, data, &callback)
446
- end
447
428
  end
448
429
  end
449
430
  end
@@ -58,7 +58,7 @@ module IOTA
58
58
  raise StandardError, balancesData
59
59
  end
60
60
 
61
- balancesData['balances'].each_with_index do |balance, index|
61
+ balancesData.each_with_index do |balance, index|
62
62
  balance = balance.to_i
63
63
  @balance += balance
64
64
 
@@ -208,7 +208,7 @@ module IOTA
208
208
  confirmedInputs = []
209
209
  totalBalance = 0
210
210
 
211
- balances['balances'].each_with_index do |balance, i|
211
+ balances.each_with_index do |balance, i|
212
212
  # If input has balance, add it to confirmedInputs
213
213
  balance = balance.to_i
214
214
 
@@ -318,9 +318,22 @@ module IOTA
318
318
  loop do
319
319
  newAddress = @seed.getAddress(index, security, checksum)
320
320
 
321
- @api.findTransactions(addresses: [newAddress]) do |status, trx_hashes|
321
+ @api.wereAddressesSpentFrom([newAddress]) do |status, spent_states|
322
322
  if !status
323
- raise StandardError, trx_hashes
323
+ raise StandardError, spent_states
324
+ end
325
+
326
+ spent = spent_states[0]
327
+
328
+ # Check transactions if not spent
329
+ if !spent
330
+ @api.findTransactions(addresses: [newAddress]) do |status, trx_hashes|
331
+ if !status
332
+ raise StandardError, trx_hashes
333
+ end
334
+
335
+ spent = trx_hashes.length > 0
336
+ end
324
337
  end
325
338
 
326
339
  if return_all
@@ -328,8 +341,7 @@ module IOTA
328
341
  end
329
342
 
330
343
  index += 1
331
-
332
- return (return_all ? allAddresses : newAddress) if trx_hashes.length == 0
344
+ return (return_all ? allAddresses : newAddress) if !spent
333
345
  end
334
346
  end
335
347
  end
@@ -125,7 +125,7 @@ module IOTA
125
125
  if !st1
126
126
  raise StandardError, "Error fetching balances: #{balances}"
127
127
  else
128
- return createBundle(balances['balances'][0].to_i, totalValue, bundle, input, remainderAddress, tag, signatureFragments)
128
+ return createBundle(balances[0].to_i, totalValue, bundle, input, remainderAddress, tag, signatureFragments)
129
129
  end
130
130
  end
131
131
  end
@@ -98,7 +98,9 @@ module IOTA
98
98
  'getTrytes' => 'trytes',
99
99
  'getInclusionStates' => 'states',
100
100
  'attachToTangle' => 'trytes',
101
- 'checkConsistency' => 'state'
101
+ 'checkConsistency' => 'state',
102
+ 'getBalances' => 'balances',
103
+ 'wereAddressesSpentFrom'=> 'states'
102
104
  }
103
105
 
104
106
  # If correct result and we want to prepare the result
data/lib/iota/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IOTA
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iota-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vivek Marakana
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-11 00:00:00.000000000 Z
11
+ date: 2018-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,6 +92,7 @@ files:
92
92
  - ".editorconfig"
93
93
  - ".gitignore"
94
94
  - ".travis.yml"
95
+ - CHANGELOG.md
95
96
  - Gemfile
96
97
  - LICENSE
97
98
  - README.md
@@ -104,6 +105,7 @@ files:
104
105
  - lib/iota.rb
105
106
  - lib/iota/api/api.rb
106
107
  - lib/iota/api/commands.rb
108
+ - lib/iota/api/transport.rb
107
109
  - lib/iota/api/wrappers.rb
108
110
  - lib/iota/crypto/bundle.rb
109
111
  - lib/iota/crypto/converter.rb