Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
59 lines (51 loc) · 1.78 KB
/
Copy pathexample_test.go
File metadata and controls
59 lines (51 loc) · 1.78 KB
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
package scuttle_test
import (
"context"
"errors"
"fmt"
"github.com/Continuum-AI-Corp/scuttle"
)
// The full write-then-read path, with the two halves in the places they
// belong. In a real deployment the writer and the reader are different
// processes, and only the reader ever sees the capture seed.
func Example() {
// Provisioning (once; see cmd/scuttle-keygen).
seed, _ := scuttle.GenerateCaptureSeed()
authKey, _ := scuttle.GenerateAuthKey()
reader, _ := scuttle.NewLocalKeyring(seed)
publicKey := reader.CapturePublicKey() // the ONLY key a writer receives
env := scuttle.Envelope{MaxPlaintext: 256 << 10, AuthKey: authKey}
// ── Writer: public key + auth key. Can seal, cannot open. ──
sealer, _ := scuttle.NewLeafSealer(publicKey)
leafKey, sealed, _ := sealer.NewLeaf("tenant:42|user:7|2026-09-12T10")
bind := scuttle.Binding{
SchemaVersion: 1,
LeafID: sealed.LeafID,
WorkspaceID: 42,
UserID: 7,
RequestID: "req_01JQ8F7YKX2M",
}
ct, err := env.Seal(leafKey, bind, scuttle.FieldRequestBody, []byte(`{"prompt":"hi"}`))
if err != nil {
panic(err)
}
// Store ct and sealed (LeafID, KemKeyID, Blob) with the record.
// ── Reader: capture seed + auth key. ──
key, err := reader.OpenLeaf(context.Background(), sealed)
switch {
case errors.Is(err, scuttle.ErrKeyUnavailable):
return // retry later
case err != nil:
return // ErrUndecryptable: investigate, do not retry
}
pt, err := env.Open(key, bind, scuttle.FieldRequestBody, ct)
fmt.Println(string(pt), err)
// The same ciphertext presented as another tenant's does not open.
other := bind
other.WorkspaceID = 43
_, err = env.Open(key, other, scuttle.FieldRequestBody, ct)
fmt.Println(errors.Is(err, scuttle.ErrUndecryptable))
// Output:
// {"prompt":"hi"} <nil>
// true
}