Skip to content

Commit 94039ae

Browse files
committed
📄 docs: add docs fro script namespace
1 parent 6cc676d commit 94039ae

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

ts_src/script.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Script tools, including decompile, compile, toASM, fromASM, toStack, isCanonicalPubKey, isCanonicalScriptSignature
3+
* @packageDocumentation
4+
*/
15
import * as bip66 from './bip66';
26
import { OPS, REVERSE_OPS } from './ops';
37
import { Stack } from './payments';
@@ -7,6 +11,7 @@ import * as scriptSignature from './script_signature';
711
import * as types from './types';
812
const { typeforce } = types;
913

14+
1015
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
1116
export { 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+
*/
5365
export 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+
*/
150168
export 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+
*/
170193
export 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+
*/
185214
export function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[] {
186215
chunks = decompile(chunks) as Stack;
187216
typeforce(isPushOnly, chunks);

ts_src/script_number.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Decodes a script number from a buffer.
3+
*
4+
* @param buffer - The buffer containing the script number.
5+
* @param maxLength - The maximum length of the script number. Defaults to 4.
6+
* @param minimal - Whether the script number should be minimal. Defaults to true.
7+
* @returns The decoded script number.
8+
* @throws {TypeError} If the script number overflows the maximum length.
9+
* @throws {Error} If the script number is not minimally encoded when minimal is true.
10+
*/
111
export function decode(
212
buffer: Buffer,
313
maxLength?: number,
@@ -50,6 +60,12 @@ function scriptNumSize(i: number): number {
5060
: 0;
5161
}
5262

63+
/**
64+
* Encodes a number into a Buffer using a specific format.
65+
*
66+
* @param _number - The number to encode.
67+
* @returns The encoded number as a Buffer.
68+
*/
5369
export function encode(_number: number): Buffer {
5470
let value = Math.abs(_number);
5571
const size = scriptNumSize(value);

ts_src/script_signature.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import * as types from './types';
33
const { typeforce } = types;
44

55
const ZERO = Buffer.alloc(1, 0);
6+
/**
7+
* Converts a buffer to a DER-encoded buffer.
8+
* @param x - The buffer to be converted.
9+
* @returns The DER-encoded buffer.
10+
*/
611
function toDER(x: Buffer): Buffer {
712
let i = 0;
813
while (x[i] === 0) ++i;
@@ -12,6 +17,13 @@ function toDER(x: Buffer): Buffer {
1217
return x;
1318
}
1419

20+
/**
21+
* Converts a DER-encoded signature to a buffer.
22+
* If the first byte of the input buffer is 0x00, it is skipped.
23+
* The resulting buffer is 32 bytes long, filled with zeros if necessary.
24+
* @param x - The DER-encoded signature.
25+
* @returns The converted buffer.
26+
*/
1527
function fromDER(x: Buffer): Buffer {
1628
if (x[0] === 0x00) x = x.slice(1);
1729
const buffer = Buffer.alloc(32, 0);
@@ -26,6 +38,12 @@ interface ScriptSignature {
2638
}
2739

2840
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
41+
/**
42+
* Decodes a buffer into a ScriptSignature object.
43+
* @param buffer - The buffer to decode.
44+
* @returns The decoded ScriptSignature object.
45+
* @throws Error if the hashType is invalid.
46+
*/
2947
export function decode(buffer: Buffer): ScriptSignature {
3048
const hashType = buffer.readUInt8(buffer.length - 1);
3149
const hashTypeMod = hashType & ~0x80;
@@ -40,6 +58,13 @@ export function decode(buffer: Buffer): ScriptSignature {
4058
return { signature, hashType };
4159
}
4260

61+
/**
62+
* Encodes a signature and hash type into a buffer.
63+
* @param signature - The signature to encode.
64+
* @param hashType - The hash type to encode.
65+
* @returns The encoded buffer.
66+
* @throws Error if the hashType is invalid.
67+
*/
4368
export function encode(signature: Buffer, hashType: number): Buffer {
4469
typeforce(
4570
{

0 commit comments

Comments
 (0)