Check for existing dkey_pub to matched btc

This commit is contained in:
Janus
2021-08-06 17:42:26 -04:00
parent 62959fd7de
commit 916a679a81
4 changed files with 48 additions and 13 deletions

View File

@@ -7,5 +7,5 @@ CREATE TABLE IF NOT EXISTS keypairs(
dkey_id INTEGER PRIMARY KEY NOT NULL,
btc_key_private BLOB NOT NULL,
btc_key_public BLOB NOT NULL,
txid BLOB NOT NULL
txid BLOB
);

View File

@@ -72,5 +72,8 @@ impl BitcoinKeys {
pub fn get_pubkey(&self) -> &PublicKey {
&self.bitcoin_public_key
}
pub fn get_privkey(&self) -> &PrivateKey {
&self.bitcoin_private_key
}
}

View File

@@ -132,23 +132,28 @@ impl CashierService {
}
async fn handle_request(
msg: (PeerId, Request),
_cashier_wallet: CashierDbPtr,
cashier_wallet: CashierDbPtr,
send_queue: async_channel::Sender<(PeerId, Reply)>,
) -> Result<()> {
let request = msg.1;
let peer = msg.0;
match request.get_command() {
0 => {
debug!(target: "Cashier", "Get command");
// Exchange zk_pubkey for bitcoin address
let _zkpub = request.get_payload();
let zkpub = request.get_payload();
//check if key has already been issued
let _check = cashier_wallet.get_keys_by_dkey(&zkpub);
// Generate bitcoin Address
let btc_keys = BitcoinKeys::new().unwrap();
let btc_pub = btc_keys.get_pubkey();
let btc_priv = btc_keys.get_privkey();
// add to watchlist
// add pairings to db
let _result = cashier_wallet.put_exchange_keys(zkpub, *btc_priv, *btc_pub);
let mut reply = Reply::from(&request, CashierError::NoError as u32, vec![]);
@@ -157,11 +162,15 @@ impl CashierService {
// send reply
send_queue.send((peer, reply)).await?;
// add to watchlist
info!("Received dkey->btc msg");
}
1 => {
// Withdraw
info!("Received withdraw request");
}
_ => {
return Err(Error::ServicesError("received wrong command"));
@@ -185,6 +194,7 @@ impl CashierClient {
}
pub async fn start(&mut self) -> Result<()> {
debug!(target: "Cashier", "Start CashierClient");
self.protocol.start().await?;
Ok(())

View File

@@ -49,20 +49,43 @@ impl CashierDb {
}
Ok(())
}
pub fn get_keys_by_dkey(&self, dkey_pub: &Vec<u8>) -> Result<()> {
println!("get keys...");
debug!(target: "CashierDB", "Check for existing dkey");
//let dkey_id = self.get_value_deserialized(dkey_pub)?;
// open connection
let conn = Connection::open(&self.path)?;
// unlock database
conn.pragma_update(None, "key", &self.password)?;
// let mut keypairs = conn.prepare("SELECT dkey_id FROM keypairs WHERE dkey_id = :dkey_id")?;
// let rows = keypairs.query_map::<Vec<u8>, _, _>(&[(":dkey_id", &secret)], |row| row.get(0))?;
let mut stmt = conn.prepare("SELECT * FROM keypairs where dkey_id = ?")?;
let mut rows = stmt.query([dkey_pub])?;
if let Some(_row) = rows.next()? {
println!("Got something");
} else {
println!("Did not get something");
}
Ok(())
}
// Update to take BitcoinKeys instance instead
pub fn put_exchange_keys(
&self,
dkey_pub: jubjub::SubgroupPoint,
dkey_pub: Vec<u8>,
btc_private: PrivKey,
btc_public: PubKey,
// Successful btc tx id
txid: String,
//txid will be updated when exists
) -> Result<()> {
debug!(target: "CashierDB", "Put exchange keys");
// prepare the values
let dkey_pub = self.get_value_serialized(&dkey_pub)?;
//let dkey_pub = self.get_value_serialized(&dkey_pub)?;
let btc_private = btc_private.to_bytes();
let btc_public = btc_public.to_bytes();
let txid = self.get_value_serialized(&txid)?;
// open connection
let conn = Connection::open(&self.path)?;
@@ -70,13 +93,12 @@ impl CashierDb {
conn.pragma_update(None, "key", &self.password)?;
conn.execute(
"INSERT INTO keypairs(dkey_id, btc_key_private, btc_key_public, txid)
VALUES (:dkey_id, :btc_key_private, :btc_key_public, :txid)",
"INSERT INTO keypairs(dkey_id, btc_key_private, btc_key_public)
VALUES (:dkey_id, :btc_key_private, :btc_key_public)",
named_params! {
":dkey_id": dkey_pub,
":btc_key_private": btc_private,
":btc_key_private": btc_public,
":txid": txid,
},
)?;
Ok(())