@vbyte/btc-dev
Version:
Batteries-included toolset for plebian bitcoin development
31 lines (30 loc) • 990 B
JavaScript
import { Buff } from '@vbyte/buff';
import { Assert } from '@vbyte/micro-lib';
import { sha256 } from '@vbyte/micro-lib/hash';
export function get_prevout(vin) {
Assert.exists(vin.prevout, 'Prevout data missing for input: ' + String(vin.txid));
return vin.prevout;
}
export function parse_txinput(txdata, config) {
let { txindex, txinput } = config ?? {};
if (txindex !== undefined) {
if (txindex >= txdata.vin.length) {
throw new Error('Input index out of bounds: ' + String(txindex));
}
txinput = txdata.vin.at(txindex);
}
Assert.ok(txinput !== undefined);
return txinput;
}
export function get_annex_data(witness) {
if (witness === undefined)
return;
if (witness.length < 2)
return;
const annex = witness.at(-1);
if (typeof annex === 'string' && annex.startsWith('50')) {
const bytes = Buff.hex(annex).prefix_varint('be');
return sha256(bytes);
}
return undefined;
}