@vbyte/btc-dev
Version:
Batteries-included toolset for plebian bitcoin development
42 lines (41 loc) • 1.44 kB
JavaScript
import { Buff } from '@vbyte/buff';
import { parse_tx } from './parse.js';
import { encode_tx, encode_tx_inputs, encode_tx_outputs, encode_tx_vout, encode_vin, encode_vin_witness } from './encode.js';
const WIT_FLAG_BYTES = 2;
export function get_vsize(bytes) {
const weight = Buff.bytes(bytes).length;
const remain = (weight % 4 > 0) ? 1 : 0;
return Math.ceil(weight / 4) + remain;
}
export function get_txsize(txdata) {
const json = parse_tx(txdata);
const base = encode_tx(json, false).length;
const total = encode_tx(json, true).length;
const weight = base * 3 + total;
const remain = (weight % 4 > 0) ? 1 : 0;
const vsize = Math.ceil(weight / 4) + remain;
return { base, total, vsize, weight };
}
export function get_vin_size(vin) {
const bytes = encode_tx_inputs(vin);
return bytes.length;
}
export function get_vout_size(vout) {
const bytes = encode_tx_outputs(vout);
return bytes.length;
}
export function get_segwit_size(txinputs) {
const segwit_data = txinputs
.filter(e => e.witness.length > 0)
.map(e => e.witness);
return WIT_FLAG_BYTES + segwit_data
.reduce((acc, e) => acc + encode_vin_witness(e).length, 0);
}
export function get_txin_size(txinput) {
const bytes = encode_vin(txinput);
return bytes.length;
}
export function get_txout_size(txoutput) {
const bytes = encode_tx_vout(txoutput);
return bytes.length;
}