S15 TypeScript seals: seal, open and sealAnonymous beside go/seal

The forge module's minting processes are TypeScript and the vault's two steps need ADR 0206's
sealed box and the NaCl anonymous box its intake opens; both now live once in mesh-sdk, proven
byte for byte against the Go package's bytes (tweetnacl 1.0.3 and blakejs 1.2.1 pinned).

Builds: design 47 H2 §808, §822, H-A-407 and its addendum
This commit is contained in:
2026-11-22 00:59:54 +01:00
parent 3ad8ccbc06
commit d6cdf3b8f5
8 changed files with 376 additions and 2 deletions
+1 -1
View File
@@ -6,6 +6,7 @@ require (
github.com/klauspost/compress v1.20.0
github.com/nats-io/nats-server/v2 v2.11.17
github.com/nats-io/nats.go v1.54.0
golang.org/x/crypto v0.57.0
)
require (
@@ -15,7 +16,6 @@ require (
github.com/nats-io/jwt/v2 v2.8.1 // indirect
github.com/nats-io/nkeys v0.4.16 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/time v0.15.0 // indirect
)
+96
View File
@@ -0,0 +1,96 @@
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)
}
+24
View File
@@ -0,0 +1,24 @@
{
"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.",
"recipient": {
"public": "n2nWtTM35RrjXe1pPBzIHWRfSvoE5AMFxFWA92lYmj4=",
"private": "0zveHnrRJV0at3GITmix/NpjX1sJPgsu0tGZIXFVKos="
},
"vectors": [
{
"plaintext": "",
"ephSecret": "CU2adSGwGEEx6/xxSvrQ8Vfq6Yc+kR+2yX7fFriYCbg=",
"sealed": "8qO+LLqjzgOtuE2KVM6bKDB3dMiMiAuEc0hgRwGe03wNBJ60VwAAIw4xPJLmmT1g"
},
{
"plaintext": "a token minted for the forge",
"ephSecret": "CvmgsKRsGyTmAFRD4EB0mAPpBVN3I4yLJJkkcfbqX0A=",
"sealed": "erK5lMtu+k90Thig1rpUq2jJ9DzF/WZWvVvMPNO+hiPF5uL5jnqNB7h3hQro13ZiKwK4O/hm3MvZm9glM8KqSBoeApTrVWnvP/Ekzg=="
},
{
"plaintext": "ünïcode and a longer value: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ephSecret": "ypSqdS+hEEoCsEDT6K87sc/I/h9yi06j08c1bKEJAQw=",
"sealed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