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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! Runtime transaction parameter schema used by `wallet-abi-0.1`.
//!
//! Serialization note:
//! enum variants are serialized in `snake_case` across this schema.
use crateWalletAbiError;
use crateTaprootPubkeyGen;
use XOnlyPublicKey;
use ;
use LockTime;
use ;
use ;
/// Top-level transaction-construction payload carried in `TxCreateRequest.params`.
///
/// This type is the request contract boundary between callers and runtime. It defines
/// declared inputs/outputs plus optional fee-rate and `lock_time` hints.
///
/// # Runtime behavior nuances
///
/// - Declared inputs/outputs are the starting point, not always the final transaction shape.
/// - Runtime may append auxiliary wallet inputs to close asset deficits.
/// - Runtime should normalize/append the fee output and may append per-asset change outputs.
/// - Input selection should be deterministic for a fixed wallet snapshot and request, but state-aware:
/// declared input order can affect later selection and issuance-derived references.
///
/// # Security
///
/// Treat this payload as untrusted caller input:
/// - `provided` outpoints, blinding material, and all nested fields are trust boundaries.
/// - Avoid logging full raw payloads; nested fields can contain sensitive key material.
/// - Misconfigured fee rate or `lock_time` can produce surprising transaction behavior.
///
/// # UX guidance
///
/// - Keep declared input ordering stable when using issuance-linked `input_index` references.
/// - Do not assume requested outputs are the exact final output set; fee/change adjustments can occur.
/// Asset constraint for wallet-sourced input selection (`UTXOSource::Wallet`).
///
/// # Runtime Semantics
///
/// During wallet candidate filtering:
/// - `none`: a candidate should pass regardless of asset id.
/// - `exact`: a candidate should pass only when
/// `candidate.unblinded.asset == asset_id`.
///
/// This predicate is conjunctive with [`AmountFilter`] and [`LockFilter`]:
/// candidates must satisfy all enabled dimensions.
/// Amount constraint for wallet-sourced input selection (`UTXOSource::Wallet`).
///
/// # Runtime Semantics
///
/// During wallet candidate filtering:
/// - `none`: a candidate should pass regardless of amount.
/// - `exact`: a candidate should pass only when
/// `candidate.unblinded.value == amount_sat`.
/// - `min`: a candidate should pass only when
/// `candidate.unblinded.value >= amount_sat`.
///
/// # Zero-Value Nuance
///
/// - `exact { amount_sat: 0 }` accepts only zero-valued UTXOs.
/// - `min { amount_sat: 0 }` should behave as an unconstraining lower bound for amount.
///
/// # UX Guidance
///
/// - Prefer `exact` when selecting a specific denomination/coin-sized input.
/// - Prefer `min` when selecting at-least funding candidates.
/// - Pair amount filters with [`AssetFilter::Exact`] for predictable selection
/// in multi-asset wallets.
/// Locking-script constraint for wallet-sourced input selection (`UTXOSource::Wallet`).
///
/// # Runtime Semantics
///
/// During wallet candidate filtering:
/// - `none`: a candidate should pass regardless of script pubkey.
/// - `script`: a candidate should pass only on byte-for-byte equality with the wallet
/// snapshot `script_pubkey`.
///
/// # Pre-sync Note
///
/// Runtime may attempt a script pre-sync for `script`-locked filters before
/// resolution.
///
/// # Security and UX Guidance
///
/// - Treat script values in requests as untrusted input.
/// - Use a deterministic script source-of-truth (for example descriptor-derived
/// script generation) instead of manual edits to avoid silent mismatch.
/// Wallet-owned UTXO constraints used by `UTXOSource::Wallet`.
///
/// All filter dimensions are conjunctive:
/// a candidate MUST satisfy `asset AND amount AND lock`.
///
/// Defaults are unconstrained:
/// - `asset = none`
/// - `amount = none`
/// - `lock = none`
///
/// With all defaults, any unused wallet UTXO is eligible.
/// Source selector for one input prevout.
///
/// # Resolution Behavior
///
/// - `Wallet`:
/// candidates should be filtered by [`WalletSourceFilter`], then should be selected by a
/// deterministic deficit-aware score (not first-match).
/// - `Provided`:
/// runtime should fetch the prevout by outpoint from esplora and should resolve
/// unblinding with [`InputUnblinding`].
///
/// # Determinism
///
/// Wallet selection should be deterministic for a fixed wallet snapshot and request,
/// but it is state-aware: declared input order can affect later selections
/// because earlier inputs update supply/deficit state.
///
/// # Failure Modes
///
/// - malformed `provided.outpoint` should be rejected during request decoding
/// - no wallet candidate matching a `wallet` filter should yield funding failure
/// - duplicate outpoint use within one request should be rejected
/// - for `provided`, outpoint existence should be checked at tx/vout fetch time;
/// there should be no schema-level guarantee that the outpoint is currently unspent
///
/// # Security and UX Notes
///
/// - `provided.outpoint` is caller-controlled and MUST be treated as untrusted input.
/// Defaults to wallet-sourced input selection with an unconstrained filter.
/// Issuance mode for [`InputSchema::issuance`].
///
/// Serialized values are:
/// - `"new"`
/// - `"reissue"`
///
/// Variant compatibility with output asset variants:
/// - [`Self::New`] supports `new_issuance_asset` and `new_issuance_token`.
/// - [`Self::Reissue`] supports `re_issuance_asset`.
///
/// Mismatched output asset variants are rejected during runtime resolution with
/// `WalletAbiError::InvalidRequest`.
/// Issuance metadata attached to one transaction input.
///
/// This object is schema-level metadata only. Runtime should consume it in two places:
/// - input resolution, to populate issuance fields in the PSET input
/// - output resolution, to derive issuance-linked `AssetId` values
///
/// # Field semantics
///
/// - `kind`: issuance mode (`"new"` or `"reissue"`).
/// - `asset_amount_sat`: amount of issued/reissued asset units to mint.
/// - `token_amount_sat`: amount of reissuance token units to mint.
/// - `entropy`: 32-byte entropy payload whose meaning depends on `kind`.
///
/// # Entropy interpretation
///
/// - `kind = "new"`: `entropy` is interpreted as contract-hash entropy.
/// Runtime should derive issuance entropy from `(selected_input_outpoint, contract_hash_entropy)`.
/// - `kind = "reissue"`: `entropy` is interpreted as already-derived asset entropy.
/// Runtime should use it directly.
///
/// # Security and UX notes
///
/// - Issuance-derived output assets are coupled to `input_index` in output asset variants.
/// `input_index` is positional in `params.inputs`; reordering inputs changes derived asset ids.
///
/// # Troubleshooting
///
/// Common request errors and likely causes:
/// - `"references missing input_index"`: output asset variant points to an out-of-range input.
/// - `"has no issuance metadata"`: output references issuance-derived asset, but input has no
/// `issuance`.
/// - `"new_issuance_* references non-new issuance"`: output variant requires `kind = "new"`.
/// - `"re_issuance_asset references non-reissue"`: output variant requires `kind = "reissue"`.
/// Source of taproot internal key material used by [`FinalizerSpec::Simf`].
///
/// # Purpose
///
/// `InternalKeySource` controls which x-only internal key is used in two runtime
/// paths:
/// - Simplicity finalization control-block construction in runtime finalization
///
/// # Runtime semantics
///
/// - `"bip0341"` should use fixed BIP-0341 example internal key bytes.
/// - `"external"` should use `key.pubkey` as the x-only source for witness/control
/// block operations.
///
/// # Security considerations
///
/// - Using `"external"` can imply that key-path spendability exists outside this
/// schema.
/// - A malformed `"external"` payload can contain internally inconsistent
/// `pubkey`/`address`/argument binding. Untrusted handles SHOULD be validated
/// with [`TaprootPubkeyGen::build_from_str`] before embedding.
/// - [`Self::get_x_only_pubkey`] is extraction-only and performs no consistency
/// checks against `address` or Simplicity program arguments.
///
/// # UX / integrator guidance
///
/// - Use `"bip0341"` for deterministic, portable defaults and compatibility with
/// existing BIP-0341-based templates.
/// - Use `"external"` when a different internal key is needed rather than `"bip0341"`.
/// Fixed BIP-0341 example internal key bytes.
///
/// Provenance: <https://en.bitcoin.it/wiki/BIP_0341>.
///
/// Compatibility note:
/// the exact 32-byte value is part of the `wallet-abi-0.1` behavior contract and
/// MUST remain stable unless the ABI is versioned accordingly.
pub
/// Input special finalization strategy attached to each [`InputSchema`].
///
/// # Wire format
///
/// This enum is externally tagged with `type` in `snake_case`.
///
/// # `simf` + internal-key nuance
///
/// - `internal_key = "bip0341"`:
/// script pubkey is derived from `(source_simf, arguments, network)` plus fixed
/// BIP-0341 key bytes.
/// - `internal_key = { "external": ... }`:
/// script pubkey is derived from `(source_simf, arguments, key.pubkey, network)`
/// and must match `external.key.address`.
/// Unblinding strategy for the prevout selected by [`UTXOSource`].
///
/// # Purpose
///
/// Declare how runtime should obtain `TxOutSecrets` for this input's resolved
/// prevout so the transaction can be balanced and blinded safely.
///
/// # Behavior by Variant
///
/// - [`Self::Wallet`]:
/// runtime should delegate unblinding to wallet-owned descriptor or blinding material.
/// - [`Self::Provided`]:
/// runtime should unblind with the caller-supplied blinding `secret_key`.
/// - [`Self::Explicit`]:
/// prevout must already carry explicit asset/value fields; confidential values
/// are rejected.
///
/// # Security Considerations
///
/// - `secret_key` is a blinding key, not a spending/signing key.
/// - Never log or persist request payloads containing `provided.secret_key`.
/// - Runtime should store resolved input secrets in PSET proprietary metadata during
/// construction. Treat intermediate PSETs as sensitive material.
///
/// # UX Guidance
///
/// - Use [`Self::Explicit`] only when the referenced prevout is known explicit.
/// - Mismatched assumptions produce deterministic request errors, for example:
/// `"unable to unblind input ... with provided unblinding key"` or
/// `"input ... is marked explicit but the provided prevout is confidential"`.
/// Output lock selector for [`OutputSchema::lock`].
///
/// # Purpose and scope
///
/// `LockVariant` declares how runtime should determine the script pubkey for one output.
/// It does not by itself prove spendability or policy correctness of the target lock.
///
/// # Runtime semantics
///
/// - `Wallet` should use the request-scoped wallet receive template frozen by runtime.
/// - `Script` should be used directly as the output script pubkey.
/// - `Finalizer` should delegate script derivation to runtime.
///
/// # Nuance notes
///
/// - empty script should be rejected at runtime.
/// Default runtime should own fee output construction; manual fee output injection should be unsupported.
///
/// # Security notes
///
/// - `Script` carries caller-controlled raw script bytes.
/// - `External` taproot handle usage crosses a trust boundary and should be
/// validated upstream before embedding into requests.
/// Asset selector for an output.
///
/// # Positional semantics
///
/// Issuance-linked variants should resolve deterministically from the referenced input index and
/// its issuance metadata. `input_index` is positional in `RuntimeParams.inputs`.
/// Reordering inputs changes which outpoint/issuance metadata is referenced.
///
/// # Runtime checks
///
/// Runtime should validate index bounds and issuance-kind compatibility:
/// - `new_issuance_asset` and `new_issuance_token` require
/// `params.inputs[input_index].issuance.kind == "new"`.
/// - `re_issuance_asset` requires
/// `params.inputs[input_index].issuance.kind == "reissue"`.
///
/// Violations should fail deterministically with `WalletAbiError::InvalidRequest`.
/// Output blinding policy selector.
///
/// # Runtime mapping
///
/// During output materialization:
/// - `Wallet` => output `blinding_key = wallet_output_template(receive).blinding_pubkey`.
/// - `Provided` => output `blinding_key = provided.pubkey`.
/// - `Explicit` => output `blinding_key = None`.
///
/// # Security and UX notes
///
/// - `Explicit` disables output amount/asset confidentiality for that output.
/// - `Wallet` is usually correct for wallet-owned receive/change outputs.
/// - Using `Wallet` for non-wallet recipients can result in Simplicity spending failure.
/// One requested output entry in `RuntimeParams.outputs`.
///
/// This struct declares requested outputs only. Runtime may additionally:
/// - append an explicit fee output,
/// - append change outputs for residual balances.
///
/// # Field semantics
///
/// - `id`: caller label used for diagnostics.
/// - `amount_sat`: requested amount for this output.
/// - `lock`: locking rule (`wallet`, `script`, or finalizer-derived script).
/// - `asset`: explicit or issuance-derived asset selector.
/// - `blinder`: output blinding policy.
///
/// # Coupling and caveats
///
/// Runtime should enforce one coupling rule:
/// `blinder = wallet` requires `lock = wallet`.
/// Other `lock`/`blinder` combinations remain caller-controlled.