walletdb.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use crate::serial;
  2. use crate::Result;
  3. use ff::Field;
  4. use log::*;
  5. use rand::rngs::OsRng;
  6. use rusqlite::{named_params, Connection};
  7. use std::path::PathBuf;
  8. // TODO: make this more generic to remove boiler plate. e.g. create_wallet(cashier) instead of
  9. // create_cashier_wallet
  10. pub struct WalletDB {}
  11. impl WalletDB {
  12. pub async fn new(path: PathBuf) -> Result<()> {
  13. let connect = Connection::open(&path).expect("Failed to connect to database.");
  14. let contents = include_str!("../../res/schema.sql");
  15. Ok(connect.execute_batch(&contents)?)
  16. }
  17. // pub async fn create_keypair() -> Result<String, String> {
  18. // let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
  19. // let pubkey = serial::serialize(&public);
  20. // let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
  21. // let privkey = serial::serialize(&secret);
  22. // Ok(pubkey, privkey)
  23. // }
  24. pub async fn path(wallet: &str) -> Result<PathBuf> {
  25. let mut path = dirs::home_dir()
  26. .expect("cannot find home directory.")
  27. .as_path()
  28. .join(".config/darkfi/");
  29. debug!(target: "walletdb", "CREATE PATH {:?}", path);
  30. path.push(wallet);
  31. Ok(path)
  32. }
  33. pub async fn key_gen(path: PathBuf, id: i32, pubkey: Vec<u8>, privkey: Vec<u8>) -> Result<()> {
  34. debug!(target: "key_gen", "Generating keys...");
  35. let connect = Connection::open(&path).expect("Failed to connect to database.");
  36. // TODO: ID should not be fixed
  37. let id = 0;
  38. // Create keys
  39. let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
  40. debug!(target: "adapter", "key_gen() [Generating public key...]");
  41. let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
  42. let pubkey = serial::serialize(&public);
  43. let privkey = serial::serialize(&secret);
  44. // Write keys to database
  45. connect.execute(
  46. "INSERT INTO keys(key_id, key_private, key_public)
  47. VALUES (:id, :privkey, :pubkey)",
  48. named_params! {":id": id,
  49. ":privkey": privkey,
  50. ":pubkey": pubkey
  51. },
  52. )?;
  53. Ok(())
  54. }
  55. pub async fn get(path: PathBuf) -> Result<()> {
  56. debug!(target: "get_cash_public", "Returning cashier keys...");
  57. let connect = Connection::open(&path).expect("Failed to connect to database.");
  58. let id = 0;
  59. let mut stmt = connect.prepare("SELECT key_public FROM keys").unwrap();
  60. let key_iter = stmt
  61. .query_map::<Vec<u8>, _, _>([], |row| row.get(0))
  62. .unwrap();
  63. let mut pub_keys = Vec::new();
  64. for key in key_iter {
  65. pub_keys.push(key.unwrap());
  66. }
  67. let key = match pub_keys.pop() {
  68. Some(key_found) => println!("{:?}", key_found),
  69. None => println!("No cashier public key found"),
  70. };
  71. Ok(key)
  72. }
  73. pub async fn save(path: PathBuf, pubkey: Vec<u8>) -> Result<()> {
  74. debug!(target: "save_cash_key", "Save cashier keys...");
  75. //let path = Self::wallet_path();
  76. let connect = Connection::open(&path).expect("Failed to connect to database.");
  77. let id = 0;
  78. // Write keys to database
  79. connect.execute(
  80. "INSERT INTO cashier(key_id, key_public)
  81. VALUES (:id, :pubkey)",
  82. named_params! {":id": id,
  83. ":pubkey": pubkey
  84. },
  85. )?;
  86. Ok(())
  87. }
  88. }
  89. fn main() {}