Files
sonr/x/did/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

423 lines
12 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/did/types"
)
// PermissionValidator wraps UCAN verifier for DID-specific permission validation
type PermissionValidator struct {
verifier *ucan.Verifier
keeper Keeper
permissions *types.UCANPermissionRegistry
}
// NewPermissionValidator creates a new DID permission validator
func NewPermissionValidator(keeper Keeper) *PermissionValidator {
didResolver := &DIDKeyResolver{keeper: keeper}
verifier := ucan.NewVerifier(didResolver)
return &PermissionValidator{
verifier: verifier,
keeper: keeper,
permissions: types.NewUCANPermissionRegistry(),
}
}
// NewPermissionValidatorWithVerifier creates a new DID permission validator with custom verifier (for testing)
func NewPermissionValidatorWithVerifier(
keeper Keeper,
verifier *ucan.Verifier,
) *PermissionValidator {
return &PermissionValidator{
verifier: verifier,
keeper: keeper,
permissions: types.NewUCANPermissionRegistry(),
}
}
// ValidatePermission validates UCAN token for DID operation
func (pv *PermissionValidator) ValidatePermission(
ctx context.Context,
tokenString string,
did string,
operation types.DIDOperation,
) 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 DID
resourceURI := pv.buildResourceURI(did)
// 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
}
// ValidateControllerPermission validates UCAN token for controller-specific DID operations
func (pv *PermissionValidator) ValidateControllerPermission(
ctx context.Context,
tokenString string,
did string,
controllerAddress string,
operation types.DIDOperation,
) 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 DID
resourceURI := pv.buildResourceURI(did)
// Verify UCAN token with controller caveat validation
token, err := pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
// Additional controller validation
if err := pv.validateControllerCaveat(token, did, controllerAddress); err != nil {
return fmt.Errorf("controller validation failed: %w", err)
}
return nil
}
// ValidateWebAuthnDelegation validates UCAN token for WebAuthn-delegated operations
func (pv *PermissionValidator) ValidateWebAuthnDelegation(
ctx context.Context,
tokenString string,
did string,
credentialID string,
operation types.DIDOperation,
) 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 DID
resourceURI := pv.buildResourceURI(did)
// Verify UCAN token
token, err := pv.verifier.VerifyCapability(
ctx,
tokenString,
resourceURI,
capabilities,
)
if err != nil {
return fmt.Errorf("UCAN validation failed: %w", err)
}
// Additional WebAuthn validation
if err := pv.validateWebAuthnDelegation(token, did, credentialID); err != nil {
return fmt.Errorf("WebAuthn delegation validation failed: %w", err)
}
return nil
}
// ValidateCredentialOperation validates UCAN token for credential operations
func (pv *PermissionValidator) ValidateCredentialOperation(
ctx context.Context,
tokenString string,
issuerDID string,
subjectDID string,
operation types.DIDOperation,
) error {
// For credential operations, validate against issuer DID
return pv.ValidatePermission(ctx, tokenString, issuerDID, operation)
}
// 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
// validateControllerCaveat validates that the token has proper controller authorization
func (pv *PermissionValidator) validateControllerCaveat(
token *ucan.Token,
did string,
controllerAddress string,
) error {
// Check each attenuation for controller caveats
for _, att := range token.Attenuations {
if att.Resource.GetURI() == pv.buildResourceURI(did) {
// Check if this is a DID capability with controller caveat
if didCapability, ok := att.Capability.(*ucan.DIDCapability); ok {
return pv.validateDIDControllerCaveat(didCapability, controllerAddress)
}
}
}
// If no specific controller caveat found, check if token issuer is the controller
return pv.validateTokenIssuerAsController(token, controllerAddress)
}
// validateDIDControllerCaveat validates controller-specific DID capability caveats
func (pv *PermissionValidator) validateDIDControllerCaveat(
capability *ucan.DIDCapability,
controllerAddress string,
) error {
// Check for controller caveat
hasControllerCaveat := false
for _, caveat := range capability.Caveats {
if caveat == "controller" {
hasControllerCaveat = true
break
}
}
if !hasControllerCaveat {
return nil // No controller caveat, proceed with normal validation
}
// Validate controller metadata
if capability.Metadata == nil {
return fmt.Errorf("missing controller metadata for controller caveat")
}
allowedController, exists := capability.Metadata["controller"]
if !exists {
return fmt.Errorf("missing controller address in capability metadata")
}
if allowedController != controllerAddress {
return fmt.Errorf(
"controller address mismatch: expected %s, got %s",
allowedController,
controllerAddress,
)
}
return nil
}
// validateTokenIssuerAsController validates that the token issuer is the controller
func (pv *PermissionValidator) validateTokenIssuerAsController(
token *ucan.Token,
controllerAddress string,
) error {
// For now, we accept any valid token issuer as a potential controller
// In a more sophisticated implementation, we could:
// 1. Resolve the issuer DID to get its controller address
// 2. Validate that the controller address matches
// 3. Check delegation chains for proper authorization
if token.Issuer == "" {
return fmt.Errorf("token issuer is required for controller validation")
}
return nil
}
// validateWebAuthnDelegation validates WebAuthn-specific delegation
func (pv *PermissionValidator) validateWebAuthnDelegation(
token *ucan.Token,
did string,
credentialID string,
) error {
// Find the relevant attenuation for this DID
for _, att := range token.Attenuations {
if att.Resource.GetURI() == pv.buildResourceURI(did) {
// Validate WebAuthn delegation capability
if err := types.ValidateWebAuthnDelegation(att.Capability, credentialID); err != nil {
return err
}
return nil
}
}
return fmt.Errorf("no matching attenuation found for DID %s", did)
}
// Helper methods
// buildResourceURI constructs DID resource URI
func (pv *PermissionValidator) buildResourceURI(did string) string {
return fmt.Sprintf("did:%s", pv.extractDIDPattern(did))
}
// extractDIDPattern extracts the method and subject from a full DID
func (pv *PermissionValidator) extractDIDPattern(did string) string {
// Remove "did:" prefix if present
if len(did) > 4 && did[:4] == "did:" {
return did[4:]
}
return did
}
// CreateAttenuation creates a UCAN attenuation for DID operations
func (pv *PermissionValidator) CreateAttenuation(
actions []string,
did string,
caveats []string,
) ucan.Attenuation {
didPattern := pv.extractDIDPattern(did)
return pv.permissions.CreateDIDAttenuation(actions, didPattern, caveats)
}
// CreateControllerAttenuation creates a controller-specific UCAN attenuation
func (pv *PermissionValidator) CreateControllerAttenuation(
actions []string,
did string,
controllerAddress string,
) ucan.Attenuation {
didPattern := pv.extractDIDPattern(did)
return pv.permissions.CreateControllerAttenuation(actions, didPattern, controllerAddress)
}
// CreateWebAuthnDelegationAttenuation creates a WebAuthn delegation attenuation
func (pv *PermissionValidator) CreateWebAuthnDelegationAttenuation(
actions []string,
did string,
credentialID string,
) ucan.Attenuation {
didPattern := pv.extractDIDPattern(did)
return pv.permissions.CreateWebAuthnDelegationAttenuation(actions, didPattern, credentialID)
}
// DIDKeyResolver implements ucan.DIDResolver for DID module
type DIDKeyResolver struct {
keeper Keeper
}
// ResolveDIDKey resolves DID to public key for UCAN verification
func (r *DIDKeyResolver) ResolveDIDKey(ctx context.Context, did string) (keys.DID, error) {
doc, err := r.keeper.GetDIDDocument(ctx, did)
if err != nil {
return keys.DID{}, fmt.Errorf("failed to resolve DID: %w", err)
}
// Extract verification method for signature verification
if len(doc.VerificationMethod) == 0 {
return keys.DID{}, fmt.Errorf("no verification methods found in DID document")
}
// Use the first verification method to parse the DID key
verificationMethod := doc.VerificationMethod[0]
if verificationMethod == nil {
return keys.DID{}, fmt.Errorf("verification method is nil")
}
// If the DID document ID is a did:key, parse it directly
if len(doc.Id) > 8 && doc.Id[:8] == "did:key:" {
didKey, err := keys.Parse(doc.Id)
if err != nil {
return keys.DID{}, fmt.Errorf("failed to parse did:key: %w", err)
}
return didKey, nil
}
// For other DID methods (like did:sonr), extract public key from verification method
return r.extractKeyFromVerificationMethod(verificationMethod)
}
// extractKeyFromVerificationMethod extracts a DID key from a verification method
func (r *DIDKeyResolver) extractKeyFromVerificationMethod(
vm *types.VerificationMethod,
) (keys.DID, error) {
// Try different public key formats
if vm.PublicKeyMultibase != "" {
// Convert multibase to did:key format
didKeyString := fmt.Sprintf("did:key:%s", vm.PublicKeyMultibase)
return keys.Parse(didKeyString)
}
if vm.PublicKeyBase58 != "" {
// Try to parse base58 key directly
didKeyString := fmt.Sprintf("did:key:z%s", vm.PublicKeyBase58)
return keys.Parse(didKeyString)
}
if vm.PublicKeyJwk != "" {
// For JWK format, we'd need to parse the JSON and extract the key
// This is more complex and would require JWK parsing
return keys.DID{}, fmt.Errorf(
"JWK public key format not yet supported for UCAN verification",
)
}
// Check for WebAuthn credential
if vm.WebauthnCredential != nil && vm.WebauthnCredential.CredentialId != "" {
// For WebAuthn credentials, we need to create a pseudo-DID key
// This is a simplified approach - in practice, you might want to use
// the actual WebAuthn public key for verification
return keys.DID{}, fmt.Errorf(
"WebAuthn credential keys require special handling for UCAN verification",
)
}
return keys.DID{}, fmt.Errorf("no supported public key format found in verification method")
}
// Gasless transaction support
// SupportsGaslessTransaction checks if a UCAN token supports gasless transactions
func (pv *PermissionValidator) SupportsGaslessTransaction(
ctx context.Context,
tokenString string,
did string,
operation types.DIDOperation,
) (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)
}
resourceURI := pv.buildResourceURI(did)
// 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
}