state.rs 3.4 KB

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