Files
node/vms/platformvm/signer/proof_of_possession.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

116 lines
2.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package signer
import (
"errors"
"github.com/go-json-experiment/json"
"github.com/luxfi/crypto/bls"
"github.com/luxfi/formatting"
)
var (
_ Signer = (*ProofOfPossession)(nil)
ErrInvalidProofOfPossession = errors.New("invalid proof of possession")
)
type ProofOfPossession struct {
PublicKey [bls.PublicKeyLen]byte `serialize:"true" json:"publicKey"`
// BLS signature proving ownership of [PublicKey]. The signed message is the
// [PublicKey].
ProofOfPossession [bls.SignatureLen]byte `serialize:"true" json:"proofOfPossession"`
// publicKey is the parsed version of [PublicKey]. It is populated in
// [Verify].
publicKey *bls.PublicKey
}
func NewProofOfPossession(sk bls.Signer) (*ProofOfPossession, error) {
pk := sk.PublicKey()
pkBytes := bls.PublicKeyToCompressedBytes(pk)
sig, err := sk.SignProofOfPossession(pkBytes)
if err != nil {
return nil, err
}
sigBytes := bls.SignatureToBytes(sig)
pop := &ProofOfPossession{
publicKey: pk,
}
copy(pop.PublicKey[:], pkBytes)
copy(pop.ProofOfPossession[:], sigBytes)
return pop, nil
}
func (p *ProofOfPossession) Verify() error {
publicKey, err := bls.PublicKeyFromCompressedBytes(p.PublicKey[:])
if err != nil {
return err
}
signature, err := bls.SignatureFromBytes(p.ProofOfPossession[:])
if err != nil {
return err
}
if !bls.VerifyProofOfPossession(publicKey, signature, p.PublicKey[:]) {
return ErrInvalidProofOfPossession
}
p.publicKey = publicKey
return nil
}
func (p *ProofOfPossession) Key() *bls.PublicKey {
return p.publicKey
}
type jsonProofOfPossession struct {
PublicKey string `json:"publicKey"`
ProofOfPossession string `json:"proofOfPossession"`
}
func (p *ProofOfPossession) MarshalJSON() ([]byte, error) {
pk, err := formatting.Encode(formatting.HexNC, p.PublicKey[:])
if err != nil {
return nil, err
}
pop, err := formatting.Encode(formatting.HexNC, p.ProofOfPossession[:])
if err != nil {
return nil, err
}
return json.Marshal(jsonProofOfPossession{
PublicKey: pk,
ProofOfPossession: pop,
})
}
func (p *ProofOfPossession) UnmarshalJSON(b []byte) error {
jsonBLS := jsonProofOfPossession{}
err := json.Unmarshal(b, &jsonBLS)
if err != nil {
return err
}
pkBytes, err := formatting.Decode(formatting.HexNC, jsonBLS.PublicKey)
if err != nil {
return err
}
pk, err := bls.PublicKeyFromCompressedBytes(pkBytes)
if err != nil {
return err
}
popBytes, err := formatting.Decode(formatting.HexNC, jsonBLS.ProofOfPossession)
if err != nil {
return err
}
copy(p.PublicKey[:], pkBytes)
copy(p.ProofOfPossession[:], popBytes)
p.publicKey = pk
return nil
}