txho
Version:
Get a Bitcoin transaction as JSON for CLI and node
119 lines (118 loc) • 5.24 kB
JavaScript
;
// Cloned from https://github.com/interplanaria/txo/blob/master/index.js to remove the hashbang. Licence: https://github.com/interplanaria/txo/blob/master/LICENSE
// @ts-nocheck
exports.__esModule = true;
exports.fromTx = void 0;
var bsv = require("bsv");
exports.fromTx = function (transaction, options) {
return new Promise(function (resolve, reject) {
var gene = new bsv.Transaction(transaction);
var t = gene.toObject();
var result = [];
var inputs = [];
var outputs = [];
var graph = {};
if (gene.inputs) {
gene.inputs.forEach(function (input, input_index) {
if (input.script) {
var xput_1 = { i: input_index, seq: input.sequenceNumber };
input.script.chunks.forEach(function (c, chunk_index) {
var chunk = c;
if (c.buf) {
if (c.buf.byteLength >= 1000000) {
xput_1['xlb' + chunk_index] = c.buf.toString('base64');
}
else if (c.buf.byteLength >= 512 && c.buf.byteLength < 1000000) {
xput_1['lb' + chunk_index] = c.buf.toString('base64');
}
else {
xput_1['b' + chunk_index] = c.buf.toString('base64');
}
if (options && options.h && options.h > 0) {
xput_1['h' + chunk_index] = c.buf.toString('hex');
}
}
else {
if (typeof c.opcodenum !== 'undefined') {
xput_1['b' + chunk_index] = {
op: c.opcodenum
};
}
else {
xput_1['b' + chunk_index] = c;
}
}
});
var sender = {
h: input.prevTxId.toString('hex'),
i: input.outputIndex
};
var address = input.script.toAddress(bsv.Networks.livenet).toString();
if (address && address.length > 0) {
sender.a = address;
}
xput_1.e = sender;
inputs.push(xput_1);
}
});
}
if (gene.outputs) {
gene.outputs.forEach(function (output, output_index) {
if (output.script) {
var xput_2 = { i: output_index };
output.script.chunks.forEach(function (c, chunk_index) {
var chunk = c;
if (c.buf) {
if (c.buf.byteLength >= 1000000) {
xput_2['xlb' + chunk_index] = c.buf.toString('base64');
xput_2['xls' + chunk_index] = c.buf.toString('utf8');
}
else if (c.buf.byteLength >= 512 && c.buf.byteLength < 1000000) {
xput_2['lb' + chunk_index] = c.buf.toString('base64');
xput_2['ls' + chunk_index] = c.buf.toString('utf8');
}
else {
xput_2['b' + chunk_index] = c.buf.toString('base64');
xput_2['s' + chunk_index] = c.buf.toString('utf8');
}
if (options && options.h && options.h > 0) {
xput_2['h' + chunk_index] = c.buf.toString('hex');
}
}
else {
if (typeof c.opcodenum !== 'undefined') {
xput_2['b' + chunk_index] = {
op: c.opcodenum
};
}
else {
xput_2['b' + chunk_index] = c;
}
}
});
var receiver = {
v: output.satoshis,
i: output_index
};
var address = output.script.toAddress(bsv.Networks.livenet).toString();
if (address && address.length > 0) {
receiver.a = address;
}
xput_2.e = receiver;
outputs.push(xput_2);
}
});
}
var r = {
tx: { h: t.hash },
"in": inputs,
out: outputs,
lock: t.nLockTime
};
// confirmations
if (options && options.confirmations) {
r.confirmations = options.confirmations;
}
resolve(r);
});
};