Module: IOTA::API::Wrappers

Included in:
Api
Defined in:
lib/iota/api/wrappers.rb

Instance Method Summary collapse

Instance Method Details

#broadcastBundle(tail, &callback) ⇒ Object


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/iota/api/wrappers.rb', line 269

def broadcastBundle(tail, &callback)
  # Check if correct tail hash
  if !@validator.isHash(tail)
    return sendData(false, "Invalid trytes provided", &callback)
  end

  getBundle(tail) do |status, transactions|
    if !status
      return sendData(false, transactions, &callback)
    end

    bundleTrytes = []
    transactions.each do |trx|
      bundleTrytes << @utils.transactionTrytes(trx);
    end

    return broadcastTransactions(bundleTrytes.reverse, &callback)
  end
end

#bundlesFromAddresses(addresses, inclusionStates, &callback) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
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
# File 'lib/iota/api/wrappers.rb', line 130

def bundlesFromAddresses(addresses, inclusionStates, &callback)
  # call wrapper function to get txs associated with addresses
  findTransactionObjects(addresses: addresses) do |status, transactionObjects|
    if !status
      return sendData(false, transactionObjects, &callback)
    end

    # set of tail transactions
    tailTransactions = []
    nonTailBundleHashes = []

    transactionObjects.each do |trx|
      # Sort tail and nonTails
      if trx.currentIndex == 0
        tailTransactions << trx.hash
      else
        nonTailBundleHashes << trx.bundle
      end
    end

    # Get tail transactions for each nonTail via the bundle hash
    findTransactionObjects(bundles: nonTailBundleHashes.uniq) do |st1, trxObjects|
      if !st1
        return sendData(false, trxObjects, &callback)
      end

      trxObjects.each do |trx|
        tailTransactions << trx.hash if trx.currentIndex == 0
      end

      finalBundles = []
      tailTransactions = tailTransactions.uniq
      tailTxStates = []

      # If inclusionStates, get the confirmation status of the tail transactions, and thus the bundles
      if inclusionStates && tailTransactions.length > 0
        getLatestInclusion(tailTransactions) do |st2, states|
          # If error, return it to original caller
          if !status
            return sendData(false, states, &callback)
          end

          tailTxStates = states
        end
      end

      tailTransactions.each do |tailTx|
        getBundle(tailTx) do |st2, bundleTransactions|
          if st2
            if inclusionStates
              thisInclusion = tailTxStates[tailTransactions.index(tailTx)]

              bundleTransactions.each do |bundleTx|
                bundleTx.persistence = thisInclusion
              end
            end

            finalBundles << IOTA::Models::Bundle.new(bundleTransactions)
          end
        end
      end      # Sort bundles by attachmentTimestamp

      finalBundles = finalBundles.sort{|a, b| a.attachmentTimestamp <=> b.attachmentTimestamp}
      return sendData(true, finalBundles, &callback)
    end
  end
end

#findTransactionObjects(input, &callback) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/iota/api/wrappers.rb', line 33

def findTransactionObjects(input, &callback)
  findTransactions(input) do |status, transactions|
    if !status
      return sendData(status, transactions, &callback)
    else
      return getTransactionsObjects(transactions, &callback)
    end
  end
end

#getBundle(transaction, &callback) ⇒ Object


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/iota/api/wrappers.rb', line 198

def getBundle(transaction, &callback)
  # Check if correct hash
  if !@validator.isHash(transaction)
     return sendData(false, "Invalid transaction input provided", &callback)
  end

  # Initiate traverseBundle
  traverseBundle(transaction, nil, []) do |status, bundle|
    if !status
      return sendData(false, bundle, &callback)
    end

    if !@utils.isBundle(bundle)
      return sendData(false, "Invalid Bundle provided", &callback)
    end

    return sendData(true, bundle, &callback)
  end
end

#getLatestInclusion(hashes, &callback) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/iota/api/wrappers.rb', line 43

def getLatestInclusion(hashes, &callback)
  ret_status = false
  ret_data = nil
  getNodeInfo do |status, data|
    if status
      ret_status = true
      ret_data = data['latestSolidSubtangleMilestone']
    end
  end

  if ret_status
    return getInclusionStates(hashes, [ret_data], &callback)
  else
    return sendData(ret_status, ret_data, &callback)
  end
