diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index bd2f907a59..ea0481332f 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -1200,16 +1200,16 @@ Remote.prototype.requestTx = function(hash, callback) { /** * Account request abstraction * + * @this Remote * @api private */ -Remote.accountRequest = function(type, account, accountIndex, ledger, peer, callback) { +Remote.accountRequest = function(type, account, ledger, peer, callback) { if (typeof account === 'object') { var options = account; - callback = accountIndex; + callback = ledger; ledger = options.ledger; - accountIndex = options.account_index || options.accountIndex; - account = options.accountID || options.account; + account = options.account || options.accountID; peer = options.peer; } @@ -1222,18 +1222,10 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call var request = new Request(this, type); var account = UInt160.json_rewrite(account); - request.message.ident = account; //DEPRECATED; request.message.account = account; + request.ledgerSelect(ledger); - if (typeof accountIndex === 'number') { - request.message.index = accountIndex; - } - - if (!/^(undefined|function)$/.test(typeof ledger)) { - request.ledgerChoose(ledger); - } - - if (!/^(undefined|function)$/.test(typeof peer)) { + if (UInt160.is_valid(peer)) { request.message.peer = UInt160.json_rewrite(peer); } @@ -1246,6 +1238,7 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call * Request account_info * * @param {String} ripple address + * @param [String|Number] ledger identifier * @param [Function] callback * @return {Request} */ @@ -1259,6 +1252,7 @@ Remote.prototype.requestAccountInfo = function(account, callback) { * Request account_currencies * * @param {String} ripple address + * @param [String|Number] ledger identifier * @param [Function] callback * @return {Request} */ @@ -1272,14 +1266,13 @@ Remote.prototype.requestAccountCurrencies = function(account, callback) { * Request account_lines * * @param {String} ripple address - * @param {Number] sub-account index - * @param [String|Number] ledger + * @param [String|Number] ledger identifier * @param [String] peer * @param [Function] callback * @return {Request} */ -Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, peer, callback) { +Remote.prototype.requestAccountLines = function(account, peer, callback) { // XXX Does this require the server to be trusted? //utils.assert(this.trusted); var args = Array.prototype.concat.apply(['account_lines'], arguments); @@ -1290,19 +1283,16 @@ Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, p * Request account_offers * * @param {String} ripple address - * @param {Number] sub-account index - * @param [String|Number] ledger - * @param [String] peer + * @param [String|Number] ledger identifier * @param [Function] callback * @return {Request} */ -Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger, callback) { +Remote.prototype.requestAccountOffers = function(account, callback) { var args = Array.prototype.concat.apply(['account_offers'], arguments); return Remote.accountRequest.apply(this, args); }; - /** * Request account_tx * @@ -1318,9 +1308,6 @@ Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger, * @param [Number] offset, defaults to 0 * @param [Number] limit * - * @param [Function] filter - * @param [Function] map - * @param [Function] reduce * @param [Function] callback * @return {Request} */ @@ -1606,6 +1593,7 @@ Remote.prototype.requestLedgerAccept = function(callback) { /** * Account root request abstraction * + * @this Remote * @api private */ diff --git a/src/js/ripple/request.js b/src/js/ripple/request.js index 0d753fa283..18859536be 100644 --- a/src/js/ripple/request.js +++ b/src/js/ripple/request.js @@ -208,6 +208,13 @@ Request.prototype.ledgerIndex = function(ledger_index) { return this; }; +/** + * Set either ledger_index or ledger_hash based on heuristic + * + * @param {Number|String} ledger identifier + */ + +Request.prototype.selectLedger = Request.prototype.ledgerSelect = function(ledger) { switch (ledger) { case 'current': @@ -217,10 +224,10 @@ Request.prototype.ledgerSelect = function(ledger) { break; default: - if (isNaN(ledger)) { - this.message.ledger_hash = ledger; - } else if ((ledger = Number(ledger))) { - this.message.ledger_index = ledger; + if (Number(ledger)) { + this.message.ledger_index = Number(ledger); + } if (/^[A-F0-9]+$/.test(ledger)) { + this.message.ledger_hash = ledger; } break; } diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 7839a631d9..2279021295 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -276,7 +276,6 @@ describe('OrderBook', function() { assert.deepEqual(request.message, { command: 'account_info', id: void(0), - ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji', account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' }); request.emit('success', { @@ -1360,7 +1359,6 @@ describe('OrderBook', function() { assert.deepEqual(request.message, { command: 'account_info', id: undefined, - ident: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }); diff --git a/test/remote-test.js b/test/remote-test.js index f790fc6d33..a2365b478a 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -185,12 +185,43 @@ describe('Remote', function () { assert(request instanceof Request); }); - it('request account balance with ledger', function() { + it('request account info with ledger index', function() { + var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219); + assert.strictEqual(request.message.command, 'account_info'); + assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); + assert.strictEqual(request.message.ledger_index, 9592219); + }); + it('request account info with ledger hash', function() { + var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); + assert.strictEqual(request.message.command, 'account_info'); + assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); + assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); + }); + it('request account info with ledger identifier', function() { + var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated'); + assert.strictEqual(request.message.command, 'account_info'); + assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); + assert.strictEqual(request.message.ledger_index, 'validated'); + }); + + it('request account balance with ledger index', function() { var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219); assert.strictEqual(request.message.command, 'ledger_entry'); assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); assert.strictEqual(request.message.ledger_index, 9592219); }); + it('request account balance with ledger hash', function() { + var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); + assert.strictEqual(request.message.command, 'ledger_entry'); + assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); + assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); + }); + it('request account balance with ledger identifier', function() { + var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated'); + assert.strictEqual(request.message.command, 'ledger_entry'); + assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); + assert.strictEqual(request.message.ledger_index, 'validated'); + }); }) it('create remote and get pending transactions', function() { diff --git a/test/request-test.js b/test/request-test.js index 290daa40f7..b6abdb82e7 100644 --- a/test/request-test.js +++ b/test/request-test.js @@ -355,13 +355,20 @@ describe('Request', function() { assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); }); - it('Select ledger - hash', function() { + it('Select ledger - undefined', function() { var remote = new Remote(); remote._connected = true; var request = new Request(remote, 'server_info'); - request.ledgerSelect('B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); - assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); + request.ledgerSelect(); + assert.strictEqual(request.message.ledger_hash, void(0)); + assert.strictEqual(request.message.ledger_index, void(0)); + request.ledgerSelect(null); + assert.strictEqual(request.message.ledger_hash, void(0)); + assert.strictEqual(request.message.ledger_index, void(0)); + request.ledgerSelect(NaN); + assert.strictEqual(request.message.ledger_hash, void(0)); + assert.strictEqual(request.message.ledger_index, void(0)); }); it('Set account_root', function() {