I wrote the next perform to construct and signal Taproot (P2TR) transactions utilizing @cmdcode/tapscript. My intention is to assist each key-path and script-path spends, and optionally each collectively.
The issue is that it doesn’t work as anticipated:
Script-path spends typically fail to validate (e.g., management block errors, invalid witness, or failed script execution).
Can somebody assessment my code and level out what’s fallacious with my logic or implementation?
I’d particularly recognize recommendation on methods to repair script-path failures and any efficiency enhancements for the key-path case.
import { Handle, Signer, Faucet, Tx } from '@cmdcode/tapscript';
protected buildTaprootTx(
senderKey: { publicKey: Uint8Array; privateKey: Uint8Array },
utxos: Array<{ txid: string; vout: quantity; worth: quantity }>,
recipient: string,
amountSat: quantity,
feeSat: quantity,
mode: 'key' | 'script' | 'each',
scriptLeaves: Array = [],
opReturnData?: Uint8Array | string,
changeAddr?: string
): string {
// ... (full code as in my gist, see hyperlink under)
}
Questions:
-
What am I doing fallacious, particularly concerning script-path spending?
-
Is there a greater solution to construction or optimize the perform for
efficiency and correctness? -
If you happen to spot any apparent bugs or misunderstandings in how I exploit
Taproot key/script path logic, please level them out.
Any code assessment, solutions, or working instance references are extremely appreciated. Thanks!