-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecomputedTransactionData.cs
More file actions
69 lines (59 loc) · 2.1 KB
/
PrecomputedTransactionData.cs
File metadata and controls
69 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using BitcoinKernel.Core.Abstractions;
using BitcoinKernel.Core.Exceptions;
using BitcoinKernel.Interop;
namespace BitcoinKernel.Core.ScriptVerification;
/// <summary>
/// Holds precomputed transaction data used to accelerate repeated script verification
/// across multiple inputs of the same transaction.
/// Required when <c>btck_ScriptVerificationFlags_TAPROOT</c> is set.
/// </summary>
public sealed class PrecomputedTransactionData : IDisposable
{
private IntPtr _handle;
private bool _disposed;
/// <summary>
/// Creates precomputed transaction data for the given transaction.
/// </summary>
/// <param name="transaction">The transaction being verified.</param>
/// <param name="spentOutputs">
/// The outputs being spent by the transaction inputs. Required when the TAPROOT
/// verification flag is set. Must match the transaction input count if provided.
/// </param>
public PrecomputedTransactionData(Transaction transaction, IReadOnlyList<TxOut>? spentOutputs = null)
{
ArgumentNullException.ThrowIfNull(transaction);
IntPtr[] handles = spentOutputs is { Count: > 0 }
? spentOutputs.Select(o => o.Handle).ToArray()
: Array.Empty<IntPtr>();
_handle = NativeMethods.PrecomputedTransactionDataCreate(
transaction.Handle,
handles,
(nuint)handles.Length);
if (_handle == IntPtr.Zero)
throw new KernelException("Failed to create precomputed transaction data");
}
internal IntPtr Handle
{
get
{
ThrowIfDisposed();
return _handle;
}
}
public void Dispose()
{
if (!_disposed && _handle != IntPtr.Zero)
{
NativeMethods.PrecomputedTransactionDataDestroy(_handle);
_handle = IntPtr.Zero;
_disposed = true;
}
GC.SuppressFinalize(this);
}
~PrecomputedTransactionData() => Dispose();
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(PrecomputedTransactionData));
}
}