md-cli 0.13.0

CLI for the Mnemonic Descriptor (MD) engravable BIP 388 wallet policy backup format
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
//! P1.1 — `md decode` / `md inspect` partial-decode of pathless/dead-card
//! shapes (`canonical_origin == None`, no explicit origin resolvable).
//!
//! Per `design/SPEC_pathless_partial_decode.md` / `design/
//! IMPLEMENTATION_PLAN_pathless_partial_decode.md` (mnemonic-toolkit repo),
//! Phase P1. Consumes P0's md-codec partial-allowing decode API
//! (`decode_md1_string_with_opts` / `reassemble_with_opts` /
//! `Descriptor::unresolved_origin_indices`).
//!
//! Contract:
//!   - A `canonical_origin == None` shape with no explicit per-`@N` origin
//!     now DECODES (instead of hard-rejecting `MissingExplicitOrigin`):
//!     the template renders as usual, PLUS a text line
//!     `origin: «unspecified — supply on restore»`, PLUS a stderr note,
//!     exit **4**.
//!   - `md inspect` additionally OMITS the `wallet-policy-id` +
//!     `wallet-policy-id-fingerprint` lines (JSON: `wallet_policy_id` key)
//!     on partial — `compute_wallet_policy_id` must not even be called
//!     (it would error under partial), while `md1-encoding-id` +
//!     `wallet-descriptor-template-id` (no origin dependency) stay present.
//!   - `--json` gains an additive `partial: {"reason":
//!     "missing_explicit_origin", "unresolved_indices":[...]}` object.
//!   - Canonical (`canonical_origin == Some`) shapes are COMPLETELY
//!     UNCHANGED: exit 0, byte-identical text/JSON (BOUNDARY, RED-proof —
//!     any drift here is a regression, not a feature).

#![allow(missing_docs)]

use assert_cmd::Command;
use std::process::Command as StdCommand;

/// Run `md encode <extra_args.. > <template>` and return the first
/// (only, unbroken) stdout line — the md1 phrase.
fn encode(template: &str, extra_args: &[&str]) -> String {
    let mut args = vec!["encode", "--group-size", "0"];
    args.extend_from_slice(extra_args);
    args.push(template);
    let out = StdCommand::new(assert_cmd::cargo::cargo_bin("md"))
        .args(&args)
        .output()
        .expect("invoke md encode");
    assert!(
        out.status.success(),
        "encode {template:?} {extra_args:?} failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8(out.stdout)
        .unwrap()
        .lines()
        .next()
        .unwrap()
        .to_string()
}

/// Run `md encode --force-chunked <extra_args..> <template>` and return the
/// ordered `md1...` chunk strings.
fn encode_chunked(template: &str, extra_args: &[&str]) -> Vec<String> {
    let mut args = vec!["encode", "--force-chunked", "--group-size", "0"];
    args.extend_from_slice(extra_args);
    args.push(template);
    let out = StdCommand::new(assert_cmd::cargo::cargo_bin("md"))
        .args(&args)
        .output()
        .expect("invoke md encode --force-chunked");
    assert!(
        out.status.success(),
        "encode --force-chunked {template:?} {extra_args:?} failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8(out.stdout)
        .unwrap()
        .lines()
        .filter(|l| l.starts_with("md1"))
        .map(String::from)
        .collect()
}

/// The exact text marker P1.1 specifies (must appear verbatim, partial only).
const ORIGIN_MARKER: &str = "origin: \u{ab}unspecified \u{2014} supply on restore\u{bb}";

// ─────────────────────────────────────────────────────────────────────────
// Dead-shape fixtures (canonical_origin == None), no --path supplied.
// One entry per SPEC-required golden class:
//   - tr(@0, pk(@1))            — tr + script tree
//   - sh(sortedmulti(2,@0,@1))  — legacy P2SH multisig
//   - wsh(pk(@0))               — bare wsh (single-key, non-multi inner)
//   - wsh(or_d(...))            — raw miniscript body
// ─────────────────────────────────────────────────────────────────────────
const DEAD_SHAPES: &[(&str, &str)] = &[
    ("tr_with_taptree", "tr(@0/<0;1>/*,pk(@1/<0;1>/*))"),
    (
        "sh_sortedmulti_legacy",
        "sh(sortedmulti(2,@0/<0;1>/*,@1/<0;1>/*))",
    ),
    ("bare_wsh_single_key", "wsh(pk(@0/<0;1>/*))"),
    (
        "raw_miniscript_body",
        "wsh(or_d(pk(@0/<0;1>/*),and_v(v:pk(@1/<0;1>/*),older(144))))",
    ),
];

// ─── decode: text ───────────────────────────────────────────────────────

#[test]
fn decode_text_partial_renders_and_exits_4_for_every_dead_shape() {
    for (name, template) in DEAD_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["decode", &phrase])
            .output()
            .unwrap();
        assert_eq!(
            out.status.code(),
            Some(4),
            "[{name}] dead shape {template:?} must partial-decode with exit 4"
        );
        let stdout = String::from_utf8(out.stdout).unwrap();
        assert!(
            stdout.contains(*template),
            "[{name}] template line missing; got {stdout:?}"
        );
        assert!(
            stdout.contains(ORIGIN_MARKER),
            "[{name}] origin-unspecified marker missing; got {stdout:?}"
        );
        let stderr = String::from_utf8(out.stderr).unwrap();
        assert!(
            !stderr.is_empty(),
            "[{name}] expected a stderr note on partial decode"
        );
    }
}

