state.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
  2. use darkfi::crypto::{
  3. coin::Coin,
  4. constants::MERKLE_DEPTH,
  5. keypair::{PublicKey, SecretKey},
  6. merkle_node::MerkleNode,
  7. nullifier::Nullifier,
  8. };
  9. use super::transfer;
  10. use crate::note::EncryptedNote2;
  11. type MerkleTree = BridgeTree<MerkleNode, MERKLE_DEPTH>;
  12. pub struct OwnCoin {
  13. pub coin: Coin,
  14. pub note: transfer::wallet::Note,
  15. pub leaf_position: incrementalmerkletree::Position,
  16. }
  17. pub struct WalletCache {
  18. // Normally this would be a HashMap, but SecretKey is not Hash-able
  19. cache: Vec<(SecretKey, Vec<OwnCoin>)>,
  20. }
  21. impl WalletCache {
  22. pub fn new() -> Self {
  23. Self { cache: Vec::new() }
  24. }
  25. /// Must be called at the start to begin tracking received coins for this secret.
  26. pub fn track(&mut self, secret: SecretKey) {
  27. self.cache.push((secret, Vec::new()));
  28. }
  29. /// Get all coins received by this secret key
  30. /// track() must be called on this secret before calling this or the function will panic.
  31. pub fn get_received(&mut self, secret: &SecretKey) -> Vec<OwnCoin> {
  32. for (other_secret, own_coins) in self.cache.iter_mut() {
  33. if *secret == *other_secret {
  34. // clear own_coins vec, and return current contents
  35. return std::mem::replace(own_coins, Vec::new())
  36. }
  37. }
  38. panic!("you forget to track() this secret!");
  39. }
  40. pub fn try_decrypt_note(
  41. &mut self,
  42. coin: Coin,
  43. ciphertext: EncryptedNote2,
  44. tree: &mut MerkleTree,
  45. ) {
  46. // Loop through all our secret keys...
  47. for (secret, own_coins) in self.cache.iter_mut() {
  48. // .. attempt to decrypt the note ...
  49. if let Ok(note) = ciphertext.decrypt(secret) {
  50. let leaf_position = tree.witness().expect("coin should be in tree");
  51. own_coins.push(OwnCoin { coin, note, leaf_position });
  52. }
  53. }
  54. }
  55. }
  56. /// The state machine, held in memory.
  57. pub struct State {
  58. /// The entire Merkle tree state
  59. pub tree: MerkleTree,
  60. /// List of all previous and the current Merkle roots.
  61. /// This is the hashed value of all the children.
  62. pub merkle_roots: Vec<MerkleNode>,
  63. /// Nullifiers prevent double spending
  64. pub nullifiers: Vec<Nullifier>,
  65. /// Public key of the cashier
  66. pub cashier_signature_public: PublicKey,
  67. /// Public key of the faucet
  68. pub faucet_signature_public: PublicKey,
  69. pub wallet_cache: WalletCache,
  70. }
  71. impl State {
  72. pub fn new(
  73. cashier_signature_public: PublicKey,
  74. faucet_signature_public: PublicKey,
  75. ) -> Box<Self> {
  76. Box::new(Self {
  77. tree: MerkleTree::new(100),
  78. merkle_roots: vec![],
  79. nullifiers: vec![],
  80. cashier_signature_public,
  81. faucet_signature_public,
  82. wallet_cache: WalletCache::new(),
  83. })
  84. }
  85. pub fn is_valid_cashier_public_key(&self, public: &PublicKey) -> bool {
  86. public == &self.cashier_signature_public
  87. }
  88. pub fn is_valid_faucet_public_key(&self, public: &PublicKey) -> bool {
  89. public == &self.faucet_signature_public
  90. }
  91. pub fn is_valid_merkle(&self, merkle_root: &MerkleNode) -> bool {
  92. self.merkle_roots.iter().any(|m| m == merkle_root)
  93. }
  94. pub fn nullifier_exists(&self, nullifier: &Nullifier) -> bool {
  95. self.nullifiers.iter().any(|n| n == nullifier)
  96. }
  97. }