Skip to content

Commit 7dc2d80

Browse files
committed
docs: remove PSBT duplicate annotations
1 parent 39c5b82 commit 7dc2d80

File tree

5 files changed

+167
-15
lines changed

5 files changed

+167
-15
lines changed

src/psbt/bip371.d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/// <reference types="node" />
22
import { Taptree } from '../types';
33
import { PsbtInput, PsbtOutput, TapLeaf } from 'bip174/src/lib/interfaces';
4+
/**
5+
* Converts a public key to an X-only public key.
6+
* @param pubKey The public key to convert.
7+
* @returns The X-only public key.
8+
*/
49
export declare const toXOnly: (pubKey: Buffer) => Buffer;
510
/**
611
* Default tapscript finalizer. It searches for the `tapLeafHashToFinalize` if provided.
@@ -14,11 +19,49 @@ export declare const toXOnly: (pubKey: Buffer) => Buffer;
1419
export declare function tapScriptFinalizer(inputIndex: number, input: PsbtInput, tapLeafHashToFinalize?: Buffer): {
1520
finalScriptWitness: Buffer | undefined;
1621
};
22+
/**
23+
* Serializes a taproot signature.
24+
* @param sig The signature to serialize.
25+
* @param sighashType The sighash type. Optional.
26+
* @returns The serialized taproot signature.
27+
*/
1728
export declare function serializeTaprootSignature(sig: Buffer, sighashType?: number): Buffer;
29+
/**
30+
* Checks if a PSBT input is a taproot input.
31+
* @param input The PSBT input to check.
32+
* @returns True if the input is a taproot input, false otherwise.
33+
*/
1834
export declare function isTaprootInput(input: PsbtInput): boolean;
35+
/**
36+
* Checks if a PSBT output is a taproot output.
37+
* @param output The PSBT output to check.
38+
* @param script The script to check. Optional.
39+
* @returns True if the output is a taproot output, false otherwise.
40+
*/
1941
export declare function isTaprootOutput(output: PsbtOutput, script?: Buffer): boolean;
42+
/**
43+
* Checks the taproot input fields for consistency.
44+
* @param inputData The original input data.
45+
* @param newInputData The new input data.
46+
* @param action The action being performed.
47+
* @throws Throws an error if the input fields are inconsistent.
48+
*/
2049
export declare function checkTaprootInputFields(inputData: PsbtInput, newInputData: PsbtInput, action: string): void;
50+
/**
51+
* Checks the taproot output fields for consistency.
52+
* @param outputData The original output data.
53+
* @param newOutputData The new output data.
54+
* @param action The action being performed.
55+
* @throws Throws an error if the output fields are inconsistent.
56+
*/
2157
export declare function checkTaprootOutputFields(outputData: PsbtOutput, newOutputData: PsbtOutput, action: string): void;
58+
/**
59+
* Tweak the internal public key for a specific input.
60+
* @param inputIndex - The index of the input.
61+
* @param input - The PsbtInput object representing the input.
62+
* @returns The tweaked internal public key.
63+
* @throws Error if the tap internal key cannot be tweaked.
64+
*/
2265
export declare function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Buffer;
2366
/**
2467
* Convert a binary tree to a BIP371 type list. Each element of the list is (according to BIP371):
@@ -38,4 +81,10 @@ export declare function tapTreeToList(tree: Taptree): TapLeaf[];
3881
* @returns the corresponding taptree, or throws an exception if the tree cannot be reconstructed
3982
*/
4083
export declare function tapTreeFromList(leaves?: TapLeaf[]): Taptree;
84+
/**
85+
* Checks the taproot input for signatures.
86+
* @param input The PSBT input to check.
87+
* @param action The action being performed.
88+
* @returns True if the input has taproot signatures, false otherwise.
89+
*/
4190
export declare function checkTaprootInputForSigs(input: PsbtInput, action: string): boolean;

src/psbt/bip371.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const psbtutils_1 = require('./psbtutils');
1818
const bip341_1 = require('../payments/bip341');
1919
const payments_1 = require('../payments');
2020
const psbtutils_2 = require('./psbtutils');
21+
/**
22+
* Converts a public key to an X-only public key.
23+
* @param pubKey The public key to convert.
24+
* @returns The X-only public key.
25+
*/
2126
const toXOnly = pubKey => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
2227
exports.toXOnly = toXOnly;
2328
/**
@@ -46,13 +51,24 @@ function tapScriptFinalizer(inputIndex, input, tapLeafHashToFinalize) {
4651
}
4752
}
4853
exports.tapScriptFinalizer = tapScriptFinalizer;
54+
/**
55+
* Serializes a taproot signature.
56+
* @param sig The signature to serialize.
57+
* @param sighashType The sighash type. Optional.
58+
* @returns The serialized taproot signature.
59+
*/
4960
function serializeTaprootSignature(sig, sighashType) {
5061
const sighashTypeByte = sighashType
5162
? Buffer.from([sighashType])
5263
: Buffer.from([]);
5364
return Buffer.concat([sig, sighashTypeByte]);
5465
}
5566
exports.serializeTaprootSignature = serializeTaprootSignature;
67+
/**
68+
* Checks if a PSBT input is a taproot input.
69+
* @param input The PSBT input to check.
70+
* @returns True if the input is a taproot input, false otherwise.
71+
*/
5672
function isTaprootInput(input) {
5773
return (
5874
input &&
@@ -66,6 +82,12 @@ function isTaprootInput(input) {
6682
);
6783
}
6884
exports.isTaprootInput = isTaprootInput;
85+
/**
86+
* Checks if a PSBT output is a taproot output.
87+
* @param output The PSBT output to check.
88+
* @param script The script to check. Optional.
89+
* @returns True if the output is a taproot output, false otherwise.
90+
*/
6991
function isTaprootOutput(output, script) {
7092
return (
7193
output &&
@@ -78,11 +100,25 @@ function isTaprootOutput(output, script) {
78100
);
79101
}
80102
exports.isTaprootOutput = isTaprootOutput;
103+
/**
104+
* Checks the taproot input fields for consistency.
105+
* @param inputData The original input data.
106+
* @param newInputData The new input data.
107+
* @param action The action being performed.
108+
* @throws Throws an error if the input fields are inconsistent.
109+
*/
81110
function checkTaprootInputFields(inputData, newInputData, action) {
82111
checkMixedTaprootAndNonTaprootInputFields(inputData, newInputData, action);
83112
checkIfTapLeafInTree(inputData, newInputData, action);
84113
}
85114
exports.checkTaprootInputFields = checkTaprootInputFields;
115+
/**
116+
* Checks the taproot output fields for consistency.
117+
* @param outputData The original output data.
118+
* @param newOutputData The new output data.
119+
* @param action The action being performed.
120+
* @throws Throws an error if the output fields are inconsistent.
121+
*/
86122
function checkTaprootOutputFields(outputData, newOutputData, action) {
87123
checkMixedTaprootAndNonTaprootOutputFields(outputData, newOutputData, action);
88124
checkTaprootScriptPubkey(outputData, newOutputData);
@@ -100,6 +136,13 @@ function checkTaprootScriptPubkey(outputData, newOutputData) {
100136
throw new Error('Error adding output. Script or address mismatch.');
101137
}
102138
}
139+
/**
140+
* Returns the Taproot script public key.
141+
*
142+
* @param tapInternalKey - The Taproot internal key.
143+
* @param tapTree - The Taproot tree (optional).
144+
* @returns The Taproot script public key.
145+
*/
103146
function getTaprootScripPubkey(tapInternalKey, tapTree) {
104147
const scriptTree = tapTree && tapTreeFromList(tapTree.leaves);
105148
const { output } = (0, payments_1.p2tr)({
@@ -108,6 +151,13 @@ function getTaprootScripPubkey(tapInternalKey, tapTree) {
108151
});
109152
return output;
110153
}
154+
/**
155+
* Tweak the internal public key for a specific input.
156+
* @param inputIndex - The index of the input.
157+
* @param input - The PsbtInput object representing the input.
158+
* @returns The tweaked internal public key.
159+
* @throws Error if the tap internal key cannot be tweaked.
160+
*/
111161
function tweakInternalPubKey(inputIndex, input) {
112162
const tapInternalKey = input.tapInternalKey;
113163
const outputKey =
@@ -155,20 +205,36 @@ function tapTreeFromList(leaves = []) {
155205
return instertLeavesInTree(leaves);
156206
}
157207
exports.tapTreeFromList = tapTreeFromList;
208+
/**
209+
* Checks the taproot input for signatures.
210+
* @param input The PSBT input to check.
211+
* @param action The action being performed.
212+
* @returns True if the input has taproot signatures, false otherwise.
213+
*/
158214
function checkTaprootInputForSigs(input, action) {
159215
const sigs = extractTaprootSigs(input);
160216
return sigs.some(sig =>
161217
(0, psbtutils_2.signatureBlocksAction)(sig, decodeSchnorrSignature, action),
162218
);
163219
}
164220
exports.checkTaprootInputForSigs = checkTaprootInputForSigs;
221+
/**
222+
* Decodes a Schnorr signature.
223+
* @param signature The signature to decode.
224+
* @returns The decoded Schnorr signature.
225+
*/
165226
function decodeSchnorrSignature(signature) {
166227
return {
167228
signature: signature.slice(0, 64),
168229
hashType:
169230
signature.slice(64)[0] || transaction_1.Transaction.SIGHASH_DEFAULT,
170231
};
171232
}
233+
/**
234+
* Extracts taproot signatures from a PSBT input.
235+
* @param input The PSBT input to extract signatures from.
236+
* @returns An array of taproot signatures.
237+
*/
172238
function extractTaprootSigs(input) {
173239
const sigs = [];
174240
if (input.tapKeySig) sigs.push(input.tapKeySig);
@@ -180,12 +246,25 @@ function extractTaprootSigs(input) {
180246
}
181247
return sigs;
182248
}
249+
/**
250+
* Gets the taproot signature from the witness.
251+
* @param finalScriptWitness The final script witness.
252+
* @returns The taproot signature, or undefined if not found.
253+
*/
183254
function getTapKeySigFromWithness(finalScriptWitness) {
184255
if (!finalScriptWitness) return;
185256
const witness = finalScriptWitness.slice(2);
186257
// todo: add schnorr signature validation
187258
if (witness.length === 64 || witness.length === 65) return witness;
188259
}
260+
/**
261+
* Converts a binary tree to a BIP371 type list.
262+
* @param tree The binary tap tree.
263+
* @param leaves A list of tapleaves. Optional.
264+
* @param depth The current depth. Optional.
265+
* @returns A list of BIP 371 tapleaves.
266+
* @throws Throws an error if the taptree cannot be converted to a tapleaf list.
267+
*/
189268
function _tapTreeToList(tree, leaves = [], depth = 0) {
190269
if (depth > bip341_1.MAX_TAPTREE_DEPTH)
191270
throw new Error('Max taptree depth exceeded.');
@@ -202,6 +281,12 @@ function _tapTreeToList(tree, leaves = [], depth = 0) {
202281
if (tree[1]) _tapTreeToList(tree[1], leaves, depth + 1);
203282
return leaves;
204283
}
284+
/**
285+
* Inserts the tapleaves into the taproot tree.
286+
* @param leaves The tapleaves to insert.
287+
* @returns The taproot tree.
288+
* @throws Throws an error if there is no room left to insert a tapleaf in the tree.
289+
*/
205290
function instertLeavesInTree(leaves) {
206291
let tree;
207292
for (const leaf of leaves) {
@@ -210,6 +295,13 @@ function instertLeavesInTree(leaves) {
210295
}
211296
return tree;
212297
}
298+
/**
299+
* Inserts a tapleaf into the taproot tree.
300+
* @param leaf The tapleaf to insert.
301+
* @param tree The taproot tree.
302+
* @param depth The current depth. Optional.
303+
* @returns The updated taproot tree.
304+
*/
213305
function instertLeafInTree(leaf, tree, depth = 0) {
214306
if (depth > bip341_1.MAX_TAPTREE_DEPTH)
215307
throw new Error('Max taptree depth exceeded.');
@@ -227,6 +319,13 @@ function instertLeafInTree(leaf, tree, depth = 0) {
227319
const rightSide = instertLeafInTree(leaf, tree && tree[1], depth + 1);
228320
if (rightSide) return [tree && tree[0], rightSide];
229321
}
322+
/**
323+
* Checks the input fields for mixed taproot and non-taproot fields.
324+
* @param inputData The original input data.
325+
* @param newInputData The new input data.
326+
* @param action The action being performed.
327+
* @throws Throws an error if the input fields are inconsistent.
328+
*/
230329
function checkMixedTaprootAndNonTaprootInputFields(
231330
inputData,
232331
newInputData,
@@ -246,6 +345,13 @@ function checkMixedTaprootAndNonTaprootInputFields(
246345
`Cannot use both taproot and non-taproot fields.`,
247346
);
248347
}
348+
/**
349+
* Checks the output fields for mixed taproot and non-taproot fields.
350+
* @param inputData The original output data.
351+
* @param newInputData The new output data.
352+
* @param action The action being performed.
353+
* @throws Throws an error if the output fields are inconsistent.
354+
*/
249355
function checkMixedTaprootAndNonTaprootOutputFields(
250356
inputData,
251357
newInputData,

src/psbt/psbtutils.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ export declare const isP2TR: (script: Buffer) => boolean;
1212
* @param witness The witness stack to convert.
1313
* @returns The script witness as a Buffer.
1414
*/
15-
/**
16-
* Converts a witness stack to a script witness.
17-
* @param witness The witness stack to convert.
18-
* @returns The converted script witness.
19-
*/
2015
export declare function witnessStackToScriptWitness(witness: Buffer[]): Buffer;
2116
/**
2217
* Finds the position of a public key in a script.

src/psbt/psbtutils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const bscript = require('../script');
1818
const transaction_1 = require('../transaction');
1919
const crypto_1 = require('../crypto');
2020
const payments = require('../payments');
21+
/**
22+
* Checks if a given payment factory can generate a payment script from a given script.
23+
* @param payment The payment factory to check.
24+
* @returns A function that takes a script and returns a boolean indicating whether the payment factory can generate a payment script from the script.
25+
*/
2126
function isPaymentFactory(payment) {
2227
return script => {
2328
try {
@@ -40,11 +45,6 @@ exports.isP2TR = isPaymentFactory(payments.p2tr);
4045
* @param witness The witness stack to convert.
4146
* @returns The script witness as a Buffer.
4247
*/
43-
/**
44-
* Converts a witness stack to a script witness.
45-
* @param witness The witness stack to convert.
46-
* @returns The converted script witness.
47-
*/
4848
function witnessStackToScriptWitness(witness) {
4949
let buffer = Buffer.allocUnsafe(0);
5050
function writeSlice(slice) {

ts_src/psbt/psbtutils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { Transaction } from '../transaction';
55
import { hash160 } from '../crypto';
66
import * as payments from '../payments';
77

8+
/**
9+
* Checks if a given payment factory can generate a payment script from a given script.
10+
* @param payment The payment factory to check.
11+
* @returns A function that takes a script and returns a boolean indicating whether the payment factory can generate a payment script from the script.
12+
*/
813
function isPaymentFactory(payment: any): (script: Buffer) => boolean {
914
return (script: Buffer): boolean => {
1015
try {
@@ -15,6 +20,7 @@ function isPaymentFactory(payment: any): (script: Buffer) => boolean {
1520
}
1621
};
1722
}
23+
1824
export const isP2MS = isPaymentFactory(payments.p2ms);
1925
export const isP2PK = isPaymentFactory(payments.p2pk);
2026
export const isP2PKH = isPaymentFactory(payments.p2pkh);
@@ -28,11 +34,6 @@ export const isP2TR = isPaymentFactory(payments.p2tr);
2834
* @param witness The witness stack to convert.
2935
* @returns The script witness as a Buffer.
3036
*/
31-
/**
32-
* Converts a witness stack to a script witness.
33-
* @param witness The witness stack to convert.
34-
* @returns The converted script witness.
35-
*/
3637
export function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
3738
let buffer = Buffer.allocUnsafe(0);
3839

@@ -114,6 +115,7 @@ type SignatureDecodeFunc = (buffer: Buffer) => {
114115
signature: Buffer;
115116
hashType: number;
116117
};
118+
117119
/**
118120
* Determines if a given action is allowed for a signature block.
119121
* @param signature - The signature block.

0 commit comments

Comments
 (0)