This commit is contained in:
Gustavo Cortez 2014-07-07 18:13:26 -03:00
commit a1e5246727
44 changed files with 3762 additions and 365 deletions

View file

@ -13,6 +13,7 @@ var buffertools = bitcore.buffertools;
var Builder = bitcore.TransactionBuilder;
var SecureRandom = bitcore.SecureRandom;
var Base58Check = bitcore.Base58.base58Check;
var Address = bitcore.Address;
var AddressIndex = require('./AddressIndex');
var PublicKeyRing = require('./PublicKeyRing');
@ -38,12 +39,6 @@ function Wallet(opts) {
this.id = opts.id || Wallet.getRandomId();
this.name = opts.name;
// Renew token every 24hs
if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) {
this.token = opts.token;
this.tokenTime = opts.tokenTime;
}
this.verbose = opts.verbose;
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
@ -51,6 +46,7 @@ function Wallet(opts) {
this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {};
this.backupOffered = opts.backupOffered || false;
this.publicKey = this.privateKey.publicHex;
}
Wallet.parent = EventEmitter;
@ -81,8 +77,8 @@ Wallet.prototype.connectToAll = function() {
Wallet.prototype._handleIndexes = function(senderId, data, isInbound) {
this.log('RECV INDEXES:', data);
var inIndexes = AddressIndex.fromObj(data.indexes);
var hasChanged = this.publicKeyRing.indexes.merge(inIndexes);
var inIndexes = AddressIndex.fromList(data.indexes);
var hasChanged = this.publicKeyRing.mergeIndexes(inIndexes);
if (hasChanged) {
this.emit('publicKeyRingUpdated');
this.store();
@ -218,11 +214,6 @@ Wallet.prototype._optsToObj = function() {
version: this.version,
};
if (this.token) {
obj.token = this.token;
obj.tokenTime = new Date().getTime();
}
return obj;
};
@ -255,6 +246,7 @@ Wallet.decodeSecret = function(secretB) {
}
};
Wallet.prototype._lockIncomming = function() {
this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds());
};
@ -279,7 +271,6 @@ Wallet.prototype.netStart = function(callback) {
var startOpts = {
copayerId: myId,
privkey: myIdPriv,
token: self.token,
maxPeers: self.totalCopayers
};
@ -289,12 +280,10 @@ Wallet.prototype.netStart = function(callback) {
net.start(startOpts, function() {
self.emit('ready', net.getPeer());
self.token = net.peer.options.token;
setTimeout(function() {
self.emit('publicKeyRingUpdated', true);
self.scheduleConnect();
self.emit('txProposalsUpdated');
self.store();
}, 10);
});
};
@ -332,7 +321,8 @@ Wallet.prototype.getRegisteredPeerIds = function() {
var pid = this.network.peerFromCopayer(cid);
this.registeredPeerIds.push({
peerId: pid,
nick: this.publicKeyRing.nicknameForCopayer(cid)
nick: this.publicKeyRing.nicknameForCopayer(cid),
index: i,
});
}
}
@ -377,8 +367,6 @@ Wallet.fromObj = function(o, storage, network, blockchain) {
Wallet.prototype.toEncryptedObj = function() {
var walletObj = this.toObj();
delete walletObj.opts.token;
delete walletObj.opts.tokenTime;
return this.storage.export(walletObj);
};
@ -434,11 +422,12 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
});
};
Wallet.prototype.sendIndexes = function(recipients) {
this.log('### INDEXES TO:', recipients || 'All', this.publicKeyRing.indexes.toObj());
var indexes = AddressIndex.serialize(this.publicKeyRing.indexes);
this.log('### INDEXES TO:', recipients || 'All', indexes);
this.network.send(recipients, {
type: 'indexes',
indexes: this.publicKeyRing.indexes.toObj(),
indexes: indexes,
walletId: this.id,
});
};
@ -457,7 +446,7 @@ Wallet.prototype.getName = function() {
};
Wallet.prototype._doGenerateAddress = function(isChange) {
return this.publicKeyRing.generateAddress(isChange);
return this.publicKeyRing.generateAddress(isChange, this.publicKey);
};
@ -511,7 +500,6 @@ Wallet.prototype.sign = function(ntxid, cb) {
if (cb) cb(false);
}
var pkr = self.publicKeyRing;
var keys = self.privateKey.getForPaths(txp.inputChainPaths);
var b = txp.builder;
@ -583,7 +571,7 @@ Wallet.prototype.getAddressesStr = function(opts) {
};
Wallet.prototype.getAddressesInfo = function(opts) {
return this.publicKeyRing.getAddressesInfo(opts);
return this.publicKeyRing.getAddressesInfo(opts, this.publicKey);
};
Wallet.prototype.addressIsOwn = function(addrStr, opts) {
@ -691,7 +679,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos
opts = opts || {};
var amountSat = bignum(amountSatStr);
preconditions.checkArgument(new Address(toAddress).network().name === this.getNetworkName());
if (!pkr.isComplete()) {
throw new Error('publicKeyRing is not complete');
}
@ -749,31 +737,44 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos
Wallet.prototype.updateIndexes = function(callback) {
var self = this;
var start = self.publicKeyRing.indexes.changeIndex;
self.log('Updating indexes...');
self.indexDiscovery(start, true, 20, function(err, changeIndex) {
var tasks = this.publicKeyRing.indexes.map(function(index) {
return function(callback) {
self.updateIndex(index, callback);
};
});
async.parallel(tasks, function(err) {
if (err) callback(err);
self.log('Indexes updated');
self.emit('publicKeyRingUpdated');
self.store();
callback();
});
}
Wallet.prototype.updateIndex = function(index, callback) {
var self = this;
var SCANN_WINDOW = 20;
self.indexDiscovery(index.changeIndex, true, index.cosigner, SCANN_WINDOW, function(err, changeIndex) {
if (err) return callback(err);
if (changeIndex != -1)
self.publicKeyRing.indexes.changeIndex = changeIndex + 1;
index.changeIndex = changeIndex + 1;
start = self.publicKeyRing.indexes.receiveIndex;
self.indexDiscovery(start, false, 20, function(err, receiveIndex) {
self.indexDiscovery(index.receiveIndex, false, index.cosigner, SCANN_WINDOW, function(err, receiveIndex) {
if (err) return callback(err);
if (receiveIndex != -1)
self.publicKeyRing.indexes.receiveIndex = receiveIndex + 1;
self.log('Indexes updated');
self.emit('publicKeyRingUpdated');
self.store();
index.receiveIndex = receiveIndex + 1;
callback();
});
});
}
Wallet.prototype.deriveAddresses = function(index, amout, isChange) {
Wallet.prototype.deriveAddresses = function(index, amout, isChange, cosigner) {
var ret = new Array(amout);
for (var i = 0; i < amout; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange).toString();
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, cosigner).toString();
}
return ret;
}
@ -781,7 +782,7 @@ Wallet.prototype.deriveAddresses = function(index, amout, isChange) {
// This function scans the publicKeyRing branch starting at index @start and reports the index with last activity,
// using a scan window of @gap. The argument @change defines the branch to scan: internal or external.
// Returns -1 if no activity is found in range.
Wallet.prototype.indexDiscovery = function(start, change, gap, cb) {
Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
var scanIndex = start;
var lastActive = -1;
var hasActivity = false;
@ -791,7 +792,7 @@ Wallet.prototype.indexDiscovery = function(start, change, gap, cb) {
function _do(next) {
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change);
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, cosigner);
self.blockchain.checkActivity(addresses, function(err, actives) {
if (err) throw err;