Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/jcs.go
T
jschoubben 21c97051c8 IX1-3: the registrations, the proposals that wait for their warrant, the seed, and the indexer's verbs and tools of them
The seat mesh-catalog's registrations, register, policy, watch and unwatch (each asks the operator, naming every value it
moves, with the change's digest, and writes only on the warrant), the state proposals with an ask id and a digest per key,
the consumer of operator-channel.decided, a restart that reads each open key, indexer_proposals, indexer_proposal_drop and
indexer_seed_again, the seed as one ask. The router's record of an ask is one injected read that fails loudly until the design
says how the program reads it (D-37); the seed's steps call is not wired.

Builds: design 47 G §667, §2, §3, §4, §5, §8, §9, §10, §342, §343, §482

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 20:53:23 +01:00

134 lines
2.9 KiB
Go

package index
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"sort"
"strings"
"unicode/utf16"
)
// Canonical is a JSON value in RFC 8785's canonical form: keys sorted by their UTF-16 code units, no white space, strings
// escaped as the RFC says, integers as digits. A number with a fraction or an exponent is refused: no change the indexer
// digests holds one.
func Canonical(v any) ([]byte, error) {
raw, err := json.Marshal(v)
if err != nil {
return nil, err
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
var tree any
if err := dec.Decode(&tree); err != nil {
return nil, err
}
var b bytes.Buffer
if err := canonical(&b, tree); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// DigestOf is "sha256:" and the digits of the canonical form of v.
func DigestOf(v any) (string, error) {
c, err := Canonical(v)
if err != nil {
return "", err
}
sum := sha256.Sum256(c)
return "sha256:" + hex.EncodeToString(sum[:]), nil
}
func canonical(b *bytes.Buffer, v any) error {
switch t := v.(type) {
case nil:
b.WriteString("null")
case bool:
if t {
b.WriteString("true")
} else {
b.WriteString("false")
}
case json.Number:
if strings.ContainsAny(string(t), ".eE") {
return fmt.Errorf("the number %s has a fraction or an exponent, which the canonical form of a change does not hold", t)
}
b.WriteString(string(t))
case string:
canonicalString(b, t)
case []any:
b.WriteByte('[')
for i, e := range t {
if i > 0 {
b.WriteByte(',')
}
if err := canonical(b, e); err != nil {
return err
}
}
b.WriteByte(']')
case map[string]any:
keys := make([]string, 0, len(t))
for k := range t {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return slices16(keys[i], keys[j]) })
b.WriteByte('{')
for i, k := range keys {
if i > 0 {
b.WriteByte(',')
}
canonicalString(b, k)
b.WriteByte(':')
if err := canonical(b, t[k]); err != nil {
return err
}
}
b.WriteByte('}')
default:
return fmt.Errorf("a value of type %T has no canonical form", v)
}
return nil
}
// slices16 orders two strings by their UTF-16 code units, as RFC 8785 sorts keys.
func slices16(a, b string) bool {
x, y := utf16.Encode([]rune(a)), utf16.Encode([]rune(b))
for i := 0; i < len(x) && i < len(y); i++ {
if x[i] != y[i] {
return x[i] < y[i]
}
}
return len(x) < len(y)
}
func canonicalString(b *bytes.Buffer, s string) {
b.WriteByte('"')
for _, r := range s {
switch {
case r == '"':
b.WriteString(`\"`)
case r == '\\':
b.WriteString(`\\`)
case r == '\b':
b.WriteString(`\b`)
case r == '\f':
b.WriteString(`\f`)
case r == '\n':
b.WriteString(`\n`)
case r == '\r':
b.WriteString(`\r`)
case r == '\t':
b.WriteString(`\t`)
case r < 0x20:
fmt.Fprintf(b, `\u%04x`, r)
default:
b.WriteRune(r)
}
}
b.WriteByte('"')
}