The `x/dwn` module is the foundational engine of the Sonr ecosystem. It provides a comprehensive **Decentralized Web Node (DWN)** implementation that serves as the backbone for user-controlled data storage, protocol management, and secure vault operations. The module enables users to maintain sovereign control over their data while participating in a decentralized ecosystem.
A DWN is a personal data store that enables individuals to manage their data independently of centralized providers. Each user's DWN serves as their agent in the decentralized web, storing data, managing permissions, and executing protocols on their behalf.
### Records
Records are the fundamental unit of data storage in a DWN. Each record can:
- Store arbitrary data with optional encryption
- Be organized hierarchically using parent-child relationships
- Conform to specific protocols and schemas
- Be published for public access or kept private
### Protocols
Protocols define structured ways for applications to interact with DWN data. They specify:
- Data schemas for validation
- Permission models
- Interaction patterns between different parties
### Permissions
The DWN uses a capability-based permission system where:
- Permissions are granted as signed tokens (JWTs)
- Access can be scoped to specific interfaces, methods, protocols, or records
- Permissions can be delegated and revoked
### Vaults
Vaults provide secure, enclave-based key management enabling:
- Hardware-backed key generation and storage
- Secure transaction signing without exposing private keys
- Multi-party computation capabilities
- WebAssembly-based secure execution environment
- Multi-chain transaction building for Cosmos SDK and EVM networks
- BIP44 HD wallet address derivation with configurable coin types
#### VaultKeeper Implementation
The VaultKeeper provides a comprehensive interface for managing cryptographic vaults within the DWN module. It implements the following core functionality:
- **Vault Creation**: Creates new vaults with enclave-based key generation using WebAssembly plugins
- **State Management**: Manages vault states including ownership, public keys, and enclave data
- **Secure Operations**: Provides signing and transaction broadcasting capabilities through secure enclaves
- **Refresh Mechanisms**: Handles vault state refresh with configurable intervals for security
- **Verification**: Cryptographic signature verification for vault operations
- **Multi-Chain Support**: Transaction building for both Cosmos SDK and EVM networks using pkg/txns
- **Address Derivation**: BIP44 HD wallet address derivation for multiple blockchain networks
-`GetVaultState(ctx, vaultID)`: Retrieves vault state by ID
-`ListVaultsByOwner(ctx, owner)`: Lists all vaults owned by a specific address
**Actor System Integration:**
-`SpawnVault(opts...)`: Creates vault actors with configuration options
-`SpawnSimpleVault()`: Creates vaults without database persistence (testing)
-`SpawnSimpleVaultNamed(name)`: Creates named vaults for testing scenarios
All methods return appropriate response types and handle error conditions including ownership verification, parameter validation, and enclave communication failures.
**Transaction Building and Address Derivation:**
The VaultKeeper integrates with the `pkg/txns` package to provide enhanced multi-chain transaction capabilities:
-`BuildCosmosTransaction(params)`: Builds unsigned Cosmos SDK transactions with proper fee estimation
-`BuildEVMTransaction(params)`: Builds unsigned Ethereum/EVM transactions with gas estimation
-`CreateVaultSigner(vaultID)`: Creates MPC-based signers for secure transaction signing
-`EstimateTransactionFee(txType, params)`: Estimates transaction fees for both Cosmos and EVM networks
-`DeriveWalletAddresses(did, salt)`: Derives both Cosmos and EVM addresses using BIP44 HD wallet derivation
**Address Derivation Features:**
- **Multi-Chain Support**: Generates addresses for both Cosmos SDK (Bech32) and EVM (0x) formats
- **Deterministic Derivation**: Uses DID and salt for reproducible address generation
- **Configurable Coin Types**: Supports different coin types for various blockchain networks
- **BIP44 Compliance**: Follows BIP44 hierarchical deterministic wallet standard
The DWN module emits comprehensive typed events for all state-changing operations. These events provide a detailed audit trail and enable efficient tracking of DWN-related activities.
### Event Types
#### 1. EventRecordWritten
- **Emitted**: When a record is written to DWN
- **Fields**:
-`record_id`: Unique record identifier
-`target`: Target DID
-`protocol`: Protocol URI defining record structure
-`schema`: Schema URI for record validation
-`data_cid`: Content Identifier for stored data
-`data_size`: Size of record data in bytes
-`encrypted`: Whether data is encrypted
-`block_height`: Block number of record creation
#### 2. EventRecordDeleted
- **Emitted**: When a record is deleted from DWN
- **Fields**:
-`record_id`: Unique record identifier
-`target`: Target DID
-`deleter`: Address performing deletion
-`block_height`: Block number of deletion
#### 3. EventProtocolConfigured
- **Emitted**: When a protocol is configured in a DWN
- **Fields**:
-`target`: Target DID
-`protocol_uri`: Unique protocol identifier
-`published`: Public visibility flag
-`block_height`: Block number of configuration
#### 4. EventPermissionGranted
- **Emitted**: When a permission is granted
- **Fields**:
-`permission_id`: Unique permission identifier
-`grantor`: DID granting permission
-`grantee`: DID receiving permission
-`interface_name`: Targeted interface
-`method`: Specific method being permitted
-`expires_at`: Expiration timestamp
-`block_height`: Block number of permission grant
#### 5. EventPermissionRevoked
- **Emitted**: When a permission is revoked
- **Fields**:
-`permission_id`: Unique permission identifier
-`revoker`: DID revoking the permission
-`block_height`: Block number of revocation
#### 6. EventVaultCreated
- **Emitted**: When a new vault is created
- **Fields**:
-`vault_id`: Unique vault identifier
-`owner`: Vault owner address
-`public_key`: Vault's public key
-`block_height`: Block number of vault creation
#### 7. EventVaultKeysRotated
- **Emitted**: When vault keys are rotated
- **Fields**:
-`vault_id`: Unique vault identifier
-`owner`: Vault owner address
-`new_public_key`: New public key
-`rotation_height`: Block number of key rotation
-`block_height`: Block number of rotation event
### Event Indexing and Querying
Events can be queried and filtered using CometBFT WebSocket or standard blockchain explorers. Example queries:
```bash
# Query all record write events
tm.event='Tx' AND dwn.v1.EventRecordWritten.record_id EXISTS