@@ -15,6 +15,55 @@ namespace BitcoinKernel.Core.ScriptVerification;
1515public static class ScriptVerifier
1616{
1717
18+ /// <summary>
19+ /// Verifies a script pubkey using externally-managed precomputed transaction data.
20+ /// Use this overload when the <see cref="PrecomputedTransactionData"/> is created
21+ /// separately and reused across multiple inputs of the same transaction.
22+ /// </summary>
23+ /// <param name="scriptPubkey">The output script to verify against.</param>
24+ /// <param name="amount">The amount of the output being spent.</param>
25+ /// <param name="transaction">The transaction containing the input to verify.</param>
26+ /// <param name="precomputedTxData">Optional externally-managed precomputed transaction data. Required for Taproot.</param>
27+ /// <param name="inputIndex">The index of the transaction input to verify.</param>
28+ /// <param name="flags">Script verification flags to use.</param>
29+ /// <returns>True if the script is valid, false if invalid.</returns>
30+ /// <exception cref="ScriptVerificationException">Thrown when verification fails with an error status.</exception>
31+ public static bool VerifyScript (
32+ ScriptPubKey scriptPubkey ,
33+ long amount ,
34+ Transaction transaction ,
35+ PrecomputedTransactionData ? precomputedTxData ,
36+ uint inputIndex ,
37+ ScriptVerificationFlags flags = ScriptVerificationFlags . All )
38+ {
39+ IntPtr precomputedPtr = precomputedTxData ? . Handle ?? IntPtr . Zero ;
40+
41+ IntPtr statusPtr = Marshal . AllocHGlobal ( 1 ) ;
42+ try
43+ {
44+ int result = NativeMethods . ScriptPubkeyVerify (
45+ scriptPubkey . Handle ,
46+ amount ,
47+ transaction . Handle ,
48+ precomputedPtr ,
49+ inputIndex ,
50+ ( uint ) flags ,
51+ statusPtr ) ;
52+
53+ byte statusCode = Marshal . ReadByte ( statusPtr ) ;
54+ var status = ( ScriptVerifyStatus ) statusCode ;
55+
56+ if ( status != ScriptVerifyStatus . OK )
57+ throw new ScriptVerificationException ( status , $ "Script verification failed: { status } ") ;
58+
59+ return result != 0 ;
60+ }
61+ finally
62+ {
63+ Marshal . FreeHGlobal ( statusPtr ) ;
64+ }
65+ }
66+
1867 /// <summary>
1968 /// Verifies a script pubkey against a transaction input, throwing an exception on error.
2069 /// </summary>
0 commit comments