Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support btc in wallet #11

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions ipc/cli/src/commands/wallet/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ impl CommandLineHandler for WalletBalances {
println!("{:?} - Balance: {}", addr, balance);
}
}
WalletType::Btc => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use the same (except the wallet name) implementation as the evm wallet type? It looks like it forwards the request to the manager

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle in #16

unimplemented!()
}
};

Ok(())
Expand Down
16 changes: 12 additions & 4 deletions ipc/cli/src/commands/wallet/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ impl CommandLineHandler for WalletSetDefault {
let wallet_type = WalletType::from_str(&arguments.wallet_type)?;

match wallet_type {
WalletType::Evm => {
let wallet = provider.evm_wallet()?;
WalletType::Evm | WalletType::Btc => {
let wallet = if wallet_type == WalletType::Evm {
provider.evm_wallet()?
} else {
provider.btc_wallet()?
};
let addr = ipc_wallet::EthKeyAddress::from_str(&arguments.address)?;
wallet.write().unwrap().set_default(&addr)?;
}
Expand Down Expand Up @@ -60,8 +64,12 @@ impl CommandLineHandler for WalletGetDefault {
let wallet_type = WalletType::from_str(&arguments.wallet_type)?;

match wallet_type {
WalletType::Evm => {
let wallet = provider.evm_wallet()?;
WalletType::Evm | WalletType::Btc => {
let wallet = if wallet_type == WalletType::Evm {
provider.evm_wallet()?
} else {
provider.btc_wallet()?
};
let mut wallet = wallet.write().unwrap();
match wallet.get_default()? {
None => println!("No default account set"),
Expand Down
28 changes: 26 additions & 2 deletions ipc/cli/src/commands/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ pub(crate) struct WalletExport;

impl WalletExport {
fn export_evm(provider: &IpcProvider, arguments: &WalletExportArgs) -> anyhow::Result<String> {
let keystore = provider.evm_wallet()?;
let wallet_type = WalletType::from_str(&arguments.wallet_type)?;
let keystore = if wallet_type == WalletType::Evm {
provider.evm_wallet()?
} else {
provider.btc_wallet()?
};

let address = ethers::types::Address::from_str(&arguments.address)?;

let key_info = keystore
Expand Down Expand Up @@ -76,7 +82,7 @@ impl CommandLineHandler for WalletExport {

let wallet_type = WalletType::from_str(&arguments.wallet_type)?;
let v = match wallet_type {
WalletType::Evm => WalletExport::export_evm(&provider, arguments),
WalletType::Evm | WalletType::Btc => WalletExport::export_evm(&provider, arguments),
WalletType::Fvm => WalletExport::export_fvm(&provider, arguments),
}?;

Expand Down Expand Up @@ -152,6 +158,23 @@ impl WalletPublicKey {
let sk = libsecp256k1::SecretKey::parse_slice(key_info.private_key())?;
Ok(hex::encode(libsecp256k1::PublicKey::from_secret_key(&sk).serialize()).to_string())
}

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

let key_info = keystore
.read()
.map_err(|e| anyhow::anyhow!("Failed to read btc keystore: {}", e))?
.get(&address.into())?
.ok_or_else(|| anyhow!("key does not exists"))?;

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

#[async_trait]
Expand All @@ -167,6 +190,7 @@ impl CommandLineHandler for WalletPublicKey {
let v = match wallet_type {
WalletType::Evm => WalletPublicKey::pubkey_evm(&provider, arguments),
WalletType::Fvm => WalletPublicKey::pubkey_fvm(&provider, arguments),
WalletType::Btc => WalletPublicKey::pubkey_btc(&provider, arguments),
}?;
println!("{v}");
Ok(())
Expand Down
23 changes: 17 additions & 6 deletions ipc/cli/src/commands/wallet/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ impl CommandLineHandler for WalletImport {
let wallet_type = WalletType::from_str(&arguments.wallet_type)?;

if let Some(key) = &arguments.private_key {
if !matches!(wallet_type, WalletType::Evm) {
bail!("--private-key only supported by --wallet-type=evm");
match wallet_type {
WalletType::Evm => println!(
"{:?}",
provider.import_evm_key_from_privkey(key)?.to_string()
),
WalletType::Fvm => bail!("--private-key not supported by --wallet-type=fvm"),
WalletType::Btc => println!(
"{:?}",
provider.import_btc_key_from_privkey(key)?.to_string()
),
}
println!(
"{:?}",
provider.import_evm_key_from_privkey(key)?.to_string()
);
Ok(())
} else {
// Get keyinfo from file or stdin
Expand All @@ -50,6 +54,13 @@ impl CommandLineHandler for WalletImport {

println!("{:?}", key.to_string())
}
WalletType::Btc => {
let key = provider
.import_btc_key_from_privkey(&keyinfo)
.or_else(|_| provider.import_btc_key_from_json(&keyinfo))?;

println!("{:?}", key.to_string())
}
};
Ok(())
}
Expand Down
21 changes: 21 additions & 0 deletions ipc/cli/src/commands/wallet/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ impl CommandLineHandler for WalletList {
}
Ok(())
}
WalletType::Btc => {
let wallet = provider.btc_wallet()?;
let addresses = wallet.read().unwrap().list()?;
for address in addresses.iter() {
print!("Address: {}", address);

let key_info = wallet.read().unwrap().get(address)?.unwrap();
let sk = libsecp256k1::SecretKey::parse_slice(key_info.private_key())?;
println!("\tSecret key: {}", hex::encode(sk.serialize()));

let pub_key =
hex::encode(libsecp256k1::PublicKey::from_secret_key(&sk).serialize())
.to_string();
println!("\tPubKey: {}", pub_key);

let x_only_pub_key =
hex::encode(ipc_wallet::get_xonly_public_key_serialized(&sk)?.to_vec());
println!("\tXonlyPubKey: {}", x_only_pub_key);
}
Ok(())
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ipc/cli/src/commands/wallet/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ impl CommandLineHandler for WalletNew {
)?;
println!("{:?}", provider.new_fvm_key(tp)?)
}
WalletType::Btc => {
println!("{:?}", provider.new_btc_key()?)
}
};

Ok(())
Expand Down
13 changes: 10 additions & 3 deletions ipc/cli/src/commands/wallet/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ impl CommandLineHandler for WalletRemove {
let wallet_type = WalletType::from_str(&arguments.wallet_type)?;

match wallet_type {
WalletType::Evm => {
let wallet = provider.evm_wallet()?;
WalletType::Evm | WalletType::Btc => {
let wallet = if wallet_type == WalletType::Evm {
provider.evm_wallet()?
} else {
provider.btc_wallet()?
};
let addr = ipc_wallet::EthKeyAddress::from_str(&arguments.address)?;
wallet.write().unwrap().remove(&addr)?;
wallet
.write()
.map_err(|e| anyhow::anyhow!("Failed to remove wallet: {}", e))?
.remove(&addr)?;
}
WalletType::Fvm => {
let wallet = provider.fvm_wallet()?;
Expand Down
2 changes: 1 addition & 1 deletion ipc/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ futures-util = { workspace = true }
hex = { workspace = true }
http = { workspace = true }
libsecp256k1 = { workspace = true }
libsecp256k1-core = "0.3.0"

log = { workspace = true }
num-traits = { workspace = true }
num-derive = { workspace = true }
Expand Down
Loading