Files
sonr/x/dex/keeper/permission_validator.go
T
40eadc995e Feat/1285 es ucan formatting (#1302)
* feat: Add Enclave Usage Examples

* feat(es/ucan): Add comprehensive integration tests

- Create integration.test.ts with full UCAN token lifecycle testing
- Cover end-to-end token creation, parsing, and validation
- Test capability attenuation and delegation chains
- Validate multi-algorithm support and timestamp scenarios
- Implement error recovery and performance test scenarios

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>

* No commit suggestions generated

* No commit suggestions generated

* chore: Remove migrated components and add migration documentation

Removed all code and references for components that have been moved to separate repositories:

**Moved to sonr-io/hway:**
- bridge/ - HTTP service with OAuth2/OIDC/WebAuthn handlers
- cmd/hway/ - Highway service binary
- internal/migrations/ - PostgreSQL schema migrations

**Moved to sonr-io/motr:**
- cmd/motr/ - Motor worker service (WASM vault operations)
- cmd/vault/ - Vault CLI tool
- crypto/ - Comprehensive cryptographic library
- packages/ - TypeScript SDK packages (es, sdk, ui, com, pkl)
- web/auth/ - Authentication web application
- web/dash/ - Dashboard web application

**Updated Configuration:**
- Makefile: Removed build/test/release targets for moved components
- CLAUDE.md: Simplified to focus on core blockchain components
- devbox.json: Removed scripts for moved services
- docker-compose.yml: Removed hway, postgres, redis, auth, dash services
- .github/scopes.yml: Removed CI scopes for migrated components
- .goreleaser.yml: Updated release configuration

**Added Migration Documentation:**
- MIGRATE_HWAY.md: Comprehensive Highway service architecture and migration guide
- MIGRATE_MOTR.md: Comprehensive Motor/Worker/Vault architecture and migration guide

These migration documents provide complete context for setting up the new repositories including architecture diagrams, component breakdowns, API documentation, and migration checklists.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* No commit suggestions generated

* chore: Remove contracts references and documentation

Removed all references to the contracts directory that was migrated to a separate repository.

**Changes:**
- .gitignore: Removed contract-specific ignore patterns for DAO and wSNR contracts
- .gitignore: Removed hway and motr binary references (already migrated)
- .rgignore: Removed contracts, chains, and crypto directory references
- docs/reference/contracts/: Removed DAO.mdx and wSNR.mdx documentation files

This completes the cleanup of migrated components from the repository.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: add crypto library migration documentation

Added comprehensive migration documentation for the crypto library that was
moved to sonr-io/crypto repository. This documentation provides complete context
for understanding the cryptographic primitives and protocols used throughout
the Sonr ecosystem.

## Key Documentation Added

### MIGRATE_CRYPTO.md
Complete documentation of the crypto library covering:

**Core Cryptographic Primitives**
- Elliptic curve implementations (Ed25519, Secp256k1, P-256, BLS12-381, Pallas/Vesta)
- Native curve arithmetic with optimized field operations
- Pairing-friendly curves for BLS signatures

**Multi-Party Computation (MPC)**
- MPC enclave for vault key generation and management
- Threshold cryptography (TECDSA, TED25519 with FROST protocol)
- Distributed Key Generation (DKG) via Gennaro and FROST protocols
- Secret sharing schemes (Shamir, Feldman VSS, Pedersen VSS)

**Digital Signature Schemes**
- BLS signatures with aggregation support
- BBS+ signatures for selective disclosure
- Schnorr signatures (standard and Mina/NEM variants)
- ECDSA with deterministic nonce generation

**Zero-Knowledge Proofs**
- Bulletproofs for range proofs
- Inner Product Arguments (IPA)
- Batch verification support

**Advanced Cryptographic Protocols**
- Cryptographic accumulators for set membership proofs
- Paillier homomorphic encryption
- Oblivious Transfer (OT) protocols
- Verifiable Random Functions (VRF)

**Key Management & Identity**
- DID key management with multi-chain support
- Multi-algorithm public key handling
- Wallet address derivation (Bitcoin, Ethereum, Cosmos, Solana, etc.)

**UCAN Integration**
- User-Controlled Authorization Networks
- Capability delegation and attenuation
- JWT-based capability tokens
- MPC-enabled UCAN signing

**Security Utilities**
- AEAD encryption (AES-GCM, AES-SIV)
- Argon2 key derivation
- ECIES encryption
- Secure memory handling

### MIGRATE_MOTR.md Updates
Updated Motor migration documentation to clarify that the crypto library
is now a separate external dependency at github.com/sonr-io/crypto v1.0.1

## Repository Context

The crypto library has been successfully migrated to its own repository
and is published as a Go module. It serves as the foundational cryptographic
layer for:
- Sonr blockchain (snrd) - DID signatures, vault operations
- Highway service (hway) - UCAN token signing, WebAuthn
- Motor/Worker (motr) - MPC vault operations, threshold signatures

## Integration Impact

All Sonr ecosystem components now depend on the external crypto library:
```go
require github.com/sonr-io/crypto v1.0.1
```

The migration enables independent versioning and maintenance of cryptographic
primitives while maintaining security and compatibility across the ecosystem.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* No commit suggestions generated

* No commit suggestions generated

* No commit suggestions generated

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 11:47:18 -04:00

331 lines
9.1 KiB
Go

package keeper
import (
"context"
"fmt"
"github.com/sonr-io/crypto/keys"
"github.com/sonr-io/crypto/ucan"
"github.com/sonr-io/sonr/x/dex/types"
)
// PermissionValidator wraps UCAN verifier for DEX-specific permission validation
type PermissionValidator struct {
verifier *ucan.Verifier
keeper Keeper
permissions *types.UCANPermissionRegistry
}
// NewPermissionValidator creates a new DEX permission validator
func NewPermissionValidator(keeper Keeper) *PermissionValidator {
didResolver := &DEXDIDResolver{keeper: keeper}
verifier := ucan.NewVerifier(didResolver)
return &PermissionValidator{
verifier: verifier,
keeper: keeper,
permissions: types.NewUCANPermissionRegistry(),
}
}
// ValidatePermission validates UCAN token for DEX operation
func (pv *PermissionValidator) ValidatePermission(
ctx context.Context,
tokenString string,
resourceType string,
resourceID string,
operation types.DEXOperation,
) error {
// Get required UCAN capabilities for the operation
capabilities, err := pv.permissions.GetRequiredUCANCapabilities(operation)
if err != nil {
return fmt.Errorf("failed to get required UCAN capabilities: %w", err)
}
// Build resource URI for DEX
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreateDEXResourceURI(resourceType, resourceID)
// Verify UCAN token grants required capabilities
_, err = pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
return nil
}
// ValidateSwapPermission validates UCAN token for swap operations
func (pv *PermissionValidator) ValidateSwapPermission(
ctx context.Context,
tokenString string,
poolID string,
amount string,
operation types.DEXOperation,
) error {
// Get required UCAN capabilities for the operation
capabilities, err := pv.permissions.GetRequiredUCANCapabilities(operation)
if err != nil {
return fmt.Errorf("failed to get required UCAN capabilities: %w", err)
}
// Build pool resource URI
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreatePoolResourceURI(poolID)
// Verify UCAN token
token, err := pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
// Additional amount validation
if err := pv.validateAmountConstraint(token, amount); err != nil {
return fmt.Errorf("amount constraint validation failed: %w", err)
}
return nil
}
// ValidateLiquidityPermission validates UCAN token for liquidity operations
func (pv *PermissionValidator) ValidateLiquidityPermission(
ctx context.Context,
tokenString string,
poolID string,
operation types.DEXOperation,
) error {
// Get required UCAN capabilities for the operation
capabilities, err := pv.permissions.GetRequiredUCANCapabilities(operation)
if err != nil {
return fmt.Errorf("failed to get required UCAN capabilities: %w", err)
}
// Build pool resource URI
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreatePoolResourceURI(poolID)
// Verify UCAN token
_, err = pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
return nil
}
// ValidateOrderPermission validates UCAN token for order operations
func (pv *PermissionValidator) ValidateOrderPermission(
ctx context.Context,
tokenString string,
orderID string,
operation types.DEXOperation,
) error {
// Get required UCAN capabilities for the operation
capabilities, err := pv.permissions.GetRequiredUCANCapabilities(operation)
if err != nil {
return fmt.Errorf("failed to get required UCAN capabilities: %w", err)
}
// Build order resource URI
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreateOrderResourceURI(orderID)
// Verify UCAN token
_, err = pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
return nil
}
// VerifyDelegationChain validates complete UCAN delegation chain
func (pv *PermissionValidator) VerifyDelegationChain(
ctx context.Context,
tokenString string,
) error {
return pv.verifier.VerifyDelegationChain(ctx, tokenString)
}
// Internal validation methods
// validateAmountConstraint validates amount constraints
func (pv *PermissionValidator) validateAmountConstraint(
token *ucan.Token,
amount string,
) error {
// For now, we'll accept all amounts
// In a real implementation, we'd check against maximum amounts
// specified in the token's attenuations
return nil
}
// validatePoolConstraint validates pool constraints
func (pv *PermissionValidator) validatePoolConstraint(
token *ucan.Token,
poolID string,
) error {
// Check if the token's resource matches the pool
for _, att := range token.Attenuations {
if simpleResource, ok := att.Resource.(*ucan.SimpleResource); ok {
// Check if resource matches pool pattern
if simpleResource.Scheme == "dex" {
expectedValue := fmt.Sprintf("pool:%s", poolID)
if simpleResource.Value == expectedValue || simpleResource.Value == "pool:*" {
return nil
}
}
}
}
return fmt.Errorf("no matching pool attenuation found for pool %s", poolID)
}
// Helper methods
// CreateAttenuation creates a UCAN attenuation for DEX operations
func (pv *PermissionValidator) CreateAttenuation(
actions []string,
resourceType string,
resourceID string,
) ucan.Attenuation {
return pv.permissions.CreateDEXAttenuation(actions, resourceType, resourceID)
}
// CreateAmountLimitedAttenuation creates an amount-limited UCAN attenuation
func (pv *PermissionValidator) CreateAmountLimitedAttenuation(
actions []string,
poolID string,
maxAmount string,
) ucan.Attenuation {
return pv.permissions.CreateAmountLimitedAttenuation(actions, poolID, maxAmount)
}
// CreatePoolRestrictedAttenuation creates a pool-restricted UCAN attenuation
func (pv *PermissionValidator) CreatePoolRestrictedAttenuation(
actions []string,
allowedPools []string,
) ucan.Attenuation {
return pv.permissions.CreatePoolRestrictedAttenuation(actions, allowedPools)
}
// DEXDIDResolver implements ucan.DIDResolver for DEX module
type DEXDIDResolver struct {
keeper Keeper
}
// ResolveDIDKey resolves DID to public key for UCAN verification
func (r *DEXDIDResolver) ResolveDIDKey(ctx context.Context, did string) (keys.DID, error) {
// For DEX module, we need to resolve DIDs from the DID module
// This would require cross-module keeper access
// Check if the DEX keeper has access to DID keeper
if r.keeper.didKeeper != nil {
didDoc, err := r.keeper.didKeeper.GetDIDDocument(ctx, did)
if err != nil {
return keys.DID{}, fmt.Errorf("failed to get DID document: %w", err)
}
if didDoc == nil {
return keys.DID{}, fmt.Errorf("DID document not found")
}
// Parse the DID string into a keys.DID
return keys.Parse(did)
}
return keys.DID{}, fmt.Errorf("DID resolver not available in DEX module")
}
// Gasless transaction support
// SupportsGaslessTransaction checks if a UCAN token supports gasless transactions
func (pv *PermissionValidator) SupportsGaslessTransaction(
ctx context.Context,
tokenString string,
poolID string,
operation types.DEXOperation,
) (bool, uint64, error) {
// Parse and verify the token
token, err := pv.verifier.VerifyToken(ctx, tokenString)
if err != nil {
return false, 0, fmt.Errorf("token verification failed: %w", err)
}
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreatePoolResourceURI(poolID)
// Check each attenuation for gasless support
for _, att := range token.Attenuations {
if att.Resource.GetURI() == resourceURI {
// Check if capability supports gasless transactions
if gaslessCapability, ok := att.Capability.(*ucan.GaslessCapability); ok {
if gaslessCapability.SupportsGasless() {
// Verify the capability grants the required operation
capabilities, err := pv.permissions.GetRequiredUCANCapabilities(operation)
if err != nil {
continue
}
if gaslessCapability.Grants(capabilities) {
return true, gaslessCapability.GetGasLimit(), nil
}
}
}
}
}
return false, 0, nil
}
// ValidateRateLimit checks if a UCAN token has rate limiting and if it's within limits
func (pv *PermissionValidator) ValidateRateLimit(
ctx context.Context,
tokenString string,
poolID string,
) (bool, uint64, uint64, error) {
// Parse and verify the token
token, err := pv.verifier.VerifyToken(ctx, tokenString)
if err != nil {
return false, 0, 0, fmt.Errorf("token verification failed: %w", err)
}
mapper := types.NewUCANCapabilityMapper()
resourceURI := mapper.CreatePoolResourceURI(poolID)
// Check each attenuation for rate limiting
for _, att := range token.Attenuations {
if att.Resource.GetURI() == resourceURI {
// Check if this is a gasless capability with limits
if gaslessCapability, ok := att.Capability.(*ucan.GaslessCapability); ok {
if gaslessCapability.AllowGasless && gaslessCapability.GasLimit > 0 {
// Use gas limit as a proxy for rate limiting
return true, gaslessCapability.GasLimit, 60, nil // 60 second window
}
}
}
}
return false, 0, 0, nil
}