clawchain/x/privacy/keeper/state_machine.go
caelum0x ed1ed54ec3 feat(privacy): expand gRPC surface, batch transfers, view keys, state machine
Adds full QueryServer gRPC with gateway REST, batch private transfers,
view key registration, commitment index/merkle proof/root history queries,
MPC trusted setup ceremony code, state machine for shielded notes, and
comprehensive integration tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 22:13:53 +03:00

143 lines
5.1 KiB
Go

package keeper
import (
"context"
"encoding/hex"
"math/big"
"strings"
"clawchain/x/privacy/types"
errorsmod "cosmossdk.io/errors"
)
// NormalizeHex canonicalizes a hex string (trim + lowercase) and returns decoded bytes.
func (k Keeper) NormalizeHex(raw string) (string, []byte, error) {
normalized := strings.ToLower(strings.TrimSpace(raw))
if normalized == "" {
return "", nil, errorsmod.Wrap(types.ErrInvalidCommitment, "hex string cannot be empty")
}
decoded, err := hex.DecodeString(normalized)
if err != nil {
return "", nil, errorsmod.Wrap(types.ErrInvalidCommitment, "invalid hex string")
}
if len(decoded) == 0 {
return "", nil, errorsmod.Wrap(types.ErrInvalidCommitment, "hex value cannot be empty")
}
return normalized, decoded, nil
}
// ConsumeNullifiers validates and marks each nullifier as spent atomically in deterministic order.
// Each nullifier is stored in state immediately after passing the Has() check to prevent
// same-block double-spend attacks where two transactions reference the same nullifier.
func (k Keeper) ConsumeNullifiers(ctx context.Context, nullifierHexes []string) error {
if len(nullifierHexes) == 0 {
return errorsmod.Wrap(types.ErrInvalidProof, "no nullifiers provided")
}
seen := make(map[string]struct{}, len(nullifierHexes))
for _, raw := range nullifierHexes {
nfHex, _, err := k.NormalizeHex(raw)
if err != nil {
return errorsmod.Wrap(types.ErrInvalidProof, "invalid nullifier hex")
}
if _, dup := seen[nfHex]; dup {
return errorsmod.Wrapf(types.ErrNullifierAlreadyUsed, "duplicate nullifier %s in transaction", nfHex)
}
seen[nfHex] = struct{}{}
exists, err := k.Nullifiers.Has(ctx, nfHex)
if err != nil {
return errorsmod.Wrap(types.ErrInvalidProof, "failed to check nullifier")
}
if exists {
return errorsmod.Wrapf(types.ErrNullifierAlreadyUsed, "nullifier %s", nfHex)
}
// Store the nullifier immediately after the Has() check passes.
// This prevents same-block double-spend: if another tx in the same block
// tries to use this nullifier, it will see it already exists.
if err := k.Nullifiers.Set(ctx, nfHex, true); err != nil {
return errorsmod.Wrap(types.ErrInvalidProof, "failed to store nullifier")
}
}
return nil
}
// ValidateKnownRoot verifies rootHex is canonical and present in historical valid roots.
func (k Keeper) ValidateKnownRoot(ctx context.Context, rootHex string) (string, error) {
normalized, _, err := k.NormalizeHex(rootHex)
if err != nil {
return "", errorsmod.Wrap(types.ErrInvalidMerkleRoot, "invalid root hex")
}
exists, err := k.MerkleRoots.Has(ctx, normalized)
if err != nil {
return "", errorsmod.Wrap(types.ErrInvalidMerkleRoot, "failed to check merkle root")
}
if !exists {
return "", errorsmod.Wrapf(types.ErrInvalidMerkleRoot, "root %s not recognized", normalized)
}
return normalized, nil
}
// AppendCommitment inserts one commitment, updates tree/root state, and records root transition.
func (k Keeper) AppendCommitment(ctx context.Context, commitmentBytes []byte) (uint64, string, string, error) {
if len(commitmentBytes) == 0 {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "empty commitment")
}
commitmentHex := strings.ToLower(hex.EncodeToString(commitmentBytes))
exists, err := k.CommitmentIndex.Has(ctx, commitmentHex)
if err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "failed to check commitment index")
}
if exists {
return 0, "", "", errorsmod.Wrapf(types.ErrInvalidCommitment, "duplicate commitment %s", commitmentHex)
}
leafIndex, err := k.CommitmentCount.Next(ctx)
if err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "failed to get next commitment index")
}
if err := k.Commitments.Set(ctx, leafIndex, commitmentBytes); err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "failed to store commitment")
}
if err := k.CommitmentIndex.Set(ctx, commitmentHex, leafIndex); err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "failed to store commitment index")
}
if err := k.insertLeafAndUpdateTree(ctx, leafIndex, new(big.Int).SetBytes(commitmentBytes)); err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrMerkleTreeFull, err.Error())
}
root, err := k.computeRootFromState(ctx)
if err != nil {
return 0, "", "", errorsmod.Wrap(types.ErrInvalidCommitment, "failed to compute merkle root")
}
rootHex := strings.ToLower(hex.EncodeToString(root.Bytes()))
if err := k.recordRootTransition(ctx, rootHex); err != nil {
return 0, "", "", err
}
return leafIndex, commitmentHex, rootHex, nil
}
func (k Keeper) recordRootTransition(ctx context.Context, rootHex string) error {
if err := k.MerkleRoots.Set(ctx, rootHex, true); err != nil {
return errorsmod.Wrap(types.ErrInvalidCommitment, "failed to store merkle root")
}
rootHistoryIndex, err := k.RootHistoryCount.Next(ctx)
if err != nil {
return errorsmod.Wrap(types.ErrInvalidCommitment, "failed to allocate root history index")
}
if err := k.RootHistory.Set(ctx, rootHistoryIndex, rootHex); err != nil {
return errorsmod.Wrap(types.ErrInvalidCommitment, "failed to store root history")
}
return nil
}