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

Update config to read btc testnet or mainnet into creating keys

Janus 4 лет назад
Родитель
Сommit
5178e1b183
5 измененных файлов с 35 добавлено и 12 удалено
  1. 5 1
      examples/configs/cashierd.toml
  2. 6 1
      src/bin/cashierd.rs
  3. 9 3
      src/cli/cli_config.rs
  4. 13 5
      src/service/btc.rs
  5. 2 2
      src/service/cashier.rs

+ 5 - 1
examples/configs/cashierd.toml

@@ -1,10 +1,14 @@
 accept_url = "127.0.0.1:7777"
 rpc_url = "http://127.0.0.1:8000"
 client_database_path = "cashier_client_database.db"
-btc_endpoint = "tcp://electrum.blockstream.info:50001"
+
 gateway_url = "127.0.0.1:3333"
 log_path = "/tmp/cashierd.log"
 cashierdb_path = "~/.config/darkfi/cashier.db"
 client_walletdb_path = "~/.config/darkfi/cashier_client_walletdb.db"
 password = "TEST_PASSWORD"
 client_password = "TEST_PASSWORD"
+
+btc_testnet = true
+btc_mainnet_endpoint = "tcp://electrum.blockstream.info:50001"
+btc_testnet_endpoint = "tcp://electrum.blockstream.info:60002"

+ 6 - 1
src/bin/cashierd.rs

@@ -43,7 +43,12 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<CashierdConfig>) -> Resu
 
     let gateway_addr: SocketAddr = config.gateway_url.parse()?;
 
-    let btc_endpoint: String = config.btc_endpoint.clone();
+    let btc_endpoint: (bool, String) = if config.btc_testnet {
+        ( true, config.btc_testnet_endpoint.clone() )
+    }
+    else {
+        ( false, config.btc_mainnet_endpoint.clone() )
+    };
 
     let database_path = config.client_database_path.clone();
     let database_path = join_config_path(&PathBuf::from(database_path))?;

+ 9 - 3
src/cli/cli_config.rs

@@ -87,9 +87,6 @@ pub struct CashierdConfig {
     #[serde(rename = "client_database_path")]
     pub client_database_path: String,
 
-    #[serde(rename = "btc_endpoint")]
-    pub btc_endpoint: String,
-
     #[serde(rename = "gateway_url")]
     pub gateway_url: String,
 
@@ -107,4 +104,13 @@ pub struct CashierdConfig {
 
     #[serde(rename = "client_password")]
     pub client_password: String,
+
+    #[serde(rename = "btc_testnet")]
+    pub btc_testnet: bool,
+
+    #[serde(rename = "btc_mainnet_endpoint")]
+    pub btc_mainnet_endpoint: String,
+
+    #[serde(rename = "btc_testnet_endpoint")]
+    pub btc_testnet_endpoint: String,
 }

+ 13 - 5
src/service/btc.rs

@@ -28,10 +28,14 @@ pub struct BitcoinKeys {
     pub bitcoin_public_key: PublicKey,
     pub pub_address: Address,
     pub script: Script,
+    pub network: Network,
 }
 
 impl BitcoinKeys {
-    pub fn new(btc_client: Arc<ElectrumClient>) -> Result<Arc<BitcoinKeys>> {
+    pub fn new(
+        btc_client: Arc<ElectrumClient>,
+        network: Network,
+    ) -> Result<Arc<BitcoinKeys>> {
         let context = secp256k1::Secp256k1::new();
 
         // Probably not good enough for release
@@ -49,12 +53,12 @@ impl BitcoinKeys {
         let secret_key = SecretKey::from_slice(&hex::decode(data_slice).unwrap()).unwrap();
 
         // Use Testnet
-        let bitcoin_private_key = PrivateKey::new(secret_key, Network::Testnet);
+        let bitcoin_private_key = PrivateKey::new(secret_key, network);
 
         let bitcoin_public_key = PublicKey::from_private_key(&context, &bitcoin_private_key);
         //let pubkey_serialized = bitcoin_public_key.to_bytes();
 
-        let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Testnet);
+        let pub_address = Address::p2pkh(&bitcoin_public_key, network);
 
         let script = Script::new_p2pk(&bitcoin_public_key);
 
@@ -65,6 +69,7 @@ impl BitcoinKeys {
             bitcoin_public_key,
             pub_address,
             script,
+            network,
         }))
     }
 
@@ -135,14 +140,17 @@ impl BitcoinKeys {
 
 pub struct BtcClient {
     client: Arc<ElectrumClient>,
+    network: Network,
 }
 
 impl BtcClient {
-    pub fn new(client_address: String) -> Result<Self> {
+    pub fn new( btc_endpoint: (bool, String) ) -> Result<Self> {
+        let (network, client_address) = btc_endpoint;
         let client = ElectrumClient::new(&client_address)
             .map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?;
         Ok(Self {
             client: Arc::new(client),
+            network: if network {Network::Testnet} else {Network::Bitcoin},
         })
     }
 }
@@ -151,7 +159,7 @@ impl BtcClient {
 impl CoinClient for BtcClient {
     async fn watch(&self) -> Result<(Vec<u8>, Vec<u8>)> {
         //// Generate bitcoin Address
-        let btc_keys = BitcoinKeys::new(self.client.clone())?;
+        let btc_keys = BitcoinKeys::new(self.client.clone(), self.network)?;
 
         let btc_pub = btc_keys.clone();
         let btc_pub = btc_pub.get_pubkey();

+ 2 - 2
src/service/cashier.rs

@@ -59,7 +59,7 @@ impl CashierService {
     pub async fn start(
         &mut self,
         executor: Arc<Executor<'_>>,
-        client_address: String,
+        btc_endpoint: (bool, String),
         // TODO: make this a vector of assets
         asset_id: jubjub::Fr,
     ) -> Result<()> {
@@ -77,7 +77,7 @@ impl CashierService {
         let bridge = bridge::Bridge::new();
 
         #[cfg(feature = "default")]
-        let btc_client = super::btc::BtcClient::new(client_address)?;
+        let btc_client = super::btc::BtcClient::new(btc_endpoint)?;
         #[cfg(feature = "default")]
         bridge
             .clone()