Files
node/cmd/ceremony/types.go
T
Hanzo AI c3b398bc7b json: migrate every encoding/json import to json/v2 (go-json-experiment)
External (HTTP / JSON-RPC) is the only place JSON is legitimate. Every
existing encoding/json import in node/ moves to github.com/go-json-experiment/json
(v2 root, not the v1 sub-package). NewEncoder/NewDecoder rewrite to
MarshalWrite/UnmarshalRead. MarshalIndent rewrites to Marshal with
jsontext.WithIndent. json.RawMessage rewrites to jsontext.Value.
*json.SyntaxError rewrites to *jsontext.SyntacticError.

81 files migrated. LLM.md captures the rule + v1->v2 delta table.

Known v2 semantic deltas surfaced by existing tests (followups, not regressions):
- [N]byte fields with no MarshalJSON now marshal as base64 string (v1 marshalled
  as JSON array of byte numbers). Affects vms/platformvm/txs/*_test.go fixtures
  with embedded BLS proofOfPossession.
- time.Duration has no v2 default representation; configs that wire-format
  Duration as nanoseconds (vms/{xvm,platformvm}/config, config/spec) need to
  switch to string-form Duration or carry an explicit option. v2 root does not
  re-export FormatDurationAsNano.
- v2 enforces strict UTF-8 (vms/chainadapter/messaging fixture has non-UTF-8).
- json.MarshalWrite does not append a trailing '\n' (v1 NewEncoder.Encode did);
  service/auth/auth_test.go expectation updated.
- nil []byte round-trips to empty (not nil); config_test deep-equal fixtures
  surface this.

All affected sites are at the API boundary; ZAP wire envelope already covers
the internal data paths (state, P2P, consensus, MPC, threshold). Internal
JSON sites that should move to ZAP next (separate work):
- vms/da/store.go            (DA blob/cert storage as JSON)
- vms/platformvm/airdrop     (airdrop claims as JSON in db)
- vms/chainadapter/appchain  (SQLite materializer schema/data blobs)
- vms/chainadapter/messaging (conversation codec)
- staking/kms.go             (KMS HTTP client — external technically, leave)
- utils/{bimap,ips}          (small marshaler shims — low priority)
2026-06-06 22:26:02 -07:00

108 lines
2.9 KiB
Go

package main
import (
"crypto/sha256"
"encoding/hex"
"github.com/go-json-experiment/json"
"fmt"
"time"
"github.com/consensys/gnark-crypto/ecc/bn254"
)
// CeremonyState holds the full state of a powers-of-tau ceremony.
type CeremonyState struct {
Circuit string `json:"circuit"`
NumConstraints int `json:"numConstraints"`
PowersNeeded int `json:"powersNeeded"`
Participants int `json:"participants"`
TauG1 []G1Point `json:"tauG1"`
TauG2 []G2Point `json:"tauG2"`
AlphaG1 []G1Point `json:"alphaG1"`
BetaG1 []G1Point `json:"betaG1"`
BetaG2 G2Point `json:"betaG2"`
Contributions []Contribution `json:"contributions"`
}
// Contribution records a single participant's contribution.
type Contribution struct {
Participant string `json:"participant"`
Hash string `json:"hash"` // SHA-256 of randomness commitment
PrevHash string `json:"prevHash"` // StateHash of previous contribution (empty for first)
StateHash string `json:"stateHash"` // SHA-256 of SRS state AFTER this contribution
Timestamp string `json:"timestamp"`
}
// G1Point is a JSON-serializable wrapper for bn254.G1Affine.
type G1Point struct {
bn254.G1Affine
}
func (p G1Point) MarshalJSON() ([]byte, error) {
b := p.G1Affine.Marshal()
return json.Marshal(hex.EncodeToString(b))
}
func (p *G1Point) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
b, err := hex.DecodeString(s)
if err != nil {
return fmt.Errorf("decode hex: %w", err)
}
return p.G1Affine.Unmarshal(b)
}
// G2Point is a JSON-serializable wrapper for bn254.G2Affine.
type G2Point struct {
bn254.G2Affine
}
func (p G2Point) MarshalJSON() ([]byte, error) {
b := p.G2Affine.Marshal()
return json.Marshal(hex.EncodeToString(b))
}
func (p *G2Point) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
b, err := hex.DecodeString(s)
if err != nil {
return fmt.Errorf("decode hex: %w", err)
}
return p.G2Affine.Unmarshal(b)
}
func newContribution(participant string, hash []byte, prevHash string, stateHash string) Contribution {
return Contribution{
Participant: participant,
Hash: hex.EncodeToString(hash),
PrevHash: prevHash,
StateHash: stateHash,
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
// computeStateHash computes SHA-256 over all SRS points for hash chain verification.
func computeStateHash(state *CeremonyState) string {
h := sha256.New()
for i := range state.TauG1 {
h.Write(state.TauG1[i].Marshal())
}
for i := range state.TauG2 {
h.Write(state.TauG2[i].Marshal())
}
for i := range state.AlphaG1 {
h.Write(state.AlphaG1[i].Marshal())
}
for i := range state.BetaG1 {
h.Write(state.BetaG1[i].Marshal())
}
h.Write(state.BetaG2.Marshal())
return hex.EncodeToString(h.Sum(nil))
}