From 3549b9dab7f41426f0948b4dab28f0e6ad447fcb Mon Sep 17 00:00:00 2001 From: Kayanski <44806566+Kayanski@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:20:25 +0200 Subject: [PATCH] Allow setting private key for sender (#482) * Added set mnemonic and set_private_key for sender * Better doc * changelog * Update cw-orch-daemon/src/senders/cosmos.rs Co-authored-by: CyberHoward <88450409+CyberHoward@users.noreply.github.com> * Update cw-orch-daemon/src/senders/cosmos.rs Co-authored-by: CyberHoward <88450409+CyberHoward@users.noreply.github.com> * Nits --------- Co-authored-by: CyberHoward <88450409+CyberHoward@users.noreply.github.com> --- CHANGELOG.md | 2 ++ cw-orch-daemon/src/env.rs | 4 +++- cw-orch-daemon/src/senders/cosmos.rs | 22 ++++++++++++++++++++++ packages/cw-orch-core/src/env.rs | 4 +++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86cd91383..26e3bfb65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unpublished + - Add methods to set the private key and mnemonic of an existing sender + ### Breaking ## 0.25.0 diff --git a/cw-orch-daemon/src/env.rs b/cw-orch-daemon/src/env.rs index 298a7e69e..180da9769 100644 --- a/cw-orch-daemon/src/env.rs +++ b/cw-orch-daemon/src/env.rs @@ -1,4 +1,6 @@ -//! This regroups all env variables used by cw-orch-daemon. It allows for easier documentation and env variable management +//! This regroups all env variables used by cw-orch-daemon. +//! +//! It allows for easier documentation and env variable management //! This is used to import environment variables with safe names (and at a centralized location) //! To get the env variable parsed value, you can use //! ```rust,no_run diff --git a/cw-orch-daemon/src/senders/cosmos.rs b/cw-orch-daemon/src/senders/cosmos.rs index 6fe999bf5..e9d371cc4 100644 --- a/cw-orch-daemon/src/senders/cosmos.rs +++ b/cw-orch-daemon/src/senders/cosmos.rs @@ -132,6 +132,28 @@ impl Wallet { self.options.clone() } + /// Replaces the private key that the [CosmosSender] is using with key derived from the provided 24-word mnemonic. + /// If you want more control over the derived private key, use [Self::set_private_key] + pub fn set_mnemonic(&mut self, mnemonic: impl Into) -> Result<(), DaemonError> { + let secp = Secp256k1::new(); + + let pk = PrivateKey::from_words( + &secp, + &mnemonic.into(), + 0, + self.options.hd_index.unwrap_or(0), + self.chain_info.network_info.coin_type, + )?; + self.set_private_key(pk); + Ok(()) + } + + /// Replaces the private key the sender is using + /// You can use a mnemonic to overwrite the key using [Self::set_mnemonic] + pub fn set_private_key(&mut self, private_key: PrivateKey) { + self.private_key = private_key + } + pub fn set_authz_granter(&mut self, granter: &Addr) { self.options.authz_granter = Some(granter.to_owned()); } diff --git a/packages/cw-orch-core/src/env.rs b/packages/cw-orch-core/src/env.rs index 3f01d6ce8..db83e5c8a 100644 --- a/packages/cw-orch-core/src/env.rs +++ b/packages/cw-orch-core/src/env.rs @@ -1,4 +1,6 @@ -//! This regroups all env variables used by cw-orch-daemon. It allows for easier documentation and env variable management +//! This regroups all env variables used by cw-orch-daemon. +//! +//! It allows for easier documentation and env variable management //! This is used to import environment variables with safe names (and at a centralized location) //! To get the env variable parsed value, you can use //! ```rust,no_run