get tx hex in javascript-land.
This commit is contained in:
parent
46f1d9b73b
commit
81addf6cef
124
lib/bitcoind.js
124
lib/bitcoind.js
@ -15,6 +15,8 @@ var bn = require('bn.js');
|
|||||||
* Bitcoin
|
* Bitcoin
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var bitcoin = Bitcoin;
|
||||||
|
|
||||||
function Bitcoin(options) {
|
function Bitcoin(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
@ -73,6 +75,7 @@ Bitcoin.prototype.start = function(callback) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
errorCaught = err;
|
errorCaught = err;
|
||||||
|
self.error('Uncaught error: shutting down safely before throwing...');
|
||||||
if (!self._shutdown) {
|
if (!self._shutdown) {
|
||||||
if (err && err.stack) {
|
if (err && err.stack) {
|
||||||
console.error(err.stack);
|
console.error(err.stack);
|
||||||
@ -235,15 +238,6 @@ Bitcoin.prototype.getTx = function(txHash, blockHash, callback) {
|
|||||||
callback = blockHash;
|
callback = blockHash;
|
||||||
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);
|
return bitcoindjs.getTx(txHash, blockHash, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -288,6 +282,10 @@ function Block(data) {
|
|||||||
if (!(this instanceof Block)) {
|
if (!(this instanceof Block)) {
|
||||||
return new Block(data);
|
return new Block(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data instanceof Block) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -299,6 +297,12 @@ function Transaction(data) {
|
|||||||
return new Transaction(data);
|
return new Transaction(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data instanceof Transaction) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
this.nMinTxFee = data.nMinTxFee || data.minTxFee || 1000;
|
this.nMinTxFee = data.nMinTxFee || data.minTxFee || 1000;
|
||||||
this.nMinRelayTxFee = data.nMinRelayTxFee || data.minRelayTxFee || 1000;
|
this.nMinRelayTxFee = data.nMinRelayTxFee || data.minRelayTxFee || 1000;
|
||||||
this.CURRENT_VERSION = 1;
|
this.CURRENT_VERSION = 1;
|
||||||
@ -306,6 +310,10 @@ function Transaction(data) {
|
|||||||
this.vin = data.vin || [];
|
this.vin = data.vin || [];
|
||||||
this.vout = data.vout || [];
|
this.vout = data.vout || [];
|
||||||
this.nLockTime = data.nLockTime || data.locktime || 0;
|
this.nLockTime = data.nLockTime || data.locktime || 0;
|
||||||
|
|
||||||
|
Object.keys(data).forEach(function(key) {
|
||||||
|
self[key] = data[key];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.getSerializeSize = function() {
|
Transaction.prototype.getSerializeSize = function() {
|
||||||
@ -368,34 +376,6 @@ Transaction.toHex = function(tx) {
|
|||||||
return new bn(Transaction.toBinary(tx)).toString('hex');
|
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) {
|
Transaction.toBinary = function(tx) {
|
||||||
var p = [];
|
var p = [];
|
||||||
var off = utils.writeU32(p, tx.nVersion || tx.version, 0);
|
var off = utils.writeU32(p, tx.nVersion || tx.version, 0);
|
||||||
@ -405,8 +385,17 @@ Transaction.toBinary = function(tx) {
|
|||||||
var input = tx.vin[i];
|
var input = tx.vin[i];
|
||||||
|
|
||||||
if (input.coinbase) {
|
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.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 {
|
} else {
|
||||||
off += utils.copy(new bn(input.txid, 'hex').toArray(), p, off, true);
|
off += utils.copy(new bn(input.txid, 'hex').toArray(), p, off, true);
|
||||||
off += utils.writeU32(p, input.vout, off);
|
off += utils.writeU32(p, input.vout, off);
|
||||||
@ -440,6 +429,46 @@ Transaction.toBinary = function(tx) {
|
|||||||
return p;
|
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 = {};
|
var script = {};
|
||||||
|
|
||||||
script.encode = function encode(s) {
|
script.encode = function encode(s) {
|
||||||
@ -551,7 +580,22 @@ utils.varint = function(arr, value, off) {
|
|||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = exports = Bitcoin;
|
module.exports = exports = bitcoin;
|
||||||
exports.Bitcoin = Bitcoin;
|
|
||||||
|
exports.Bitcoin = bitcoin;
|
||||||
|
exports.bitcoin = bitcoin;
|
||||||
|
exports.bitcoind = bitcoin;
|
||||||
|
|
||||||
exports.native = bitcoindjs;
|
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;
|
exports.utils = utils;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user