UNPKG

@vbyte/btc-dev

Version:

Batteries-included toolset for plebian bitcoin development

27 lines (26 loc) 993 B
import { hash340 } from '@vbyte/micro-lib/hash'; import { Assert } from '@vbyte/micro-lib'; import { prefix_script_size } from '../../lib/script/index.js'; import { TAPLEAF_DEFAULT_VERSION } from '../../const.js'; const DEFAULT_VERSION = TAPLEAF_DEFAULT_VERSION; export function encode_tapscript(script, version = DEFAULT_VERSION) { const preimg = prefix_script_size(script); return encode_tapleaf(preimg, version); } export function encode_tapleaf(data, version = DEFAULT_VERSION) { const vbyte = encode_leaf_version(version); return hash340('TapLeaf', vbyte, data); } export function encode_tapbranch(leaf_a, leaf_b) { if (leaf_b < leaf_a) { [leaf_a, leaf_b] = [leaf_b, leaf_a]; } return hash340('TapBranch', leaf_a, leaf_b); } export function encode_leaf_version(version = 0xc0) { return version & 0xfe; } export function encode_taptweak(pubkey, data = new Uint8Array()) { Assert.size(pubkey, 32); return hash340('TapTweak', pubkey, data); }