Skip to content

Commit

Permalink
Allow setting private key for sender (#482)
Browse files Browse the repository at this point in the history
* Added set mnemonic and set_private_key for sender

* Better doc

* changelog

* Update cw-orch-daemon/src/senders/cosmos.rs

Co-authored-by: CyberHoward <[email protected]>

* Update cw-orch-daemon/src/senders/cosmos.rs

Co-authored-by: CyberHoward <[email protected]>

* Nits

---------

Co-authored-by: CyberHoward <[email protected]>
  • Loading branch information
Kayanski and CyberHoward authored Sep 9, 2024
1 parent cbab5f5 commit 3549b9d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unpublished

- Add methods to set the private key and mnemonic of an existing sender

### Breaking

## 0.25.0
Expand Down
4 changes: 3 additions & 1 deletion cw-orch-daemon/src/env.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 22 additions & 0 deletions cw-orch-daemon/src/senders/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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());
}
Expand Down
4 changes: 3 additions & 1 deletion packages/cw-orch-core/src/env.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3549b9d

Please sign in to comment.