// ─── decode: json ───────────────────────────────────────────────────────

#[cfg(feature = "json")]
#[test]
fn decode_json_partial_has_reason_and_nonempty_indices_for_every_dead_shape() {
    for (name, template) in DEAD_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["decode", &phrase, "--json"])
            .output()
            .unwrap();
        assert_eq!(
            out.status.code(),
            Some(4),
            "[{name}] json partial-decode must exit 4"
        );
        let stdout = String::from_utf8(out.stdout).unwrap();
        let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
        assert_eq!(
            v["partial"]["reason"], "missing_explicit_origin",
            "[{name}] partial.reason mismatch; got {v}"
        );
        let idxs = v["partial"]["unresolved_indices"]
            .as_array()
            .unwrap_or_else(|| panic!("[{name}] unresolved_indices must be an array; got {v}"));
        assert!(
            !idxs.is_empty(),
            "[{name}] unresolved_indices must be non-empty; got {v}"
        );
        // The raw path_decl must NOT be double-represented — still "m".
        assert_eq!(
            v["descriptor"]["path_decl"]["data"], "m",
            "[{name}] raw path_decl must stay elided \"m\"; got {v}"
        );
        assert_eq!(v["schema"], "md-cli/1");
    }
}

// ─── inspect: text ──────────────────────────────────────────────────────

#[test]
fn inspect_text_partial_omits_policy_id_lines_for_every_dead_shape() {
    for (name, template) in DEAD_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["inspect", &phrase])
            .output()
            .unwrap();
        assert_eq!(
            out.status.code(),
            Some(4),
            "[{name}] inspect on dead shape must exit 4"
        );
        let stdout = String::from_utf8(out.stdout).unwrap();
        assert!(
            stdout.contains(&format!("template: {template}")),
            "[{name}] template line missing; got {stdout:?}"
        );
        assert!(
            stdout.contains(ORIGIN_MARKER),
            "[{name}] origin marker missing; got {stdout:?}"
        );
        assert!(
            stdout.contains("md1-encoding-id:"),
            "[{name}] md1-encoding-id must stay present (no origin dep); got {stdout:?}"
        );
        assert!(
            stdout.contains("wallet-descriptor-template-id:"),
            "[{name}] wallet-descriptor-template-id must stay present; got {stdout:?}"
        );
        assert!(
            !stdout.contains("wallet-policy-id:"),
            "[{name}] wallet-policy-id line must be OMITTED under partial; got {stdout:?}"
        );
        assert!(
            !stdout.contains("wallet-policy-id-fingerprint:"),
            "[{name}] wallet-policy-id-fingerprint line must be OMITTED under partial; got {stdout:?}"
        );
    }
}

// ─── inspect: json ──────────────────────────────────────────────────────