end

#getTransactionsObjects(hashes, &callback) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iota/api/wrappers.rb', line 4

def getTransactionsObjects(hashes, &callback)
  # If not array of hashes, return error
  if !@validator.isArrayOfHashes(hashes)
    return sendData(false, "Invalid inputs provided", &callback)
  end

  ret_status = false
  ret_data = nil  # get the trytes of the transaction hashes

  getTrytes(hashes) do |status, trytes|
    ret_status = status
    if status
      transactionObjects = []
      trytes.each do |tryte|
        if !tryte
          transactionObjects << nil
        else
          transactionObjects << @utils.transactionObject(tryte)
        end
      end
      ret_data = transactionObjects
    else
      ret_data = trytes
    end
  end

  sendData(ret_status, ret_data, &callback)
end

#isReattachable(inputAddresses, &callback) ⇒ Object


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/iota/api/wrappers.rb', line 340

def isReattachable(inputAddresses, &callback)
  # if string provided, make array
  inputAddresses = [inputAddresses] if @validator.isString(inputAddresses)

  # Categorized value transactions
  # hash -> txarray map
  addressTxsMap = {}
  addresses = []

  inputAddresses.each do |address|
    if !@validator.isAddress(address)
      return sendData(false, "Invalid inputs provided", &callback)
    end

    address = @utils.noChecksum(address)

    addressTxsMap[address] = []
    addresses << address
  end

  findTransactionObjects(addresses: addresses) do |status, transactions|
    if !status
      return sendData(false, transactions, &callback)
    end

    valueTransactions = []

    transactions.each do |trx|
      if trx.value < 0
        txAddress = trx.address
        txHash = trx.hash

        addressTxsMap[txAddress] << txHash
        valueTransactions << txHash
      end
    end

    if valueTransactions.length > 0      # get the includion states of all the transactions

      getLatestInclusion(valueTransactions) do |st1, inclusionStates|
        if !st1
          return sendData(false, inclusionStates, &callback)
        end

        # bool array
        results = addresses.map do |address|
          txs = addressTxsMap[address]
          numTxs = txs.length

          if numTxs == 0
            true
          else
            shouldReattach = true

            (0...numTxs).step(1) do |i|
              tx = txs[i]
              txIndex = valueTransactions.index(tx)
              isConfirmed = inclusionStates[txIndex]
              shouldReattach = isConfirmed ? false : true
              break if isConfirmed
            end

            shouldReattach
          end
        end

        # If only one entry, return first
        results = results.first if results.length == 1

        return sendData(true, results, &callback)
      end
    else
      results = [];
      numAddresses = addresses.length;

      # prepare results array if multiple addresses
      if numAddresses > 1
        numAddresses.each do |i|
          results << true
        end
      else
        results = true
      end

      return sendData(true, results, &callback)
    end
  end
end

#replayBundle(tail, depth, minWeightMagnitude, forcedReplay = false, &callback) ⇒ Object

Replays or promote transactions based on tail consistency


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/iota/api/wrappers.rb', line 219

def replayBundle(tail, depth, minWeightMagnitude, forcedReplay = false, &callback)
  # Check if correct tail hash
  if !@validator.isHash(tail)
    return sendData(false, "Invalid trytes provided", &callback)
  end

  # Check if correct depth and minWeightMagnitude
  if !@validator.isValue(depth) || !@validator.isValue(minWeightMagnitude)
    return sendData(false, "Invalid inputs provided", &callback)
  end

  isPromotable = false
  if !forcedReplay
    checkConsistency([tail]) do |status, isConsistent|
      isPromotable = status && isConsistent
    end
  end

  getBundle(tail) do |status, transactions|
    if !status
      return sendData(false, transactions, &callback)
    end

    if !isPromotable
      bundleTrytes = []
      transactions.each do |trx|
        bundleTrytes << @utils.transactionTrytes(trx);
      end

      return sendTrytes(bundleTrytes.reverse, depth, minWeightMagnitude, &callback)
    else
       = IOTA::Models::Account.new(nil, transactions.first.address, self, @validator, @utils)

      transfers = [{
        address: '9' * 81,
        value: 0,
        message: '',
        tag: ''
      }]

      begin
        .sendTransfer(depth, minWeightMagnitude, transfers, { reference: tail })
        return sendData(status, transactions, &callback)
      rescue => e
        return sendData(false, e.message, &callback)
      end
    end
  end
