Class: IOTA::API::Api

Inherits:
Object
  • Object
show all
Includes:
Transport, Wrappers
Defined in:
lib/iota/api/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transport

#sendBatchedCommand, #sendCommand

Methods included from Wrappers

#broadcastBundle, #bundlesFromAddresses, #findTransactionObjects, #getBundle, #getLatestInclusion, #getTransactionsObjects, #isReattachable, #replayBundle, #sendTrytes, #storeAndBroadcast, #traverseBundle

Constructor Details

#initialize(broker, sandbox, batch_size = 500, local_pow = false) ⇒ Api

Returns a new instance of Api.



9
10
11
12
13
14
15
16
17
# File 'lib/iota/api/api.rb', line 9

def initialize(broker, sandbox, batch_size = 500, local_pow = false)
  @broker = broker
  @sandbox = sandbox
  @commands = Commands.new
  @utils = IOTA::Utils::Utils.new
  @validator = @utils.validator
  @batch_size = batch_size
  @pow_provider = local_pow ? IOTA::Crypto::PowProvider.new : nil
end

Instance Attribute Details

#pow_providerObject (readonly)

Returns the value of attribute pow_provider.



7
8
9
# File 'lib/iota/api/api.rb', line 7

def pow_provider
  @pow_provider
end

Instance Method Details

#addNeighbors(uris, &callback) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/iota/api/api.rb', line 113

def addNeighbors(uris, &callback)
  (0...uris.length).step(1) do |i|
    return sendData(false, "You have provided an invalid URI for your Neighbor: " + uris[i], &callback) if !@validator.isUri(uris[i])
  end

  sendCommand(@commands.addNeighbors(uris), &callback)
end

#attachToTangle(trunkTransaction, branchTransaction, minWeightMagnitude, trytes, &callback) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/iota/api/api.rb', line 142

def attachToTangle(trunkTransaction, branchTransaction, minWeightMagnitude, trytes, &callback)
  # Check if correct trunk
  if !@validator.isHash(trunkTransaction)
    return sendData(false, "You have provided an invalid hash as a trunk: #{trunkTransaction}", &callback)
  end

  # Check if correct branch
  if !@validator.isHash(branchTransaction)
    return sendData(false, "You have provided an invalid hash as a branch: #{branchTransaction}", &callback)
  end

  # Check if minweight is integer
  if !@validator.isValue(minWeightMagnitude)
    return sendData(false, "Invalid minWeightMagnitude provided", &callback)
  end

  # Check if array of trytes
  if !@validator.isArrayOfTrytes(trytes)
    return sendData(false, "Invalid Trytes provided", &callback)
  end

  if @pow_provider.nil?
    command = @commands.attachToTangle(trunkTransaction, branchTransaction, minWeightMagnitude, trytes)

    sendCommand(command, &callback)
  else
    previousTxHash = nil
    finalBundleTrytes = []

    trytes.each do |current_trytes|
      txObject = @utils.transactionObject(current_trytes)

      if !previousTxHash
        if txObject.lastIndex != txObject.currentIndex
          return sendData(false, "Wrong bundle order. The bundle should be ordered in descending order from currentIndex", &callback)
        end

        txObject.trunkTransaction = trunkTransaction
        txObject.branchTransaction = branchTransaction
      else
        txObject.trunkTransaction = previousTxHash
        txObject.branchTransaction = trunkTransaction
      end

      txObject.attachmentTimestamp = (Time.now.to_f * 1000).to_i
      txObject.attachmentTimestampLowerBound = 0
      txObject.attachmentTimestampUpperBound = (3**27 - 1) / 2

      newTrytes = @utils.transactionTrytes(txObject)

      begin
        returnedTrytes = @pow_provider.pow(newTrytes, minWeightMagnitude)

        newTxObject= @utils.transactionObject(returnedTrytes)
        previousTxHash = newTxObject.hash

        finalBundleTrytes << returnedTrytes
      rescue => e
        return sendData(false, e.message, &callback)
      end
    end

    sendData(true, finalBundleTrytes, &callback)
  end
end

#broadcastTransactions(trytes, &callback) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/iota/api/api.rb', line 212

def broadcastTransactions(trytes, &callback)
  if !@validator.isArrayOfAttachedTrytes(trytes)
    return sendData(false, "Invalid attached Trytes provided", &callback)
  end

  sendCommand(@commands.broadcastTransactions(trytes), &callback)
end

#checkConsistency(tails, &callback) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/iota/api/api.rb', line 228

def checkConsistency(tails, &callback)
  if !@validator.isArrayOfHashes(tails)
    return sendData(false, "Invalid tails provided", &callback)
  end

  sendCommand(@commands.checkConsistency(tails), &callback)
end

#findTransactions(searchValues, &callback) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
77
78
# File 'lib/iota/api/api.rb', line 19

