get tx hex in javascript-land.

This commit is contained in:
Christopher Jeffrey 2014-09-25 13:39:06 -07:00
parent 46f1d9b73b
commit 81addf6cef
1 changed files with 84 additions and 40 deletions

View File

@ -15,6 +15,8 @@ var bn = require('bn.js');
* Bitcoin
*/
var bitcoin = Bitcoin;
function Bitcoin(options) {
var self = this;
@ -73,6 +75,7 @@ Bitcoin.prototype.start = function(callback) {
return;
}
errorCaught = err;
self.error('Uncaught error: shutting down safely before throwing...');
if (!self._shutdown) {
if (err && err.stack) {
console.error(err.stack);
@ -235,15 +238,6 @@ Bitcoin.prototype.getTx = function(txHash, blockHash, callback) {
callback = blockHash;
blockHash = '';
}
// if (txHash[1] === 'x') txHash = txHash.slice(2);
// txHash = utils.revHex(txHash);
// if (blockHash) {
// if (blockHash[1] === 'x') blockHash = blockHash.slice(2);
// blockHash = utils.revHex(blockHash);
// }
return bitcoindjs.getTx(txHash, blockHash, callback);
};
@ -288,6 +282,10 @@ function Block(data) {
if (!(this instanceof Block)) {
return new Block(data);
}
if (data instanceof Block) {
return data;
}
}
/**
@ -299,6 +297,12 @@ function Transaction(data) {
return new Transaction(data);
}
if (data instanceof Transaction) {
return data;
}
var self = this;
this.nMinTxFee = data.nMinTxFee || data.minTxFee || 1000;
this.nMinRelayTxFee = data.nMinRelayTxFee || data.minRelayTxFee || 1000;
this.CURRENT_VERSION = 1;
@ -306,6 +310,10 @@ function Transaction(data) {
this.vin = data.vin || [];
this.vout = data.vout || [];
this.nLockTime = data.nLockTime || data.locktime || 0;
Object.keys(data).forEach(function(key) {
self[key] = data[key];
});
}
Transaction.prototype.getSerializeSize = function() {
@ -368,34 +376,6 @@ Transaction.toHex = function(tx) {
return new bn(Transaction.toBinary(tx)).toString('hex');
};
/**
* Broadcast TX
*/
Bitcoin._broadcastTx =
Bitcoin.prototype._broadcastTx = function(tx, options, callback) {
if (typeof tx === 'string') {
tx = { hex: tx };
}
if (!callback) {
callback = options;
options = null;
}
if (!options) {
options = {};
}
options.overrideFees = options.overrideFees || false;
options.ownOnly = options.ownOnly || false;
return bitcoindjs.broadcastTx(tx,
options.overrideFees,
options.ownOnly,
callback);
};
Transaction.toBinary = function(tx) {
var p = [];
var off = utils.writeU32(p, tx.nVersion || tx.version, 0);
@ -405,8 +385,17 @@ Transaction.toBinary = function(tx) {
var input = tx.vin[i];
if (input.coinbase) {
off += utils.copy(new bn(input.coinbase, 'hex').toArray(), p, off, true);
off += utils.copy(new bn(Array(64 + 1).join('0'), 'hex').toArray(), p, off, true);
off += utils.writeU32(p, 0, off);
var s = script.encode(new bn(input.coinbase, 'hex').toArray());
off += utils.varint(p, s.length, off);
off += utils.copy(s, p, off, true);
off += utils.writeU32(p, input.sequence, off);
// off += utils.copy(new bn(input.coinbase, 'hex').toArray(), p, off, true);
// off += utils.writeU32(p, input.sequence, off);
} else {
off += utils.copy(new bn(input.txid, 'hex').toArray(), p, off, true);
off += utils.writeU32(p, input.vout, off);
@ -440,6 +429,46 @@ Transaction.toBinary = function(tx) {
return p;
};
Transaction.prototype.toBinary = function() {
return Transaction.toBinary(this);
};
Transaction.broadcast = function(tx, options, callback) {
if (typeof tx === 'string') {
tx = { hex: tx };
}
if (!callback) {
callback = options;
options = null;
}
if (!options) {
options = {};
}
options.overrideFees = options.overrideFees || false;
options.ownOnly = options.ownOnly || false;
if (!tx.hex) {
tx = bitcoin.tx(tx);
tx.toHex();
}
return bitcoindjs.broadcastTx(tx,
options.overrideFees,
options.ownOnly,
callback);
};
Transaction.prototype.broadcast = function() {
return Transaction.broadcast(this);
};
/**
* Script
*/
var script = {};
script.encode = function encode(s) {
@ -551,7 +580,22 @@ utils.varint = function(arr, value, off) {
* Expose
*/
module.exports = exports = Bitcoin;
exports.Bitcoin = Bitcoin;
module.exports = exports = bitcoin;
exports.Bitcoin = bitcoin;
exports.bitcoin = bitcoin;
exports.bitcoind = bitcoin;
exports.native = bitcoindjs;
exports.bitcoindjs = bitcoindjs;
exports.Block = Block;
exports.block = Block;
exports.Transaction = Transaction;
exports.transaction = Transaction;
exports.tx = Transaction;
exports.script = script;
exports.utils = utils;