The eight Go test functions H2 added become subtests of existing tests (catalogue's state test, the bundle's state test, the views test, the process state test, the testbus bus test, the seal byte-for-byte test), and four more are folded beside them (two seal, two process watch); the two TypeScript seal tests become one, and two more TypeScript tests are folded into their neighbours. No assertion is lost. Builds: design 47 H2 §819
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package seal
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"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
|
|
}
|
|
|
|
func mustJSON(v any) string {
|
|
raw, _ := json.Marshal(v)
|
|
return string(raw)
|
|
}
|