end

#sendTrytes(trytes, depth, minWeightMagnitude, options = {}, &callback) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/iota/api/wrappers.rb', line 70

def sendTrytes(trytes, depth, minWeightMagnitude, options = {}, &callback)
  # Check if correct depth and minWeightMagnitude
  if !@validator.isValue(depth) || !@validator.isValue(minWeightMagnitude)
    return sendData(false, "Invalid inputs provided", &callback)
  end

  reference = options[:reference] || options['reference']

  getTransactionsToApprove(depth, reference) do |status, approval_data|
    if !status
      return sendData(false, approval_data, &callback)
    end

    attachToTangle(approval_data['trunkTransaction'], approval_data['branchTransaction'], minWeightMagnitude, trytes) do |status1, attached_data|
      if !status1
        return sendData(false, attached_data, &callback)
      end

      # If the user is connected to the sandbox, we have to monitor the POW queue
      # to check if the POW job was completed
      if @sandbox        # Implement sandbox processing

        jobUri = @sandbox + '/jobs/' + attached_data['id']

        # Do the Sandbox send function
        @broker.sandboxSend(jobUri) do |status2, sandbox_data|
          if !status2
            return sendData(false, sandbox_data, &callback)
          end

          storeAndBroadcast(sandbox_data) do |status3, data|
            if status3
              return sendData(false, data, &callback)
            end

            finalTxs = []

            attachedTrytes.each do |trytes1|
              finalTxs << @utils.transactionObject(trytes1)
            end

            return sendData(true, finalTxs, &callback)
          end
        end
      else
        # Broadcast and store tx
        storeAndBroadcast(attached_data) do |status2, data|
          if !status2
            return sendData(false, data, &callback)
          end

          transactions = attached_data.map { |tryte| @utils.transactionObject(tryte) }

          sendData(true, transactions, &callback)
        end
      end
    end
  end
end

#storeAndBroadcast(trytes, &callback) ⇒ Object


60
61
62
63
64
65
66
67
68
# File 'lib/iota/api/wrappers.rb', line 60

def storeAndBroadcast(trytes, &callback)
  storeTransactions(trytes) do |status, data|
    if !status
      return sendData(status, data, &callback)
    else
      return broadcastTransactions(trytes, &callback)
    end
  end
end

#traverseBundle(trunkTx, bundleHash, bundle, &callback) ⇒ Object


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/iota/api/wrappers.rb', line 289

def traverseBundle(trunkTx, bundleHash, bundle, &callback)
  # Get trytes of transaction hash
  getTrytes([trunkTx]) do |status, trytesList|
    if !status
      return sendData(false, trytesList, &callback)
    end

    trytes = trytesList[0]

    if !trytes
      return sendData(false, "Bundle transactions not visible", &callback)
    end

    # get the transaction object
    txObject = @utils.transactionObject(trytes)

    if !trytes
      return sendData(false, "Invalid trytes, could not create object", &callback)
    end

    # If first transaction to search is not a tail, return error
    if !bundleHash && txObject.currentIndex != 0
      return sendData(false, "Invalid tail transaction supplied", &callback)
    end

    # If no bundle hash, define it
    if !bundleHash
      bundleHash = txObject.bundle
    end

    # If different bundle hash, return with bundle
    if bundleHash != txObject.bundle
      return sendData(true, bundle, &callback)
    end

    # If only one bundle element, return
    if txObject.lastIndex == 0 && txObject.currentIndex == 0
      return sendData(true, [txObject], &callback)
    end

    # Define new trunkTransaction for search
    trunkTx = txObject.trunkTransaction

    # Add transaction object to bundle
    bundle << txObject

    # Continue traversing with new trunkTx
    return traverseBundle(trunkTx, bundleHash, bundle, &callback)
  end
end