Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
fm-306: add wallet pub-key cmd and several fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adlrocha committed Oct 17, 2023
1 parent 1231eb3 commit f39529d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 7 deletions.
3 changes: 2 additions & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ hex = "0.4.3"
tempfile = "3.4.0"
serde_json = { version = "1.0.91", features = ["raw_value"] }
libipld = { version = "0.14", default-features = false, features = ["dag-cbor"] }
libsecp256k1 = "0.7"
ethers = "2.0.8"
ethers-contract = "2.0.8"

Expand Down
1 change: 1 addition & 0 deletions ipc/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ thiserror = { workspace = true }
hex = { workspace = true }
serde_tuple = { workspace = true }
zeroize = "1.6.0"
libsecp256k1 = { workspace = true }

ethers-contract = { workspace = true }
ethers = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion ipc/cli/src/commands/subnet/list_subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl CommandLineHandler for ListSubnets {
for (_, s) in ls.iter() {
println!(
"{:?} - status: {:?}, collateral: {:?} FIL, circ.supply: {:?} FIL",
s.id,
s.id.to_string(),
s.status,
TokenAmount::from_whole(s.stake.atto().clone()),
TokenAmount::from_whole(s.circ_supply.atto().clone()),
Expand Down
4 changes: 3 additions & 1 deletion ipc/cli/src/commands/wallet/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl CommandLineHandler for WalletBalances {
.collect::<anyhow::Result<Vec<(TokenAmount, &EthKeyAddress)>>>()?;

for (balance, addr) in r {
println!("{:?} - Balance: {}", addr.to_string(), balance);
if addr.to_string() != String::from("default-key") {
println!("{:?} - Balance: {}", addr.to_string(), balance);
}
}
}
WalletType::Fvm => {
Expand Down
62 changes: 62 additions & 0 deletions ipc/cli/src/commands/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,65 @@ pub(crate) struct WalletExportArgs {
#[arg(long, short, help = "The type of the wallet, i.e. fvm, evm")]
pub wallet_type: String,
}

pub(crate) struct WalletPublicKey;

impl WalletPublicKey {
fn pubkey_evm(
provider: &IpcProvider,
arguments: &WalletPublicKeyArgs,
) -> anyhow::Result<String> {
let keystore = provider.evm_wallet()?;
let address = ethers::types::Address::from_str(&arguments.address)?;

let key_info = keystore
.read()
.unwrap()
.get(&address.into())?
.ok_or_else(|| anyhow!("key does not exists"))?;

let sk = libsecp256k1::SecretKey::parse_slice(key_info.private_key())?;
Ok(hex::encode(libsecp256k1::PublicKey::from_secret_key(&sk).serialize()).to_string())
}

fn pubkey_fvm(
provider: &IpcProvider,
arguments: &WalletPublicKeyArgs,
) -> anyhow::Result<String> {
let wallet = provider.fvm_wallet()?;

let addr = Address::from_str(&arguments.address)?;
let key_info = wallet.write().unwrap().export(&addr)?;

let sk = libsecp256k1::SecretKey::parse_slice(key_info.private_key())?;
Ok(hex::encode(libsecp256k1::PublicKey::from_secret_key(&sk).serialize()).to_string())
}
}

#[async_trait]
impl CommandLineHandler for WalletPublicKey {
type Arguments = WalletPublicKeyArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("export wallet with args: {:?}", arguments);

let provider = get_ipc_provider(global)?;

let wallet_type = WalletType::from_str(&arguments.wallet_type)?;
let v = match wallet_type {
WalletType::Evm => WalletPublicKey::pubkey_evm(&provider, arguments),
WalletType::Fvm => WalletPublicKey::pubkey_fvm(&provider, arguments),
}?;
println!("{v}");
Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "Return public key from a wallet address")]
pub(crate) struct WalletPublicKeyArgs {
#[arg(long, short, help = "Address of the key to export")]
pub address: String,
#[arg(long, short, help = "The type of the wallet, i.e. fvm, evm")]
pub wallet_type: String,
}
4 changes: 3 additions & 1 deletion ipc/cli/src/commands/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap::{Args, Subcommand};
use self::default::{
WalletGetDefault, WalletGetDefaultArgs, WalletSetDefault, WalletSetDefaultArgs,
};
use self::export::{WalletExport, WalletExportArgs};
use self::export::{WalletExport, WalletExportArgs, WalletPublicKey, WalletPublicKeyArgs};
use self::import::{WalletImport, WalletImportArgs};
use self::remove::{WalletRemove, WalletRemoveArgs};

Expand Down Expand Up @@ -38,6 +38,7 @@ impl WalletCommandsArgs {
Commands::Remove(args) => WalletRemove::handle(global, args).await,
Commands::SetDefault(args) => WalletSetDefault::handle(global, args).await,
Commands::GetDefault(args) => WalletGetDefault::handle(global, args).await,
Commands::PubKey(args) => WalletPublicKey::handle(global, args).await,
}
}
}
Expand All @@ -51,4 +52,5 @@ pub(crate) enum Commands {
Remove(WalletRemoveArgs),
SetDefault(WalletSetDefaultArgs),
GetDefault(WalletGetDefaultArgs),
PubKey(WalletPublicKeyArgs),
}
2 changes: 1 addition & 1 deletion ipc/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ anyhow = { workspace = true }
base64 = { workspace = true }
blake2b_simd = { workspace = true }
rand = { workspace = true }
libsecp256k1 = { workspace = true }
ahash = "0.8"
libsecp256k1 = "0.7"
argon2 = "0.5"
xsalsa20poly1305 = "0.9"
serde_ipld_dagcbor = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion ipc/provider/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const JSON_RPC_VERSION: &str = "2.0";
/// DefaulDEFAULT_CHAIN_IDSUBNET_e
pub const DEFAULT_CONFIG_TEMPLATE: &str = r#"
# Default configuration for Filecoin Calibration
repo_path = "~/.ipc"
keystore_path = "~/.ipc"
[[subnets]]
id = "/r314159"
Expand Down
4 changes: 3 additions & 1 deletion ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@ fn new_fvm_wallet_from_config(config: Arc<Config>) -> anyhow::Result<KeyStore> {
if let Some(repo_str) = repo_str {
new_fvm_keystore_from_path(repo_str)
} else {
Err(anyhow!("No keystore repo found in config"))
Err(anyhow!(
"No keystore repo found in config. Try using absolute path"
))
}
}

Expand Down

0 comments on commit f39529d

Please sign in to comment.