Agent skill
midnight-core-concepts:data-models
Use when asking about UTXO vs account models, ledger tokens, shielded/unshielded tokens, nullifiers, coins, balances, or choosing between token paradigms in Midnight.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/data-models
SKILL.md
Midnight Data Models
Midnight supports two distinct token paradigms: UTXO-based ledger tokens and account-based contract tokens. Choose based on privacy requirements and use case complexity.
Quick Decision Guide
| Requirement | Use UTXO (Ledger Tokens) | Use Account (Contract Tokens) |
|---|---|---|
| Privacy critical | Yes - independent, shieldable | No - balances visible |
| Parallel processing | Yes - no ordering deps | No - sequential nonce |
| Simple transfers | Yes | Overkill |
| Complex DeFi logic | Limited | Yes |
| Gaming state machines | No | Yes |
| Governance/delegation | No | Yes |
UTXO Model (Ledger Tokens)
UTXO = Unspent Transaction Output. Each token is a discrete digital coin that must be spent entirely.
Core Mechanics
Creation → Existence → Consumption → Prevention of Reuse
- Creation: UTXO born with value, owner, cryptographic commitment
- Existence: Queryable in active UTXO set
- Consumption: Entire UTXO spent in transaction (change returned as new UTXO)
- Prevention: Nullifier added to global set, prevents double-spend
Nullifier Innovation
Unlike Bitcoin's spent markers, Midnight uses nullifiers:
nullifier = Hash(UTXO_commitment, ownerSecret)
Privacy benefit: Nullifier reveals nothing about which UTXO was spent. The nullifier can be computed without exposing the original UTXO identity.
Shielded vs Unshielded
Each UTXO independently chooses privacy level:
- Shielded: Commitment hidden, value/owner private
- Unshielded: Value visible for regulatory compliance
// Receiving shielded tokens
receive coins: Coin[];
// Sending tokens (can be shielded or unshielded)
send value: QualifiedValue, to: Address;
Account Model (Contract Tokens)
Maintain address-to-balance mappings within Compact contracts. Similar to ERC-20.
When to Use
- Complex DeFi state machines requiring intricate interactions
- Gaming systems with stateful game logic
- Governance tokens with delegation mechanics
- Social tokens tracking relationships
Trade-offs
| Aspect | Account Model Limitation |
|---|---|
| Privacy | Every transaction visible forever |
| Ordering | Nonce creates sequential dependency |
| MEV | Mempool visibility enables front-running |
| Scalability | Redundant computation on every node |
Ledger Structure
Midnight's ledger has two components:
1. Zswap State
- Merkle tree of coin commitments
- Free slot index
- Nullifier set
- Valid past Merkle roots
2. Contract Map
- Associates contract addresses with states
- Contains public and private state components
Token Types
Token types are 256-bit collision-resistant hashes:
- Native token: Predefined zero value
- Custom tokens: Hash of contract address + domain separator
// Issue custom token from contract
// Type = Hash(contractAddress, domainSeparator)
Practical Application
Choose UTXO When:
- Users need transaction privacy
- High throughput required (parallel processing)
- Simple value transfers dominate
- Regulatory compliance via selective disclosure (viewing keys)
Choose Account When:
- Complex state logic required
- Tokens interact with sophisticated contract logic
- Privacy is secondary to functionality
- Integration with existing DeFi patterns
References
For detailed technical information:
references/utxo-mechanics.md- Complete UTXO lifecycle, nullifier computationreferences/ledger-structure.md- Zswap state internals, Merkle tree details
Examples
Working Compact patterns:
examples/token-handling.compact- Receiving and sending tokens
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?