Skip to content

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.

Notifications You must be signed in to change notification settings

dymchenkko/utxo-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UTXO Rust

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.

Project Structure and Concepts

The project is divided into several modules, each handling a specific part of the UTXO model:

1. Wallet Management (src/wallet.rs)

  • Wallets: A Wallet holds a user's cryptographic keys and their unspent transaction outputs (UTXOs).
  • Key Pair: Each wallet has a SigningKey (private key) and a VerifyingKey (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 utxos field 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.

2. Minting (src/minting.rs)

  • Creating "Coins": The mint function 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.

3. Transactions (src/transaction.rs)

  • Transaction Structure: A Transaction contains:
    • 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 id with their private key. This signature proves that the owner of the wallet initiated the transaction.
  • Verification: The verify method 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:
    1. The sender has enough funds (their total balance from UTXOs is sufficient).
    2. The transaction's signature is valid.

4. Transaction Processing (src/processing.rs)

  • State Update: Once a transaction is validated, it is processed to update the state of the system.
  • Spending UTXOs: The process_transaction function 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.

5. Simulation (src/simulation.rs)

  • The run function in the simulation module demonstrates the entire flow:
    1. Creates two wallets, A and B.
    2. Mints new coins for Wallet A.
    3. Creates a transaction from Wallet A to Wallet B.
    4. Signs the transaction with Wallet A's private key.
    5. Validates the transaction.
    6. Processes the transaction, updating the balances of both wallets.

Features

  • 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-dalek for robust and secure key pair generation.

Getting Started

Prerequisites

Running the Project

  1. Clone the repository:

    git clone <repository-url>
    cd utxo-rust
  2. Run the application:

    cargo run
  3. Run the tests:

    cargo test
    ``` # utxo-rust

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages