Class: IOTA::Models::Account
Instance Attribute Summary collapse
-
#addresses ⇒ Object
readonly
Returns the value of attribute addresses.
-
#balance ⇒ Object
readonly
Returns the value of attribute balance.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#latestAddress ⇒ Object
readonly
Returns the value of attribute latestAddress.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
-
#transfers ⇒ Object
readonly
Returns the value of attribute transfers.
Instance Method Summary collapse
- #getAccountDetails(options = {}, fetchInputs = true, fetchTransfers = true) ⇒ Object
- #getInputs(options = {}) ⇒ Object
- #getNewAddress(options = {}) ⇒ Object
- #getTransfers(options = {}) ⇒ Object
-
#initialize(client, seed, api = nil, validator = nil, utils = nil) ⇒ Account
constructor
A new instance of Account.
- #prepareTransfers(transfers, options = {}) ⇒ Object
- #reset ⇒ Object
- #sendTransfer(depth, minWeightMagnitude, transfers, options = {}) ⇒ Object
Constructor Details
#initialize(client, seed, api = nil, validator = nil, utils = nil) ⇒ Account
Returns a new instance of Account.
6 7 8 9 10 11 12 13 |
# File 'lib/iota/models/account.rb', line 6 def initialize(client, seed, api = nil, validator = nil, utils = nil) @api = client ? client.api : api @validator = client ? client.validator : validator @utils = client ? client.utils : utils @seed = seed.class == String ? Seed.new(seed) : seed reset end |
Instance Attribute Details
#addresses ⇒ Object (readonly)
Returns the value of attribute addresses.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def addresses @addresses end |
#balance ⇒ Object (readonly)
Returns the value of attribute balance.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def balance @balance end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def client @client end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def inputs @inputs end |
#latestAddress ⇒ Object (readonly)
Returns the value of attribute latestAddress.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def latestAddress @latestAddress end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def seed @seed end |
#transfers ⇒ Object (readonly)
Returns the value of attribute transfers.
4 5 6 |
# File 'lib/iota/models/account.rb', line 4 def transfers @transfers end |
Instance Method Details
#getAccountDetails(options = {}, fetchInputs = true, fetchTransfers = true) ⇒ Object
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 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/iota/models/account.rb', line 23 def getAccountDetails( = {}, fetchInputs = true, fetchTransfers = true) reset = symbolize_keys() startIndex = [:start] || 0 endIndex = [:end] || nil security = [:security] || 2 # If start value bigger than end, return error or if difference between end and start is bigger than 1000 keys if endIndex && (startIndex > endIndex || endIndex > (startIndex + 1000)) raise StandardError, "Invalid inputs provided" end addressOptions = { index: startIndex, total: endIndex ? endIndex - startIndex : nil, returnAll: true, security: security, checksum: true } # Get a list of all addresses associated with the users seed addresses = getNewAddress(addressOptions) # assign the last address as the latest address # since it has no transactions associated with it @latestAddress = addresses.last # Add all returned addresses to the lsit of addresses # remove the last element as that is the most recent address @addresses = addresses.slice(0, addresses.length) if fetchInputs # Get the correct balance count of all addresses @api.getBalances(addresses, 100) do |status2, balancesData| if !status2 raise StandardError, balancesData end balancesData.each_with_index do |balance, index| balance = balance.to_i @balance += balance if balance > 0 @inputs << IOTA::Models::Input.new( address: @addresses[index], keyIndex: index, security: security, balance: balance ) end end end end if fetchTransfers # get all bundles from a list of addresses @api.bundlesFromAddresses(addresses, true) do |status1, bundlesData| if !status1 raise StandardError, bundlesData end # add all transfers @transfers = bundlesData end end self end |
#getInputs(options = {}) ⇒ Object
92 93 94 95 |
# File 'lib/iota/models/account.rb', line 92 def getInputs( = {}) self.getAccountDetails(, true, false) @inputs end |
#getNewAddress(options = {}) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 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 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/iota/models/account.rb', line 276 def getNewAddress( = {}) # default index value = symbolize_keys() index = [:index] || 0 # validate the index option if !@validator.isValue(index) || index < 0 raise StandardError, "Invalid index provided: #{index}" end checksum = [:checksum] || false total = [:total] || nil return_all = [:returnAll] || false # If no user defined security, use the standard value of 2 security = [:security] || 2 # validate the security option if !@validator.isValue(security) || security < 1 || security > 3 raise StandardError, "Invalid security provided: #{index}" end allAddresses = [] # Case 1: total # # If total number of addresses to generate is supplied, simply generate # and return the list of all addresses if total # Increase index with each iteration (0...total).step(1) do |i| address = @seed.getAddress(index, security, checksum) allAddresses << address index += 1 end return allAddresses else # Case 2: no total provided # # Continue calling findTransactions to see if address was already created # if null, return list of addresses loop do newAddress = @seed.getAddress(index, security, checksum) @api.wereAddressesSpentFrom([newAddress]) do |status, spent_states| if !status raise StandardError, spent_states end spent = spent_states[0] # Check transactions if not spent if !spent @api.findTransactions(addresses: [newAddress]) do |status, trx_hashes| if !status raise StandardError, trx_hashes end spent = trx_hashes.length > 0 end end if return_all allAddresses << newAddress end index += 1 return (return_all ? allAddresses : newAddress) if !spent end end end end |
#getTransfers(options = {}) ⇒ Object
97 98 99 100 |
# File 'lib/iota/models/account.rb', line 97 def getTransfers( = {}) self.getAccountDetails(, false, true) @transfers end |
#prepareTransfers(transfers, options = {}) ⇒ Object
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 129 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 |
# File 'lib/iota/models/account.rb', line 102 def prepareTransfers(transfers, = {}) = symbolize_keys() hmacKey = [:hmacKey] || nil if !hmacKey.nil? raise StandardError, "Invalid trytes provided: #{hmacKey}" if !@validator.isTrytes(hmacKey) end # If message or tag is not supplied, provide it # Also remove the checksum of the address if it's there after validating it (0...transfers.length).step(1) do |index| if transfers[index].class != IOTA::Models::Transfer transfers[index] = IOTA::Models::Transfer.new(transfers[index].merge({ hmacKey: hmacKey })) end end # Input validation of transfers object raise StandardError, "Invalid transfers provided" if !@validator.isTransfersArray(transfers) # If inputs provided, validate the format chosenInputs = [:inputs] || nil raise StandardError, "Invalid inputs provided" if chosenInputs && !@validator.isInputs(chosenInputs) remainderAddress = [:address] || nil chosenInputs = chosenInputs || [] security = [:security] || 2 remainderAddress = @utils.noChecksum(remainderAddress) if remainderAddress && remainderAddress.length == 90 # Create a new bundle bundle = IOTA::Crypto::Bundle.new totalValue = 0 signatureFragments = [] tag = nil # Iterate over all transfers, get totalValue and prepare the signatureFragments, message and tag (0...transfers.length).step(1) do |i| signatureMessageLength = 1 # If message longer than 2187 trytes, increase signatureMessageLength (add 2nd transaction) if transfers[i]..length > 2187 # Get total length, message / maxLength (2187 trytes) signatureMessageLength += (transfers[i]..length / 2187).floor msgCopy = transfers[i]. # While there is still a message, copy it while msgCopy fragment = msgCopy[0...2187] msgCopy = msgCopy[2187...msgCopy.length] # Pad remainder of fragment while fragment.length < 2187 fragment += '9' end signatureFragments << fragment end else # Else, get single fragment with 2187 of 9's trytes fragment = '' fragment = transfers[i].[0...2187] if transfers[i]. while fragment.length < 2187 fragment += '9' end signatureFragments << fragment end # get current timestamp in seconds = Time.now.utc.to_i # If no tag defined, get 27 tryte tag. tag = transfers[i].obsoleteTag || '' # Pad for required 27 tryte length while tag.length < 27 tag += '9' end # Add first entries to the bundle # Slice the address in case the user provided a checksummed one bundle.addEntry(signatureMessageLength, transfers[i].address, transfers[i].value, tag, ) # Sum up total value totalValue += transfers[i].value.to_i end # Get inputs if we are sending tokens if totalValue > 0 # Case 1: user provided inputs # # Validate the inputs by calling getBalances if chosenInputs && chosenInputs.length > 0 # Get list if addresses of the provided inputs inputsAddresses = []; (0...chosenInputs.length).step(1) do |i| if chosenInputs[i].class != IOTA::Models::Input chosenInputs[i] = IOTA::Models::Input.new(chosenInputs[i]) end inputsAddresses << chosenInputs[i].address end @api.getBalances(inputsAddresses, 100) do |status, balances| raise StandardError, balances if !status confirmedInputs = [] totalBalance = 0 balances.each_with_index do |balance, i| # If input has balance, add it to confirmedInputs balance = balance.to_i if balance > 0 totalBalance += balance input = chosenInputs[i] input.balance = balance confirmedInputs << input # if we've already reached the intended input value, break out of loop if totalBalance >= totalValue break end end end # Return not enough balance error raise StandardError, "Not enough balance" if totalValue > totalBalance return addRemainder(confirmedInputs, totalValue, bundle, tag, security, signatureFragments, remainderAddress, hmacKey) end else # Case 2: Get inputs deterministically # If no inputs provided, derive the addresses from the seed and # confirm that the inputs exceed the threshold self.getInputs(security: security) raise StandardError, "Not enough balance" if totalValue > @balance return addRemainder(@inputs, totalValue, bundle, tag, security, signatureFragments, remainderAddress, hmacKey) end else # If no input required, don't sign and simply finalize the bundle bundle.finalize() bundle.addTrytes(signatureFragments) bundleTrytes = [] bundle.bundle.each do |bndl| bundleTrytes << @utils.transactionTrytes(bndl) end return bundleTrytes.reverse end end |
#reset ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/iota/models/account.rb', line 15 def reset @latestAddress = nil @addresses = [] @transfers = [] @inputs = [] @balance = 0.0 end |
#sendTransfer(depth, minWeightMagnitude, transfers, options = {}) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/iota/models/account.rb', line 259 def sendTransfer(depth, minWeightMagnitude, transfers, = {}) # Check if correct depth and minWeightMagnitude if !@validator.isValue(depth) || !@validator.isValue(minWeightMagnitude) raise StandardError, "Invalid inputs provided" end trytes = prepareTransfers(transfers, ) @api.sendTrytes(trytes, depth, minWeightMagnitude, ) do |status, data| if !status raise StandardError, data else return data end end end |