#[cfg(feature = "json")]
#[test]
fn inspect_json_partial_omits_wallet_policy_id_key_for_every_dead_shape() {
    for (name, template) in DEAD_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["inspect", &phrase, "--json"])
            .output()
            .unwrap();
        assert_eq!(out.status.code(), Some(4), "[{name}] must exit 4");
        let stdout = String::from_utf8(out.stdout).unwrap();
        let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
        assert!(
            v.get("wallet_policy_id").is_none(),
            "[{name}] wallet_policy_id key must be OMITTED under partial; got {v}"
        );
        assert!(
            v.get("md1_encoding_id").is_some(),
            "[{name}] md1_encoding_id must stay present; got {v}"
        );
        assert!(
            v.get("wallet_descriptor_template_id").is_some(),
            "[{name}] wallet_descriptor_template_id must stay present; got {v}"
        );
        assert_eq!(
            v["partial"]["reason"], "missing_explicit_origin",
            "[{name}] partial.reason mismatch; got {v}"
        );
        assert!(
            !v["partial"]["unresolved_indices"]
                .as_array()
                .unwrap()
                .is_empty(),
            "[{name}] unresolved_indices must be non-empty; got {v}"
        );
    }
}

// ─────────────────────────────────────────────────────────────────────────
// BOUNDARY (RED-proof): canonical shapes stay byte-identical / exit 0.
// Any drift here (e.g. accidentally emitting the origin marker, or
// dropping a policy-id line) is a REGRESSION, not a feature — this is the
// funds-relevant guard: canonical cards must not start reading as partial.
// ─────────────────────────────────────────────────────────────────────────
const CANONICAL_SHAPES: &[(&str, &str)] = &[
    ("tr_keypath_only", "tr(@0/<0;1>/*)"),
    ("wpkh_single_key", "wpkh(@0/<0;1>/*)"),
    ("sh_wpkh_nested", "sh(wpkh(@0/<0;1>/*))"),
    ("wsh_multi", "wsh(multi(2,@0/<0;1>/*,@1/<0;1>/*))"),
];

#[test]
fn decode_text_canonical_shapes_stay_exit_0_and_never_show_origin_marker() {
    for (name, template) in CANONICAL_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["decode", &phrase])
            .output()
            .unwrap();
        assert_eq!(
            out.status.code(),
            Some(0),
            "[{name}] canonical shape {template:?} must stay exit 0"
        );
        let stdout = String::from_utf8(out.stdout).unwrap();
        assert_eq!(
            stdout.trim_end(),
            *template,
            "[{name}] canonical decode text must be BYTE-IDENTICAL to the template line alone"
        );
        assert!(
            !stdout.contains(ORIGIN_MARKER),
            "[{name}] canonical shape must NEVER show the origin-unspecified marker"
        );
    }
}

#[cfg(feature = "json")]
#[test]
fn decode_json_canonical_shapes_never_carry_partial_key() {
    for (name, template) in CANONICAL_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["decode", &phrase, "--json"])
            .output()
            .unwrap();
        assert_eq!(out.status.code(), Some(0), "[{name}] must stay exit 0");
        let stdout = String::from_utf8(out.stdout).unwrap();
        let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
        assert!(
            v.get("partial").is_none(),
            "[{name}] canonical shape JSON must NOT carry a `partial` key; got {v}"
        );
    }
}

#[test]
fn inspect_text_canonical_shapes_keep_all_policy_id_lines_and_exit_0() {
    for (name, template) in CANONICAL_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["inspect", &phrase])
            .output()
            .unwrap();
        assert_eq!(out.status.code(), Some(0), "[{name}] must stay exit 0");
        let stdout = String::from_utf8(out.stdout).unwrap();
        assert!(
            stdout.contains("wallet-policy-id:"),
            "[{name}] canonical shape must KEEP wallet-policy-id; got {stdout:?}"
        );
        assert!(
            stdout.contains("wallet-policy-id-fingerprint:"),
            "[{name}] canonical shape must KEEP wallet-policy-id-fingerprint; got {stdout:?}"
        );
        assert!(
            !stdout.contains(ORIGIN_MARKER),
            "[{name}] canonical shape must never show the origin marker"
        );
    }
}

#[cfg(feature = "json")]
#[test]
fn inspect_json_canonical_shapes_keep_wallet_policy_id_and_no_partial_key() {
    for (name, template) in CANONICAL_SHAPES {
        let phrase = encode(template, &[]);
        let out = Command::cargo_bin("md")
            .unwrap()
            .args(["inspect", &phrase, "--json"])
            .output()
            .unwrap();
        assert_eq!(out.status.code(), Some(0), "[{name}] must stay exit 0");
        let stdout = String::from_utf8(out.stdout).unwrap();
        let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
        assert!(
            v.get("wallet_policy_id").is_some(),
            "[{name}] canonical shape must KEEP wallet_policy_id; got {v}"
        );
        assert!(
            v.get("partial").is_none(),
            "[{name}] canonical shape must NOT carry a partial key; got {v}"
        );
    }
}

// ─────────────────────────────────────────────────────────────────────────
// Chunked (multi-chunk) dead card also partial-renders + exit 4 — proves
// the partial variant threads through `reassemble_with_opts`, not just the
// single-string `decode_payload_with_opts` path.
// ─────────────────────────────────────────────────────────────────────────

/// A dead shape (`wsh(thresh(...))` — Thresh is not a canonical
/// wsh-inner-multi tag) large enough (17 placeholders) to force
/// `--force-chunked` into 2+ chunks. No `--path` supplied.
fn multi_chunk_dead_template() -> String {
    let n = 17;
    let mut parts = vec!["pk(@0/<0;1>/*)".to_string()];
    for i in 1..n {
        parts.push(format!("s:pk(@{i}/<0;1>/*)"));
    }
    format!("wsh(thresh({n},{}))", parts.join(","))
}

#[test]
fn decode_text_multi_chunk_dead_card_partial_renders_and_exits_4() {
    let template = multi_chunk_dead_template();
    let chunks = encode_chunked(&template, &[]);
    assert!(
        chunks.len() >= 2,
        "fixture must force 2+ chunks; got {}: {chunks:?}",
        chunks.len()
    );
    let mut args = vec!["decode".to_string()];
    args.extend(chunks.iter().cloned());
    let out = Command::cargo_bin("md")
        .unwrap()
        .args(&args)
        .output()
        .unwrap();
    assert_eq!(
        out.status.code(),
        Some(4),
        "multi-chunk dead card must partial-decode with exit 4"
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains(&template),
        "template line missing; got {stdout:?}"
    );
    assert!(
        stdout.contains(ORIGIN_MARKER),
        "origin marker missing; got {stdout:?}"
    );
}

#[cfg(feature = "json")]
#[test]
fn decode_json_multi_chunk_dead_card_partial_has_reason() {
    let template = multi_chunk_dead_template();
    let chunks = encode_chunked(&template, &[]);
    assert!(chunks.len() >= 2, "fixture must force 2+ chunks");
    let mut args = vec!["decode".to_string()];
    args.extend(chunks.iter().cloned());
    args.push("--json".to_string());
    let out = Command::cargo_bin("md")
        .unwrap()
        .args(&args)
        .output()
        .unwrap();
    assert_eq!(out.status.code(), Some(4));
    let stdout = String::from_utf8(out.stdout).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["partial"]["reason"], "missing_explicit_origin");
    assert!(
        !v["partial"]["unresolved_indices"]
            .as_array()
            .unwrap()
            .is_empty()
    );
}

#[test]
fn inspect_multi_chunk_dead_card_partial_renders_and_exits_4() {
    let template = multi_chunk_dead_template();
    let chunks = encode_chunked(&template, &[]);
    assert!(chunks.len() >= 2, "fixture must force 2+ chunks");
    let mut args = vec!["inspect".to_string()];
    args.extend(chunks.iter().cloned());
    let out = Command::cargo_bin("md")
        .unwrap()
        .args(&args)
        .output()
        .unwrap();
    assert_eq!(out.status.code(), Some(4));
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains(ORIGIN_MARKER));
    assert!(!stdout.contains("wallet-policy-id:"));
}

/// Chunked-of-one dead card (single `md1...` string bearing a chunk header,
/// e.g. from `md encode --force-chunked` on a small dead shape) also
/// partial-decodes via the `decode_md1_string`-with-opts auto-dispatch path
/// (chunked_flag branch), not just the multi-arg `reassemble` path.
#[test]
fn decode_chunked_of_one_dead_card_partial_renders_and_exits_4() {
    let chunks = encode_chunked("sh(sortedmulti(2,@0/<0;1>/*,@1/<0;1>/*))", &[]);
    assert_eq!(
        chunks.len(),
        1,
        "small dead shape --force-chunked should still be a single chunk; got {chunks:?}"
    );
    let out = Command::cargo_bin("md")
        .unwrap()
        .args(["decode", &chunks[0]])
        .output()
        .unwrap();
    assert_eq!(out.status.code(), Some(4));
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains(ORIGIN_MARKER));
}