PublicKeyRing handles one index for each cosigner
This commit is contained in:
parent
b02cb17989
commit
04b6aa4003
7 changed files with 140 additions and 90 deletions
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -148,10 +148,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 = [];
|
||||
|
|
@ -174,17 +174,19 @@ 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 = typeof id === 'string' ? this.getCosigner(id) : 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;
|
||||
};
|
||||
|
||||
|
|
@ -192,7 +194,10 @@ PublicKeyRing.prototype.getSharedIndex = function() {
|
|||
return this.getIndex(Structure.SHARED_INDEX);
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getIndex = function(cosigner) {
|
||||
// Overloaded to receive a PubkeyString or a consigner index
|
||||
PublicKeyRing.prototype.getIndex = function(id) {
|
||||
var cosigner = typeof id === 'string' ? this.getCosigner(id) : 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];
|
||||
|
|
@ -205,18 +210,20 @@ 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 shared = this.getIndex(Structure.SHARED_INDEX);
|
||||
var index = isChange ? shared.getChangeIndex() : shared.getReceiveIndex();
|
||||
var ret = this.getAddress(index, isChange);
|
||||
shared.increment(isChange);
|
||||
var cosigner = this.getCosigner(pubkey);
|
||||
var addrIndex = this.getIndex(cosigner);
|
||||
var index = isChange ? addrIndex.getChangeIndex() : addrIndex.getReceiveIndex();
|
||||
var ret = this.getAddress(index, isChange, cosigner);
|
||||
addrIndex.increment(isChange);
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
|
@ -226,16 +233,37 @@ PublicKeyRing.prototype.getAddresses = function(opts) {
|
|||
});
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getCosigner = function(pubKey) {
|
||||
preconditions.checkArgument(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) {
|
||||
var ret = [];
|
||||
var self = this;
|
||||
this.indexes.forEach(function(index) {
|
||||
ret = ret.concat(self.getAddressesInfoForIndex(index, opts));
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts) {
|
||||
opts = opts || {};
|
||||
|
||||
var shared = this.getIndex(Structure.SHARED_INDEX);
|
||||
var ret = [];
|
||||
if (!opts.excludeChange) {
|
||||
for (var i = 0; i < shared.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
|
||||
});
|
||||
|
|
@ -243,8 +271,8 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts) {
|
|||
}
|
||||
|
||||
if (!opts.excludeMain) {
|
||||
for (var i = 0; i < shared.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(),
|
||||
|
|
@ -259,7 +287,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');
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ Structure.indicesForPath = function(path) {
|
|||
return {
|
||||
isChange: s[3] === '1',
|
||||
index: parseInt(s[4]),
|
||||
cosigner: parseInt(s[2])
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ function Wallet(opts) {
|
|||
this.registeredPeerIds = [];
|
||||
this.addressBook = opts.addressBook || {};
|
||||
this.backupOffered = opts.backupOffered || false;
|
||||
this.publicKey = this.privateKey.publicHex;
|
||||
}
|
||||
|
||||
Wallet.parent = EventEmitter;
|
||||
|
|
@ -462,7 +463,7 @@ Wallet.prototype.getName = function() {
|
|||
};
|
||||
|
||||
Wallet.prototype._doGenerateAddress = function(isChange) {
|
||||
return this.publicKeyRing.generateAddress(isChange);
|
||||
return this.publicKeyRing.generateAddress(isChange, this.publicKey);
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -516,7 +517,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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue