bc-envelope-cli 0.35.0

Gordian Envelope Command Line Tool.
Documentation
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
# Walking Envelope Nodes

The `walk` command provides tools for navigating and manipulating envelope nodes based on their obscuration state (elided, encrypted, or compressed).

## Table of Contents

- [Walking Envelope Nodes]#walking-envelope-nodes
  - [Table of Contents]#table-of-contents
  - [Overview]#overview
  - [Basic Walk]#basic-walk
  - [Matching Obscured Nodes]#matching-obscured-nodes
  - [Uneliding Nodes]#uneliding-nodes
  - [Decrypting Nodes]#decrypting-nodes
  - [Decompressing Nodes]#decompressing-nodes
  - [Replacing Nodes]#replacing-nodes
    - [Basic Replacement]#basic-replacement
    - [Replacing Elided Nodes]#replacing-elided-nodes
    - [Replacing Multiple Different Nodes]#replacing-multiple-different-nodes
    - [Replacement Validation]#replacement-validation
  - [Using Target Filters]#using-target-filters
  - [Complete Example]#complete-example

## Overview

The `walk` command traverses an envelope's structure and provides several operations:

- **Default behavior**: List all node digests
- **`matching`**: Find nodes by obscuration type
- **`unelide`**: Restore elided nodes
- **`decrypt`**: Decrypt encrypted nodes
- **`decompress`**: Decompress compressed nodes
- **`replace`**: Replace nodes matching target digests with a replacement envelope

All subcommands can optionally filter nodes using the `--target` option.

## Basic Walk

Without any subcommand, `walk` outputs all node digests in the envelope. First, let's set up our example envelope:

```
ALICE_KNOWS_BOB=ur:envelope/lftpsoihfpjziniaihoytpsoihjejtjlktjktpsoiafwjlidutgmnnns
```

Now let's walk the envelope to see all its node digests:

```
envelope walk $ALICE_KNOWS_BOB

│ ur:digest/hdcxbwmwcwfdkecauerfvsdirpwpfhfgtalfmulesnstvlrpoyfzuyenamdpmdcfutdlstyaqzrk ur:digest/hdcxbwrlfpmwnsemrovtnssrtnotcfgshdvezcjedlbbtypatiwtecoxjnjnhtcafhbysptsnsnl ur:digest/hdcxkstbiywmmygsasktnbfwhtrppkclwdcmmugejesokejlbnftrdwspsmdcechbboerhzebtws ur:digest/hdcxldgouyhyadimzmpaeourhfsectvaskspdlotaxidiatbgydejnbwgskbhfrtwlwzneroatds ur:digest/hdcxuykitdcegyinqzlrlgdrcwsbbkihcemtchsntabdpldtbzjepkwsrkdrlernykrddpjtgdfh
```

This is useful for getting a complete inventory of all nodes in an envelope. The output is space-separated digests, suitable for use in shell scripts.

## Matching Obscured Nodes

The `matching` subcommand finds nodes that match specific obscuration types. Let's create an envelope with an elided part to demonstrate. We'll elide the entire assertion:

```
KNOWS_ASSERTION=$(envelope assertion at 0 $ALICE_KNOWS_BOB)
ASSERTION_DIGEST=$(envelope digest $KNOWS_ASSERTION)
ELIDED=$(envelope elide removing $ASSERTION_DIGEST $ALICE_KNOWS_BOB)
envelope format $ELIDED

│ "Alice" [
│     ELIDED
│ ]
```

Now we can find all elided nodes:

```
envelope walk $ELIDED matching --elided

│ ur:digest/hdcxkstbiywmmygsasktnbfwhtrppkclwdcmmugejesokejlbnftrdwspsmdcechbboerhzebtws
```

You can also search for encrypted or compressed nodes. Let's create a compressed envelope:

```
COMPRESSED=$(envelope compress $ALICE_KNOWS_BOB)
envelope format $COMPRESSED

│ COMPRESSED
```

Find all compressed nodes:

```
envelope walk $COMPRESSED matching --compressed

│ ur:digest/hdcxldgouyhyadimzmpaeourhfsectvaskspdlotaxidiatbgydejnbwgskbhfrtwlwzneroatds
```

You can combine multiple flags to find nodes that are either elided or compressed:

```
envelope walk $ELIDED matching --elided --compressed

│ ur:digest/hdcxkstbiywmmygsasktnbfwhtrppkclwdcmmugejesokejlbnftrdwspsmdcechbboerhzebtws
```

## Uneliding Nodes

The `unelide` subcommand restores elided nodes by providing the original envelopes. Let's restore our elided envelope by providing the original assertion:

```
RESTORED=$(envelope walk $ELIDED unelide $KNOWS_ASSERTION)
envelope format $RESTORED

│ "Alice" [
│     "knows": "Bob"
│ ]
```

The envelope is now fully restored. You can provide multiple envelopes to unelide different parts. Let's create an envelope with multiple elided parts - we'll elide both the subject "Alice" and the object "Bob":

```
ALICE_DIGEST=$(envelope subject type string "Alice" | envelope digest)
BOB_DIGEST=$(envelope subject type string "Bob" | envelope digest)
MULTI_ELIDED=$(envelope elide removing "$ALICE_DIGEST $BOB_DIGEST" $ALICE_KNOWS_BOB)
envelope format $MULTI_ELIDED

│ ELIDED [
│     "knows": ELIDED
│ ]
```

Now restore both parts by providing the individual envelopes:

```
ALICE_ENV=$(envelope subject type string "Alice")
BOB_ENV=$(envelope subject type string "Bob")
MULTI_RESTORED=$(envelope walk $MULTI_ELIDED unelide $ALICE_ENV $BOB_ENV)
envelope format $MULTI_RESTORED

│ "Alice" [
│     "knows": "Bob"
│ ]
```

## Decrypting Nodes

The `decrypt` subcommand decrypts encrypted nodes using symmetric keys. First, let's generate a key and encrypt an envelope:

Note that encrypting an envelope only encrypts its subject, not its assertions. To encrypt the whole envelope including assertions, you would need to wrap it first.

```
KEY=$(envelope generate key)
ENCRYPTED=$(envelope encrypt --key $KEY $ALICE_KNOWS_BOB)
envelope format $ENCRYPTED

│ ENCRYPTED [
│     "knows": "Bob"
│ ]
```

Now decrypt it using the walk command:

```
DECRYPTED=$(envelope walk $ENCRYPTED decrypt $KEY)
envelope format $DECRYPTED

│ "Alice" [
│     "knows": "Bob"
│ ]
```

You can provide multiple keys to decrypt different parts that were encrypted with different keys. Let's encrypt the assertion as well with a different key:

```
KEY2=$(envelope generate key)
ASSERTION=$(envelope assertion at 0 $ALICE_KNOWS_BOB)
ASSERTION_DIGEST=$(envelope digest $ASSERTION)
DOUBLE_ENCRYPTED=$(envelope elide removing $ASSERTION_DIGEST --action encrypt --key $KEY2 $ENCRYPTED)
envelope format $DOUBLE_ENCRYPTED

│ ENCRYPTED [
│     ENCRYPTED
│ ]
```

Now decrypt with both keys:

```
DOUBLE_DECRYPTED=$(envelope walk $DOUBLE_ENCRYPTED decrypt $KEY $KEY2)
envelope format $DOUBLE_DECRYPTED

│ "Alice" [
│     "knows": "Bob"
│ ]
```

## Decompressing Nodes

The `decompress` subcommand decompresses compressed nodes. We already created a compressed envelope earlier, so let's decompress it:

```
DECOMPRESSED=$(envelope walk $COMPRESSED decompress)
envelope format $DECOMPRESSED

│ "Alice" [
│     "knows": "Bob"
│ ]
```

The envelope is now fully decompressed and readable.

## Replacing Nodes

The `replace` subcommand allows you to replace nodes in an envelope that match specific digests with a replacement envelope. This is useful for transforming envelopes by substituting specific elements throughout the structure, regardless of whether those elements are plaintext or obscured (elided, encrypted, or compressed).

The `replace` subcommand requires the `--target` option to specify which digests to replace.

### Basic Replacement

Let's start by creating an envelope where "Bob" appears in multiple places:

```
BOB=$(envelope subject type string "Bob")
CHARLIE=$(envelope subject type string "Charlie")
ENVELOPE_WITH_BOB=$(envelope assertion add pred-obj string "likes" envelope $BOB $ALICE_KNOWS_BOB)
envelope format $ENVELOPE_WITH_BOB

│ "Alice" [
│     "knows": "Bob"
│     "likes": "Bob"
│ ]
```

Now let's replace all instances of "Bob" with "Charlie". First, we need Bob's digest:

```
BOB_DIGEST=$(envelope digest $BOB)
```

Now we can replace all occurrences:

```
REPLACED=$(envelope walk --target $BOB_DIGEST $ENVELOPE_WITH_BOB replace $CHARLIE)
envelope format $REPLACED

│ "Alice" [
│     "knows": "Charlie"
│     "likes": "Charlie"
│ ]
```

Both instances of "Bob" have been replaced with "Charlie" because they share the same digest.

### Replacing Elided Nodes

You can replace ELIDED nodes with envelopes that *do not* match the elided content. If you do this, you are no longer *transforming* the original content, which would leave the Merkle structure the same, but rather substituting new content in place of the elided parts. This will change all the digests in the envelope from the point of the change up to the envelope's root digest. Doing this is likely to invalidate any signatures on the envelope, so use this feature with caution.

Let's demonstrate this by first creating an envelope and checking its root digest:

```
ENVELOPE_WITH_BOB=$(envelope assertion add pred-obj string "likes" envelope $BOB $ALICE_KNOWS_BOB)
envelope format $ENVELOPE_WITH_BOB

│ "Alice" [
│     "knows": "Bob"
│     "likes": "Bob"
│ ]
```

```
envelope digest $ENVELOPE_WITH_BOB

│ ur:digest/hdcxbesedketplchinhntebnvevlsremctoxvsynckzcnychtnrekbwzhdvtzesoamidutsksekn
```

Now elide Bob and check the root digest again. Notice that it remains the same because elision preserves the Merkle structure:

```
ELIDED=$(envelope elide removing $BOB_DIGEST $ENVELOPE_WITH_BOB)
envelope format $ELIDED

│ "Alice" [
│     "knows": ELIDED
│     "likes": ELIDED
│ ]
```

```
envelope digest $ELIDED

│ ur:digest/hdcxbesedketplchinhntebnvevlsremctoxvsynckzcnychtnrekbwzhdvtzesoamidutsksekn
```

Now replace the elided nodes (which have Bob's digest) with Charlie. This substitutes different content, so the root digest will change:

```
REPLACED_ELIDED=$(envelope walk --target $BOB_DIGEST $ELIDED replace $CHARLIE)
envelope format $REPLACED_ELIDED

│ "Alice" [
│     "knows": "Charlie"
│     "likes": "Charlie"
│ ]
```

```
envelope digest $REPLACED_ELIDED

│ ur:digest/hdcxcytdknwzttswkiaetyylbdmncmrkemlbfhcljegaosayjnsgbdptfdcewkinmdfllksrbebt
```

The root digest has changed because we've substituted different content. This is fundamentally different from uneliding with the original content, which would preserve the original digest.

### Replacing Multiple Different Nodes

You can replace multiple different elements with the same replacement by providing multiple target digests. Let's create an envelope with both Bob and Carol, then replace both with "REDACTED":

```
CAROL=$(envelope subject type string "Carol")
REDACTED=$(envelope subject type string "REDACTED")
ENVELOPE=$(envelope assertion add pred-obj string "likes" envelope $CAROL $ALICE_KNOWS_BOB)
envelope format $ENVELOPE

│ "Alice" [
│     "knows": "Bob"
│     "likes": "Carol"
│ ]
```

Get both digests and replace them in one operation:

```
BOB_DIGEST=$(envelope digest $BOB)
CAROL_DIGEST=$(envelope digest $CAROL)
MULTI_REPLACED=$(envelope walk --target "$BOB_DIGEST $CAROL_DIGEST" $ENVELOPE replace $REDACTED)
envelope format $MULTI_REPLACED

│ "Alice" [
│     "knows": "REDACTED"
│     "likes": "REDACTED"
│ ]
```

This is useful for anonymizing or redacting multiple pieces of information in a structured way.

### Replacement Validation

The `replace` subcommand validates that replacements maintain envelope structure integrity. Specifically, assertions (elements in a node's assertions array) can only be replaced with other assertions or obscured elements (which are presumed to be obscured assertions).

Let's try to replace an entire assertion with a plain string, which should fail:

```
KNOWS_ASSERTION=$(envelope assertion at 0 $ALICE_KNOWS_BOB)
ASSERTION_DIGEST=$(envelope digest $KNOWS_ASSERTION)
CHARLIE=$(envelope subject type string "Charlie")
envelope walk --target $ASSERTION_DIGEST $ALICE_KNOWS_BOB replace $CHARLIE

│ Error: invalid format
```

This error occurs because "Charlie" is a plain string envelope, not an assertion. The envelope structure requires that elements in the assertions array be either:
- Actual assertions (predicate-object pairs)
- Obscured elements (ELIDED, ENCRYPTED, or COMPRESSED) which are presumed to be obscured assertions

This validation ensures that envelopes maintain their structural integrity even when performing transformations.

## Using Target Filters

All `walk` operations support the `--target` option to filter which nodes are processed. Let's get the top-level digest:

```
TOP_DIGEST=$(envelope digest $ALICE_KNOWS_BOB)
```

Now only process nodes matching that specific digest:

```
envelope walk --target $TOP_DIGEST $ALICE_KNOWS_BOB

│ ur:digest/hdcxldgouyhyadimzmpaeourhfsectvaskspdlotaxidiatbgydejnbwgskbhfrtwlwzneroatds
```

This is particularly useful for selective operations. Let's create an envelope with a large data field and selectively compress just that assertion:

```
LARGE_TEXT=$(envelope subject type string "$(printf 'A%.0s' {1..1000})")
ENVELOPE_WITH_LARGE=$(envelope assertion add pred-obj string "data" envelope $LARGE_TEXT $ALICE_KNOWS_BOB)
DATA_ASSERTION=$(envelope assertion find predicate string "data" $ENVELOPE_WITH_LARGE)
DATA_DIGEST=$(envelope digest $DATA_ASSERTION)
SELECTIVE_COMPRESSED=$(envelope elide removing $DATA_DIGEST --action compress $ENVELOPE_WITH_LARGE)
envelope format $SELECTIVE_COMPRESSED

│ "Alice" [
│     "knows": "Bob"
│     COMPRESSED
│ ]
```

Now we can decompress everything:

```
FULL_DECOMPRESSED=$(envelope walk $SELECTIVE_COMPRESSED decompress)
envelope format $FULL_DECOMPRESSED

│ "Alice" [
│     "data": "AAAAAAAAAA…AAAAAAAAAA"
│     "knows": "Bob"
│ ]
```

This selective compression and decompression is useful for revealing only specific parts of an envelope while keeping other parts obscured.

## Complete Example

Here's a complete workflow demonstrating how to work with mixed obscuration types. Let's create an envelope with some assertions and then apply different types of obscuration:

```
COMPLEX=$(envelope subject type string "Alice")
COMPLEX=$(envelope assertion add pred-obj string "name" string "Alice Smith" $COMPLEX)
COMPLEX=$(envelope assertion add pred-obj string "age" number 30 $COMPLEX)
BIO_TEXT=$(envelope subject type string "$(printf 'X%.0s' {1..1000})")
COMPLEX=$(envelope assertion add pred-obj string "bio" envelope $BIO_TEXT $COMPLEX)
envelope format $COMPLEX

│ "Alice" [
│     "age": 30
│     "bio": "XXXXXXXXXX…XXXXXXXXXX"
│     "name": "Alice Smith"
│ ]
```

Now let's apply mixed obscuration - elide the name, encrypt the age, and compress the bio:

```
NAME_DIGEST=$(envelope assertion find predicate string "name" $COMPLEX | envelope digest)
AGE_DIGEST=$(envelope assertion find predicate string "age" $COMPLEX | envelope digest)
BIO_DIGEST=$(envelope assertion find predicate string "bio" $COMPLEX | envelope digest)
ENCRYPT_KEY=$(envelope generate key)
MIXED=$(envelope elide removing $NAME_DIGEST $COMPLEX)
MIXED=$(envelope elide removing $AGE_DIGEST --action encrypt --key $ENCRYPT_KEY $MIXED)
MIXED=$(envelope elide removing $BIO_DIGEST --action compress $MIXED)
envelope format $MIXED

│ "Alice" [
│     COMPRESSED
│     ELIDED
│     ENCRYPTED
│ ]
```

Find all the different types of obscured nodes (note: the actual digest values will vary since they depend on the randomly-generated encryption key):

```
envelope walk $MIXED matching --elided

│ ur:digest/hdcxmktnlylaahtlfnbyndkepscpwztthetkknatrkltjewkftpyrylemdeojtnnnsrpjpzccmga
```

```
envelope walk $MIXED matching --encrypted

│ ur:digest/hdcxbarehnndlolujypllofdhgylwzjtrlwtaxlpcmpadmqdostnwkskylmnvaghwllsjzsbaocw
```

```
envelope walk $MIXED matching --compressed

│ ur:digest/hdcxsnfdlpaygsksjypkwzahoxurdyuorkmhlrkesfemldwmwnjygdotiansvegsztnddedyykje
```

Now let's restore everything step by step. First, unelide the name by providing the specific assertion:

```
NAME_ASSERTION=$(envelope assertion find predicate string "name" $COMPLEX)
NAME_RESTORED=$(envelope walk $MIXED unelide $NAME_ASSERTION)
envelope format $NAME_RESTORED

│ "Alice" [
│     "name": "Alice Smith"
│     COMPRESSED
│     ENCRYPTED
│ ]
```

Next, decrypt the age:

```
AGE_RESTORED=$(envelope walk $NAME_RESTORED decrypt $ENCRYPT_KEY)
envelope format $AGE_RESTORED

│ "Alice" [
│     "age": 30
│     "name": "Alice Smith"
│     COMPRESSED
│ ]
```

Finally, decompress the bio:

```
FULLY_RESTORED=$(envelope walk $AGE_RESTORED decompress)
envelope format $FULLY_RESTORED

│ "Alice" [
│     "age": 30
│     "bio": "XXXXXXXXXX…XXXXXXXXXX"
│     "name": "Alice Smith"
│ ]
```

Or do it all in one command:

```
ALL_RESTORED=$(envelope walk $MIXED unelide $NAME_ASSERTION | envelope walk decrypt $ENCRYPT_KEY | envelope walk decompress)
envelope format $ALL_RESTORED

│ "Alice" [
│     "age": 30
│     "bio": "XXXXXXXXXX…XXXXXXXXXX"
│     "name": "Alice Smith"
│ ]
```