@vbyte/btc-dev
Version:
Batteries-included toolset for plebian bitcoin development
24 lines (23 loc) • 937 B
JavaScript
import { Buff } from '@vbyte/buff';
import { ECC } from '@vbyte/micro-lib';
import { parse_tx } from '../../lib/tx/parse.js';
import { SIGHASH_DEFAULT } from '../../const.js';
import { hash_segwit_tx } from '../../lib/sighash/segwit.js';
import { hash_taproot_tx } from '../../lib/sighash/taproot.js';
export function sign_segwit_tx(seckey, txdata, options) {
const tx = parse_tx(txdata);
const msg = hash_segwit_tx(tx, options);
const sig = ECC.sign_ecdsa(seckey, msg).hex;
const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT);
return sig + flag;
}
export function sign_taproot_tx(seckey, txdata, options) {
const tx = parse_tx(txdata);
const msg = hash_taproot_tx(tx, options);
const sig = ECC.sign_bip340(seckey, msg).hex;
const flag = format_sigflag(options.sigflag ?? 0);
return sig + flag;
}
function format_sigflag(flag) {
return (flag !== 0) ? Buff.num(flag, 1).hex : '';
}