Skip to content
This repository was archived by the owner on Sep 6, 2024. It is now read-only.

Commit 82fe4e8

Browse files
author
domschiener
committed
inclusionStates for getTransfers, getLatestInclusion function. Readme
1 parent 865197c commit 82fe4e8

File tree

5 files changed

+124
-38
lines changed

5 files changed

+124
-38
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is the official Javascript library for the IOTA Core. It implements both the [official API](https://iota.readme.io/), as well as newly proposed functionality (such as signing, bundles, utilities and conversion).
44

5-
It should be noted that the Javascript Library as it stands right now is an **early beta release**. As such, there might be some unexpected results. Please join the community (see links below) and post [issues on here](https://github.com/iotaledger/iota.lib.js/issues), to ensure that the developers of the library can improve it.
5+
It should be noted that the Javascript Library as it stands right now is an **early beta release**. As such, there might be some unexpected results. Please join the community (see links below) and post [issues on here](https://github.com/iotaledger/iota.lib.js/issues), to ensure that the developers of the library can improve it.
66

77
> **Join the Discussion**
88
@@ -33,11 +33,14 @@ It should be noted that this is a temporary home for the official documentation.
3333

3434
## Getting Started
3535

36-
After you've successfully installed the library, it is fairly easy to get started by simply launching a new instance of the IOTA object:
36+
After you've successfully installed the library, it is fairly easy to get started by simply launching a new instance of the IOTA object. When instantiating the object you have the option to decide the `host` and `port` that are used for sending the requests to, as can be seen in the example below:
3737

3838
```
3939
// Create IOTA instance
40-
var iota = new IOTA();
40+
var iota = new IOTA({
41+
'host': 'http://localhost',
42+
'port': 14265
43+
});
4144
4245
// now you can start using all of the functions
4346
iota.api.getNodeInfo();
@@ -72,6 +75,7 @@ iota.api.getNodeInfo(function(error, success) {
7275
- **[api](#api)**
7376
- **[Standard API](#standard-api)**
7477
- **[getTransactionsObjects](#gettransactionsobjects)**
78+
- **[getLatestInclusion](#getlatestinclusion)**
7579
- **[broadcastAndStore](#broadcastandstore)**
7680
- **[getNewAddress](#getnewaddress)**
7781
- **[getInputs](#getinputs)**
@@ -127,6 +131,25 @@ iota.api.getTransactionsObjects(hashes, callback)
127131

128132
---
129133

134+
### `getLatestInclusion`
135+
136+
Wrapper function for `getNodeInfo` and `getInclusionStates`. It simply takes the most recent solid milestone as returned by getNodeInfo, and uses it to get the inclusion states of a list of transaction hashes.
137+
138+
139+
#### Input
140+
```
141+
iota.api.getLatestInclusion(hashes, callback)
142+
```
143+
144+
1. **`hashes`**: `Array` List of transaction hashes
145+
2. **`callback`**: `Function` callback.
146+
147+
#### Return Value
148+
149+
1. **`Array`** - list of all the inclusion states of the transaction hashes
150+
151+
---
152+
130153
### `broadcastAndStore`
131154

132155
Wrapper function for `broadcastTransactions` and `storeTransactions`.
@@ -318,7 +341,7 @@ iota.api.getBundle(transaction, callback)
318341

319342
### `getTransfers`
320343

321-
Returns the transfers which are associated with a seed. The transfers are determined by either calculating deterministically which addresses were already used, or by providing a list of indexes to get the transfers from.
344+
Returns the transfers which are associated with a seed. The transfers are determined by either calculating deterministically which addresses were already used, or by providing a list of indexes to get the addresses and the associated transfers from. The transfers are sorted by their timestamp. It should be noted that, because timestamps are not enforced in IOTA, that this may lead to incorrectly sorted bundles (meaning that their chronological ordering in the Tangle is different).
322345

323346
#### Input
324347
```
@@ -327,7 +350,8 @@ getTransfers(seed [, options], callback)
327350

328351
1. **`seed`**: `String` tryte-encoded seed. It should be noted that this seed is not transferred
329352
2. **`options`**: `Object` which is optional:
330-
- **`indexes`**: `Array` - optional. If the index of addresses is provided, it will be used to get all transfers associated with the addresses.
353+
- **`start`**: `Int` Starting key index for search
354+
- **`end`**: `Int` Ending key index for search
331355
- **`inclusionStates`**: `Bool` If True, it gets the inclusion states of the transfers.
332356
3. **`callback`**: `Function` Optional callback.
333357

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iota.lib.js",
3-
"version": "0.0.4",
3+
"version": "0.0.4.1",
44
"description": "Javascript Library for the IOTA API.",
55
"main": "./dist/iota.js",
66
"authors": [

lib/api/api.js

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ api.prototype.storeTransactions = function(trytes, callback) {
430430

431431
/*************************************
432432
433-
WRAPPER FUNCTIONS
433+
WRAPPER AND CUSTOM FUNCTIONS
434434
435435
**************************************/
436436

@@ -473,6 +473,28 @@ api.prototype.getTransactionsObjects = function(hashes, callback) {
473473
})
474474
}
475475

476+
/**
477+
* Wrapper function for getNodeInfo and getInclusionStates
478+
*
479+
* @method getLatestInclusion
480+
* @param {array} hashes
481+
* @returns {function} callback
482+
* @returns {object} success
483+
**/
484+
api.prototype.getLatestInclusion = function(hashes, callback) {
485+
486+
var self = this;
487+
488+
self.getNodeInfo(function(e, nodeInfo) {
489+
490+
if (e) return callback(e);
491+
492+
var latestMilestone = nodeInfo.latestSolidSubtangleMilestone;
493+
494+
return self.getInclusionStates(hashes, latestMilestone, callback);
495+
})
496+
}
497+
476498
/**
477499
* Broadcasts and stores transaction trytes
478500
*
@@ -659,11 +681,11 @@ api.prototype._newAddress = function(seed, index, checksum) {
659681
* @param {string} seed
660682
* @param {object} options
661683
* @property {int} index Key index to start search from
662-
* @property {bool} checksum
663-
* @property {int} total Total number o f addresses to return
664-
* @property {bool} returnAll return all searched addresses or not
684+
* @property {bool} checksum add 9-tryte checksum
685+
* @property {int} total Total number of addresses to return
686+
* @property {bool} returnAll return all searched addresses
665687
* @param {function} callback
666-
* @returns {array} address List of addresses
688+
* @returns {string | array} address List of addresses
667689
**/
668690
api.prototype.getNewAddress = function(seed, options, callback) {
669691

@@ -737,7 +759,7 @@ api.prototype.getNewAddress = function(seed, options, callback) {
737759

738760
// If returnAll, return list of allAddresses
739761
// else return the last address that was generated
740-
var addressToReturn = options.returnAll ? allAddresses : Array(address);
762+
var addressToReturn = options.returnAll ? allAddresses : address;
741763

742764
return callback(null, addressToReturn);
743765
}
@@ -1317,16 +1339,13 @@ api.prototype.getBundle = function(transaction, callback) {
13171339
}
13181340

13191341

1320-
1321-
1322-
13231342
/**
13241343
* @method getTransfers
13251344
* @param {string} seed
13261345
* @param {object} options
13271346
* @property {int} start Starting key index
13281347
* @property {int} end Ending key index
1329-
* @property {bool} inclusionStates
1348+
* @property {bool} inclusionStates returns confirmation status of all transactions
13301349
* @param {function} callback
13311350
* @returns {object} success
13321351
**/
@@ -1350,12 +1369,10 @@ api.prototype.getTransfers = function(seed, options, callback) {
13501369
return callback(new Error("Invalid inputs provided"))
13511370
}
13521371

1353-
13541372
// first call findTransactions
13551373
// If a transaction is non tail, get the tail transactions associated with it
13561374
// add it to the list of tail transactions
13571375

1358-
13591376
var addressOptions = {
13601377
index: start,
13611378
total: end ? end - start : null,
@@ -1398,26 +1415,72 @@ api.prototype.getTransfers = function(seed, options, callback) {
13981415
var finalBundles = [];
13991416
var tailTxArray = Array.from(tailTransactions);
14001417

1401-
async.map(tailTxArray, function(tailTx, cb) {
14021418

1403-
self.getBundle(tailTx, function(error, bundle) {
1419+
// If inclusionStates, get the confirmation status
1420+
// of the tail transactions, and thus the bundles
1421+
async.waterfall([
14041422

1405-
if (!error) {
1406-
finalBundles.push(bundle);
1407-
}
1408-
cb(null, true);
1409-
})
1410-
}, function(error, results) {
1423+
//
1424+
// 1. Function
1425+
//
1426+
function(cb) {
14111427

1412-
// credit: http://stackoverflow.com/a/8837505
1413-
// Sort by timestamp
1414-
finalBundles.sort(function(a, b) {
1415-
var x = parseInt(a[0]['timestamp']); var y = parseInt(b[0]['timestamp']);
1416-
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
1417-
});
1428+
if (inclusionStates) {
14181429

1419-
return callback(error, finalBundles);
1420-
})
1430+
self.getLatestInclusion(tailTxArray, function(error, states) {
1431+
1432+
// If error, return it to original caller
1433+
if (error) return callback(error);
1434+
1435+
cb(null, states);
1436+
})
1437+
} else {
1438+
cb(null, []);
1439+
}
1440+
},
1441+
1442+
//
1443+
// 2. Function
1444+
//
1445+
function(tailTxStates, cb) {
1446+
1447+
// Map each tail transaction to the getBundle function
1448+
// format the returned bundles and add inclusion states if necessary
1449+
async.map(tailTxArray, function(tailTx, cb2) {
1450+
1451+
self.getBundle(tailTx, function(error, bundle) {
1452+
1453+
// If error returned from getBundle, simply ignore it
1454+
// because the bundle was most likely incorrect
1455+
if (!error) {
1456+
1457+
// If inclusion states, add to each bundle entry
1458+
if (inclusionStates) {
1459+
var thisInclusion = tailTxStates[tailTxArray.indexOf(tailTx)];
1460+
1461+
bundle.forEach(function(bundleTx) {
1462+
1463+
bundleTx['persistence'] = thisInclusion;
1464+
})
1465+
}
1466+
1467+
finalBundles.push(bundle);
1468+
}
1469+
cb2(null, true);
1470+
})
1471+
}, function(error, results) {
1472+
1473+
// credit: http://stackoverflow.com/a/8837505
1474+
// Sort bundles by timestamp
1475+
finalBundles.sort(function(a, b) {
1476+
var x = parseInt(a[0]['timestamp']); var y = parseInt(b[0]['timestamp']);
1477+
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
1478+
});
1479+
1480+
return callback(error, finalBundles);
1481+
})
1482+
}
1483+
])
14211484
})
14221485
})
14231486
})

lib/utils/inputValidator.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var isTransfersArray = function(transfersArray) {
6767

6868
if (!isArray(transfersArray)) return false;
6969

70-
for (var i = 0; i < hashesArray.length; i++) {
70+
for (var i = 0; i < transfersArray.length; i++) {
7171

7272
var transfer = transfersArray[i];
7373

@@ -94,7 +94,6 @@ var isTransfersArray = function(transfersArray) {
9494
if (!isTrytes(tag, "0,27")) {
9595
return false;
9696
}
97-
9897
}
9998

10099
return true;
@@ -233,7 +232,7 @@ var isInputs = function(inputs) {
233232

234233
var input = inputs[i];
235234

236-
// If input does not have keyIndex and address, return false
235+
// If input does not have keyIndex and address, return false
237236
if (!input.hasOwnProperty('keyIndex') || !input.hasOwnProperty('address')) return false;
238237
}
239238

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iota.lib.js",
3-
"version": "0.0.4",
3+
"version": "0.0.4.1",
44
"description": "Javascript Library for the IOTA API.",
55
"main": "./lib/iota.js",
66
"scripts": {

0 commit comments

Comments
 (0)