This project is a simple implementation of a UTXO (Unspent Transaction Output) model in Rust, created for educational purposes. It demonstrates the fundamental concepts of a cryptocurrency, including wallet management, transaction signing, and UTXO-based accounting.
The project is divided into several modules, each handling a specific part of the UTXO model:
- Wallets: A
Walletholds a user's cryptographic keys and their unspent transaction outputs (UTXOs). - Key Pair: Each wallet has a
SigningKey(private key) and aVerifyingKey(public key).- The private key is used to sign transactions, proving ownership of the funds.
- The public key is used as the wallet's address and to verify signatures.
- UTXOs: The
utxosfield is a map where keys are UTXO IDs (unique strings) and values are the amount of "coins" in each UTXO. A wallet's total balance is the sum of all its UTXOs. - Nonce: A
nonce(number used once) is used to prevent replay attacks. Each transaction from a wallet increments its nonce.
- Creating "Coins": The
mintfunction simulates the creation of new coins. In a real blockchain, this would be part of a more complex process like mining. - UTXO Creation: When coins are minted, a new UTXO is created and added to a wallet. The UTXO's ID is generated by hashing the wallet's public key, the amount, and the wallet's current nonce, ensuring a unique ID for each new UTXO.
- Transaction Structure: A
Transactioncontains:id: A unique identifier for the transaction, generated by hashing its contents.sender: The public key of the sender.receiver: The public key of the receiver.amount: The amount to be transferred.nonce: The sender's nonce at the time of creation.signature: An optional cryptographic signature.
- Signing: To authorize a transaction, the sender signs the transaction
idwith their private key. This signature proves that the owner of the wallet initiated the transaction. - Verification: The
verifymethod checks if the signature is valid for the given transaction and the sender's public key. This ensures the transaction has not been tampered with. - Validation: Before processing, a transaction is validated to ensure two things:
- The sender has enough funds (their total balance from UTXOs is sufficient).
- The transaction's signature is valid.
- State Update: Once a transaction is validated, it is processed to update the state of the system.
- Spending UTXOs: The
process_transactionfunction gathers enough of the sender's UTXOs to cover the transaction amount. These UTXOs are "spent" and removed from the sender's wallet. - Creating New UTXOs:
- A new UTXO is created for the receiver with the transaction amount.
- If the sender's spent UTXOs had a total value greater than the transaction amount, a "change" UTXO is created and given back to the sender.
- Nonce Increment: The sender's nonce is incremented to ensure they cannot create another transaction with the same nonce.
- The
runfunction in the simulation module demonstrates the entire flow:- Creates two wallets, A and B.
- Mints new coins for Wallet A.
- Creates a transaction from Wallet A to Wallet B.
- Signs the transaction with Wallet A's private key.
- Validates the transaction.
- Processes the transaction, updating the balances of both wallets.
- Wallet Management: Create wallets with public/private key pairs for secure transactions.
- Cryptographic Transactions: Transactions are signed using the sender’s cryptographic key and verified to ensure they haven’t been tampered with.
- Transaction Validation: A validation function ensures that transactions are legitimate by checking for sufficient funds and a valid signature.
- Secure Key Generation: Uses
ed25519-dalekfor robust and secure key pair generation.
-
Clone the repository:
git clone <repository-url> cd utxo-rust
-
Run the application:
cargo run
-
Run the tests:
cargo test ``` # utxo-rust