@@ -169,6 +169,13 @@ function checkTaprootScriptPubkey(
169169 }
170170}
171171
172+ /**
173+ * Returns the Taproot script public key.
174+ *
175+ * @param tapInternalKey - The Taproot internal key.
176+ * @param tapTree - The Taproot tree (optional).
177+ * @returns The Taproot script public key.
178+ */
172179function getTaprootScripPubkey (
173180 tapInternalKey : TapInternalKey ,
174181 tapTree ?: TapTree ,
@@ -181,6 +188,13 @@ function getTaprootScripPubkey(
181188 return output ! ;
182189}
183190
191+ /**
192+ * Tweak the internal public key for a specific input.
193+ * @param inputIndex - The index of the input.
194+ * @param input - The PsbtInput object representing the input.
195+ * @returns The tweaked internal public key.
196+ * @throws Error if the tap internal key cannot be tweaked.
197+ */
184198export function tweakInternalPubKey (
185199 inputIndex : number ,
186200 input : PsbtInput ,
@@ -232,6 +246,12 @@ export function tapTreeFromList(leaves: TapLeaf[] = []): Taptree {
232246 return instertLeavesInTree ( leaves ) ;
233247}
234248
249+ /**
250+ * Checks the taproot input for signatures.
251+ * @param input The PSBT input to check.
252+ * @param action The action being performed.
253+ * @returns True if the input has taproot signatures, false otherwise.
254+ */
235255export function checkTaprootInputForSigs (
236256 input : PsbtInput ,
237257 action : string ,
@@ -242,6 +262,11 @@ export function checkTaprootInputForSigs(
242262 ) ;
243263}
244264
265+ /**
266+ * Decodes a Schnorr signature.
267+ * @param signature The signature to decode.
268+ * @returns The decoded Schnorr signature.
269+ */
245270function decodeSchnorrSignature ( signature : Buffer ) : {
246271 signature : Buffer ;
247272 hashType : number ;
@@ -252,6 +277,11 @@ function decodeSchnorrSignature(signature: Buffer): {
252277 } ;
253278}
254279
280+ /**
281+ * Extracts taproot signatures from a PSBT input.
282+ * @param input The PSBT input to extract signatures from.
283+ * @returns An array of taproot signatures.
284+ */
255285function extractTaprootSigs ( input : PsbtInput ) : Buffer [ ] {
256286 const sigs : Buffer [ ] = [ ] ;
257287 if ( input . tapKeySig ) sigs . push ( input . tapKeySig ) ;
@@ -265,6 +295,11 @@ function extractTaprootSigs(input: PsbtInput): Buffer[] {
265295 return sigs ;
266296}
267297
298+ /**
299+ * Gets the taproot signature from the witness.
300+ * @param finalScriptWitness The final script witness.
301+ * @returns The taproot signature, or undefined if not found.
302+ */
268303function getTapKeySigFromWithness (
269304 finalScriptWitness ?: Buffer ,
270305) : Buffer | undefined {
@@ -274,6 +309,14 @@ function getTapKeySigFromWithness(
274309 if ( witness . length === 64 || witness . length === 65 ) return witness ;
275310}
276311
312+ /**
313+ * Converts a binary tree to a BIP371 type list.
314+ * @param tree The binary tap tree.
315+ * @param leaves A list of tapleaves. Optional.
316+ * @param depth The current depth. Optional.
317+ * @returns A list of BIP 371 tapleaves.
318+ * @throws Throws an error if the taptree cannot be converted to a tapleaf list.
319+ */
277320function _tapTreeToList (
278321 tree : Taptree ,
279322 leaves : TapLeaf [ ] = [ ] ,
@@ -299,6 +342,13 @@ type PartialTaptree =
299342 | [ PartialTaptree | Tapleaf , PartialTaptree | Tapleaf ]
300343 | Tapleaf
301344 | undefined ;
345+
346+ /**
347+ * Inserts the tapleaves into the taproot tree.
348+ * @param leaves The tapleaves to insert.
349+ * @returns The taproot tree.
350+ * @throws Throws an error if there is no room left to insert a tapleaf in the tree.
351+ */
302352function instertLeavesInTree ( leaves : TapLeaf [ ] ) : Taptree {
303353 let tree : PartialTaptree ;
304354 for ( const leaf of leaves ) {
@@ -309,6 +359,13 @@ function instertLeavesInTree(leaves: TapLeaf[]): Taptree {
309359 return tree as Taptree ;
310360}
311361
362+ /**
363+ * Inserts a tapleaf into the taproot tree.
364+ * @param leaf The tapleaf to insert.
365+ * @param tree The taproot tree.
366+ * @param depth The current depth. Optional.
367+ * @returns The updated taproot tree.
368+ */
312369function instertLeafInTree (
313370 leaf : TapLeaf ,
314371 tree ?: PartialTaptree ,
@@ -332,6 +389,13 @@ function instertLeafInTree(
332389 if ( rightSide ) return [ tree && tree [ 0 ] , rightSide ] ;
333390}
334391
392+ /**
393+ * Checks the input fields for mixed taproot and non-taproot fields.
394+ * @param inputData The original input data.
395+ * @param newInputData The new input data.
396+ * @param action The action being performed.
397+ * @throws Throws an error if the input fields are inconsistent.
398+ */
335399function checkMixedTaprootAndNonTaprootInputFields (
336400 inputData : PsbtOutput ,
337401 newInputData : PsbtInput ,
@@ -352,6 +416,14 @@ function checkMixedTaprootAndNonTaprootInputFields(
352416 `Cannot use both taproot and non-taproot fields.` ,
353417 ) ;
354418}
419+
420+ /**
421+ * Checks the output fields for mixed taproot and non-taproot fields.
422+ * @param inputData The original output data.
423+ * @param newInputData The new output data.
424+ * @param action The action being performed.
425+ * @throws Throws an error if the output fields are inconsistent.
426+ */
355427function checkMixedTaprootAndNonTaprootOutputFields (
356428 inputData : PsbtOutput ,
357429 newInputData : PsbtOutput ,
0 commit comments