theultraman20

blockchain.rs

Nov 7th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.41 KB | None | 0 0
  1. use crate::tx::*;
  2. use std::collections::HashMap;
  3. use ethnum::*;
  4. use serde::{Deserialize, Serialize};
  5. use sha3::*;
  6. use k256::{
  7.     ecdsa::{SigningKey, signature::Verifier, VerifyingKey, Signature, signature::Signer},
  8.     SecretKey,
  9.     PublicKey,
  10. };
  11.  
  12.  
  13. //#[derive(Serialize, Deserialize, Debug)]
  14. pub struct BlockChain {
  15.     blocks: Vec<Block>,
  16.     utxo_set: HashMap<Outpoint, TxOutput>,
  17. }
  18.  
  19. //#[derive(Serialize, Deserialize, Debug)]
  20. pub struct Block {
  21.     prev_hash: u256,
  22.     nonce: u256,
  23.     txs: Vec<Tx>,
  24. }
  25.  
  26. //lol XD
  27. //const TITTIES: Decimal = dec!(7177135);
  28. //const BOOBIES: Decimal = dec!(8008135);
  29. //consider using these constants in the project somehow
  30.  
  31. impl BlockChain {
  32.     const START_SUPPLY: u64 = 420 * 1_000_000;
  33.     const TOTAL_SUPPLY: u64 = 69 * 1_000_000;
  34.  
  35.     pub fn verify_blockchain(&self) -> bool {
  36.         //keep track of balances
  37.         let mut input_total: u64 = 0;
  38.         let mut output_total: u64 = 0;
  39.  
  40.         for block in &self.blocks {
  41.             //verify hashes
  42.             for tx in &block.txs {
  43.                 for (i, input) in tx.inputs.iter().enumerate() {
  44.                     let Some(prev_out) = self.utxo_set.get(&input.prev_out) else {
  45.                         //uh oh...
  46.                         return false;
  47.                     };
  48.  
  49.                     if !Self::verify_sig(input.signature, &prev_out.spender, &tx, i as u64) {
  50.                         //nice try hackers
  51.                         return false;
  52.                     }
  53.  
  54.                     input_total += input.amount;
  55.                 }
  56.  
  57.                 for output in &tx.outputs {
  58.                     output_total += output.amount;
  59.                 }
  60.  
  61.                 if input_total < output_total {
  62.                     return false;
  63.                 }
  64.             }
  65.         }
  66.  
  67.         true
  68.     }
  69.  
  70.     //check that signature equals the hash of tall transactions
  71.     //and the transaction index combined, all signed by the spender
  72.     fn verify_sig(sig: Signature, predicate: &TxPredicate, tx: &Tx, idx: u64) -> bool {
  73.         //better hasher for cryptographic applications
  74.         let mut hasher = Sha3_256::new();
  75.         hasher.update(tx.as_bytes().as_slice());
  76.         hasher.update(idx.to_be_bytes());
  77.         let message = hasher.finalize();
  78.  
  79.         let verifying_key = VerifyingKey::from(predicate.unwrap_key());
  80.         verifying_key.verify(&message[..], &sig).is_ok()
  81.     }
  82. }
  83.  
Add Comment
Please, Sign In to add comment