Class: IOTA::API::Api

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

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) ⇒ Api

Returns a new instance of Api.


7
8
9
10
11
12
13
14
# File 'lib/iota/api/api.rb', line 7

def initialize(broker, sandbox, batch_size = 500)
  @broker = broker
  @sandbox = sandbox
  @commands = Commands.new
  @utils = IOTA::Utils::Utils.new
  @validator = @utils.validator
  @batch_size = batch_size
end

Instance Method Details

#addNeighbors(uris, &callback) ⇒ Object


110
111
112
113
114
115
116
# File 'lib/iota/api/api.rb', line 110

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


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
# File 'lib/iota/api/api.rb', line 139

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 inputs provided", &callback)
  end

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

  command = @commands.attachToTangle(trunkTransaction, branchTransaction, minWeightMagnitude, trytes)

  sendCommand(command, &callback)
end

#broadcastTransactions(trytes, &callback) ⇒ Object


169
170
171
172
173
174
175
# File 'lib/iota/api/api.rb', line 169

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


185
186
187
188
189
190
191
# File 'lib/iota/api/api.rb', line 185

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


16
17
18
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
# File 'lib/iota/api/api.rb', line 16

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


77
78
79
80
81
82
83
84
# File 'lib/iota/api/api.rb', line 77

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


94
95
96
97
98
99
100
# File 'lib/iota/api/api.rb', line 94

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

#getNeighbors(&callback) ⇒ Object


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

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

#getNodeInfo(&callback) ⇒ Object


102
103
104
# File 'lib/iota/api/api.rb', line 102

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

#getTips(&callback) ⇒ Object


126
127
128
# File 'lib/iota/api/api.rb', line 126

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

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


130
131
132
133
134
135
136
137
# File 'lib/iota/api/api.rb', line 130

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


86
87
88
89
90
91
92
# File 'lib/iota/api/api.rb', line 86

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


165
166
167
# File 'lib/iota/api/api.rb', line 165

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

#removeNeighbors(uris, &callback) ⇒ Object


118
119
120
121
122
123
124
# File 'lib/iota/api/api.rb', line 118

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


177
178
179
180
181
182
183
# File 'lib/iota/api/api.rb', line 177

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


193
194
195
196
197
198
199
200
# File 'lib/iota/api/api.rb', line 193

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