Skip to content

Commit

Permalink
Merge pull request #50 from fpco/feature/generate-signing-keys
Browse files Browse the repository at this point in the history
Generate signing keys from cosmos-rs
  • Loading branch information
rgomezr391 authored Nov 25, 2024
2 parents 3eee107 + 2d1e668 commit caaf2b4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cosmos-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cw4-group = "2.0.0"
cw-utils = "2.0.0"
cosmwasm-std = "2.1.4"
csv = "1.3.0"
hex = "0.4"

[dev-dependencies]
quickcheck = "1"
Expand Down
4 changes: 4 additions & 0 deletions packages/cosmos-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ impl Subcommand {
fn gen_wallet(hrp: AddressHrp) -> Result<()> {
let phrase = cosmos::SeedPhrase::random();
let wallet = phrase.with_hrp(hrp)?;
let private_key = wallet.get_privkey().private_key.display_secret();
let public_key = hex::encode(wallet.public_key_bytes());
println!("Mnemonic: {}", phrase.phrase());
println!("Address: {wallet}");
println!("Private Key: {}", private_key);
println!("Public Key : {}", public_key);
Ok(())
}
18 changes: 18 additions & 0 deletions packages/cosmos/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ impl Wallet {
)
.await
}

/// Retrieves the private key associated with the wallet.
///
/// This function returns the private key (`Xpriv`) of the wallet.
/// The private key is crucial for signing transactions and should be kept secure.
pub fn get_privkey(&self) -> Xpriv {
self.privkey
}
}

fn cosmos_address_from_public_key(public_key: &[u8]) -> [u8; 20] {
Expand Down Expand Up @@ -545,4 +553,14 @@ mod tests {
hex::encode(hash)
);
}

#[test]
fn test_gen_key_pair() {
let address_hrp = AddressHrp::from_static("cosmos");
let wallet = Wallet::generate(address_hrp).unwrap();
let private_key = wallet.get_privkey().private_key.display_secret();
let public_key = hex::encode(wallet.public_key_bytes());
assert_eq!(private_key.to_string().len(), 64);
assert_eq!(public_key.to_string().len(), 66);
}
}

0 comments on commit caaf2b4

Please sign in to comment.