Fix conflicts:
js/controllers/transactions.js
This commit is contained in:
commit
e17aed54b0
13 changed files with 196 additions and 94 deletions
|
|
@ -71,7 +71,6 @@ PrivateKey.prototype.get = function(index,isChange) {
|
|||
|
||||
PrivateKey.prototype.getAll = function(addressIndex, changeAddressIndex) {
|
||||
var ret = [];
|
||||
|
||||
for(var i=0;i<addressIndex; i++) {
|
||||
ret.push(this.get(i,false));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
|
|||
var bip32 = this.copayersBIP32[i].derive(path);
|
||||
pubKeys[i] = bip32.eckey.public;
|
||||
}
|
||||
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex')});
|
||||
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
|
||||
} else {
|
||||
pubKeys = pubKeys.map(function(s){return new Buffer(s,'hex')});
|
||||
}
|
||||
|
|
@ -158,6 +158,7 @@ PublicKeyRing.prototype._checkIndexRange = function (index, isChange) {
|
|||
}
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
|
||||
this._checkIndexRange(index, isChange);
|
||||
|
||||
|
|
@ -166,13 +167,21 @@ PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
|
|||
return script;
|
||||
};
|
||||
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getAddress = function (index, isChange) {
|
||||
this._checkIndexRange(index, isChange);
|
||||
var script = this.getRedeemScript(index,isChange);
|
||||
return Address.fromScript(script, this.network.name);
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
|
||||
this._checkIndexRange(index, isChange);
|
||||
var script = this.getRedeemScript(index,isChange);
|
||||
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
|
||||
};
|
||||
|
||||
// TODO this could be cached
|
||||
PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
|
||||
this._checkIndexRange(index, isChange);
|
||||
var addr = this.getAddress(index,isChange);
|
||||
|
|
@ -180,7 +189,6 @@ PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
|
|||
};
|
||||
|
||||
|
||||
|
||||
//generate a new address, update index.
|
||||
PublicKeyRing.prototype.generateAddress = function(isChange) {
|
||||
|
||||
|
|
@ -215,11 +223,10 @@ PublicKeyRing.prototype.getRedeemScriptMap = function () {
|
|||
var ret = {};
|
||||
|
||||
for (var i=0; i<this.changeAddressIndex; i++) {
|
||||
ret[this.getAddress(i,true)] = this.getRedeemScript(i,true).getBuffer().toString('hex');
|
||||
this._addScriptMap(ret,i,true);
|
||||
}
|
||||
|
||||
for (var i=0; i<this.addressIndex; i++) {
|
||||
ret[this.getAddress(i)] = this.getRedeemScript(i).getBuffer().toString('hex');
|
||||
this._addScriptMap(ret,i,false);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
|
@ -267,6 +274,8 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
|||
var self = this;
|
||||
var hasChanged = false;
|
||||
var l= self.copayersBIP32.length;
|
||||
if (self.isComplete())
|
||||
return;
|
||||
|
||||
inPKR.copayersBIP32.forEach( function(b) {
|
||||
var haveIt = false;
|
||||
|
|
@ -279,7 +288,6 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
|||
}
|
||||
if (!haveIt) {
|
||||
if (self.isComplete()) {
|
||||
//console.log('[PublicKeyRing.js.318] REPEATED KEY', epk); //TODO
|
||||
throw new Error('trying to add more pubkeys, when PKR isComplete at merge');
|
||||
}
|
||||
self.copayersBIP32.push(new BIP32(epk));
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ function TxProposal(opts) {
|
|||
this.createdTs = opts.createdTs;
|
||||
this.seenBy = opts.seenBy || {};
|
||||
this.signedBy = opts.signedBy || {};
|
||||
this.rejectedBy = opts.rejectedBy || {};
|
||||
this.builder = opts.builder;
|
||||
this.sentTs = opts.sentTs || null;
|
||||
this.sentTxid = opts.sentTxid || null;
|
||||
|
|
@ -120,6 +121,7 @@ TxProposals.prototype._startMerge = function(myTxps, theirTxps) {
|
|||
};
|
||||
};
|
||||
|
||||
// TODO add signatures.
|
||||
TxProposals.prototype._mergeMetadata = function(myTxps, theirTxps, mergeInfo) {
|
||||
|
||||
var toMerge = mergeInfo.toMerge;
|
||||
|
|
@ -130,7 +132,7 @@ TxProposals.prototype._mergeMetadata = function(myTxps, theirTxps, mergeInfo) {
|
|||
var v1 = toMerge[hash];
|
||||
|
||||
Object.keys(v1.seenBy).forEach(function(k) {
|
||||
if (!v0.seenBy[k] || v0.seenBy[k] !== v1.seenBy[k]) {
|
||||
if (!v0.seenBy[k]) {
|
||||
v0.seenBy[k] = v1.seenBy[k];
|
||||
hasChanged++;
|
||||
}
|
||||
|
|
@ -143,6 +145,13 @@ TxProposals.prototype._mergeMetadata = function(myTxps, theirTxps, mergeInfo) {
|
|||
}
|
||||
});
|
||||
|
||||
Object.keys(v1.rejectedBy).forEach(function(k) {
|
||||
if (!v0.rejectedBy[k]) {
|
||||
v0.rejectedBy[k] = v1.rejectedBy[k];
|
||||
hasChanged++;
|
||||
}
|
||||
});
|
||||
|
||||
if (!v0.sentTxid && v1.sentTxid) {
|
||||
v0.sentTs = v1.sentTs;
|
||||
v0.sentTxid = v1.sentTxid;
|
||||
|
|
@ -181,10 +190,41 @@ TxProposals.prototype.setSent = function(ntxid,txid) {
|
|||
this.txps[ntxid].setSent(txid);
|
||||
};
|
||||
|
||||
TxProposals.prototype.getUsedUnspent = function() {
|
||||
|
||||
TxProposals.prototype.getTxProposal = function(ntxid) {
|
||||
var txp = this.txps[ntxid];
|
||||
var i = JSON.parse(JSON.stringify(txp));
|
||||
i.builder = txp.builder;
|
||||
i.ntxid = ntxid;
|
||||
i.peerActions = {};
|
||||
for(var p in txp.seenBy){
|
||||
i.peerActions[p]={seen: txp.seenBy[p]};
|
||||
}
|
||||
for(var p in txp.signedBy){
|
||||
i.peerActions[p]= i.peerActions[p] || {};
|
||||
i.peerActions[p].sign = txp.signedBy[p];
|
||||
}
|
||||
var r=0;
|
||||
for(var p in txp.rejectedBy){
|
||||
i.peerActions[p]= i.peerActions[p] || {};
|
||||
i.peerActions[p].rejected = txp.rejectedBy[p];
|
||||
r++;
|
||||
}
|
||||
i.rejectCount=r;
|
||||
|
||||
var c = txp.creator;
|
||||
i.peerActions[c] = i.peerActions[c] || {};
|
||||
i.peerActions[c].create = txp.createdTs;
|
||||
return i;
|
||||
};
|
||||
|
||||
TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
|
||||
var ret = [];
|
||||
for(var i in this.txps) {
|
||||
var u = this.txps[i].builder.getSelectedUnspent();
|
||||
if (this.getTxProposal(i).rejectCount>maxRejectCount)
|
||||
continue;
|
||||
|
||||
for (var j in u){
|
||||
ret.push(u[j].txid);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,56 +269,46 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
|||
|
||||
Wallet.prototype.generateAddress = function(isChange) {
|
||||
var addr = this.publicKeyRing.generateAddress(isChange);
|
||||
console.log('[Wallet.js.281:addr:]',addr, this.publicKeyRing.toObj(), this.getAddresses()); //TODO
|
||||
this.sendPublicKeyRing();
|
||||
this.store(true);
|
||||
return addr;
|
||||
};
|
||||
|
||||
// TODO : sort by time... / signed.
|
||||
|
||||
Wallet.prototype.getTxProposals = function() {
|
||||
var ret = [];
|
||||
for(var k in this.txProposals.txps) {
|
||||
var txp = this.txProposals.txps[k];
|
||||
var i = JSON.parse(JSON.stringify(txp));
|
||||
i.builder = txp.builder;
|
||||
i.ntxid = k;
|
||||
i.signedByUs = txp.signedBy[this.getMyPeerId()]?true:false;
|
||||
|
||||
i.peerActions = {};
|
||||
for(var p in txp.seenBy){
|
||||
i.peerActions[p]={seen: txp.seenBy[p]};
|
||||
}
|
||||
for(var p in txp.signedBy){
|
||||
i.peerActions[p]= i.peerActions[p] || {};
|
||||
i.peerActions[p].sign = txp.signedBy[p];
|
||||
}
|
||||
var c = txp.creator;
|
||||
i.peerActions[c] = i.peerActions[c] || {};
|
||||
i.peerActions[c].create = txp.createdTs;
|
||||
|
||||
var i = this.txProposals.getTxProposal(k);
|
||||
i.signedByUs = i.signedBy[this.getMyPeerId()]?true:false;
|
||||
i.rejectedByUs = i.rejectedBy[this.getMyPeerId()]?true:false;
|
||||
if (this.totalCopayers-i.rejectCount < this.requiredCopayers)
|
||||
i.finallyRejected=true;
|
||||
|
||||
ret.push(i);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
Wallet.prototype.getTxProposal = function(ntxid) {
|
||||
|
||||
Wallet.prototype.reject = function(ntxid) {
|
||||
var myId=this.getMyPeerId();
|
||||
var txp = this.txProposals.txps[ntxid];
|
||||
var i = {txp:txp};
|
||||
i.ntxid = ntxid;
|
||||
i.signedByUs = txp.signedBy[this.getMyPeerId()]?true:false;
|
||||
return i;
|
||||
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
||||
|
||||
txp.rejectedBy[myId] = Date.now();
|
||||
this.sendTxProposals();
|
||||
this.store(true);
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.sign = function(ntxid) {
|
||||
var self = this;
|
||||
var myId=this.getMyPeerId();
|
||||
var txp = self.txProposals.txps[ntxid];
|
||||
if (!txp) return;
|
||||
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
||||
|
||||
var pkr = self.publicKeyRing;
|
||||
var keys = self.privateKey.getAll(pkr.addressIndex, pkr.changeAddressIndex);
|
||||
console.log('[Wallet.js.329:keys:]',keys); //TODO
|
||||
|
||||
var b = txp.builder;
|
||||
var before = b.signaturesAdded;
|
||||
|
|
@ -326,7 +316,7 @@ console.log('[Wallet.js.329:keys:]',keys); //TODO
|
|||
|
||||
var ret = false;
|
||||
if (b.signaturesAdded > before) {
|
||||
txp.signedBy[self.getMyPeerId()] = Date.now();
|
||||
txp.signedBy[myId] = Date.now();
|
||||
this.sendTxProposals();
|
||||
this.store(true);
|
||||
ret = true;
|
||||
|
|
@ -373,7 +363,6 @@ Wallet.prototype.addSeenToTxProposals = function() {
|
|||
return ret;
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.getAddresses = function(onlyMain) {
|
||||
return this.publicKeyRing.getAddresses(onlyMain);
|
||||
};
|
||||
|
|
@ -440,7 +429,8 @@ Wallet.prototype.getSafeUnspent = function(cb) {
|
|||
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
|
||||
|
||||
var ret=[];
|
||||
var uu = self.txProposals.getUsedUnspent();
|
||||
var maxRejectCount = self.totalCopayers - self.requiredCopayers;
|
||||
var uu = self.txProposals.getUsedUnspent(maxRejectCount);
|
||||
|
||||
for(var i in unspentList){
|
||||
if (uu.indexOf(unspentList[i].txid) === -1)
|
||||
|
|
@ -465,11 +455,11 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
|
|||
}
|
||||
|
||||
self.getSafeUnspent(function(unspentList) {
|
||||
// TODO check enough funds, etc.
|
||||
self.createTxSync(toAddress, amountSatStr, unspentList, opts);
|
||||
self.sendPublicKeyRing(); // Change Address
|
||||
self.sendTxProposals();
|
||||
self.store();
|
||||
if (self.createTxSync(toAddress, amountSatStr, unspentList, opts)) {
|
||||
self.sendPublicKeyRing(); // Change Address
|
||||
self.sendTxProposals();
|
||||
self.store();
|
||||
}
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
|
@ -497,25 +487,27 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
|
|||
|
||||
var signRet;
|
||||
if (priv) {
|
||||
console.log('[Wallet.js.486] aLL Priv', priv.getAll(pkr.addressIndex, pkr.changeAddressIndex)); //TODO
|
||||
b.sign( priv.getAll(pkr.addressIndex, pkr.changeAddressIndex) );
|
||||
}
|
||||
console.log('[Wallet.js.494]', b, b.build().serialize().toString('hex')); //TODO
|
||||
var me = {};
|
||||
var myId = this.getMyPeerId();
|
||||
var now = Date.now();
|
||||
|
||||
if (priv) me[myId] = now;
|
||||
var me = {};
|
||||
if (priv && b.signaturesAdded) me[myId] = now;
|
||||
|
||||
var meSeen = {};
|
||||
if (priv) meSeen[myId] = now;
|
||||
|
||||
var data = {
|
||||
signedBy: (priv && b.signaturesAdded ? me : {}),
|
||||
seenBy: (priv ? me : {}),
|
||||
signedBy: me,
|
||||
seenBy: meSeen,
|
||||
creator: myId,
|
||||
createdTs: now,
|
||||
builder: b,
|
||||
};
|
||||
|
||||
this.txProposals.add(data);
|
||||
return true;
|
||||
};
|
||||
|
||||
Wallet.prototype.connectTo = function(peerId) {
|
||||
|
|
|
|||
|
|
@ -261,7 +261,6 @@ console.log('[WebRTC.js.255] WARN: NO CONNECTION TO:', peerId); //TODO
|
|||
|
||||
Network.prototype.send = function(peerIds, data, cb) {
|
||||
var self=this;
|
||||
console.log('[WebRTC.js.242] SENDING ', data.type); //TODO
|
||||
if (!peerIds) {
|
||||
peerIds = this.connectedPeers;
|
||||
data.isBroadcast = 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue