2014-04-09 17:28:35 -03:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var imports = require('soop').imports();
|
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
|
var coinUtil = bitcore.util;
|
|
|
|
|
var Transaction = bitcore.Transaction;
|
|
|
|
|
var Builder = bitcore.TransactionBuilder;
|
|
|
|
|
var buffertools = bitcore.buffertools;
|
|
|
|
|
|
|
|
|
|
var Storage = imports.Storage || require('./Storage');
|
|
|
|
|
var storage = Storage.default();
|
|
|
|
|
|
2014-04-09 20:37:14 -03:00
|
|
|
function TxProposal(opts) {
|
2014-04-10 02:16:57 -03:00
|
|
|
this.tx = opts.tx;
|
|
|
|
|
this.seenBy = opts.seenBy || {};
|
|
|
|
|
this.signedBy = opts.signedBy || {};
|
2014-04-09 20:37:14 -03:00
|
|
|
};
|
|
|
|
|
module.exports = require('soop')(TxProposal);
|
|
|
|
|
|
2014-04-09 17:28:35 -03:00
|
|
|
|
|
|
|
|
function TxProposals(opts) {
|
|
|
|
|
opts = opts || {};
|
|
|
|
|
this.network = opts.networkName === 'livenet' ?
|
|
|
|
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
|
|
|
|
this.publicKeyRing = opts.publicKeyRing;
|
|
|
|
|
this.txs = [];
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 02:16:57 -03:00
|
|
|
TxProposals.fromObj = function(o) {
|
|
|
|
|
var ret = new TxProposals({
|
|
|
|
|
networkName: o.networkName,
|
|
|
|
|
});
|
|
|
|
|
o.txs.forEach(function(t) {
|
|
|
|
|
var tx = new Transaction;
|
|
|
|
|
tx.parse(t.txHex);
|
|
|
|
|
ret.txs.push({
|
|
|
|
|
seenBy: t.seenBy,
|
|
|
|
|
signedBy: t.signedBy,
|
|
|
|
|
tx: tx,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
2014-04-09 17:28:35 -03:00
|
|
|
|
2014-04-10 02:16:57 -03:00
|
|
|
TxProposals.prototype.toObj = function() {
|
|
|
|
|
var ret = [];
|
|
|
|
|
this.txs.forEach(function(t) {
|
|
|
|
|
ret.push({
|
|
|
|
|
seenBy: t.seenBy,
|
|
|
|
|
signedBy: t.signedBy,
|
|
|
|
|
txHex: t.tx.serialize(),
|
|
|
|
|
});
|
2014-04-09 17:28:35 -03:00
|
|
|
});
|
2014-04-10 02:16:57 -03:00
|
|
|
return {
|
|
|
|
|
txs: ret,
|
|
|
|
|
networkName: this.network.name,
|
|
|
|
|
};
|
2014-04-09 17:28:35 -03:00
|
|
|
};
|
|
|
|
|
|
2014-04-10 02:16:57 -03:00
|
|
|
TxProposals.prototype.create = function(toAddress, amountSat, utxos, priv) {
|
2014-04-09 17:28:35 -03:00
|
|
|
var pkr = this.publicKeyRing;
|
|
|
|
|
|
|
|
|
|
if (! pkr.isComplete() ) {
|
|
|
|
|
throw new Error('publicKeyRing is not complete');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
|
remainderOut: { address: pkr.generateAddress(true) }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var b = new Builder(opts)
|
|
|
|
|
.setUnspent(utxos)
|
|
|
|
|
.setHashToScriptMap(pkr.getRedeemScriptMap())
|
|
|
|
|
.setOutputs([{address: toAddress, amountSat: amountSat}])
|
|
|
|
|
;
|
|
|
|
|
|
2014-04-10 02:16:57 -03:00
|
|
|
if (priv) {
|
|
|
|
|
//console.log('*** SIGNING IDX:', pkr.addressIndex, pkr.changeAddressIndex);
|
|
|
|
|
b.sign( priv.getAll(pkr.addressIndex, pkr.changeAddressIndex) );
|
2014-04-09 17:28:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = b.build();
|
2014-04-10 02:16:57 -03:00
|
|
|
var me = {};
|
|
|
|
|
if (priv)
|
|
|
|
|
me[priv.id] = Date.now();
|
|
|
|
|
|
2014-04-09 20:37:14 -03:00
|
|
|
this.txs.push(
|
|
|
|
|
new TxProposal({
|
2014-04-10 02:16:57 -03:00
|
|
|
signedBy: me,
|
|
|
|
|
seenBy: me,
|
|
|
|
|
tx: tx,
|
2014-04-09 20:37:14 -03:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
return tx;
|
2014-04-09 17:28:35 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TxProposals.prototype.sign = function(index) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = require('soop')(TxProposals);
|