walletdb.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use crate::crypto::{coin::Coin, merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note};
  2. use crate::serial;
  3. use crate::serial::{deserialize, serialize, Decodable, Encodable};
  4. use crate::Error;
  5. use crate::Result;
  6. use async_std::sync::Arc;
  7. use ff::Field;
  8. use log::*;
  9. use rand::rngs::OsRng;
  10. use rusqlite::{named_params, Connection, OpenFlags};
  11. use std::path::{Path, PathBuf};
  12. pub struct WalletDB {
  13. pub path: PathBuf,
  14. pub secrets: Vec<jubjub::Fr>,
  15. pub cashier_secrets: Vec<jubjub::Fr>,
  16. pub own_coins: Vec<(Coin, Note, jubjub::Fr, IncrementalWitness<MerkleNode>)>,
  17. pub cashier_public: jubjub::SubgroupPoint,
  18. //conn: Arc<Connection>,
  19. }
  20. impl WalletDB {
  21. pub fn new(wallet: &str) -> Result<Self> {
  22. let path = Self::create_path(wallet)?;
  23. let conn = Connection::open_with_flags(&path, OpenFlags::SQLITE_OPEN_CREATE)?;
  24. let contents = include_str!("../../res/schema.sql");
  25. let cashier_secret = jubjub::Fr::random(&mut OsRng);
  26. let secret = jubjub::Fr::random(&mut OsRng);
  27. let _public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
  28. let cashier_public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * cashier_secret;
  29. match conn.execute_batch(&contents) {
  30. Ok(v) => println!("Database initalized successfully {:?}", v),
  31. Err(err) => println!("Error: {}", err),
  32. };
  33. Ok(Self {
  34. path,
  35. own_coins: vec![],
  36. cashier_secrets: vec![cashier_secret.clone()],
  37. secrets: vec![secret.clone()],
  38. cashier_public,
  39. //conn,
  40. })
  41. }
  42. pub async fn put_own_coins(&self) -> Result<()> {
  43. let note = &self.own_coins[0].1;
  44. let coin = self.get_value_serialized(&self.own_coins[0].0.repr).await?;
  45. let serial = self.get_value_serialized(&note.serial).await?;
  46. let coin_blind = self.get_value_serialized(&note.coin_blind).await?;
  47. let valcom_blind = self.get_value_serialized(&note.valcom_blind).await?;
  48. let value = self.get_value_serialized(&note.value).await?;
  49. let conn = Connection::open(&self.path)?;
  50. // witness deserialization not implemented
  51. conn.execute(
  52. "INSERT INTO coins(coin, serial, value, coin_blind, valcom_blind, witness, key_id)
  53. VALUES (NULL, :coin, :serial, :value, :coin_blind, :valcom_blind, :witness, :key_id)",
  54. named_params! {
  55. ":coin": coin,
  56. ":serial": serial,
  57. ":value": value,
  58. ":coin_blind": coin_blind,
  59. ":valcom_blind": valcom_blind,
  60. //":privkey": privkey,
  61. //":pubkey": pubkey
  62. },
  63. )?;
  64. Ok(())
  65. }
  66. fn create_path(wallet: &str) -> Result<PathBuf> {
  67. let mut path = dirs::home_dir()
  68. .ok_or(Error::PathNotFound)?
  69. .as_path()
  70. .join(".config/darkfi/");
  71. path.push(wallet);
  72. debug!(target: "walletdb", "CREATE PATH {:?}", path);
  73. Ok(path)
  74. }
  75. pub async fn key_gen(&self) -> (Vec<u8>, Vec<u8>) {
  76. debug!(target: "key_gen", "Generating keys...");
  77. let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
  78. let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
  79. let pubkey = serial::serialize(&public);
  80. let privkey = serial::serialize(&secret);
  81. (pubkey, privkey)
  82. }
  83. pub async fn put_keypair(&self, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
  84. //debug!(target: "key_gen", "Generating keys...");
  85. let conn = Connection::open(&self.path)?;
  86. //debug!(target: "adapter", "key_gen() [Saving public key...]");
  87. conn.execute(
  88. "INSERT INTO keys(key_id, key_private, key_public)
  89. VALUES (NULL, :privkey, :pubkey)",
  90. named_params! {
  91. ":privkey": privkey,
  92. ":pubkey": pubkey
  93. },
  94. )?;
  95. Ok(())
  96. }
  97. pub async fn put_cashier_pub(&self, pubkey: Vec<u8>) -> Result<()> {
  98. debug!(target: "save_cash_key", "Save cashier keys...");
  99. let conn = Connection::open(&self.path)?;
  100. // Write keys to database
  101. conn.execute(
  102. "INSERT INTO cashier(key_id, key_public)
  103. VALUES (NULL, :pubkey)",
  104. named_params! {":pubkey": pubkey},
  105. )?;
  106. Ok(())
  107. }
  108. pub async fn get_public(&self) -> Result<Vec<u8>> {
  109. debug!(target: "get", "Returning keys...");
  110. let conn = Connection::open(&self.path)?;
  111. let mut stmt = conn.prepare("SELECT key_public FROM keys")?;
  112. let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
  113. let mut pub_keys = Vec::new();
  114. for key in key_iter {
  115. pub_keys.push(key?);
  116. }
  117. Ok(pub_keys)
  118. }
  119. pub fn get_private(&self) -> Result<Vec<u8>> {
  120. debug!(target: "get", "Returning keys...");
  121. let conn = Connection::open(&self.path)?;
  122. let mut stmt = conn.prepare("SELECT key_private FROM keys")?;
  123. let key_iter = stmt.query_map::<u8, _, _>([], |row| row.get(0))?;
  124. let mut keys = Vec::new();
  125. for key in key_iter {
  126. keys.push(key?);
  127. }
  128. Ok(keys)
  129. }
  130. pub async fn get_value_serialized<T: Encodable>(&self, data: &T) -> Result<Vec<u8>> {
  131. let v = serialize(data);
  132. Ok(v)
  133. }
  134. pub async fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
  135. let v: D = deserialize(&key)?;
  136. Ok(v)
  137. }
  138. }