Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #123 from blockchain/small-refactor
Browse files Browse the repository at this point in the history
Small refactor
  • Loading branch information
mpfluger committed Feb 19, 2016
2 parents 4fa2ba8 + 883a33a commit 6216250
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 2,316 deletions.
10 changes: 3 additions & 7 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ module.exports = function(karma) {
// 'tests/**/*.coffee',
// Or specify individual test files:
'tests/mocks/*.coffee',
//'tests/wallet_spender_spec.js.coffee', //(FAIL)
// 'tests/claim_redeem_spec.js.coffee', //(requires refactor)
'tests/transaction_spec.js.coffee', //(OK)
'tests/transaction_spend_spec.js.coffee', //(OK)
'tests/wallet_spec.js.coffee', //(PARTIAL)
'tests/wallet_store_spec.js.coffee',
'tests/bip38_spec.js.coffee', //(OK)
'tests/transaction_spend_spec.js.coffee',
'tests/wallet_spec.js.coffee',
'tests/bip38_spec.js.coffee',
'tests/address_spec.js.coffee',
'tests/keychain_spec.js.coffee',
'tests/keyring_spec.js.coffee',
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ BlockchainSocket.prototype.connect = function (onOpen, onMessage, onClose) {
var connect = this.connectOnce.bind(this, onOpen, onMessage, onClose);
if (!this.socket || this.socket.readyState === 3) connect();
}.bind(this);
var pingSocket = function () { this.send('{"op":"ping_block"}'); }.bind(this);
var pingSocket = function () { this.send('{"op":"ping"}'); }.bind(this);
this.reconnect();
this.reconnectInterval = setInterval(this.reconnect, 20000);
this.pingInterval = setInterval(pingSocket, 30013);
Expand Down
85 changes: 18 additions & 67 deletions src/blockchain-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,7 @@ function Wallet(object) {
this._totalReceived = 0;
this._finalBalance = 0;
this._numberTxTotal = 0;
this._numberTxFetched = 0;
this._txPerScroll = 50;

var getWalletContext = function () {
var xpubs = this.hdwallet && this.hdwallet.xpubs;
return this.addresses.concat(xpubs || []);
}.bind(this);

this._txList = new TxList(getWalletContext, this._txPerScroll);
this._txList = new TxList();
}

Object.defineProperties(Wallet.prototype, {
Expand All @@ -97,6 +89,13 @@ Object.defineProperties(Wallet.prototype, {
configurable: false,
get: function() { return this._sharedKey;}
},
"context": {
configurable: false,
get: function() {
var xpubs = this.hdwallet && this.hdwallet.xpubs;
return this.addresses.concat(xpubs || []);
}
},
"isDoubleEncrypted": {
configurable: false,
get: function() { return this._double_encryption;}
Expand Down Expand Up @@ -170,20 +169,6 @@ Object.defineProperties(Wallet.prototype, {
throw 'Error: wallet.numberTx must be a number';
}
},
"numberTxFetched": {
configurable: false,
get: function() { return this._numberTxFetched;},
set: function(value) {
if(Helpers.isNumber(value))
this._numberTxFetched = value;
else
throw 'Error: wallet.numberTxFetched must be a number';
}
},
"txPerScroll": {
configurable: false,
get: function() { return this._txPerScroll;}
},
"addresses": {
configurable: false,
get: function(){return Object.keys(this._addresses);}
Expand Down Expand Up @@ -317,10 +302,6 @@ Object.defineProperties(Wallet.prototype, {
// update-wallet-balances after multiaddr call
Wallet.prototype._updateWalletInfo = function(obj) {

// delete all transactions stored
var transactions = WalletStore.getTransactions();
transactions.length = 0;

if (obj.info) {
if (obj.info.symbol_local)
shared.setLocalSymbol(obj.info.symbol_local);
Expand All @@ -335,7 +316,6 @@ Wallet.prototype._updateWalletInfo = function(obj) {
this.totalReceived = 0;
this.finalBalance = 0;
this.numberTxTotal = 0;
this.numberTxFetched = 0;
return true;
}

Expand All @@ -352,9 +332,7 @@ Wallet.prototype._updateWalletInfo = function(obj) {
account.n_tx = e.n_tx;
account.lastUsedReceiveIndex = e.account_index;
account.receiveIndex = Math.max(account.lastUsedReceiveIndex, account.maxLabeledReceiveIndex);

account.changeIndex = e.change_index;

if (account.getLabelForReceivingAddress(account.receiveIndex)) {
account.incrementReceiveIndex();
}
Expand All @@ -368,53 +346,26 @@ Wallet.prototype._updateWalletInfo = function(obj) {
}
};

obj.addresses.forEach(updateAccountAndAddressesInfo.bind(this));

this.numberTxFetched += obj.txs.length;
for (var i = 0; i < obj.txs.length; ++i) {
var tx = shared.TransactionFromJSON(obj.txs[i]);
WalletStore.pushTransaction(tx);
}

if (obj.info.latest_block)
WalletStore.setLatestBlock(obj.info.latest_block);

obj.addresses.forEach(updateAccountAndAddressesInfo.bind(this));
this.txList.pushTxs(obj.txs);

WalletStore.sendEvent('did_multiaddr');

return true;
return obj.txs.length;
};

// equivalent to MyWallet.get_history(success, error) but returning a promise
Wallet.prototype.getHistory = function() {
var allAddresses = this.activeAddresses;
if (this.isUpgradedToHD) {
this.hdwallet.accounts.forEach(
function(account){ allAddresses.push(account.extendedPublicKey);}
);
}
// TODO: obtain paidTo addresses too
var promise = API.getHistory(allAddresses, 0 ,0, 50).then(this._updateWalletInfo.bind(this));
return promise;
};

Wallet.prototype.fetchMoreTransactions = function(didFetchOldestTransaction) {
var xpubs = this.isUpgradedToHD ? this.hdwallet.activeXpubs : [];
var list = this.activeAddresses.concat(xpubs);
var txListP = API.getHistory(list, null, this.numberTxFetched, this.txPerScroll);
function processTxs(data) {
var pTx = data.txs.map(MyWallet.processTransaction.compose(shared.TransactionFromJSON));
this.numberTxFetched += pTx.length;
if (pTx.length < this.txPerScroll) { didFetchOldestTransaction(); }
return pTx;
}
return txListP.then(processTxs.bind(this));
return API.getHistory(this.context, 0, 0, this.txList.loadNumber)
.then(function (obj) { this.txList.wipe(); return obj; }.bind(this))
.then(this._updateWalletInfo.bind(this));
};

Wallet.prototype.ask100TxTest = function(){
var context = this.activeAddresses.concat(this.hdwallet.activeXpubs);
var txListP = API.getHistory(context, null, 0, 100);
function processTxs(data) { return data.txs.map(Tx.factory);};
return txListP.then(processTxs);
Wallet.prototype.fetchTransactions = function() {
return API.getHistory(this.context, 0, this.txList.fetched, this.txList.loadNumber)
.then(this._updateWalletInfo.bind(this));
};
////////////////////////////////////////////////////////////////////////////////

Expand Down
64 changes: 0 additions & 64 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ module.exports = {
setBTCSymbol: setBTCSymbol,
playSound: playSound,
sShift: sShift,
webSocketConnect: webSocketConnect,
TransactionFromJSON: TransactionFromJSON,
BlockFromJSON: BlockFromJSON
};

Expand Down Expand Up @@ -85,47 +83,6 @@ function sShift(symbol) {
return (satoshi / symbol.conversion).toString().length-1;
}

var ws;
var reconnectInterval;
function webSocketConnect(success) {
try {
function reallyConnect() {
try {
var url = "wss://blockchain.info/inv";

console.log('Connect ' + url);

ws = new WebSocket(url);

if (!ws) {
return;
}

if (success)
success(ws);
} catch (e) {
console.log(e);
}
}

//Updates time last block was received and check for websocket connectivity
function reconnectTimer () {
if (!ws || ws.readyState == WebSocket.CLOSED) {
reallyConnect();
}
}

if (window.WebSocket) {
reallyConnect();

if (!reconnectInterval)
reconnectInterval = setInterval(reconnectTimer, 20000);
}
} catch (e) {
console.log(e);
}
}

function BlockFromJSON(json) {
return {
hash : json.hash,
Expand All @@ -139,27 +96,6 @@ function BlockFromJSON(json) {
};
}

function TransactionFromJSON(json) {
return {
hash : json.hash,
size : json.size,
txIndex : json.tx_index,
time : json.time,
inputs : json.inputs,
out : json.out,
blockIndex : json.block_index,
// result : json.result,
blockHeight : json.block_height,
balance : json.balance,
double_spend : json.double_spend,
note : json.note,
account_indexes : [], // should be filled later
setConfirmations : function(n_confirmations) {
this.confirmations = n_confirmations;
}
};
}

function formatSatoshi(value, shift, no_comma) {
if (!value)
return '0.00';
Expand Down
39 changes: 13 additions & 26 deletions src/transaction-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
var assert = require('assert')
, EventEmitter = require('events')
, Helpers = require('./helpers')
, API = require('./api')
, Tx = require('./wallet-transaction');

var TransactionList = function (getContext, loadNumber) {
var TransactionList = function (loadNumber) {
var DEFAULT_TX_LOAD = 50;
this._loadNumber = loadNumber || DEFAULT_TX_LOAD;
this._getContext = getContext;
this._context = getContext();
this._transactions = [];
this._txsFetched = 0;
this._events = new EventEmitter();
};

Expand All @@ -27,6 +23,12 @@ Object.defineProperties(TransactionList.prototype, {
});
}
},
'transactionsForIOS': {
configurable: false,
get: function () {
return this._transactions.map(Tx.IOSfactory);
}
},
'transaction': {
configurable: false,
value: function (hash) {
Expand All @@ -38,22 +40,15 @@ Object.defineProperties(TransactionList.prototype, {
'loadNumber': {
configurable: false,
get: function () { return this._loadNumber; }
},
'fetched': {
configurable: false,
get: function () { return this._transactions.length; }
}
});

TransactionList.prototype.fetchTxs = function (amount, refresh) {
var refresh = this._getContext().join() !== this._context.join() || refresh
, context = this._context = refresh ? this._getContext() : this._context
, txIndex = refresh ? 0 : this._txsFetched
, amount = amount || this.loadNumber
, txListP = API.getHistory(context, null, txIndex, amount);
var processTxs = (function (data) {
if (refresh) { this._transactions = []; this._txsFetched = 0; }
this.pushTxs(data.txs);
this._txsFetched += data.txs.length;
return data.txs.length;
}).bind(this);
return txListP.then(processTxs);
TransactionList.prototype.wipe = function () {
this._transactions = [];
};

TransactionList.prototype.pushTxs = function (txs) {
Expand All @@ -64,14 +59,6 @@ TransactionList.prototype.pushTxs = function (txs) {
this._events.emit('update');
};

TransactionList.prototype.shiftTxs = function (txs) {
txs = Helpers.toArrayFormat(txs).map(Tx.factory).filter(function (tx) {
return !this.transaction(tx.hash);
}.bind(this));
this._transactions = txs.concat(this._transactions);
this._events.emit('update');
};

TransactionList.prototype.subscribe = function (listener) {
if ('function' !== typeof listener) return;
this._events.addListener('update', listener);
Expand Down
19 changes: 0 additions & 19 deletions src/wallet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ var WalletStore = (function() {
var password; //Password
var guid; //Wallet identifier
var language = 'en';
var transactions = [];
var pbkdf2_iterations = 5000; // pbkdf2_interations of the main password (to encrypt the full payload)
var disable_logout = false;
var mixer_fee = 0.5;
Expand Down Expand Up @@ -101,18 +100,6 @@ var WalletStore = (function() {
getCurrencies: function() {
return currencyCodeToCurrency;
},
getTransaction: function(hash) {
return transactions.filter(function(tx){return tx.hash === hash})[0]
},
getTransactions: function() {
return transactions;
},
pushTransaction: function(tx) {
transactions.push(tx);
},
getAllTransactions: function() {
return transactions.map(MyWallet.processTransaction);
},
disableLogout: function() {
disable_logout = true;
},
Expand All @@ -126,14 +113,8 @@ var WalletStore = (function() {
return latest_block;
},
setLatestBlock: function(block) {
var i, len, ref, tx;
if (block != null) {
latest_block = block;
ref = this.getTransactions();
for (i = 0, len = ref.length; i < len; i++) {
tx = ref[i];
tx.setConfirmations(MyWallet.getConfirmationsForTx(latest_block, tx));
}
this.sendEvent('did_set_latest_block');
}
},
Expand Down

0 comments on commit 6216250

Please sign in to comment.