Skip to content

Commit 6cc676d

Browse files
committed
📄 docs: add docs for payments module
1 parent 5bf4367 commit 6cc676d

File tree

10 files changed

+76
-0
lines changed

10 files changed

+76
-0
lines changed

ts_src/payments/bip341.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const isHashBranch = (ht: HashTree): ht is HashBranch =>
3434
*/
3535
export type HashTree = HashLeaf | HashBranch;
3636

37+
/**
38+
* Calculates the root hash from a given control block and leaf hash.
39+
* @param controlBlock - The control block buffer.
40+
* @param leafHash - The leaf hash buffer.
41+
* @returns The root hash buffer.
42+
* @throws {TypeError} If the control block length is less than 33.
43+
*/
3744
export function rootHashFromPath(
3845
controlBlock: Buffer,
3946
leafHash: Buffer,

ts_src/payments/embed.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
1515
}
1616

1717
// output: OP_RETURN ...
18+
/**
19+
* Embeds data in a Bitcoin payment.
20+
* @param a - The payment object.
21+
* @param opts - Optional payment options.
22+
* @returns The modified payment object.
23+
* @throws {TypeError} If there is not enough data or if the output is invalid.
24+
*/
1825
export function p2data(a: Payment, opts?: PaymentOpts): Payment {
1926
if (!a.data && !a.output) throw new TypeError('Not enough data');
2027
opts = Object.assign({ validate: true }, opts || {});

ts_src/payments/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Represents a payment object, which is used to create a payment.
3+
*
4+
* Supports P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
5+
*
6+
* @packageDocumentation
7+
*/
18
import { Network } from '../networks';
29
import { Taptree } from '../types';
310
import { p2data as embed } from './embed';

ts_src/payments/p2ms.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
1717

1818
// input: OP_0 [signatures ...]
1919
// output: m [pubKeys ...] n OP_CHECKMULTISIG
20+
/**
21+
* Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
22+
* @param a - The payment object.
23+
* @param opts - Optional payment options.
24+
* @returns The created payment object.
25+
* @throws {TypeError} If the provided data is not valid.
26+
*/
2027
export function p2ms(a: Payment, opts?: PaymentOpts): Payment {
2128
if (
2229
!a.input &&

ts_src/payments/p2pk.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const OPS = bscript.OPS;
77

88
// input: {signature}
99
// output: {pubKey} OP_CHECKSIG
10+
/**
11+
* Creates a pay-to-public-key (P2PK) payment object.
12+
*
13+
* @param a - The payment object containing the necessary data.
14+
* @param opts - Optional payment options.
15+
* @returns The P2PK payment object.
16+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
17+
*/
1018
export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
1119
if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
1220
throw new TypeError('Not enough data');

ts_src/payments/p2pkh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const OPS = bscript.OPS;
99

1010
// input: {signature} {pubkey}
1111
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
12+
/**
13+
* Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
14+
*
15+
* @param a - The payment object containing the necessary data.
16+
* @param opts - Optional payment options.
17+
* @returns The P2PKH payment object.
18+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
19+
*/
1220
export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
1321
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
1422
throw new TypeError('Not enough data');

ts_src/payments/p2sh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
2424
// input: [redeemScriptSig ...] {redeemScript}
2525
// witness: <?>
2626
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
27+
/**
28+
* Creates a Pay-to-Script-Hash (P2SH) payment object.
29+
*
30+
* @param a - The payment object containing the necessary data.
31+
* @param opts - Optional payment options.
32+
* @returns The P2SH payment object.
33+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
34+
*/
2735
export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
2836
if (!a.address && !a.hash && !a.output && !a.redeem && !a.input)
2937
throw new TypeError('Not enough data');

ts_src/payments/p2tr.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const OPS = bscript.OPS;
1919
const TAPROOT_WITNESS_VERSION = 0x01;
2020
const ANNEX_PREFIX = 0x50;
2121

22+
/**
23+
* Creates a Pay-to-Taproot (P2TR) payment object.
24+
*
25+
* @param a - The payment object containing the necessary data for P2TR.
26+
* @param opts - Optional payment options.
27+
* @returns The P2TR payment object.
28+
* @throws {TypeError} If the provided data is invalid or insufficient.
29+
*/
2230
export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
2331
if (
2432
!a.address &&

ts_src/payments/p2wpkh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const EMPTY_BUFFER = Buffer.alloc(0);
1212
// witness: {signature} {pubKey}
1313
// input: <>
1414
// output: OP_0 {pubKeyHash}
15+
/**
16+
* Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
17+
*
18+
* @param a - The payment object containing the necessary data.
19+
* @param opts - Optional payment options.
20+
* @returns The p2wpkh payment object.
21+
* @throws {TypeError} If the required data is missing or invalid.
22+
*/
1523
export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
1624
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
1725
throw new TypeError('Not enough data');

ts_src/payments/p2wsh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ function chunkHasUncompressedPubkey(chunk: StackElement): boolean {
3333
// input: <>
3434
// witness: [redeemScriptSig ...] {redeemScript}
3535
// output: OP_0 {sha256(redeemScript)}
36+
/**
37+
* Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object.
38+
*
39+
* @param a - The payment object containing the necessary data.
40+
* @param opts - Optional payment options.
41+
* @returns The P2WSH payment object.
42+
* @throws {TypeError} If the required data is missing or invalid.
43+
*/
3644
export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
3745
if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
3846
throw new TypeError('Not enough data');

0 commit comments

Comments
 (0)