1+ /**
2+ * Script tools, including decompile, compile, toASM, fromASM, toStack, isCanonicalPubKey, isCanonicalScriptSignature
3+ * @packageDocumentation
4+ */
15import * as bip66 from './bip66' ;
26import { OPS , REVERSE_OPS } from './ops' ;
37import { Stack } from './payments' ;
@@ -7,6 +11,7 @@ import * as scriptSignature from './script_signature';
711import * as types from './types' ;
812const { typeforce } = types ;
913
14+
1015const OP_INT_BASE = OPS . OP_RESERVED ; // OP_1 - 1
1116export { OPS } ;
1217
@@ -50,6 +55,13 @@ function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer {
5055 return Buffer . isBuffer ( buf ) ;
5156}
5257
58+ /**
59+ * Compiles an array of chunks into a Buffer.
60+ *
61+ * @param chunks - The array of chunks to compile.
62+ * @returns The compiled Buffer.
63+ * @throws Error if the compilation fails.
64+ */
5365export function compile ( chunks : Buffer | Stack ) : Buffer {
5466 // TODO: remove me
5567 if ( chunksIsBuffer ( chunks ) ) return chunks ;
@@ -147,6 +159,12 @@ export function decompile(
147159 return chunks ;
148160}
149161
162+ /**
163+ * Converts the given chunks into an ASM (Assembly) string representation.
164+ * If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
165+ * @param chunks - The chunks to convert into ASM.
166+ * @returns The ASM string representation of the chunks.
167+ */
150168export function toASM ( chunks : Buffer | Array < number | Buffer > ) : string {
151169 if ( chunksIsBuffer ( chunks ) ) {
152170 chunks = decompile ( chunks ) as Stack ;
@@ -167,6 +185,11 @@ export function toASM(chunks: Buffer | Array<number | Buffer>): string {
167185 . join ( ' ' ) ;
168186}
169187
188+ /**
189+ * Converts an ASM string to a Buffer.
190+ * @param asm The ASM string to convert.
191+ * @returns The converted Buffer.
192+ */
170193export function fromASM ( asm : string ) : Buffer {
171194 typeforce ( types . String , asm ) ;
172195
@@ -182,6 +205,12 @@ export function fromASM(asm: string): Buffer {
182205 ) ;
183206}
184207
208+ /**
209+ * Converts the given chunks into a stack of buffers.
210+ *
211+ * @param chunks - The chunks to convert.
212+ * @returns The stack of buffers.
213+ */
185214export function toStack ( chunks : Buffer | Array < number | Buffer > ) : Buffer [ ] {
186215 chunks = decompile ( chunks ) as Stack ;
187216 typeforce ( isPushOnly , chunks ) ;
0 commit comments