UNPKG

@vbyte/btc-dev

Version:

Batteries-included toolset for plebian bitcoin development

63 lines (62 loc) 1.9 kB
import { Buff, Stream } from '@vbyte/buff'; import { get_op_code, get_op_type, is_valid_op } from './words.js'; export function parse_script(script) { const bytes = Buff.bytes(script); return { asm: decode_script(bytes), hex: bytes.hex }; } export function decode_script(script) { const stream = new Stream(script); const stack = []; const stack_size = stream.size; let word; let word_type; let word_size; let count = 0; while (count < stack_size) { word = stream.read(1).num; word_type = get_op_type(word); count++; switch (word_type) { case 'varint': stack.push(stream.read(word).hex); count += word; break; case 'pushdata1': word_size = stream.read(1).reverse().num; stack.push(stream.read(word_size).hex); count += word_size + 1; break; case 'pushdata2': word_size = stream.read(2).reverse().num; stack.push(stream.read(word_size).hex); count += word_size + 2; break; case 'pushdata4': word_size = stream.read(4).reverse().num; stack.push(stream.read(word_size).hex); count += word_size + 4; break; case 'opcode': if (!is_valid_op(word)) { throw new Error(`Invalid OPCODE: ${word}`); } stack.push(get_op_code(word)); break; default: throw new Error(`Word type undefined: ${word}`); } } return stack; } export function is_valid_script(script) { try { const stack = decode_script(script); return stack.length > 0; } catch { return false; } }