md_codec/error.rs
1//! Error variants for the md-codec wire-format codec.
2
3use thiserror::Error;
4
5/// Operator-context kind — where in the descriptor tree an operator appears.
6/// Per SPEC v0.30 §11. Carried by [`Error::OperatorContextViolation`] to name
7/// which tree-position a forbidden tag was encountered in.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ContextKind {
10 /// Top-level descriptor position (e.g., bare `PkK` as the descriptor root).
11 TopLevel,
12 /// Inside a `tr()` tap-script leaf (BIP-342 tapscript-only operators).
13 TapLeaf,
14 /// Inside a multi-family body (non-key tag among multi children).
15 MultiBody,
16}
17
18/// Errors produced by md-codec wire-format components.
19#[derive(Debug, Error, PartialEq, Eq)]
20pub enum Error {
21 /// A read of `requested` bits was attempted but only `available` bits remained.
22 #[error("attempted to read {requested} bits with only {available} bits remaining")]
23 BitStreamTruncated {
24 /// Number of bits the caller requested.
25 requested: usize,
26 /// Number of bits actually available in the stream.
27 available: usize,
28 },
29
30 /// Wire-format version field doesn't match v0.30 (=4). Returned when a
31 /// payload or chunk-header is read with a version value outside the
32 /// accepted v0.30 set. Per SPEC v0.30 §2.4 + §2.5 + §11.1.
33 #[error("wire-format version mismatch: got {got}, expected 4")]
34 WireVersionMismatch {
35 /// Version value parsed from the wire.
36 got: u8,
37 },
38
39 /// Header malformed in a way other than version mismatch — e.g., chunked-
40 /// flag inconsistent with caller context, or chunk-header internal field
41 /// out of range. Per SPEC v0.30 §11.1.
42 #[error("malformed header: {detail}")]
43 MalformedHeader {
44 /// Free-form description of the malformedness.
45 detail: String,
46 },
47
48 /// Path depth exceeds MAX_PATH_COMPONENTS (15).
49 #[error("path depth {got} exceeds maximum {max}")]
50 PathDepthExceeded {
51 /// Actual depth of the path.
52 got: usize,
53 /// Maximum allowed depth (15).
54 max: usize,
55 },
56
57 /// Key count `n` out of range. Per SPEC v0.30 §4: `1 ≤ n ≤ 32`.
58 #[error("key count {n} out of range; require 1 ≤ n ≤ 32")]
59 KeyCountOutOfRange {
60 /// Actual key count provided.
61 n: u8,
62 },
63
64 /// Divergent path count doesn't match key count.
65 #[error("divergent path count {got} does not match key count {n}")]
66 DivergentPathCountMismatch {
67 /// Expected key count.
68 n: u8,
69 /// Actual path count provided.
70 got: usize,
71 },
72
73 /// Multipath alt-count out of range. Per SPEC v0.30 §8: `2 ≤ count ≤ 9`.
74 #[error("multipath alt-count {got} out of range; require 2 ≤ count ≤ 9")]
75 AltCountOutOfRange {
76 /// Provided alt-count.
77 got: usize,
78 },
79
80 /// Tag value outside the allocated v0.30 set: 6-bit primary in reserved
81 /// range 0x24..=0x3E, or extension prefix 0x3F followed by an unrecognized
82 /// 4-bit subcode 0x00..=0x0F (the entire extension subspace is reserved
83 /// in v0.30). `primary` carries the raw 6-bit value read off the wire
84 /// (0x3F for extension-subspace failures); the 4-bit subcode is consumed
85 /// but not reported. Per SPEC v0.30 §3.2 and §11.1.
86 #[error("tag value 0x{primary:02x} out of range")]
87 TagOutOfRange {
88 /// The raw 6-bit primary value read off the wire.
89 primary: u8,
90 },
91
92 /// Threshold `k` out of range. Per SPEC v0.30 §4: `1 ≤ k ≤ 32`.
93 #[error("threshold k={k} out of range; require 1 ≤ k ≤ 32")]
94 ThresholdOutOfRange {
95 /// Provided k value.
96 k: u8,
97 },
98
99 /// Variable-arity child count out of range. Per SPEC v0.30 §4: `1 ≤ count ≤ 32`.
100 #[error("child count {count} out of range; require 1 ≤ count ≤ 32")]
101 ChildCountOutOfRange {
102 /// Provided child count.
103 count: usize,
104 },
105
106 /// k > n in k-of-n threshold/multisig.
107 #[error("threshold k={k} exceeds child count n={n}; require k ≤ n")]
108 KGreaterThanN {
109 /// Threshold k.
110 k: u8,
111 /// Child count n.
112 n: usize,
113 },
114
115 /// TLV ordering violation: a TLV tag was followed by a smaller-or-equal tag.
116 #[error(
117 "TLV ordering violation: tag 0x{prev:02x} followed by 0x{current:02x}; require ascending"
118 )]
119 TlvOrderingViolation {
120 /// Previous tag value.
121 prev: u8,
122 /// Current tag value.
123 current: u8,
124 },
125
126 /// Placeholder index in TLV entry exceeds key count n.
127 #[error("placeholder index {idx} out of range; require idx < n={n}")]
128 PlaceholderIndexOutOfRange {
129 /// Provided index.
130 idx: u8,
131 /// Key count n.
132 n: u8,
133 },
134
135 /// Per-`@N` override entries within a TLV must be in ascending `@N`-index order.
136 #[error("override ordering violation: @{prev} followed by @{current}; require ascending")]
137 OverrideOrderViolation {
138 /// Previous index.
139 prev: u8,
140 /// Current index.
141 current: u8,
142 },
143
144 /// TLV entry has zero entries; encoder MUST omit empty TLVs per spec §7.5.
145 #[error("TLV entry tag 0x{tag:02x} has empty payload; encoder MUST omit empty TLVs")]
146 EmptyTlvEntry {
147 /// Tag of the empty entry.
148 tag: u8,
149 },
150
151 /// TLV length exceeds remaining bits in stream.
152 #[error("TLV length {length} exceeds remaining bits {remaining}")]
153 TlvLengthExceedsRemaining {
154 /// Declared length.
155 length: usize,
156 /// Available bits.
157 remaining: usize,
158 },
159
160 /// Placeholder @i was not referenced anywhere in the tree (BIP 388 well-formedness).
161 #[error("placeholder @{idx} not referenced in tree; n={n}")]
162 PlaceholderNotReferenced {
163 /// The unreferenced placeholder index.
164 idx: u8,
165 /// Key count.
166 n: u8,
167 },
168
169 /// First-occurrence ordering violated (BIP 388 well-formedness).
170 #[error(
171 "placeholder first-occurrence ordering violated: expected first={expected_first}, got first={got_first}"
172 )]
173 PlaceholderFirstOccurrenceOutOfOrder {
174 /// Expected placeholder index in canonical first-occurrence position.
175 expected_first: u8,
176 /// Actual placeholder index encountered first.
177 got_first: u8,
178 },
179
180 /// All multipaths in a template must share the same alt-count.
181 #[error("multipath alt-count mismatch: expected {expected}, got {got}")]
182 MultipathAltCountMismatch {
183 /// Expected alt-count.
184 expected: usize,
185 /// Mismatched alt-count.
186 got: usize,
187 },
188
189 /// Tap-script-tree leaf has a tag that is forbidden per spec §6.3.1.
190 #[error("forbidden tap-script-tree leaf tag: 0x{tag:02x}")]
191 ForbiddenTapTreeLeaf {
192 /// Primary 6-bit tag code (bytecode space) of the forbidden leaf.
193 tag: u8,
194 },
195
196 /// Operator appears in a forbidden context per SPEC v0.30 §11.
197 /// `TopLevel` is enforced decoder-side at `decode_payload`; `TapLeaf` is
198 /// covered by the narrower [`Error::ForbiddenTapTreeLeaf`]; `MultiBody` is
199 /// structurally unreachable post-v0.30 Phase C (multi-family bodies carry
200 /// raw kiw-bit indices, not child tags).
201 #[error("operator {tag:?} not allowed in context {context:?}")]
202 OperatorContextViolation {
203 /// The offending operator tag.
204 tag: crate::tag::Tag,
205 /// Which tree-position the tag is forbidden in.
206 context: ContextKind,
207 },
208
209 /// Chunk count out of range. Per SPEC v0.30 §2.5: `1 ≤ count ≤ 64`.
210 #[error("chunk count {count} out of range; require 1 ≤ count ≤ 64")]
211 ChunkCountOutOfRange {
212 /// Provided count.
213 count: u8,
214 },
215
216 /// Chunk index ≥ count; require index < count.
217 #[error("chunk index {index} ≥ count {count}")]
218 ChunkIndexOutOfRange {
219 /// Provided index.
220 index: u8,
221 /// Provided count.
222 count: u8,
223 },
224
225 /// Chunk-set-id exceeds 20-bit range.
226 #[error("chunk-set-id 0x{id:x} exceeds 20-bit range")]
227 ChunkSetIdOutOfRange {
228 /// Provided ID.
229 id: u32,
230 },
231
232 /// Chunk header missing chunked-flag. Per SPEC v0.30 §2.2: bit 0 of the
233 /// first 5-bit symbol of a chunked payload is the chunked-flag (followed
234 /// by the 4-bit version field); it MUST be 1 in every chunk header.
235 #[error("chunk header chunked-flag missing; per SPEC §2.2 bit 0 of the first symbol must be 1")]
236 ChunkHeaderChunkedFlagMissing,
237
238 /// Encoding requires more chunks than the spec maximum (64).
239 #[error("encoding requires {needed} chunks; max is 64 per spec §9.8")]
240 ChunkCountExceedsMax {
241 /// Number of chunks needed.
242 needed: usize,
243 },
244
245 /// Codex32 decode error (HRP mismatch, alphabet violation, BCH verification failure).
246 #[error("codex32 decode error: {0}")]
247 Codex32DecodeError(String),
248
249 /// Codex32 encode error (BCH layer failure).
250 #[error("codex32 encode error: {0}")]
251 Codex32EncodeError(String),
252
253 /// Chunk set is empty (no strings provided to reassemble).
254 #[error("chunk set is empty (no strings provided)")]
255 ChunkSetEmpty,
256
257 /// Chunks in the set disagree on version, chunk-set-id, or count.
258 #[error("chunks in the set disagree on version, chunk-set-id, or count")]
259 ChunkSetInconsistent,
260
261 /// Chunk set incomplete: got fewer chunks than `expected`.
262 #[error("chunk set incomplete: got {got} chunks, expected {expected}")]
263 ChunkSetIncomplete {
264 /// Provided chunk count.
265 got: usize,
266 /// Expected chunk count.
267 expected: usize,
268 },
269
270 /// Chunk index gap: expected index N, got M.
271 #[error("chunk index gap: expected index {expected}, got {got}")]
272 ChunkIndexGap {
273 /// Expected index in the sequence.
274 expected: u8,
275 /// Actual index encountered.
276 got: u8,
277 },
278
279 /// Chunk-set-id mismatch between expected and reassembled-then-derived.
280 #[error("chunk-set-id mismatch: expected 0x{expected:x}, derived 0x{derived:x}")]
281 ChunkSetIdMismatch {
282 /// Expected (from chunks).
283 expected: u32,
284 /// Derived (from reassembled payload).
285 derived: u32,
286 },
287
288 /// LP4-ext varint value exceeds single-extension payload range (29 bits).
289 #[error("varint value {value} exceeds single-extension range (max 2^29 - 1)")]
290 VarintOverflow {
291 /// The offending value.
292 value: u32,
293 },
294
295 /// A non-canonical wrapper has no explicit origin path for some `@N`,
296 /// either via `OriginPathOverrides` or a populated `path_decl` entry,
297 /// and `canonical_origin(&d.tree)` is `None`. Per spec v0.13 §6.3.
298 #[error("non-canonical wrapper requires explicit origin for @{idx}, but none provided")]
299 MissingExplicitOrigin {
300 /// The placeholder index for which an explicit origin is required.
301 idx: u8,
302 },
303
304 /// `presence_byte` had non-zero reserved bits (bits 2..7) inside a
305 /// `WalletPolicyId` canonical-record preimage. Per spec v0.13 §5.3:
306 /// encoders MUST set reserved bits to 0 and decoders MUST reject
307 /// inputs with non-zero reserved bits. v0.13's encoder masks reserved
308 /// bits explicitly when building the hash preimage; the helper
309 /// [`crate::identity::validate_presence_byte`] enforces the
310 /// decoder-side contract for canonical-record consumers.
311 #[error("WalletPolicyId presence_byte has non-zero reserved bits: 0x{reserved_bits:02x}")]
312 InvalidPresenceByte {
313 /// The reserved-bit field (bits 2..7) of the offending presence byte.
314 reserved_bits: u8,
315 },
316
317 /// A `Pubkeys` TLV entry's 33-byte compressed-pubkey field (bytes
318 /// 32..65 of the 65-byte xpub payload) failed to parse as a valid
319 /// secp256k1 point. The 32-byte chain code prefix is unvalidated.
320 /// Per spec v0.13 §6.4.
321 #[error("invalid xpub bytes for @{idx}: pubkey field is not a valid secp256k1 point")]
322 InvalidXpubBytes {
323 /// The placeholder index whose xpub failed to parse.
324 idx: u8,
325 },
326 /// Address derivation requires a populated `Pubkeys` TLV entry for
327 /// every `@N`; this descriptor is missing one (template-only or
328 /// partial-keys mode). v0.14+ derivation surface only.
329 #[error(
330 "missing xpub for @{idx}; address derivation requires wallet-policy mode with all @N populated"
331 )]
332 MissingPubkey {
333 /// The placeholder index whose xpub is absent.
334 idx: u8,
335 },
336
337 /// `Descriptor::derive_address` was called with a `chain` index
338 /// outside the use-site multipath alt-count (or non-zero when no
339 /// multipath is present).
340 #[error("chain index {chain} out of range; use-site multipath alt-count is {alt_count}")]
341 ChainIndexOutOfRange {
342 /// The provided chain index.
343 chain: u32,
344 /// The number of alternatives in the use-site multipath (`0` when
345 /// no multipath component is present).
346 alt_count: usize,
347 },
348
349 /// Address derivation requires non-hardened use-site components,
350 /// but this descriptor's use-site path declares a hardened
351 /// alternative or hardened wildcard. BIP 32 forbids hardened
352 /// derivation from a public key, so an xpub-only restore cannot
353 /// produce addresses for this wallet.
354 #[error(
355 "hardened public-key derivation: use-site path requires hardened component, which BIP 32 forbids on xpub-only restore"
356 )]
357 HardenedPublicDerivation,
358
359 /// Address derivation failed at the miniscript layer (or in the
360 /// AST → miniscript converter). Carries a free-form `detail` string
361 /// describing the underlying error — typically a `miniscript::Error`,
362 /// a `Tr`/`Wsh` constructor failure (type-check / context error), or
363 /// an arity/context mismatch raised by the converter.
364 #[error("address derivation failed: {detail}")]
365 AddressDerivationFailed {
366 /// Free-form description of the underlying failure.
367 detail: String,
368 },
369
370 /// Inside a `tr()` body, `is_nums = false` was paired with a `key_index`
371 /// out of range (`key_index >= n`). Per SPEC v0.30 §7 + §11: the
372 /// placeholder-index range is `0..n` strictly; the v0.x NUMS sentinel
373 /// slot at `key_index = n` is gone (NUMS is now flag-driven via
374 /// `Body::Tr.is_nums`). Raised by `validate_placeholder_usage` when the
375 /// in-`tr()` overflow condition is hit.
376 #[error("NUMS sentinel conflict: is_nums=false with key_index out of range (SPEC §7 §11)")]
377 NUMSSentinelConflict,
378
379 /// Decode-side recursion depth exceeded the hardening cap.
380 /// `read_node` calls itself recursively for tags with child bodies
381 /// (`Tag::Sh`, `Tag::AndV`, `Tag::TapTree`, `Tag::Multi`, `Tag::Tr`,
382 /// etc.); a hostile wire payload nesting these tags arbitrarily deep
383 /// would blow the Rust stack. The cap is shared across all recursive
384 /// tags as a generic anti-DOS hardening bound. v0.19 introduced.
385 #[error("decode recursion depth {depth} exceeded maximum {max}")]
386 DecodeRecursionDepthExceeded {
387 /// Current recursion depth at which the cap fired.
388 depth: u8,
389 /// Maximum allowed depth.
390 max: u8,
391 },
392
393 /// BCH correction capacity exceeded: a chunk's syndrome pattern
394 /// indicated more than `t = 4` errors (BCH(93, 80, 8) singleton
395 /// bound `2t = 8`), so a unique correction cannot be derived.
396 /// v0.34.0 introduced; raised by [`crate::decode_with_correction`].
397 /// Atomic per plan §1 D28: any chunk failing this check fails the
398 /// whole multi-chunk call without partial output.
399 #[error("chunk {chunk_index} has more than {bound} errors; uncorrectable")]
400 TooManyErrors {
401 /// 0-indexed position of the offending chunk in the caller's
402 /// `&[&str]` slice.
403 chunk_index: usize,
404 /// The BCH singleton bound `2t = 8` (i.e. 4 correctable errors).
405 bound: u8,
406 },
407}
408
409#[cfg(test)]
410mod tests {
411 use super::*;
412 use crate::tag::Tag;
413
414 /// SPEC v0.30 §11: `OperatorContextViolation` carries the offending tag
415 /// + a `ContextKind` discriminator. Pins the type shape and Display
416 /// output against future drift; does NOT claim live wire reachability
417 /// (see FOLLOWUP `v0.30-phase-g-operator-context-violation-unwired`).
418 #[test]
419 fn operator_context_violation_constructs() {
420 let err = Error::OperatorContextViolation {
421 tag: Tag::Multi,
422 context: ContextKind::MultiBody,
423 };
424 let s = err.to_string();
425 assert!(s.contains("Multi"), "Display must mention tag: {s}");
426 assert!(s.contains("MultiBody"), "Display must mention context: {s}");
427 }
428
429 /// SPEC v0.30 §7 + §11: `NUMSSentinelConflict` Display pins the SPEC-cite
430 /// substring so the doc-comment + format string don't silently drift.
431 #[test]
432 fn nums_sentinel_conflict_display() {
433 let s = Error::NUMSSentinelConflict.to_string();
434 assert!(s.contains("§7"), "Display must cite SPEC §7: {s}");
435 assert!(s.contains("§11"), "Display must cite SPEC §11: {s}");
436 }
437}