package seal import ( "bytes" "crypto/sha256" "encoding/base64" "encoding/json" "os" "testing" "golang.org/x/crypto/nacl/box" ) // anonymousFixture holds the NaCl anonymous boxes golang.org/x/crypto/nacl/box seals (the box the vault's intake opens), // with their one-time keys, for the SDK's TypeScript sealAnonymous to make byte for byte (design 47 H2, H-A-407). type anonymousFixture struct { About string `json:"about"` Recipient struct { Public string `json:"public"` Private string `json:"private"` } `json:"recipient"` Vectors []struct { Plaintext string `json:"plaintext"` EphSecret string `json:"ephSecret"` Sealed string `json:"sealed"` } `json:"vectors"` } const anonymousFixturePath = "testdata/sealed-anonymous-by-go.json" // anonymousVectors seals each plaintext with box.SealAnonymous to a recipient and one-time keys derived from fixed // labels: test keys that guard nothing. func anonymousVectors(t *testing.T) anonymousFixture { t.Helper() b64 := base64.StdEncoding.EncodeToString seed := sha256.Sum256([]byte("mesh-sdk anonymous box test recipient")) pub, priv, err := box.GenerateKey(bytes.NewReader(seed[:])) if err != nil { t.Fatal(err) } var f anonymousFixture f.About = "Made by this package's anonymous_test.go with golang.org/x/crypto/nacl/box's SealAnonymous, its one-time keys fixed; the test proves the file is Go's bytes on every run. Test keys; they guard nothing." f.Recipient.Public, f.Recipient.Private = b64(pub[:]), b64(priv[:]) for i, plain := range []string{"", "a token minted for the forge", "ünïcode and a longer value: " + string(bytes.Repeat([]byte("x"), 300))} { eph := sha256.Sum256([]byte{byte(i), 'e', 'p', 'h'}) sealed, err := box.SealAnonymous(nil, []byte(plain), pub, bytes.NewReader(eph[:])) if err != nil { t.Fatal(err) } f.Vectors = append(f.Vectors, struct { Plaintext string `json:"plaintext"` EphSecret string `json:"ephSecret"` Sealed string `json:"sealed"` }{plain, b64(eph[:]), b64(sealed)}) } return f } // The anonymous-box fixture the TypeScript tests read is what box.SealAnonymous makes, and each opens with the // recipient's key as the vault's intake opens it (box.OpenAnonymous). MESH_SDK_WRITE_FIXTURES=1 writes it anew. func TestTheAnonymousBoxFixtureIsGosBytes(t *testing.T) { want := anonymousVectors(t) if os.Getenv("MESH_SDK_WRITE_FIXTURES") == "1" { raw, _ := json.MarshalIndent(want, "", " ") if err := os.WriteFile(anonymousFixturePath, append(raw, '\n'), 0o644); err != nil { t.Fatal(err) } } raw, err := os.ReadFile(anonymousFixturePath) if err != nil { t.Fatal(err) } var got anonymousFixture if err := json.Unmarshal(raw, &got); err != nil { t.Fatal(err) } if g, w := mustJSON(got), mustJSON(want); g != w { t.Fatalf("the fixture is not Go's bytes:\n%s\nwant\n%s", g, w) } var pub, priv [32]byte p, _ := base64.StdEncoding.DecodeString(got.Recipient.Public) k, _ := base64.StdEncoding.DecodeString(got.Recipient.Private) copy(pub[:], p) copy(priv[:], k) for _, v := range got.Vectors { sealed, _ := base64.StdEncoding.DecodeString(v.Sealed) if out, ok := box.OpenAnonymous(nil, sealed, &pub, &priv); !ok || string(out) != v.Plaintext { t.Errorf("a box does not open to %q: %q, %v", v.Plaintext, out, ok) } } } func mustJSON(v any) string { raw, _ := json.Marshal(v) return string(raw) }