Просмотр исходного кода

btc: check if there is keypair path in cashierd.toml before generate new one

ghassmo 4 лет назад
Родитель
Сommit
61cf278c6c
4 измененных файлов с 26 добавлено и 17 удалено
  1. 23 12
      src/bin/cashierd.rs
  2. 1 1
      src/bin/darkfid.rs
  3. 1 2
      src/service/btc.rs
  4. 1 2
      src/service/sol.rs

+ 23 - 12
src/bin/cashierd.rs

@@ -32,7 +32,9 @@ use drk::{
 fn handle_bridge_error(error_code: u32) -> Result<()> {
     match error_code {
         1 => Err(Error::BridgeError("Not Supported Client".into())),
-        2 => Err(Error::BridgeError("Unable to watch the deposit address".into())),
+        2 => Err(Error::BridgeError(
+            "Unable to watch the deposit address".into(),
+        )),
         3 => Err(Error::BridgeError("Unable to send the token".into())),
         _ => Err(Error::BridgeError("Unknown error_code".into())),
     }
@@ -505,7 +507,7 @@ impl Cashierd {
                     }
 
                     let sol_client =
-                        SolClient::new(serialize(&main_keypair), &network.blockchain).await?;
+                        SolClient::new(main_keypair, &network.blockchain).await?;
 
                     bridge2.add_clients(NetworkName::Solana, sol_client).await?;
                 }
@@ -513,7 +515,7 @@ impl Cashierd {
                 #[cfg(feature = "btc")]
                 NetworkName::Bitcoin => {
                     debug!(target: "CASHIER DAEMON", "Add btc network");
-                    use drk::service::btc::{BtcClient, Keypair};
+                    use drk::service::btc::{BtcClient, Keypair, BtcFailed};
 
                     let bridge2 = self.bridge.clone();
 
@@ -521,19 +523,28 @@ impl Cashierd {
 
                     let main_keypairs = self.cashier_wallet.get_main_keys(&NetworkName::Bitcoin)?;
 
-                    if main_keypairs.is_empty() {
-                        main_keypair = Keypair::new();
-                        self.cashier_wallet.put_main_keys(
-                            &serialize(&main_keypair),
-                            &serialize(&main_keypair.pubkey()),
-                            &NetworkName::Bitcoin,
-                        )?;
+                    if network.keypair.is_empty() {
+                        if main_keypairs.is_empty() {
+                            main_keypair = Keypair::new();
+                            self.cashier_wallet.put_main_keys(
+                                &serialize(&main_keypair),
+                                &serialize(&main_keypair.pubkey()),
+                                &NetworkName::Bitcoin,
+                            )?;
+                        } else {
+                            main_keypair = deserialize(&main_keypairs[main_keypairs.len() - 1].0)?;
+                        }
                     } else {
-                        main_keypair = deserialize(&main_keypairs[main_keypairs.len() - 1].0)?;
+                        let keypair_str = drk::cli::cli_config::load_keypair_to_str(
+                            PathBuf::from(expand_path(&network.keypair.clone())?),
+                        )?;
+                        let keypair_bytes: Vec<u8> = serde_json::from_str(&keypair_str)?;
+                        main_keypair = Keypair::from_bytes(&keypair_bytes)
+                            .map_err(|e| BtcFailed::DecodeAndEncodeError(e.to_string()))?;
                     }
 
                     let btc_client =
-                        BtcClient::new(serialize(&main_keypair), &network.blockchain).await?;
+                        BtcClient::new(main_keypair, &network.blockchain).await?;
 
                     bridge2
                         .add_clients(NetworkName::Bitcoin, btc_client)

+ 1 - 1
src/bin/darkfid.rs

@@ -213,7 +213,7 @@ impl Darkfid {
     async fn features(&self, id: Value, _params: Value) -> JsonResult {
         let req = jsonreq(json!("features"), json!([]));
         let rep: JsonResult;
-        // TODO: this just selects the first cashier in the list
+        // NOTE: this just selects the first cashier in the list
         match send_request(&self.cashiers[0].rpc_url, json!(req)).await {
             Ok(v) => rep = v,
             Err(e) => {

+ 1 - 2
src/service/btc.rs

@@ -168,8 +168,7 @@ pub struct BtcClient {
 }
 
 impl BtcClient {
-    pub async fn new(main_keypair: Vec<u8>, network: &str) -> Result<Arc<Self>> {
-        let main_keypair: Keypair = deserialize(&main_keypair)?;
+    pub async fn new(main_keypair: Keypair, network: &str) -> Result<Arc<Self>> {
 
         let notify_channel = async_channel::unbounded();
 

+ 1 - 2
src/service/sol.rs

@@ -50,8 +50,7 @@ pub struct SolClient {
 }
 
 impl SolClient {
-    pub async fn new(main_keypair: Vec<u8>, network: &str) -> Result<Arc<Self>> {
-        let main_keypair: Keypair = deserialize(&main_keypair)?;
+    pub async fn new(main_keypair: Keypair, network: &str) -> Result<Arc<Self>> {
         let notify_channel = async_channel::unbounded();
 
         info!(target: "SOL BRIDGE", "Main SOL wallet pubkey: {:?}", &main_keypair.pubkey());