Skip to content

Instantly share code, notes, and snippets.

@Reecepbcups
Created January 21, 2024 00:42
Show Gist options
  • Save Reecepbcups/013db582da8976cd94977dfbaa0777fc to your computer and use it in GitHub Desktop.
Save Reecepbcups/013db582da8976cd94977dfbaa0777fc to your computer and use it in GitHub Desktop.
// cosmrs = { version = "0.15.0", features = [ "rpc", "dev", "bip32"] }
// bip32 = { version = "0.5.1" }
// serde = { version = "1.0.130" }
// serde_json = { version = "1.0.68"}
use bip32::{DerivationPath, Language};
use cosmrs::proto::tendermint::Error;
use serde::Deserialize;
// derivation path for cosmos wallets is 118. Terra/Secret is 330, and ETH is 60.
const HD_PATH: &str = "m/44'/118'/0'/0/0";
const BECH_PREFIX: &str = "osmo";
fn main() -> Result<(), Error> {
// put mnemonic phrase here
let mnemonic_str = "";
let mnemonic = bip32::Mnemonic::new(mnemonic_str, Language::English).unwrap();
let seed = mnemonic.to_seed("");
let private_key = cosmrs::crypto::secp256k1::SigningKey::derive_from_path(
&seed,
&HD_PATH.parse::<DerivationPath>().unwrap(),
)
.unwrap();
let public_key = private_key.public_key();
let public_key_str = public_key.to_string();
let parsed = serde_json::from_str(&public_key_str);
if let Ok(parsed_data) = parsed {
let data: Data = serde_json::from_value(parsed_data).unwrap();
println!("public key: {}", data.key);
} else {
println!("Error parsing JSON string.");
}
let account_id: cosmrs::AccountId = public_key.account_id(BECH_PREFIX).unwrap();
println!("address: {account_id}");
Ok(())
}
#[derive(Deserialize)]
struct Data {
key: String,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment