include derivation path in tx proposal

This commit is contained in:
Manuel Araoz 2014-05-30 15:07:52 -03:00
parent 2b3c29a563
commit 6af9ee68e2
4 changed files with 33 additions and 7 deletions

View File

@ -61,8 +61,11 @@ PrivateKey.prototype._getHK = function(path) {
return this.bip.derive(path);
};
PrivateKey.prototype.get = function(index,isChange) {
var path = Structure.FullBranch(index, isChange);
PrivateKey.prototype.getForPaths = function(paths) {
return paths.map(this.getForPath.bind(this));
};
PrivateKey.prototype.getForPath = function(path) {
var pk = this.privateKeyCache[path];
if (!pk) {
var derivedHK = this._getHK(path);
@ -73,6 +76,11 @@ PrivateKey.prototype.get = function(index,isChange) {
return wk;
};
PrivateKey.prototype.get = function(index,isChange) {
var path = Structure.FullBranch(index, isChange);
return this.getForPath(path);
};
PrivateKey.prototype.getAll = function(addressIndex, changeAddressIndex) {
var ret = [];
for(var i=0;i<addressIndex; i++) {

View File

@ -36,6 +36,7 @@ function PublicKeyRing(opts) {
this.publicKeysCache = opts.publicKeysCache || {};
this.nicknameFor = opts.nicknameFor || {};
this.copayerIds = [];
this.addressToPath = {};
}
PublicKeyRing.fromObj = function (data) {
@ -181,7 +182,15 @@ PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
// TODO this could be cached
PublicKeyRing.prototype.getAddress = function (index, isChange) {
var script = this.getRedeemScript(index,isChange);
return Address.fromScript(script, this.network.name);
var address = Address.fromScript(script, this.network.name);
this.addressToPath[address.toString()] = Structure.FullBranch(index, isChange);
return address;
};
PublicKeyRing.prototype.pathForAddress = function(address) {
var path = this.addressToPath[address];
if (!path) throw new Error('Couldn\'t find path for address '+address);
return path;
};
// TODO this could be cached

View File

@ -22,6 +22,7 @@ function TxProposal(opts) {
this.builder = opts.builder;
this.sentTs = opts.sentTs || null;
this.sentTxid = opts.sentTxid || null;
this.inputChainPaths = opts.inputChainPaths || [];
}
TxProposal.prototype.toObj = function() {

View File

@ -432,7 +432,7 @@ Wallet.prototype.sign = function(ntxid, cb) {
}
var pkr = self.publicKeyRing;
var keys = self.privateKey.getAll(pkr.addressIndex, pkr.changeAddressIndex);
var keys = self.privateKey.getForPaths(txp.inputChainPaths);
var b = txp.builder;
var before = b.signaturesAdded;
@ -626,9 +626,15 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
amountSat: amountSat
}]);
var signRet;
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
if (priv) {
var signed = b.sign(priv.getAll(pkr.addressIndex, pkr.changeAddressIndex));
var keys = priv.getForPaths(inputChainPaths);
var signed = b.sign(keys);
}
var myId = this.getMyCopayerId();
var now = Date.now();
@ -640,6 +646,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
if (priv) meSeen[myId] = now;
var data = {
inputChainPaths: inputChainPaths,
signedBy: me,
seenBy: meSeen,
creator: myId,
@ -647,7 +654,8 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
builder: b,
};
return this.txProposals.add(data);
var ntxid = this.txProposals.add(data);
return ntxid;
};
Wallet.prototype.disconnect = function() {