def findTransactions(searchValues, &callback)
  if !@validator.isObject(searchValues)
    return sendData(false, "You have provided an invalid key value", &callback)
  end

  searchKeys = searchValues.keys
  validKeys = ['bundles', 'addresses', 'tags', 'approvees']

  error = false
  entry_count = 0

  searchKeys.each do |key|
    if !validKeys.include?(key.to_s)
      error = "You have provided an invalid key value"
      break
    end

    hashes = searchValues[key]
    entry_count += hashes.count

    if key.to_s == 'addresses'
      searchValues[key] = hashes.map do |address|
        @utils.noChecksum(address)
      end
    end

    # If tags, append to 27 trytes
    if key.to_s == 'tags'
      searchValues[key] = hashes.map do |hash|
        # Simple padding to 27 trytes
        while hash.length < 27
          hash += '9'
        end
        # validate hash
        if !@validator.isTrytes(hash, 27)
          error = "Invalid Trytes provided"
          break
        end

        hash
      end
    else
      # Check if correct array of hashes
      if !@validator.isArrayOfHashes(hashes)
        error = "Invalid Trytes provided"
        break
      end
    end
  end

  if error
    return sendData(false, error, &callback)
  else
    if entry_count <= @batch_size || searchKeys.count > 1
      return sendCommand(@commands.findTransactions(searchValues), &callback)
    else
      return sendBatchedCommand(@commands.findTransactions(searchValues), &callback)
    end
  end
end

#getBalances(addresses, threshold, &callback) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/iota/api/api.rb', line 80

def getBalances(addresses, threshold, &callback)
  if !@validator.isArrayOfHashes(addresses)
    return sendData(false, "Invalid Trytes provided", &callback)
  end

  command = @commands.getBalances(addresses.map{|address| @utils.noChecksum(address)}, threshold)
  sendBatchedCommand(command, &callback)
end

#getInclusionStates(transactions, tips, &callback) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/iota/api/api.rb', line 97

def getInclusionStates(transactions, tips, &callback)
  if !@validator.isArrayOfHashes(transactions) || !@validator.isArrayOfHashes(tips)
    return sendData(false, "Invalid Trytes provided", &callback)
  end

  sendBatchedCommand(@commands.getInclusionStates(transactions, tips), &callback)
end

#getMissingTransactions(&callback) ⇒ Object



249
250
251
# File 'lib/iota/api/api.rb', line 249

def getMissingTransactions(&callback)
  sendCommand(@commands.getMissingTransactions, &callback)
end

#getNeighbors(&callback) ⇒ Object



109
110
111
# File 'lib/iota/api/api.rb', line 109

def getNeighbors(&callback)
  sendCommand(@commands.getNeighbors, &callback)
end

#getNodeAPIConfiguration(&callback) ⇒ Object



245
246
247
# File 'lib/iota/api/api.rb', line 245

def getNodeAPIConfiguration(&callback)
  sendCommand(@commands.getNodeAPIConfiguration, &callback)
end

#getNodeInfo(&callback) ⇒ Object



105
106
107
# File 'lib/iota/api/api.rb', line 105

def getNodeInfo(&callback)
  sendCommand(@commands.getNodeInfo, &callback)
end

#getTips(&callback) ⇒ Object



129
130
131
# File 'lib/iota/api/api.rb', line 129

def getTips(&callback)
  sendCommand(@commands.getTips, &callback)
end

#getTransactionsToApprove(depth, reference = nil, &callback) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/iota/api/api.rb', line 133

def getTransactionsToApprove(depth, reference = nil, &callback)
  # Check if correct depth
  if !@validator.isValue(depth)
    return sendData(false, "Invalid inputs provided", &callback)
  end

  sendCommand(@commands.getTransactionsToApprove(depth, reference), &callback)
end

#getTrytes(hashes, &callback) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/iota/api/api.rb', line 89

def getTrytes(hashes, &callback)
  if !@validator.isArrayOfHashes(hashes)
    return sendData(false, "Invalid Trytes provided", &callback)
  end

  sendBatchedCommand(@commands.getTrytes(hashes), &callback)
end

#interruptAttachingToTangle(&callback) ⇒ Object



208
209
210
# File 'lib/iota/api/api.rb', line 208

def interruptAttachingToTangle(&callback)
  sendCommand(@commands.interruptAttachingToTangle, &callback)
end

#removeNeighbors(uris, &callback) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/iota/api/api.rb', line 121

def removeNeighbors(uris, &callback)
  (0...uris.length).step(1) do |i|
    return sendData(false, "You have provided an invalid URI for your Neighbor: " + uris[i], &callback) if !@validator.isUri(uris[i])
  end

  sendCommand(@commands.removeNeighbors(uris), &callback)
end

#storeTransactions(trytes, &callback) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/iota/api/api.rb', line 220

def storeTransactions(trytes, &callback)
  if !@validator.isArrayOfAttachedTrytes(trytes)
    return sendData(false, "Invalid attached Trytes provided", &callback)
  end

  sendCommand(@commands.storeTransactions(trytes), &callback)
end

#wereAddressesSpentFrom(addresses, &callback) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/iota/api/api.rb', line 236

def wereAddressesSpentFrom(addresses, &callback)
  if !@validator.isArrayOfHashes(addresses)
    return sendData(false, "Invalid Trytes provided", &callback)
  end

  command = @commands.wereAddressesSpentFrom(addresses.map{|address| @utils.noChecksum(address)})
  sendBatchedCommand(command, &callback)
end