From d8d0ae78f9103d834d6ceb345662f7b37188d098 Mon Sep 17 00:00:00 2001 From: pernas Date: Tue, 28 Jun 2016 19:22:47 -0400 Subject: [PATCH] feat(txlist): independent tx-list fetching for each account. --- src/api.js | 6 +++++- src/blockchain-wallet.js | 24 ++++++++++++++++++++++++ src/hd-account.js | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/api.js b/src/api.js index d685787fa..17051fb76 100644 --- a/src/api.js +++ b/src/api.js @@ -179,7 +179,7 @@ API.prototype.getUnspent = function (fromAddresses, confirmations) { return this.retry(this.request.bind(this, 'POST', 'unspent', data)); }; -API.prototype.getHistory = function (addresses, txFilter, offset, n, syncBool) { +API.prototype.getHistory = function (addresses, txFilter, offset, n, syncBool, onlyShow) { var clientTime = (new Date()).getTime(); offset = offset || 0; n = n || 0; @@ -195,6 +195,10 @@ API.prototype.getHistory = function (addresses, txFilter, offset, n, syncBool) { no_buttons: true }; + if (onlyShow) { + data.onlyShow = onlyShow.join('|'); + } + if (txFilter !== undefined && txFilter !== null) { data.filter = txFilter; } diff --git a/src/blockchain-wallet.js b/src/blockchain-wallet.js index ba14f741e..f96d797da 100644 --- a/src/blockchain-wallet.js +++ b/src/blockchain-wallet.js @@ -71,6 +71,7 @@ function Wallet (object) { this._finalBalance = 0; this._numberTxTotal = 0; this._txList = new TxList(); + this._txListLegacy = new TxList(); this._latestBlock = null; this._accountInfo = null; this._external = null; @@ -156,6 +157,10 @@ Object.defineProperties(Wallet.prototype, { configurable: false, get: function () { return this._txList; } }, + 'txListLegacy': { + configurable: false, + get: function () { return this._txListLegacy; } + }, 'numberTxTotal': { configurable: false, get: function () { return this._numberTxTotal; }, @@ -384,6 +389,25 @@ Wallet.prototype.fetchTransactions = function () { .then(this._updateWalletInfo.bind(this)); }; +Wallet.prototype.fetchTransactionsForLegacy = function () { + var _updateAllLegacy = function (o) { + var updateAddress = function (e) { + var address = this.activeKey(e.address); + if (address) { + address.balance = e.final_balance; + address.totalReceived = e.total_received; + address.totalSent = e.total_sent; + } + }; + o.addresses.forEach(updateAddress.bind(this)); + this.txListLegacy.pushTxs(o.txs); + return o.txs.length; + }; + return API.getHistory(this.context, 0, this.txListLegacy.fetched, + this.txListLegacy.loadNumber, false, this.activeAddresses) + .then(_updateAllLegacy.bind(this)); +}; + Wallet.prototype.getBalancesForArchived = function () { var updateBalance = function (key) { if (this.containsLegacyAddress(key.address)) { diff --git a/src/hd-account.js b/src/hd-account.js index 8b5ff2be9..29a786a27 100644 --- a/src/hd-account.js +++ b/src/hd-account.js @@ -7,6 +7,8 @@ var assert = require('assert'); var Helpers = require('./helpers'); var KeyRing = require('./keyring'); var MyWallet = require('./wallet'); // This cyclic import should be avoided once the refactor is complete +var TxList = require('./transaction-list'); +var API = require('./api'); // HDAccount Class @@ -32,6 +34,7 @@ function HDAccount (object) { this._lastUsedReceiveIndex = 0; this._changeIndex = 0; this._n_tx = 0; + this._txList = new TxList(); this._balance = null; this._index = Helpers.isPositiveInteger(obj.index) ? obj.index : null; } @@ -63,6 +66,10 @@ Object.defineProperties(HDAccount.prototype, { } } }, + 'txList': { + configurable: false, + get: function () { return this._txList; } + }, 'n_tx': { get: function () { return this._n_tx; }, set: function (num) { @@ -345,3 +352,21 @@ HDAccount.prototype.persist = function () { delete this._temporal_xpriv; return this; }; + +HDAccount.prototype.fetchTransactions = function () { + var _updateAccount = function (o) { + this.balance = o.addresses[0].final_balance; + this.n_tx = o.addresses[0].n_tx; + this.lastUsedReceiveIndex = o.addresses[0].account_index; + this.receiveIndex = Math.max(this.lastUsedReceiveIndex, this.maxLabeledReceiveIndex); + this.changeIndex = o.addresses[0].change_index; + if (this.getLabelForReceivingAddress(this.receiveIndex)) { + this.incrementReceiveIndex(); + } + this.txList.pushTxs(o.txs); + return o.txs.length; + }; + return API.getHistory(MyWallet.wallet.context, 0, this.txList.fetched, + this.txList.loadNumber, false, [this._xpub]) + .then(_updateAccount.bind(this)); +};