@originals/sdk 1.8.1 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/hash.js +1 -0
- package/package.json +6 -5
- package/src/adapters/FeeOracleMock.ts +9 -0
- package/src/adapters/index.ts +5 -0
- package/src/adapters/providers/OrdHttpProvider.ts +126 -0
- package/src/adapters/providers/OrdMockProvider.ts +101 -0
- package/src/adapters/types.ts +66 -0
- package/src/bitcoin/BitcoinManager.ts +329 -0
- package/src/bitcoin/BroadcastClient.ts +54 -0
- package/src/bitcoin/OrdinalsClient.ts +120 -0
- package/src/bitcoin/PSBTBuilder.ts +106 -0
- package/src/bitcoin/fee-calculation.ts +38 -0
- package/src/bitcoin/providers/OrdNodeProvider.ts +92 -0
- package/src/bitcoin/providers/OrdinalsProvider.ts +56 -0
- package/src/bitcoin/providers/types.ts +59 -0
- package/src/bitcoin/transactions/commit.ts +465 -0
- package/src/bitcoin/transactions/index.ts +13 -0
- package/src/bitcoin/transfer.ts +43 -0
- package/src/bitcoin/utxo-selection.ts +322 -0
- package/src/bitcoin/utxo.ts +113 -0
- package/src/cel/ExternalReferenceManager.ts +87 -0
- package/src/cel/OriginalsCel.ts +460 -0
- package/src/cel/algorithms/createEventLog.ts +68 -0
- package/src/cel/algorithms/deactivateEventLog.ts +109 -0
- package/src/cel/algorithms/index.ts +11 -0
- package/src/cel/algorithms/updateEventLog.ts +99 -0
- package/src/cel/algorithms/verifyEventLog.ts +306 -0
- package/src/cel/algorithms/witnessEvent.ts +87 -0
- package/src/cel/cli/create.ts +330 -0
- package/src/cel/cli/index.ts +383 -0
- package/src/cel/cli/inspect.ts +549 -0
- package/src/cel/cli/migrate.ts +473 -0
- package/src/cel/cli/verify.ts +249 -0
- package/src/cel/hash.ts +71 -0
- package/src/cel/index.ts +16 -0
- package/src/cel/layers/BtcoCelManager.ts +408 -0
- package/src/cel/layers/PeerCelManager.ts +371 -0
- package/src/cel/layers/WebVHCelManager.ts +361 -0
- package/src/cel/layers/index.ts +27 -0
- package/src/cel/serialization/cbor.ts +189 -0
- package/src/cel/serialization/index.ts +10 -0
- package/src/cel/serialization/json.ts +209 -0
- package/src/cel/types.ts +160 -0
- package/src/cel/witnesses/BitcoinWitness.ts +184 -0
- package/src/cel/witnesses/HttpWitness.ts +241 -0
- package/src/cel/witnesses/WitnessService.ts +51 -0
- package/src/cel/witnesses/index.ts +11 -0
- package/src/contexts/credentials-v1.json +237 -0
- package/src/contexts/credentials-v2-examples.json +5 -0
- package/src/contexts/credentials-v2.json +340 -0
- package/src/contexts/credentials.json +237 -0
- package/src/contexts/data-integrity-v2.json +81 -0
- package/src/contexts/dids.json +58 -0
- package/src/contexts/ed255192020.json +93 -0
- package/src/contexts/ordinals-plus.json +23 -0
- package/src/contexts/originals.json +22 -0
- package/src/core/OriginalsSDK.ts +420 -0
- package/src/crypto/Multikey.ts +194 -0
- package/src/crypto/Signer.ts +262 -0
- package/src/crypto/noble-init.ts +138 -0
- package/src/did/BtcoDidResolver.ts +231 -0
- package/src/did/DIDManager.ts +705 -0
- package/src/did/Ed25519Verifier.ts +68 -0
- package/src/did/KeyManager.ts +239 -0
- package/src/did/WebVHManager.ts +499 -0
- package/src/did/createBtcoDidDocument.ts +60 -0
- package/src/did/providers/OrdinalsClientProviderAdapter.ts +68 -0
- package/src/events/EventEmitter.ts +222 -0
- package/src/events/index.ts +19 -0
- package/src/events/types.ts +331 -0
- package/src/examples/basic-usage.ts +78 -0
- package/src/examples/create-module-original.ts +435 -0
- package/src/examples/full-lifecycle-flow.ts +514 -0
- package/src/examples/run.ts +60 -0
- package/src/index.ts +204 -0
- package/src/kinds/KindRegistry.ts +320 -0
- package/src/kinds/index.ts +74 -0
- package/src/kinds/types.ts +470 -0
- package/src/kinds/validators/AgentValidator.ts +257 -0
- package/src/kinds/validators/AppValidator.ts +211 -0
- package/src/kinds/validators/DatasetValidator.ts +242 -0
- package/src/kinds/validators/DocumentValidator.ts +311 -0
- package/src/kinds/validators/MediaValidator.ts +269 -0
- package/src/kinds/validators/ModuleValidator.ts +225 -0
- package/src/kinds/validators/base.ts +276 -0
- package/src/kinds/validators/index.ts +12 -0
- package/src/lifecycle/BatchOperations.ts +381 -0
- package/src/lifecycle/LifecycleManager.ts +2156 -0
- package/src/lifecycle/OriginalsAsset.ts +524 -0
- package/src/lifecycle/ProvenanceQuery.ts +280 -0
- package/src/lifecycle/ResourceVersioning.ts +163 -0
- package/src/migration/MigrationManager.ts +587 -0
- package/src/migration/audit/AuditLogger.ts +176 -0
- package/src/migration/checkpoint/CheckpointManager.ts +112 -0
- package/src/migration/checkpoint/CheckpointStorage.ts +101 -0
- package/src/migration/index.ts +33 -0
- package/src/migration/operations/BaseMigration.ts +126 -0
- package/src/migration/operations/PeerToBtcoMigration.ts +105 -0
- package/src/migration/operations/PeerToWebvhMigration.ts +62 -0
- package/src/migration/operations/WebvhToBtcoMigration.ts +105 -0
- package/src/migration/rollback/RollbackManager.ts +170 -0
- package/src/migration/state/StateMachine.ts +92 -0
- package/src/migration/state/StateTracker.ts +156 -0
- package/src/migration/types.ts +356 -0
- package/src/migration/validation/BitcoinValidator.ts +107 -0
- package/src/migration/validation/CredentialValidator.ts +62 -0
- package/src/migration/validation/DIDCompatibilityValidator.ts +151 -0
- package/src/migration/validation/LifecycleValidator.ts +64 -0
- package/src/migration/validation/StorageValidator.ts +79 -0
- package/src/migration/validation/ValidationPipeline.ts +213 -0
- package/src/resources/ResourceManager.ts +655 -0
- package/src/resources/index.ts +21 -0
- package/src/resources/types.ts +202 -0
- package/src/storage/LocalStorageAdapter.ts +64 -0
- package/src/storage/MemoryStorageAdapter.ts +29 -0
- package/src/storage/StorageAdapter.ts +25 -0
- package/src/storage/index.ts +3 -0
- package/src/types/bitcoin.ts +98 -0
- package/src/types/common.ts +92 -0
- package/src/types/credentials.ts +89 -0
- package/src/types/did.ts +31 -0
- package/src/types/external-shims.d.ts +53 -0
- package/src/types/index.ts +7 -0
- package/src/types/network.ts +178 -0
- package/src/utils/EventLogger.ts +298 -0
- package/src/utils/Logger.ts +324 -0
- package/src/utils/MetricsCollector.ts +358 -0
- package/src/utils/bitcoin-address.ts +132 -0
- package/src/utils/cbor.ts +31 -0
- package/src/utils/encoding.ts +135 -0
- package/src/utils/hash.ts +12 -0
- package/src/utils/retry.ts +46 -0
- package/src/utils/satoshi-validation.ts +196 -0
- package/src/utils/serialization.ts +102 -0
- package/src/utils/telemetry.ts +44 -0
- package/src/utils/validation.ts +123 -0
- package/src/vc/CredentialManager.ts +955 -0
- package/src/vc/Issuer.ts +105 -0
- package/src/vc/Verifier.ts +54 -0
- package/src/vc/cryptosuites/bbs.ts +253 -0
- package/src/vc/cryptosuites/bbsSimple.ts +21 -0
- package/src/vc/cryptosuites/eddsa.ts +99 -0
- package/src/vc/documentLoader.ts +81 -0
- package/src/vc/proofs/data-integrity.ts +33 -0
- package/src/vc/utils/jsonld.ts +18 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UTXO Selection for Ordinals Transactions
|
|
3
|
+
*
|
|
4
|
+
* This module implements functions for selecting UTXOs for ordinals transactions.
|
|
5
|
+
* It provides a simple coin selection algorithm optimized for ordinals inscriptions.
|
|
6
|
+
*
|
|
7
|
+
* Ported from legacy ordinalsplus transaction infrastructure.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
Utxo,
|
|
12
|
+
DUST_LIMIT_SATS,
|
|
13
|
+
ResourceUtxo,
|
|
14
|
+
ResourceUtxoSelectionOptions,
|
|
15
|
+
ResourceUtxoSelectionResult
|
|
16
|
+
} from '../types/bitcoin.js';
|
|
17
|
+
import { calculateFee } from './fee-calculation.js';
|
|
18
|
+
|
|
19
|
+
// Minimum dust limit for Bitcoin outputs (546 satoshis)
|
|
20
|
+
const MIN_DUST_LIMIT = DUST_LIMIT_SATS;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Estimates transaction size in vbytes based on input and output counts
|
|
24
|
+
* This is a simplified calculation, and actual size may vary based on script types
|
|
25
|
+
*
|
|
26
|
+
* @param inputCount Number of inputs in the transaction
|
|
27
|
+
* @param outputCount Number of outputs in the transaction
|
|
28
|
+
* @returns Estimated transaction size in vbytes
|
|
29
|
+
*/
|
|
30
|
+
export function estimateTransactionSize(inputCount: number, outputCount: number): number {
|
|
31
|
+
// Rough estimation based on segwit transaction format
|
|
32
|
+
// Transaction overhead: ~10 vbytes
|
|
33
|
+
// Each input: ~68 vbytes (P2WPKH)
|
|
34
|
+
// Each output: ~31 vbytes
|
|
35
|
+
return 10 + (inputCount * 68) + (outputCount * 31);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Tags UTXOs as resource-containing or regular based on provided data
|
|
40
|
+
*
|
|
41
|
+
* @param utxos List of UTXOs to tag
|
|
42
|
+
* @param resourceData Optional data about which UTXOs contain resources
|
|
43
|
+
* @returns Tagged ResourceUtxo[] list with hasResource flags set appropriately
|
|
44
|
+
*/
|
|
45
|
+
export function tagResourceUtxos(
|
|
46
|
+
utxos: ResourceUtxo[],
|
|
47
|
+
resourceData?: {[utxoId: string]: boolean}
|
|
48
|
+
): ResourceUtxo[] {
|
|
49
|
+
return utxos.map(utxo => {
|
|
50
|
+
const utxoId = `${utxo.txid}:${utxo.vout}`;
|
|
51
|
+
const hasResource = resourceData ? !!resourceData[utxoId] : utxo.hasResource || false;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
...utxo,
|
|
55
|
+
hasResource
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Options for simple UTXO selection
|
|
62
|
+
*/
|
|
63
|
+
export interface SimpleUtxoSelectionOptions {
|
|
64
|
+
/** Target amount to reach (in satoshis) */
|
|
65
|
+
targetAmount: number;
|
|
66
|
+
/** Optional maximum amount of UTXOs to use */
|
|
67
|
+
maxNumUtxos?: number;
|
|
68
|
+
/** Optional preference for UTXO selection strategy */
|
|
69
|
+
strategy?: 'minimize_change' | 'minimize_inputs' | 'optimize_size';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Result of simple UTXO selection
|
|
74
|
+
*/
|
|
75
|
+
export interface SimpleUtxoSelectionResult {
|
|
76
|
+
/** Selected UTXOs for the transaction */
|
|
77
|
+
selectedUtxos: Utxo[];
|
|
78
|
+
/** Total value of selected UTXOs */
|
|
79
|
+
totalInputValue: number;
|
|
80
|
+
/** Estimated change amount (if any) */
|
|
81
|
+
changeAmount: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Selects UTXOs to cover a target amount using a simplified approach.
|
|
86
|
+
* This version is used specifically for commit transactions where we
|
|
87
|
+
* don't need the more complex resource-aware selection.
|
|
88
|
+
*
|
|
89
|
+
* @param utxos - Available UTXOs
|
|
90
|
+
* @param options - Target amount or detailed options
|
|
91
|
+
* @returns Selected UTXOs and related information
|
|
92
|
+
*/
|
|
93
|
+
export function selectUtxos(
|
|
94
|
+
utxos: Utxo[],
|
|
95
|
+
options: number | SimpleUtxoSelectionOptions
|
|
96
|
+
): SimpleUtxoSelectionResult {
|
|
97
|
+
// Handle simple number input
|
|
98
|
+
const targetAmount = typeof options === 'number'
|
|
99
|
+
? options
|
|
100
|
+
: options.targetAmount;
|
|
101
|
+
|
|
102
|
+
const maxNumUtxos = typeof options === 'number'
|
|
103
|
+
? undefined
|
|
104
|
+
: options.maxNumUtxos;
|
|
105
|
+
|
|
106
|
+
const strategy = typeof options === 'number'
|
|
107
|
+
? 'minimize_inputs'
|
|
108
|
+
: options.strategy || 'minimize_inputs';
|
|
109
|
+
|
|
110
|
+
// Validate inputs
|
|
111
|
+
if (!utxos || utxos.length === 0) {
|
|
112
|
+
throw new Error('No UTXOs provided for selection.');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (targetAmount <= 0) {
|
|
116
|
+
throw new Error(`Invalid target amount: ${targetAmount}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Sort UTXOs based on selected strategy
|
|
120
|
+
const sortedUtxos = [...utxos];
|
|
121
|
+
|
|
122
|
+
if (strategy === 'minimize_inputs') {
|
|
123
|
+
// Sort by value descending to use fewest inputs
|
|
124
|
+
sortedUtxos.sort((a, b) => b.value - a.value);
|
|
125
|
+
} else if (strategy === 'minimize_change') {
|
|
126
|
+
// Sort by value ascending to minimize change
|
|
127
|
+
sortedUtxos.sort((a, b) => a.value - b.value);
|
|
128
|
+
} else if (strategy === 'optimize_size') {
|
|
129
|
+
// Sort by value/size ratio (value density) for optimal fee efficiency
|
|
130
|
+
// For now, just sort by value as a reasonable approximation
|
|
131
|
+
sortedUtxos.sort((a, b) => b.value - a.value);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const selected: Utxo[] = [];
|
|
135
|
+
let totalValue = 0;
|
|
136
|
+
|
|
137
|
+
// Add UTXOs until we reach the target amount
|
|
138
|
+
for (const utxo of sortedUtxos) {
|
|
139
|
+
// Skip invalid UTXOs
|
|
140
|
+
if (!utxo.txid || utxo.vout === undefined || !utxo.value) {
|
|
141
|
+
console.warn(`Skipping invalid UTXO: ${utxo.txid}:${utxo.vout}`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
selected.push(utxo);
|
|
146
|
+
totalValue += utxo.value;
|
|
147
|
+
|
|
148
|
+
// Check if we've reached the target
|
|
149
|
+
if (totalValue >= targetAmount) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Check if we've reached the maximum allowed number of UTXOs
|
|
154
|
+
if (maxNumUtxos && selected.length >= maxNumUtxos) {
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Check if we have enough funds
|
|
160
|
+
if (totalValue < targetAmount) {
|
|
161
|
+
throw new Error(`Insufficient funds. Required: ${targetAmount}, Available: ${totalValue} from ${utxos.length} UTXOs.`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Calculate change amount
|
|
165
|
+
const changeAmount = totalValue - targetAmount;
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
selectedUtxos: selected,
|
|
169
|
+
totalInputValue: totalValue,
|
|
170
|
+
changeAmount
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Selects UTXOs for a transaction, excluding UTXOs with resources unless explicitly allowed
|
|
176
|
+
*
|
|
177
|
+
* @param availableUtxos List of available UTXOs to select from
|
|
178
|
+
* @param options Configuration options for the selection process
|
|
179
|
+
* @returns Selection result with chosen UTXOs and fee information
|
|
180
|
+
* @throws Error if insufficient funds or if all available UTXOs contain resources
|
|
181
|
+
*/
|
|
182
|
+
export function selectResourceUtxos(
|
|
183
|
+
availableUtxos: ResourceUtxo[],
|
|
184
|
+
options: ResourceUtxoSelectionOptions
|
|
185
|
+
): ResourceUtxoSelectionResult {
|
|
186
|
+
const {
|
|
187
|
+
requiredAmount,
|
|
188
|
+
feeRate,
|
|
189
|
+
allowResourceUtxos = false,
|
|
190
|
+
preferOlder = false,
|
|
191
|
+
preferCloserAmount = false,
|
|
192
|
+
avoidUtxoIds = []
|
|
193
|
+
} = options;
|
|
194
|
+
|
|
195
|
+
// Convert requiredAmount to bigint for compatibility with fee calculations
|
|
196
|
+
const requiredAmountBigInt = BigInt(requiredAmount);
|
|
197
|
+
|
|
198
|
+
// Filter out UTXOs to avoid and those with resources if not allowed
|
|
199
|
+
const eligibleUtxos = availableUtxos.filter(utxo => {
|
|
200
|
+
const utxoId = `${utxo.txid}:${utxo.vout}`;
|
|
201
|
+
const shouldAvoid = avoidUtxoIds.includes(utxoId);
|
|
202
|
+
const containsResource = utxo.hasResource === true;
|
|
203
|
+
|
|
204
|
+
// Skip this UTXO if it's in the avoid list
|
|
205
|
+
if (shouldAvoid) return false;
|
|
206
|
+
|
|
207
|
+
// Skip this UTXO if it contains a resource and we're not allowed to use resource UTXOs
|
|
208
|
+
if (containsResource && !allowResourceUtxos) return false;
|
|
209
|
+
|
|
210
|
+
return true;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (eligibleUtxos.length === 0) {
|
|
214
|
+
// Special error message if we have UTXOs but they all contain resources
|
|
215
|
+
if (availableUtxos.length > 0 && availableUtxos.every(u => u.hasResource)) {
|
|
216
|
+
throw new Error('All available UTXOs contain resources and cannot be used for fees/payments. Please add non-resource UTXOs to your wallet.');
|
|
217
|
+
}
|
|
218
|
+
throw new Error('No eligible UTXOs available for selection');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Apply sorting strategy
|
|
222
|
+
if (preferCloserAmount) {
|
|
223
|
+
// Sort by closest to required amount (but still above it)
|
|
224
|
+
eligibleUtxos.sort((a, b) => {
|
|
225
|
+
const aDiff = a.value - requiredAmount;
|
|
226
|
+
const bDiff = b.value - requiredAmount;
|
|
227
|
+
|
|
228
|
+
// Prioritize UTXOs that cover the amount
|
|
229
|
+
if (aDiff >= 0 && bDiff < 0) return -1;
|
|
230
|
+
if (aDiff < 0 && bDiff >= 0) return 1;
|
|
231
|
+
|
|
232
|
+
// If both cover or both don't cover, prefer the one closer to required amount
|
|
233
|
+
return Math.abs(aDiff) - Math.abs(bDiff);
|
|
234
|
+
});
|
|
235
|
+
} else if (preferOlder) {
|
|
236
|
+
// Prefer older UTXOs (by txid as a proxy for age - not perfect but simple)
|
|
237
|
+
eligibleUtxos.sort((a, b) => a.txid.localeCompare(b.txid));
|
|
238
|
+
} else {
|
|
239
|
+
// Default: sort by value descending (largest first)
|
|
240
|
+
eligibleUtxos.sort((a, b) => b.value - a.value);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Initial fee estimation (1 input, 2 outputs - payment and change)
|
|
244
|
+
let estimatedVbytes = estimateTransactionSize(1, 2);
|
|
245
|
+
let estimatedFee = calculateFee(estimatedVbytes, feeRate);
|
|
246
|
+
|
|
247
|
+
// Target amount including estimated fee
|
|
248
|
+
let targetAmount = requiredAmountBigInt + estimatedFee;
|
|
249
|
+
|
|
250
|
+
// Select UTXOs
|
|
251
|
+
const selectedUtxos: ResourceUtxo[] = [];
|
|
252
|
+
let totalSelectedValue = 0n;
|
|
253
|
+
|
|
254
|
+
// First pass: try to find a single UTXO that covers the amount
|
|
255
|
+
const singleUtxo = eligibleUtxos.find(utxo => BigInt(utxo.value) >= targetAmount);
|
|
256
|
+
|
|
257
|
+
if (singleUtxo) {
|
|
258
|
+
selectedUtxos.push(singleUtxo);
|
|
259
|
+
totalSelectedValue = BigInt(singleUtxo.value);
|
|
260
|
+
} else {
|
|
261
|
+
// Second pass: accumulate UTXOs until we reach the target amount
|
|
262
|
+
for (const utxo of eligibleUtxos) {
|
|
263
|
+
selectedUtxos.push(utxo);
|
|
264
|
+
totalSelectedValue += BigInt(utxo.value);
|
|
265
|
+
|
|
266
|
+
// Recalculate fee as we add more inputs
|
|
267
|
+
estimatedVbytes = estimateTransactionSize(selectedUtxos.length, 2);
|
|
268
|
+
estimatedFee = calculateFee(estimatedVbytes, feeRate);
|
|
269
|
+
targetAmount = requiredAmountBigInt + estimatedFee;
|
|
270
|
+
|
|
271
|
+
if (totalSelectedValue >= targetAmount) {
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Final fee calculation based on actual number of inputs
|
|
278
|
+
estimatedVbytes = estimateTransactionSize(selectedUtxos.length, 2);
|
|
279
|
+
estimatedFee = calculateFee(estimatedVbytes, feeRate);
|
|
280
|
+
|
|
281
|
+
// Check if we have enough funds
|
|
282
|
+
if (totalSelectedValue < requiredAmountBigInt + estimatedFee) {
|
|
283
|
+
throw new Error(`Insufficient funds. Required: ${requiredAmountBigInt + estimatedFee}, Available: ${totalSelectedValue}`);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Calculate change
|
|
287
|
+
let changeAmount = totalSelectedValue - requiredAmountBigInt - estimatedFee;
|
|
288
|
+
|
|
289
|
+
// If change is less than dust limit, add it to the fee
|
|
290
|
+
if (changeAmount > 0n && changeAmount < BigInt(MIN_DUST_LIMIT)) {
|
|
291
|
+
estimatedFee += changeAmount;
|
|
292
|
+
changeAmount = 0n;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
selectedUtxos,
|
|
297
|
+
totalSelectedValue: Number(totalSelectedValue),
|
|
298
|
+
estimatedFee: Number(estimatedFee),
|
|
299
|
+
changeAmount: Number(changeAmount)
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Convenience function to select UTXOs for a payment, explicitly avoiding resource UTXOs
|
|
305
|
+
*
|
|
306
|
+
* @param availableUtxos List of available UTXOs
|
|
307
|
+
* @param requiredAmount Amount needed for the payment in satoshis
|
|
308
|
+
* @param feeRate Fee rate in satoshis per vbyte
|
|
309
|
+
* @returns Selection result with UTXOs, fee and change information
|
|
310
|
+
*/
|
|
311
|
+
export function selectUtxosForPayment(
|
|
312
|
+
availableUtxos: ResourceUtxo[],
|
|
313
|
+
requiredAmount: number,
|
|
314
|
+
feeRate: number
|
|
315
|
+
): ResourceUtxoSelectionResult {
|
|
316
|
+
return selectResourceUtxos(availableUtxos, {
|
|
317
|
+
requiredAmount,
|
|
318
|
+
feeRate,
|
|
319
|
+
allowResourceUtxos: false // Never use resource UTXOs for payments
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { DUST_LIMIT_SATS, Utxo } from '../types';
|
|
2
|
+
|
|
3
|
+
export interface FeeEstimateOptions {
|
|
4
|
+
bytesPerInput?: number;
|
|
5
|
+
bytesPerOutput?: number;
|
|
6
|
+
baseTxBytes?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface SelectionOptions {
|
|
10
|
+
feeRateSatsPerVb: number; // sats/vbyte
|
|
11
|
+
targetAmountSats: number; // includes recipient output value
|
|
12
|
+
allowLocked?: boolean;
|
|
13
|
+
forbidInscriptionBearingInputs?: boolean;
|
|
14
|
+
changeAddress?: string;
|
|
15
|
+
feeEstimate?: FeeEstimateOptions;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SelectionResult {
|
|
19
|
+
selected: Utxo[];
|
|
20
|
+
feeSats: number;
|
|
21
|
+
changeSats: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const DEFAULT_FEE_ESTIMATE: Required<FeeEstimateOptions> = {
|
|
25
|
+
bytesPerInput: 148,
|
|
26
|
+
bytesPerOutput: 34,
|
|
27
|
+
baseTxBytes: 10
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function estimateFeeSats(numInputs: number, numOutputs: number, feeRateSatsPerVb: number, feeEstimate: FeeEstimateOptions = {}): number {
|
|
31
|
+
const est = { ...DEFAULT_FEE_ESTIMATE, ...feeEstimate };
|
|
32
|
+
const bytes = est.baseTxBytes + numInputs * est.bytesPerInput + numOutputs * est.bytesPerOutput;
|
|
33
|
+
return Math.ceil(bytes * feeRateSatsPerVb);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class UtxoSelectionError extends Error {
|
|
37
|
+
code: 'INSUFFICIENT_FUNDS' | 'TOO_LOW_FEE' | 'DUST_OUTPUT' | 'CONFLICTING_LOCKS' | 'SAT_SAFETY';
|
|
38
|
+
constructor(code: UtxoSelectionError['code'], message?: string) {
|
|
39
|
+
super(message || code);
|
|
40
|
+
this.code = code;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function selectUtxos(utxos: Utxo[], options: SelectionOptions): SelectionResult {
|
|
45
|
+
const { feeRateSatsPerVb, targetAmountSats, allowLocked, forbidInscriptionBearingInputs, feeEstimate } = options;
|
|
46
|
+
if (feeRateSatsPerVb <= 0) {
|
|
47
|
+
const err = new UtxoSelectionError('TOO_LOW_FEE', 'TOO_LOW_FEE');
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
if (targetAmountSats < DUST_LIMIT_SATS) {
|
|
51
|
+
const err = new UtxoSelectionError('DUST_OUTPUT', 'DUST_OUTPUT');
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Filter UTXOs based on policy
|
|
56
|
+
let candidateUtxos = utxos.slice().filter(u => typeof u.value === 'number' && u.value > 0);
|
|
57
|
+
const hasLocked = candidateUtxos.some(u => u.locked);
|
|
58
|
+
if (hasLocked && !allowLocked) {
|
|
59
|
+
// If excluding locked leaves insufficient funds, surface a specific error later; but first mark conflict
|
|
60
|
+
// We'll check after filtering
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!allowLocked) {
|
|
64
|
+
candidateUtxos = candidateUtxos.filter(u => !u.locked);
|
|
65
|
+
}
|
|
66
|
+
if (forbidInscriptionBearingInputs) {
|
|
67
|
+
candidateUtxos = candidateUtxos.filter(u => !u.inscriptions || u.inscriptions.length === 0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Greedy accumulate until amount + fee is satisfied. Start with 2 outputs (recipient + change), adjust if change is dust.
|
|
71
|
+
const selected: Utxo[] = [];
|
|
72
|
+
let accumulated = 0;
|
|
73
|
+
|
|
74
|
+
// Sort largest first to reduce change outputs and input count
|
|
75
|
+
candidateUtxos.sort((a, b) => b.value - a.value);
|
|
76
|
+
|
|
77
|
+
// We'll iteratively include inputs and recompute fee until covered
|
|
78
|
+
for (const utxo of candidateUtxos) {
|
|
79
|
+
selected.push(utxo);
|
|
80
|
+
accumulated += utxo.value;
|
|
81
|
+
|
|
82
|
+
// Assume two outputs initially
|
|
83
|
+
let fee = estimateFeeSats(selected.length, 2, feeRateSatsPerVb, feeEstimate);
|
|
84
|
+
let required = targetAmountSats + fee;
|
|
85
|
+
|
|
86
|
+
if (accumulated >= required) {
|
|
87
|
+
// Compute change and dust policy
|
|
88
|
+
let change = accumulated - required;
|
|
89
|
+
if (change > 0 && change < DUST_LIMIT_SATS) {
|
|
90
|
+
// If change would be dust, try recomputing fee for single output (recipient only)
|
|
91
|
+
fee = estimateFeeSats(selected.length, 1, feeRateSatsPerVb, feeEstimate);
|
|
92
|
+
required = targetAmountSats + fee;
|
|
93
|
+
change = accumulated - required;
|
|
94
|
+
if (change > 0 && change < DUST_LIMIT_SATS) {
|
|
95
|
+
// Force add to fee (better than creating dust)
|
|
96
|
+
change = 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (accumulated >= targetAmountSats + fee) {
|
|
100
|
+
return { selected, feeSats: fee, changeSats: Math.max(0, change) };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// If we got here, insufficient funds with the given policy
|
|
106
|
+
if (hasLocked && !allowLocked) {
|
|
107
|
+
const err = new UtxoSelectionError('CONFLICTING_LOCKS', 'CONFLICTING_LOCKS');
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
const err = new UtxoSelectionError('INSUFFICIENT_FUNDS', 'INSUFFICIENT_FUNDS');
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External Reference Manager
|
|
3
|
+
*
|
|
4
|
+
* Creates and verifies external resource references as specified in the CEL specification.
|
|
5
|
+
* External references point to data outside the event log (e.g., large media files)
|
|
6
|
+
* and include a cryptographic hash for content verification.
|
|
7
|
+
*
|
|
8
|
+
* @see https://w3c-ccg.github.io/cel-spec/
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ExternalReference } from './types';
|
|
12
|
+
import { computeDigestMultibase, verifyDigestMultibase } from './hash';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates an external reference for content.
|
|
16
|
+
*
|
|
17
|
+
* Computes a CEL-compliant digestMultibase hash from the content bytes
|
|
18
|
+
* and creates an ExternalReference with the provided metadata.
|
|
19
|
+
*
|
|
20
|
+
* @param content - The raw bytes of the content to reference
|
|
21
|
+
* @param mediaType - The MIME type of the content (e.g., "image/png")
|
|
22
|
+
* @param urls - Optional array of URLs where the content can be retrieved
|
|
23
|
+
* @returns An ExternalReference object with the computed digest
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const imageBytes = await fs.readFile('./image.png');
|
|
28
|
+
* const ref = createExternalReference(
|
|
29
|
+
* imageBytes,
|
|
30
|
+
* 'image/png',
|
|
31
|
+
* ['https://example.com/image.png']
|
|
32
|
+
* );
|
|
33
|
+
* // Returns: { digestMultibase: "uLCA...", mediaType: "image/png", url: ["https://example.com/image.png"] }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function createExternalReference(
|
|
37
|
+
content: Uint8Array,
|
|
38
|
+
mediaType: string,
|
|
39
|
+
urls?: string[]
|
|
40
|
+
): ExternalReference {
|
|
41
|
+
// Compute the CEL-compliant hash of the content
|
|
42
|
+
const digestMultibase = computeDigestMultibase(content);
|
|
43
|
+
|
|
44
|
+
// Build the reference object
|
|
45
|
+
const ref: ExternalReference = {
|
|
46
|
+
digestMultibase,
|
|
47
|
+
mediaType,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Only include urls if provided and non-empty
|
|
51
|
+
if (urls && urls.length > 0) {
|
|
52
|
+
ref.url = urls;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return ref;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Verifies that content matches an external reference.
|
|
60
|
+
*
|
|
61
|
+
* Computes the hash of the provided content and compares it
|
|
62
|
+
* against the digestMultibase in the reference.
|
|
63
|
+
*
|
|
64
|
+
* @param ref - The external reference to verify against
|
|
65
|
+
* @param content - The raw bytes of the content to verify
|
|
66
|
+
* @returns True if the content hash matches the reference digest, false otherwise
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const imageBytes = await fs.readFile('./image.png');
|
|
71
|
+
* const ref = createExternalReference(imageBytes, 'image/png');
|
|
72
|
+
*
|
|
73
|
+
* // Verify the content
|
|
74
|
+
* const isValid = verifyExternalReference(ref, imageBytes); // true
|
|
75
|
+
*
|
|
76
|
+
* // Modified content will fail verification
|
|
77
|
+
* const modified = new Uint8Array([...imageBytes, 0]);
|
|
78
|
+
* const isValid2 = verifyExternalReference(ref, modified); // false
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function verifyExternalReference(
|
|
82
|
+
ref: ExternalReference,
|
|
83
|
+
content: Uint8Array
|
|
84
|
+
): boolean {
|
|
85
|
+
// Use the hash verification function to check content against digest
|
|
86
|
+
return verifyDigestMultibase(content, ref.digestMultibase);
|
|
87
|
+
}
|