state.rs 3.4 KB

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