Explorar o código

change the word coin to token in cashier

ghassmo %!s(int64=4) %!d(string=hai) anos
pai
achega
1dc0682603
Modificáronse 6 ficheiros con 64 adicións e 62 borrados
  1. 3 3
      sql/cashier.sql
  2. 4 4
      src/bin/cashierd.rs
  3. 8 8
      src/service/bridge.rs
  4. 5 5
      src/service/btc.rs
  5. 8 8
      src/service/sol.rs
  6. 36 34
      src/wallet/cashierdb.rs

+ 3 - 3
sql/cashier.sql

@@ -1,12 +1,12 @@
 CREATE TABLE IF NOT EXISTS deposit_keypairs(
     d_key_public INTEGER PRIMARY KEY NOT NULL,
-   	coin_key_private BLOB NOT NULL,
-    coin_key_public BLOB NOT NULL,
+   	token_key_private BLOB NOT NULL,
+    token_key_public BLOB NOT NULL,
 	asset_id BLOB NOT NULL
 );
 
 CREATE TABLE IF NOT EXISTS withdraw_keypairs(
-    coin_key_id BLOB PRIMARY KEY NOT NULL,
+    token_key_id BLOB PRIMARY KEY NOT NULL,
 	d_key_private BLOB NOT NULL,
     d_key_public BLOB NOT NULL,
 	asset_id BLOB NOT NULL,

+ 4 - 4
src/bin/cashierd.rs

@@ -153,12 +153,12 @@ impl Cashierd {
         debug!(target: "CASHIER DAEMON", "Receive coin with following address and amount: {}, {}"
             , drk_pub_key, amount);
 
-        // get public key, and asset_id of the coin get received
-        let coin = cashier_wallet.get_withdraw_coin_public_key_by_dkey_public(&drk_pub_key)?;
+        // get public key, and asset_id of the token
+        let token = cashier_wallet.get_withdraw_token_public_key_by_dkey_public(&drk_pub_key)?;
 
         // send a request to bridge to send equivalent amount of
         // received drk coin to token publickey
-        if let Some((addr, asset_id)) = coin {
+        if let Some((addr, asset_id)) = token {
             bridge_subscribtion
                 .sender
                 .send(bridge::BridgeRequests {
@@ -242,7 +242,7 @@ impl Cashierd {
         //// TODO: Sanity check.
         let _check = self
             .cashier_wallet
-            .get_deposit_coin_keys_by_dkey_public(&pubkey, &serialize(&1));
+            .get_deposit_token_keys_by_dkey_public(&pubkey, &serialize(&1));
 
         // TODO: implement bridge communication
         // this just returns the user public key

+ 8 - 8
src/service/bridge.rs

@@ -32,19 +32,19 @@ pub struct BridgeSubscribtion {
     pub receiver: async_channel::Receiver<BridgeResponse>,
 }
 
-pub struct CoinSubscribtion {
+pub struct TokenSubscribtion {
     pub secret_key: Vec<u8>,
     pub public_key: Vec<u8>,
 }
 
-pub struct CoinNotification {
+pub struct TokenNotification {
     pub secret_key: Vec<u8>,
     pub received_balance: u64,
 }
 
 pub struct Bridge {
-    clients: Mutex<HashMap<Vec<u8>, Arc<dyn CoinClient + Send + Sync>>>,
-    notifiers: Mutex<HashMap<Vec<u8>, async_channel::Receiver<CoinNotification>>>,
+    clients: Mutex<HashMap<Vec<u8>, Arc<dyn TokenClient + Send + Sync>>>,
+    notifiers: Mutex<HashMap<Vec<u8>, async_channel::Receiver<TokenNotification>>>,
 }
 
 impl Bridge {
@@ -58,7 +58,7 @@ impl Bridge {
     pub async fn add_clients(
         self: Arc<Self>,
         asset_id: jubjub::Fr,
-        client: Arc<dyn CoinClient + Send + Sync>,
+        client: Arc<dyn TokenClient + Send + Sync>,
     ) -> Result<()> {
         let asset_id = serialize(&asset_id);
 
@@ -118,8 +118,8 @@ impl Bridge {
 }
 
 #[async_trait]
-pub trait CoinClient {
-    async fn subscribe(&self) -> Result<CoinSubscribtion>;
-    async fn get_notifier(&self) -> Result<async_channel::Receiver<CoinNotification>>;
+pub trait TokenClient {
+    async fn subscribe(&self) -> Result<TokenSubscribtion>;
+    async fn get_notifier(&self) -> Result<async_channel::Receiver<TokenNotification>>;
     async fn send(&self, address: Vec<u8>, amount: u64) -> Result<()>;
 }

+ 5 - 5
src/service/btc.rs

@@ -1,4 +1,4 @@
-use super::bridge::{CoinClient, CoinNotification, CoinSubscribtion};
+use super::bridge::{TokenClient, TokenNotification, TokenSubscribtion};
 use crate::serial::{serialize, Decodable, Encodable};
 use crate::Result;
 
@@ -155,8 +155,8 @@ impl BtcClient {
 }
 
 #[async_trait]
-impl CoinClient for BtcClient {
-    async fn subscribe(&self) -> Result<CoinSubscribtion> {
+impl TokenClient for BtcClient {
+    async fn subscribe(&self) -> Result<TokenSubscribtion> {
         //// Generate bitcoin Address
         let btc_keys = BitcoinKeys::new(self.client.clone(), self.network)?;
 
@@ -171,13 +171,13 @@ impl CoinClient for BtcClient {
         let (_txid, _balance) = btc_keys.start_subscribe().await?;
         //let _script = btc_keys.get_script();
 
-        Ok(CoinSubscribtion {
+        Ok(TokenSubscribtion {
             secret_key: serialize(&btc_priv.to_bytes()),
             public_key: serialize(&btc_pub.to_bytes()),
         })
     }
 
-    async fn get_notifier(&self) -> Result<async_channel::Receiver<CoinNotification>> {
+    async fn get_notifier(&self) -> Result<async_channel::Receiver<TokenNotification>> {
         // TODO this not implemented yet
         let (_, notifier) = async_channel::unbounded();
         Ok(notifier)

+ 8 - 8
src/service/sol.rs

@@ -2,7 +2,7 @@ use crate::rpc::{jsonrpc, jsonrpc::JsonResult};
 use crate::serial::{deserialize, serialize, Decodable, Encodable};
 use crate::{Error, Result};
 
-use super::bridge::{ CoinSubscribtion, CoinNotification, CoinClient};
+use super::bridge::{ TokenSubscribtion, TokenNotification, TokenClient};
 
 use async_trait::async_trait;
 
@@ -44,8 +44,8 @@ pub struct SolClient {
     subscriptions: Arc<Mutex<HashMap<Pubkey, (Keypair, u64)>>>,
 
     notify_channel: (
-        async_channel::Sender<CoinNotification>,
-        async_channel::Receiver<CoinNotification>,
+        async_channel::Sender<TokenNotification>,
+        async_channel::Receiver<TokenNotification>,
     ),
 
     subscribe_channel: (
@@ -154,7 +154,7 @@ impl SolClient {
 
                     self.notify_channel
                         .0
-                        .send(CoinNotification {
+                        .send(TokenNotification {
                             secret_key: serialize(keypair),
                             received_balance,
                         })
@@ -203,8 +203,8 @@ impl SolClient {
 }
 
 #[async_trait]
-impl CoinClient for SolClient {
-    async fn subscribe(&self) -> Result<CoinSubscribtion> {
+impl TokenClient for SolClient {
+    async fn subscribe(&self) -> Result<TokenSubscribtion> {
         let keypair = Keypair::generate(&mut OsRng);
 
         // Parameters for subscription to events related to `pubkey`.
@@ -237,10 +237,10 @@ impl CoinClient for SolClient {
         //  send
         self.subscribe_channel.0.send(sub_msg).await?;
 
-        Ok(CoinSubscribtion { secret_key, public_key})
+        Ok(TokenSubscribtion { secret_key, public_key})
     }
 
-    async fn get_notifier(&self) -> Result<async_channel::Receiver<CoinNotification>>{
+    async fn get_notifier(&self) -> Result<async_channel::Receiver<TokenNotification>>{
         Ok(self.notify_channel.1.clone())
     }
 

+ 36 - 34
src/wallet/cashierdb.rs

@@ -48,7 +48,7 @@ impl CashierDb {
     }
 
     // return private and public keys as a tuple
-    pub fn get_deposit_coin_keys_by_dkey_public(
+    pub fn get_deposit_token_keys_by_dkey_public(
         &self,
         d_key_public: &jubjub::SubgroupPoint,
         asset_id: &Vec<u8>,
@@ -76,12 +76,11 @@ impl CashierDb {
         Ok(keys)
     }
 
-    // Update to take BitcoinKeys instance instead
     pub fn put_exchange_keys(
         &self,
         d_key_public: &jubjub::SubgroupPoint,
-        coin_private: &Vec<u8>,
-        coin_public: &Vec<u8>,
+        token_private: &Vec<u8>,
+        token_public: &Vec<u8>,
         asset_id: &Vec<u8>,
     ) -> Result<()> {
         debug!(target: "CASHIERDB", "Put exchange keys");
@@ -94,17 +93,19 @@ impl CashierDb {
         conn.pragma_update(None, "key", &self.password)?;
 
         conn.execute(
-            "INSERT INTO deposit_keypairs(d_key_public, coin_key_private, coin_key_public, asset_id)
-            VALUES (:d_key_public, :coin_key_private, :coin_key_public, :asset_id)",
+            "INSERT INTO deposit_keypairs(d_key_public, token_key_private, token_public_key_public, asset_id)
+            VALUES (:d_key_public, :token_key_private, :token_key_public, :asset_id)",
             named_params! {
                 ":d_key_public": d_key_public,
-                ":coin_key_private": coin_private,
-                ":coin_key_public": coin_public,
+                ":token_key_private": token_private,
+                ":token_key_public": token_public,
                 ":asset_id": asset_id,
             },
         )?;
         Ok(())
     }
+
+    // TODO convert this to generic function work with different tokens
     pub fn put_btc_utxo(
         &self,
         tx_id: &Vec<u8>,
@@ -131,6 +132,7 @@ impl CashierDb {
         )?;
         Ok(())
     }
+
     pub fn get_withdraw_private_keys(&self) -> Result<Vec<jubjub::Fr>> {
         debug!(target: "CASHIERDB", "Get withdraw private keys");
         // open connection
@@ -158,12 +160,12 @@ impl CashierDb {
         Ok(private_keys)
     }
 
-    pub fn get_withdraw_keys_by_coin_public_key(
+    pub fn get_withdraw_keys_by_token_public_key(
         &self,
-        coin_public_key: &Vec<u8>,
+        token_public_key: &Vec<u8>,
         asset_id: &Vec<u8>,
     ) -> Result<Option<Keypair>> {
-        debug!(target: "CASHIERDB", "Check for existing coin address");
+        debug!(target: "CASHIERDB", "Check for existing token address");
         // open connection
         let conn = Connection::open(&self.path)?;
         // unlock database
@@ -173,11 +175,11 @@ impl CashierDb {
 
         let mut stmt =
             conn.prepare(
-                "SELECT * FROM withdraw_keypairs WHERE coin_key_id = :coin_key_id AND asset_id = :asset_id AND confirm = :confirm;")?;
+                "SELECT * FROM withdraw_keypairs WHERE token_key_id = :token_key_id AND asset_id = :asset_id AND confirm = :confirm;")?;
 
         let addr_iter = stmt.query_map::<Keypair, _, _>(
             &[
-                (":coin_key_id", &coin_public_key),
+                (":token_key_id", &token_public_key),
                 (":asset_id", &asset_id),
                 (":confirm", &&confirm),
             ],
@@ -201,11 +203,11 @@ impl CashierDb {
         Ok(addresses.pop())
     }
 
-    pub fn get_withdraw_coin_public_key_by_dkey_public(
+    pub fn get_withdraw_token_public_key_by_dkey_public(
         &self,
         pub_key: &jubjub::SubgroupPoint,
     ) -> Result<Option<(Vec<u8>, jubjub::Fr)>> {
-        debug!(target: "CASHIERDB", "Get coin address by pub_key");
+        debug!(target: "CASHIERDB", "Get token address by pub_key");
         // open connection
         let conn = Connection::open(&self.path)?;
         // unlock database
@@ -216,32 +218,32 @@ impl CashierDb {
         let confirm = self.get_value_serialized(&false)?;
 
         let mut stmt = conn.prepare(
-            "SELECT coin_key_id, asset_id FROM withdraw_keypairs WHERE d_key_public = :d_key_public AND confirm = :confirm;",
+            "SELECT token_key_id, asset_id FROM withdraw_keypairs WHERE d_key_public = :d_key_public AND confirm = :confirm;",
         )?;
         let addr_iter = stmt.query_map::<(Vec<u8>, jubjub::Fr), _, _>(
             &[(":d_key_public", &d_key_public), (":confirm", &&confirm)],
             |row| {
-                let coin_public_key = row.get(0)?;
+                let token_public_key = row.get(0)?;
                 let asset_id = row.get(1)?;
                 let asset_id: jubjub::Fr = self
                     .get_value_deserialized(asset_id)
                     .expect("deserialize asset_id");
-                Ok((coin_public_key, asset_id))
+                Ok((token_public_key, asset_id))
             },
         )?;
 
-        let mut coin_addresses = vec![];
+        let mut token_addresses = vec![];
 
         for addr in addr_iter {
-            coin_addresses.push(addr?);
+            token_addresses.push(addr?);
         }
 
-        Ok(coin_addresses.pop())
+        Ok(token_addresses.pop())
     }
 
     pub fn confirm_withdraw_key_record(
         &self,
-        coin_address: &Vec<u8>,
+        token_address: &Vec<u8>,
         asset_id: &Vec<u8>,
     ) -> Result<()> {
         debug!(target: "CASHIERDB", "Confirm withdraw keys");
@@ -254,8 +256,8 @@ impl CashierDb {
         let confirm = self.get_value_serialized(&true)?;
 
         conn.execute(
-            "UPDATE withdraw_keypairs SET confirm = ?1  WHERE coin_key_id = ?2 AND asset_id = ?3;",
-            params![confirm, coin_address, asset_id],
+            "UPDATE withdraw_keypairs SET confirm = ?1  WHERE token_key_id = ?2 AND asset_id = ?3;",
+            params![confirm, token_address, asset_id],
         )?;
 
         Ok(())
@@ -263,7 +265,7 @@ impl CashierDb {
 
     pub fn put_withdraw_keys(
         &self,
-        coin_key_id: &Vec<u8>,
+        token_key_id: &Vec<u8>,
         d_key_public: &jubjub::SubgroupPoint,
         d_key_private: &jubjub::Fr,
         asset_id: &Vec<u8>,
@@ -281,10 +283,10 @@ impl CashierDb {
         let confirm = self.get_value_serialized(&false)?;
 
         conn.execute(
-            "INSERT INTO withdraw_keypairs(coin_key_id, d_key_private, d_key_public, asset_id, confirm)
-            VALUES (:coin_key_id, :d_key_private, :d_key_public, :asset_id, :confirm)",
+            "INSERT INTO withdraw_keypairs(token_key_id, d_key_private, d_key_public, asset_id, confirm)
+            VALUES (:token_key_id, :d_key_private, :d_key_public, :asset_id, :confirm)",
             named_params! {
-                ":coin_key_id": coin_key_id,
+                ":token_key_id": token_key_id,
                 ":d_key_private": d_key_private,
                 ":d_key_public": d_key_public,
                 ":asset_id": asset_id,
@@ -308,7 +310,7 @@ mod tests {
     // TODO add more tests
 
     #[test]
-    pub fn test_put_withdraw_keys_and_load_them_with_coin_key() -> Result<()> {
+    pub fn test_put_withdraw_keys_and_load_them_with_token_key() -> Result<()> {
         let walletdb_path = join_config_path(&PathBuf::from("cashier_wallet_test.db"))?;
         let wallet = CashierDb::new(&walletdb_path, "darkfi".into())?;
         wallet.init_db()?;
@@ -317,19 +319,19 @@ mod tests {
         let public2 = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret2;
 
         // btc addr testnet
-        let coin_addr = serialize(&String::from("mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk"));
+        let token_addr = serialize(&String::from("mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk"));
 
         let asset_id = serialize(&1);
 
-        wallet.put_withdraw_keys(&coin_addr, &public2, &secret2, &asset_id)?;
+        wallet.put_withdraw_keys(&token_addr, &public2, &secret2, &asset_id)?;
 
-        let addr = wallet.get_withdraw_keys_by_coin_public_key(&coin_addr, &asset_id)?;
+        let addr = wallet.get_withdraw_keys_by_token_public_key(&token_addr, &asset_id)?;
 
         assert_eq!(addr.is_some(), true);
 
-        wallet.confirm_withdraw_key_record(&coin_addr, &asset_id)?;
+        wallet.confirm_withdraw_key_record(&token_addr, &asset_id)?;
 
-        let addr = wallet.get_withdraw_keys_by_coin_public_key(&coin_addr, &asset_id)?;
+        let addr = wallet.get_withdraw_keys_by_token_public_key(&token_addr, &asset_id)?;
 
         assert_eq!(addr.is_none(), true);