Merge pull request #816 from yemel/feature/bipNNN-refactor
Feature/bip nnn refactor
This commit is contained in:
commit
f497d76405
15 changed files with 394 additions and 154 deletions
|
|
@ -1,29 +1,56 @@
|
|||
'use strict';
|
||||
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var Structure = require('./Structure');
|
||||
|
||||
function AddressIndex(opts) {
|
||||
opts = opts || {};
|
||||
this.walletId = opts.walletId;
|
||||
|
||||
this.cosigner = opts.cosigner
|
||||
this.changeIndex = opts.changeIndex || 0;
|
||||
this.receiveIndex = opts.receiveIndex || 0;
|
||||
|
||||
if (typeof this.cosigner === 'undefined') {
|
||||
this.cosigner = Structure.SHARED_INDEX;
|
||||
}
|
||||
}
|
||||
|
||||
AddressIndex.init = function(totalCopayers) {
|
||||
preconditions.shouldBeNumber(totalCopayers);
|
||||
var indexes = [new AddressIndex()];
|
||||
for (var i = 0 ; i < totalCopayers ; i++) {
|
||||
indexes.push(new AddressIndex({cosigner: i}));
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
AddressIndex.fromList = function(indexes) {
|
||||
return indexes.map(function(i) { return AddressIndex.fromObj(i); });
|
||||
}
|
||||
|
||||
AddressIndex.fromObj = function(data) {
|
||||
if (data instanceof AddressIndex) {
|
||||
throw new Error('bad data format: Did you use .toObj()?');
|
||||
}
|
||||
var ret = new AddressIndex(data);
|
||||
return ret;
|
||||
return new AddressIndex(data);
|
||||
};
|
||||
|
||||
AddressIndex.serialize = function(indexes) {
|
||||
return indexes.map(function(i) { return i.toObj(); });
|
||||
}
|
||||
|
||||
AddressIndex.update = function(shared, totalCopayers) {
|
||||
var indexes = this.init(totalCopayers);
|
||||
indexes[0].changeIndex = shared.changeIndex;
|
||||
indexes[0].receiveIndex = shared.receiveIndex;
|
||||
return this.serialize(indexes);
|
||||
};
|
||||
|
||||
AddressIndex.prototype.toObj = function() {
|
||||
return {
|
||||
walletId: this.walletId,
|
||||
cosigner: this.cosigner,
|
||||
changeIndex: this.changeIndex,
|
||||
receiveIndex: this.receiveIndex,
|
||||
receiveIndex: this.receiveIndex
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -34,10 +61,10 @@ AddressIndex.prototype.checkRange = function(index, isChange) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
AddressIndex.prototype.getChangeIndex = function() {
|
||||
return this.changeIndex;
|
||||
};
|
||||
|
||||
AddressIndex.prototype.getReceiveIndex = function() {
|
||||
return this.receiveIndex;
|
||||
};
|
||||
|
|
@ -51,6 +78,9 @@ AddressIndex.prototype.increment = function(isChange) {
|
|||
};
|
||||
|
||||
AddressIndex.prototype.merge = function(inAddressIndex) {
|
||||
preconditions.shouldBeObject(inAddressIndex)
|
||||
.checkArgument(this.cosigner == inAddressIndex.cosigner);
|
||||
|
||||
var hasChanged = false;
|
||||
|
||||
// Indexes
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ function PrivateKey(opts) {
|
|||
var init = opts.extendedPrivateKeyString || this.network.name;
|
||||
this.bip = opts.HK || new HK(init);
|
||||
this.privateKeyCache = opts.privateKeyCache || {};
|
||||
this.publicHex = this.deriveBIP45Branch().eckey.public.toString('hex');
|
||||
};
|
||||
|
||||
PrivateKey.prototype.getId = function() {
|
||||
|
|
@ -101,21 +102,21 @@ PrivateKey.prototype.getForPath = function(path) {
|
|||
return wk;
|
||||
};
|
||||
|
||||
PrivateKey.prototype.get = function(index, isChange) {
|
||||
var path = Structure.FullBranch(index, isChange);
|
||||
PrivateKey.prototype.get = function(index, isChange, cosigner) {
|
||||
var path = Structure.FullBranch(index, isChange, cosigner);
|
||||
return this.getForPath(path);
|
||||
};
|
||||
|
||||
PrivateKey.prototype.getAll = function(receiveIndex, changeIndex) {
|
||||
PrivateKey.prototype.getAll = function(receiveIndex, changeIndex, cosigner) {
|
||||
if (typeof receiveIndex === 'undefined' || typeof changeIndex === 'undefined')
|
||||
throw new Error('Invalid parameters');
|
||||
|
||||
var ret = [];
|
||||
for (var i = 0; i < receiveIndex; i++) {
|
||||
ret.push(this.get(i, false));
|
||||
ret.push(this.get(i, false, cosigner));
|
||||
}
|
||||
for (var i = 0; i < changeIndex; i++) {
|
||||
ret.push(this.get(i, true));
|
||||
ret.push(this.get(i, true, cosigner));
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@ function PublicKeyRing(opts) {
|
|||
|
||||
this.copayersHK = opts.copayersHK || [];
|
||||
|
||||
this.indexes = AddressIndex.fromObj(opts.indexes) || new AddressIndex(opts);
|
||||
this.indexes = opts.indexes ? AddressIndex.fromList(opts.indexes)
|
||||
: AddressIndex.init(this.totalCopayers);
|
||||
|
||||
this.publicKeysCache = opts.publicKeysCache || {};
|
||||
this.nicknameFor = opts.nicknameFor || {};
|
||||
|
|
@ -36,6 +37,12 @@ PublicKeyRing.fromObj = function(data) {
|
|||
if (data instanceof PublicKeyRing) {
|
||||
throw new Error('bad data format: Did you use .toObj()?');
|
||||
}
|
||||
|
||||
// Support old indexes schema
|
||||
if (!Array.isArray(data.indexes)) {
|
||||
data.indexes = AddressIndex.update(data.indexes, data.totalCopayers);
|
||||
}
|
||||
|
||||
var ret = new PublicKeyRing(data);
|
||||
|
||||
for (var k in data.copayersExtPubKeys) {
|
||||
|
|
@ -51,7 +58,7 @@ PublicKeyRing.prototype.toObj = function() {
|
|||
networkName: this.network.name,
|
||||
requiredCopayers: this.requiredCopayers,
|
||||
totalCopayers: this.totalCopayers,
|
||||
indexes: this.indexes.toObj(),
|
||||
indexes: AddressIndex.serialize(this.indexes),
|
||||
|
||||
copayersExtPubKeys: this.copayersHK.map(function(b) {
|
||||
return b.extendedPublicKeyString();
|
||||
|
|
@ -136,10 +143,10 @@ PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
|
|||
return newEpk;
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
|
||||
PublicKeyRing.prototype.getPubKeys = function(index, isChange, cosigner) {
|
||||
this._checkKeys();
|
||||
|
||||
var path = Structure.Branch(index, isChange);
|
||||
var path = Structure.Branch(index, isChange, cosigner);
|
||||
var pubKeys = this.publicKeysCache[path];
|
||||
if (!pubKeys) {
|
||||
pubKeys = [];
|
||||
|
|
@ -162,20 +169,29 @@ PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
|
|||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getRedeemScript = function(index, isChange) {
|
||||
var pubKeys = this.getPubKeys(index, isChange);
|
||||
PublicKeyRing.prototype.getRedeemScript = function(index, isChange, cosigner) {
|
||||
var pubKeys = this.getPubKeys(index, isChange, cosigner);
|
||||
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
|
||||
return script;
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getAddress = function(index, isChange) {
|
||||
var script = this.getRedeemScript(index, isChange);
|
||||
PublicKeyRing.prototype.getAddress = function(index, isChange, id) {
|
||||
var cosigner = this.getCosigner(id);
|
||||
var script = this.getRedeemScript(index, isChange, cosigner);
|
||||
var address = Address.fromScript(script, this.network.name);
|
||||
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange);
|
||||
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange, cosigner);
|
||||
return address;
|
||||
};
|
||||
|
||||
// Overloaded to receive a PubkeyString or a consigner index
|
||||
PublicKeyRing.prototype.getIndex = function(id) {
|
||||
var cosigner = this.getCosigner(id);
|
||||
var index = this.indexes.filter(function(i) { return i.cosigner == cosigner });
|
||||
if (index.length != 1) throw new Error('no index for cosigner');
|
||||
return index[0];
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.pathForAddress = function(address) {
|
||||
var path = this.addressToPath[address];
|
||||
if (!path) throw new Error('Couldn\'t find path for address ' + address);
|
||||
|
|
@ -183,17 +199,19 @@ PublicKeyRing.prototype.pathForAddress = function(address) {
|
|||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange) {
|
||||
var addr = this.getAddress(index, isChange);
|
||||
PublicKeyRing.prototype.getScriptPubKeyHex = function(index, isChange, pubkey) {
|
||||
var cosigner = this.getCosigner(pubkey);
|
||||
var addr = this.getAddress(index, isChange, cosigner);
|
||||
return Script.createP2SH(addr.payload()).getBuffer().toString('hex');
|
||||
};
|
||||
|
||||
//generate a new address, update index.
|
||||
PublicKeyRing.prototype.generateAddress = function(isChange) {
|
||||
PublicKeyRing.prototype.generateAddress = function(isChange, pubkey) {
|
||||
isChange = !!isChange;
|
||||
var index = isChange ? this.indexes.getChangeIndex() : this.indexes.getReceiveIndex();
|
||||
var ret = this.getAddress(index, isChange);
|
||||
this.indexes.increment(isChange);
|
||||
var addrIndex = this.getIndex(pubkey);
|
||||
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex();
|
||||
var ret = this.getAddress(index, isChange, addrIndex.cosigner);
|
||||
addrIndex.increment(isChange);
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
|
@ -203,28 +221,58 @@ PublicKeyRing.prototype.getAddresses = function(opts) {
|
|||
});
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getAddressesInfo = function(opts) {
|
||||
PublicKeyRing.prototype.getCosigner = function(pubKey) {
|
||||
if (typeof pubKey == 'undefined') return Structure.SHARED_INDEX;
|
||||
if (typeof pubKey == 'number') return pubKey;
|
||||
|
||||
var sorted = this.copayersHK.map(function(h, i){
|
||||
return h.eckey.public.toString('hex');
|
||||
}).sort(function(h1, h2){ return h1.localeCompare(h2); });
|
||||
|
||||
var index = sorted.indexOf(pubKey);
|
||||
if (index == -1) throw new Error('no public key in ring');
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) {
|
||||
var ret = [];
|
||||
var self = this;
|
||||
var cosigner = pubkey && this.getCosigner(pubkey);
|
||||
this.indexes.forEach(function(index) {
|
||||
ret = ret.concat(self.getAddressesInfoForIndex(index, opts, cosigner));
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, cosigner) {
|
||||
opts = opts || {};
|
||||
|
||||
var isOwned = index.cosigner == Structure.SHARED_INDEX
|
||||
|| index.cosigner == cosigner;
|
||||
|
||||
var ret = [];
|
||||
if (!opts.excludeChange) {
|
||||
for (var i = 0; i < this.indexes.getChangeIndex(); i++) {
|
||||
var a = this.getAddress(i, true);
|
||||
for (var i = 0; i < index.changeIndex; i++) {
|
||||
var a = this.getAddress(i, true, index.cosigner);
|
||||
ret.unshift({
|
||||
address: this.getAddress(i, true),
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: true
|
||||
isChange: true,
|
||||
owned: isOwned
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!opts.excludeMain) {
|
||||
for (var i = 0; i < this.indexes.getReceiveIndex(); i++) {
|
||||
var a = this.getAddress(i, false);
|
||||
for (var i = 0; i < index.receiveIndex; i++) {
|
||||
var a = this.getAddress(i, false, index.cosigner);
|
||||
ret.unshift({
|
||||
address: a,
|
||||
addressStr: a.toString(),
|
||||
isChange: false
|
||||
isChange: false,
|
||||
owned: isOwned
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -235,7 +283,7 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
|
|||
// TODO this could be cached
|
||||
PublicKeyRing.prototype._addScriptMap = function(map, path) {
|
||||
var p = Structure.indicesForPath(path);
|
||||
var script = this.getRedeemScript(p.index, p.isChange);
|
||||
var script = this.getRedeemScript(p.index, p.isChange, p.cosigner);
|
||||
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
|
||||
};
|
||||
|
||||
|
|
@ -303,17 +351,25 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
|||
};
|
||||
|
||||
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
|
||||
var hasChanged = false;
|
||||
|
||||
this._checkInPKR(inPKR, ignoreId);
|
||||
|
||||
if (this.indexes.merge(inPKR.indexes))
|
||||
hasChanged = true;
|
||||
var hasChanged = false;
|
||||
hasChanged |= this.mergeIndexes(inPKR.indexes);
|
||||
hasChanged |= this._mergePubkeys(inPKR);
|
||||
|
||||
if (this._mergePubkeys(inPKR))
|
||||
hasChanged = true;
|
||||
|
||||
return hasChanged;
|
||||
return !!hasChanged;
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.mergeIndexes = function(indexes) {
|
||||
var self = this;
|
||||
var hasChanged = false;
|
||||
|
||||
indexes.forEach(function(theirs) {
|
||||
var mine = self.getIndex(theirs.cosigner);
|
||||
hasChanged |= mine.merge(theirs);
|
||||
});
|
||||
|
||||
return !!hasChanged
|
||||
}
|
||||
|
||||
module.exports = require('soop')(PublicKeyRing);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ Structure.indicesForPath = function(path) {
|
|||
return {
|
||||
isChange: s[3] === '1',
|
||||
index: parseInt(s[4]),
|
||||
cosigner: parseInt(s[2])
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ function Wallet(opts) {
|
|||
this.registeredPeerIds = [];
|
||||
this.addressBook = opts.addressBook || {};
|
||||
this.backupOffered = opts.backupOffered || false;
|
||||
this.publicKey = this.privateKey.publicHex;
|
||||
}
|
||||
|
||||
Wallet.parent = EventEmitter;
|
||||
|
|
@ -82,8 +83,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();
|
||||
|
|
@ -440,11 +441,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,
|
||||
});
|
||||
};
|
||||
|
|
@ -463,7 +465,7 @@ Wallet.prototype.getName = function() {
|
|||
};
|
||||
|
||||
Wallet.prototype._doGenerateAddress = function(isChange) {
|
||||
return this.publicKeyRing.generateAddress(isChange);
|
||||
return this.publicKeyRing.generateAddress(isChange, this.publicKey);
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -517,7 +519,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;
|
||||
|
|
@ -589,7 +590,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) {
|
||||
|
|
@ -755,31 +756,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;
|
||||
}
|
||||
|
|
@ -787,7 +801,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;
|
||||
|
|
@ -797,7 +811,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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue