Merge pull request #385 from matiu/feature/txprposal-limit

Feature/txprposal limit
This commit is contained in:
Mario Colque 2014-05-15 17:58:48 -03:00
commit 7a9e18023d
17 changed files with 141 additions and 97 deletions

View file

@ -1,7 +1,6 @@
'use strict';
angular.module('copay.footer').controller('FooterController',
function($scope) {
angular.module('copay.footer').controller('FooterController', function($scope, $http) {
if (config.themes && Array.isArray(config.themes) && config.themes[0]) {
$scope.themes = config.themes;
@ -15,5 +14,5 @@ angular.module('copay.footer').controller('FooterController',
$scope.change_theme = function(name) {
$scope.theme = 'css/tpl-' + name + '.css';
};
$scope.version = copay.version;
});

View file

@ -3,7 +3,7 @@
var imports = require('soop').imports();
var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
@ -14,7 +14,7 @@ function PrivateKey(opts) {
this.network = opts.networkName === 'testnet' ?
networks.testnet : networks.livenet;
var init = opts.extendedPrivateKeyString || this.network.name;
this.bip = opts.BIP32 || new BIP32(init);
this.bip = opts.HK || new HK(init);
this.privateKeyCache = opts.privateKeyCache || {};
};
@ -47,7 +47,7 @@ PrivateKey.prototype.getExtendedPrivateKeyString = function() {
return this.bip.extendedPrivateKeyString();
};
PrivateKey.prototype._getBIP32 = function(path) {
PrivateKey.prototype._getHK = function(path) {
if (typeof path === 'undefined') {
return this.bip;
}
@ -58,8 +58,8 @@ PrivateKey.prototype.get = function(index,isChange) {
var path = PublicKeyRing.Branch(index, isChange);
var pk = this.privateKeyCache[path];
if (!pk) {
var derivedBIP32 = this._getBIP32(path);
pk = this.privateKeyCache[path] = derivedBIP32.eckey.private.toString('hex');
var derivedHK = this._getHK(path);
pk = this.privateKeyCache[path] = derivedHK.eckey.private.toString('hex');
} else {
//console.log('cache hit!');
}

View file

@ -4,7 +4,7 @@
var imports = require('soop').imports();
var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var HK = bitcore.HierarchicalKey;
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil = bitcore.util;
@ -26,7 +26,7 @@ function PublicKeyRing(opts) {
this.requiredCopayers = opts.requiredCopayers || 3;
this.totalCopayers = opts.totalCopayers || 5;
this.copayersBIP32 = opts.copayersBIP32 || [];
this.copayersHK = opts.copayersHK || [];
this.changeAddressIndex= opts.changeAddressIndex || 0;
this.addressIndex= opts.addressIndex || 0;
@ -74,7 +74,7 @@ PublicKeyRing.prototype.toObj = function() {
changeAddressIndex: this.changeAddressIndex,
addressIndex: this.addressIndex,
copayersExtPubKeys: this.copayersBIP32.map( function (b) {
copayersExtPubKeys: this.copayersHK.map( function (b) {
return b.extendedPublicKeyString();
}),
nicknameFor: this.nicknameFor,
@ -87,7 +87,7 @@ PublicKeyRing.prototype.getCopayerId = function(i) {
};
PublicKeyRing.prototype.registeredCopayers = function () {
return this.copayersBIP32.length;
return this.copayersHK.length;
};
PublicKeyRing.prototype.isComplete = function () {
@ -109,14 +109,14 @@ PublicKeyRing.prototype._checkKeys = function() {
};
PublicKeyRing.prototype._newExtendedPublicKey = function () {
return new BIP32(this.network.name)
return new HK(this.network.name)
.extendedPublicKeyString();
};
PublicKeyRing.prototype._updateBip = function (index) {
var path = PublicKeyRing.ID_BRANCH;
var bip32 = this.copayersBIP32[index].derive(path);
this.copayerIds[index]= bip32.eckey.public.toString('hex');
var hk = this.copayersHK[index].derive(path);
this.copayerIds[index]= hk.eckey.public.toString('hex');
};
PublicKeyRing.prototype._setNicknameForIndex = function (index, nickname) {
@ -139,14 +139,14 @@ PublicKeyRing.prototype.addCopayer = function (newEpk, nickname) {
newEpk = this._newExtendedPublicKey();
}
this.copayersBIP32.forEach(function(b){
this.copayersHK.forEach(function(b){
if (b.extendedPublicKeyString() === newEpk)
throw new Error('already have that key');
});
var i=this.copayersBIP32.length;
var bip = new BIP32(newEpk);
this.copayersBIP32.push(bip);
var i=this.copayersHK.length;
var bip = new HK(newEpk);
this.copayersHK.push(bip);
this._updateBip(i);
if (nickname) {
this._setNicknameForIndex(i,nickname);
@ -161,10 +161,10 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
var pubKeys = this.publicKeysCache[path];
if (!pubKeys) {
pubKeys = [];
var l = this.copayersBIP32.length;
var l = this.copayersHK.length;
for(var i=0; i<l; i++) {
var bip32 = this.copayersBIP32[i].derive(path);
pubKeys[i] = bip32.eckey.public;
var hk = this.copayersHK[i].derive(path);
pubKeys[i] = hk.eckey.public;
}
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
}
@ -307,15 +307,15 @@ PublicKeyRing.prototype._mergeIndexes = function(inPKR) {
PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
var self = this;
var hasChanged = false;
var l= self.copayersBIP32.length;
var l= self.copayersHK.length;
if (self.isComplete())
return;
inPKR.copayersBIP32.forEach( function(b) {
inPKR.copayersHK.forEach( function(b) {
var haveIt = false;
var epk = b.extendedPublicKeyString();
for(var j=0; j<l; j++) {
if (self.copayersBIP32[j].extendedPublicKeyString() === epk) {
if (self.copayersHK[j].extendedPublicKeyString() === epk) {
haveIt=true;
break;
}
@ -324,8 +324,8 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
if (self.isComplete()) {
throw new Error('trying to add more pubkeys, when PKR isComplete at merge');
}
var l2 = self.copayersBIP32.length;
self.copayersBIP32.push(new BIP32(epk));
var l2 = self.copayersHK.length;
self.copayersHK.push(new HK(epk));
self._updateBip(l2);
if (inPKR.nicknameFor[self.getCopayerId(l2)])
self._setNicknameForIndex(l2,inPKR.nicknameFor[self.getCopayerId(l2)]);

View file

@ -72,10 +72,17 @@ TxProposals.fromObj = function(o) {
return ret;
};
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposals.prototype.toObj = function() {
TxProposals.prototype.toObj = function(onlyThisNtxid) {
var ret = [];
for(var id in this.txps){
if (onlyThisNtxid && id != onlyThisNtxid)
continue;
var t = this.txps[id];
if (!t.sent)
ret.push(t.toObj());
@ -87,7 +94,6 @@ TxProposals.prototype.toObj = function() {
};
};
TxProposals.prototype._startMerge = function(myTxps, theirTxps) {
var fromUs=0, fromTheirs=0, merged =0;
var toMerge = {}, ready={};
@ -182,6 +188,7 @@ TxProposals.prototype._mergeBuilder = function(myTxps, theirTxps, mergeInfo) {
TxProposals.prototype.add = function(data) {
var ntxid = data.builder.build().getNormalizedHash().toString('hex');
this.txps[ntxid] = new TxProposal(data);
return ntxid;
};

View file

@ -18,7 +18,7 @@ function Wallet(opts) {
//required params
['storage', 'network', 'blockchain',
'requiredCopayers', 'totalCopayers', 'spendUnconfirmed',
'publicKeyRing', 'txProposals', 'privateKey'
'publicKeyRing', 'txProposals', 'privateKey', 'version'
].forEach(function(k) {
if (typeof opts[k] === 'undefined') throw new Error('missing key:' + k);
self[k] = opts[k];
@ -89,15 +89,26 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
var recipients;
var inTxp = copay.TxProposals.fromObj(data.txProposals);
var ids = inTxp.getNtxids();
if (ids.lenght>1) {
this.emit('badMessage', senderId);
this.log('Received BAD TxProposal messsage FROM:', senderId); //TODO
return;
}
var newId = ids[0];
var mergeInfo = this.txProposals.merge(inTxp, true);
var addSeen = this.addSeenToTxProposals();
if (mergeInfo.hasChanged || addSeen) {
this.log('### BROADCASTING txProposals. ');
recipients = null;
this.sendTxProposals(recipients);
this.sendTxProposals(recipients, newId);
}
if (data.lastInBatch) {
this.emit('txProposalsUpdated', this.txProposals);
this.store();
}
this.emit('txProposalsUpdated', this.txProposals);
this.store();
};
Wallet.prototype._handleData = function(senderId, data, isInbound) {
@ -114,7 +125,7 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
break;
case 'walletReady':
this.sendPublicKeyRing(senderId);
this.sendTxProposals(senderId);
this.sendTxProposals(senderId); // send old
break;
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
@ -146,6 +157,7 @@ Wallet.prototype._optsToObj = function() {
totalCopayers: this.totalCopayers,
name: this.name,
netKey: this.netKey,
version: this.version,
};
return obj;
@ -285,14 +297,23 @@ Wallet.prototype.toEncryptedObj = function() {
return this.storage.export(walletObj);
};
Wallet.prototype.sendTxProposals = function(recipients) {
Wallet.prototype.sendTxProposals = function(recipients, ntxid) {
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
this.network.send(recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(),
walletId: this.id,
});
var toSend = ntxid ? [ntxid] : this.txProposals.getNtxids();
var last = toSend[toSend];
for(var i in toSend) {
var id = toSend[i];
var lastInBatch = (i == toSend.length - 1);
this.network.send(recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(id),
walletId: this.id,
lastInBatch: lastInBatch,
});
}
};
Wallet.prototype.sendWalletReady = function(recipients) {
@ -356,7 +377,7 @@ Wallet.prototype.reject = function(ntxid) {
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
txp.rejectedBy[myId] = Date.now();
this.sendTxProposals();
this.sendTxProposals(null, ntxid);
this.store();
this.emit('txProposalsUpdated');
};
@ -377,7 +398,7 @@ Wallet.prototype.sign = function(ntxid) {
if (b.signaturesAdded > before) {
txp.signedBy[myId] = Date.now();
this.sendTxProposals();
this.sendTxProposals(null, ntxid);
this.store();
this.emit('txProposalsUpdated');
return true;
@ -403,7 +424,7 @@ Wallet.prototype.sendTx = function(ntxid, cb) {
if (txid) {
self.txProposals.setSent(ntxid, txid);
}
self.sendTxProposals();
self.sendTxProposals(null, ntxid);
self.store();
return cb(txid);
});
@ -453,15 +474,15 @@ Wallet.prototype.addressIsOwn = function(addrStr, opts) {
return ret;
};
Wallet.prototype.getBalance = function(safe, cb) {
Wallet.prototype.getBalance = function(cb) {
var balance = 0;
var safeBalance = 0;
var balanceByAddr = {};
var isMain = {};
var COIN = bitcore.util.COIN;
var f = safe ? this.getSafeUnspent.bind(this) : this.getUnspent.bind(this);
f(function(utxos) {
for (var i = 0; i < utxos.length; i++) {
var u = utxos[i];
this.getUnspent(function(safeUnspent, unspent) {
for (var i = 0; i < unspent.length; i++) {
var u = unspent[i];
var amt = u.amount * COIN;
balance += amt;
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt;
@ -472,30 +493,30 @@ Wallet.prototype.getBalance = function(safe, cb) {
balanceByAddr[a] = balanceByAddr[a] / COIN;
}
balance = balance / COIN;
return cb(balance, balanceByAddr, isMain);
for (var i = 0; i < safeUnspent.length; i++) {
var u = safeUnspent[i];
var amt = u.amount * COIN;
safeBalance += amt;
}
safeBalance = safeBalance / COIN;
return cb(balance, balanceByAddr, safeBalance);
});
};
Wallet.prototype.getUnspent = function(cb) {
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
return cb(unspentList);
});
};
Wallet.prototype.getSafeUnspent = function(cb) {
var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
var ret = [];
var safeUnspendList = [];
var maxRejectCount = self.totalCopayers - self.requiredCopayers;
var uu = self.txProposals.getUsedUnspent(maxRejectCount);
for (var i in unspentList) {
if (uu.indexOf(unspentList[i].txid) === -1)
ret.push(unspentList[i]);
safeUnspendList.push(unspentList[i]);
}
return cb(ret);
return cb(safeUnspendList, unspentList);
});
};
@ -512,10 +533,11 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.spendUnconfirmed = this.spendUnconfirmed;
}
self.getSafeUnspent(function(unspentList) {
if (self.createTxSync(toAddress, amountSatStr, unspentList, opts)) {
self.sendPublicKeyRing(); // Change Address
self.sendTxProposals();
self.getUnspent(function(safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts);
if (ntxid) {
self.sendPublicKeyRing(); // For the new change Address
self.sendTxProposals(null, ntxid);
self.store();
self.emit('txProposalsUpdated');
}
@ -569,8 +591,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
builder: b,
};
this.txProposals.add(data);
return true;
return this.txProposals.add(data);
};
Wallet.prototype.disconnect = function() {

View file

@ -13,14 +13,9 @@ var Wallet = require('./Wallet');
/*
* WalletFactory
*
*
* var wallet = WF.read(config,walletId); -> always go to storage
* var wallet = WF.create(config,walletId); -> create wallets, with the given ID (or random is not given)
*
* var wallet = WF.open(config,walletId); -> try to read walletId, if fails, create a new wallet with that id
*/
function WalletFactory(config) {
function WalletFactory(config, version) {
var self = this;
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
@ -29,6 +24,7 @@ function WalletFactory(config) {
this.networkName = config.networkName;
this.verbose = config.verbose;
this.walletDefaults = config.wallet;
this.version = version;
}
WalletFactory.prototype.log = function(){
@ -129,11 +125,28 @@ WalletFactory.prototype.create = function(opts) {
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = this.version;
var w = new Wallet(opts);
w.store();
return w;
};
WalletFactory.prototype._checkVersion = function(inVersion) {
var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
//We only check for major version differences
if( thisV0 < inV0 ) {
throw new Error('Major difference in software versions' +
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
}
};
WalletFactory.prototype.open = function(walletId, opts) {
opts = opts || {};
opts.id = walletId;
@ -141,11 +154,12 @@ WalletFactory.prototype.open = function(walletId, opts) {
this.storage._setPassphrase(opts.passphrase);
var w = this.read(walletId);
if (w) {
this._checkVersion(w.version);
w.store();
}
return w;
};

View file

@ -101,23 +101,21 @@ angular.module('copay.controllerUtils')
};
root.updateBalance = function(cb) {
console.log('Updating balance...');
$rootScope.balanceByAddr = {};
var w = $rootScope.wallet;
$rootScope.addrInfos = w.getAddressesInfo();
if ($rootScope.addrInfos.length === 0) return;
$rootScope.loading = true;
w.getBalance(false, function(balance, balanceByAddr) {
w.getBalance(function(balance, balanceByAddr, safeBalance) {
$rootScope.loading = false;
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
$rootScope.loading = false;
$rootScope.$digest();
if (cb) cb();
});
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.loading = false;
$rootScope.availableBalance = safeBalance;
$rootScope.$digest();
console.log('Done updating balance.'); //TODO
if (cb) cb();
});
root.setSocketHandlers();

View file

@ -1,7 +1,3 @@
'use strict';
var passphrase;
angular.module('copay.passphrase').factory('Passphrase', function($rootScope) {
passphrase = passphrase || new copay.Passphrase(config.passphrase);
return passphrase;
});
angular.module('copay.passphrase').value('Passphrase', new copay.Passphrase(config.passphrase));

View file

@ -1,4 +1,4 @@
'use strict';
angular.module('copay.walletFactory').value('walletFactory', new copay.WalletFactory(config));
angular.module('copay.walletFactory').value('walletFactory', new copay.WalletFactory(config, copay.version));