broadcastTx

This commit is contained in:
Matias Alejo Garcia 2015-02-05 17:22:38 -03:00
parent bca18b4292
commit dba306045c
3 changed files with 65 additions and 43 deletions

View File

@ -64,7 +64,7 @@ TxProposal.prototype._updateStatus = function() {
};
TxProposal.prototype._getBitcoreTx = function(n) {
TxProposal.prototype._getBitcoreTx = function() {
var self = this;
var t = new Bitcore.Transaction();
@ -80,6 +80,13 @@ TxProposal.prototype._getBitcoreTx = function(n) {
};
TxProposal.prototype.getRawTx = function() {
var t = this._getBitcoreTx();
return t.serialize();
};
TxProposal.prototype.addAction = function(copayerId, type, signatures) {
var action = new TxProposalAction({
copayerId: copayerId,

View File

@ -96,6 +96,10 @@ Wallet.prototype.getCopayer = function(copayerId) {
};
Wallet.prototype.getNetworkName = function() {
return this.isTestnet ? 'testnet' : 'livenet';
};
Wallet.prototype._getBitcoreNetwork = function() {
return this.isTestnet ? Bitcore.Networks.testnet : Bitcore.Networks.livenet;
};

View File

@ -263,9 +263,18 @@ CopayServer.prototype._getBlockExplorer = function(provider, network) {
}
};
/**
* _getUtxos
*
* @param opts.walletId
*/
CopayServer.prototype._getUtxos = function(opts, cb) {
var self = this;
self.storage.fetchWallet(opts.walletId, function(err, wallet) {
if (err) return cb(err);
// Get addresses for this wallet
self.storage.fetchAddresses(opts.walletId, function(err, addresses) {
if (err) return cb(err);
@ -274,7 +283,7 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
var addressStrs = _.pluck(addresses, 'address');
var addressToPath = _.indexBy(addresses, 'address'); // TODO : check performance
var bc = self._getBlockExplorer('insight', opts.network);
var bc = self._getBlockExplorer('insight', wallet.getNetworkName());
bc.getUnspentUtxos(addressStrs, function(err, utxos) {
if (err) return cb(err);
@ -312,6 +321,7 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
});
});
});
});
};
@ -452,11 +462,12 @@ CopayServer.prototype.getTx = function(opts, cb) {
});
};
CopayServer.prototype._broadcastTx = function(rawTx, cb) {
// TODO: this should attempt to broadcast _all_ accepted and not-yet broadcasted (status=='accepted') txps?
cb = cb || function() {};
throw 'not implemented';
CopayServer.prototype._broadcastTx = function(txp, networkName, cb) {
var raw = txp.getRawTx();
var bc = self._getBlockExplorer('insight', networkName);
bc.broadcast(raw, function(err, txid) {
return cb(err, txid);
})
};
/**
@ -502,7 +513,7 @@ CopayServer.prototype.signTx = function(opts, cb) {
if (err) return cb(err);
if (txp.status == 'accepted') {
self._broadcastTx(txp.rawTx, function(err, txid) {
self._broadcastTx(txp, wallet.getNetworkName(), function(err, txid) {
if (err) return cb(err);
tx.setBroadcasted(txid);