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

[bitcoin] Cli bitcoin address binding #1282

Closed
wants to merge 2 commits into from
Closed
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: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/rooch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ parking_lot = { workspace = true }
bcs-ext = { workspace = true }
rpassword = { workspace = true }
fastcrypto = { workspace = true }
bitcoin = { workspace = true }
bitcoincore-rpc = { workspace = true }

move-bytecode-utils = { workspace = true }
move-binary-format = { workspace = true }
Expand Down
101 changes: 101 additions & 0 deletions crates/rooch/src/commands/account/commands/binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::{TransactionOptions, WalletContextOptions};
use bitcoincore_rpc::{Auth, Client, RpcApi};
use clap::Parser;
use rooch_types::{
address::{BitcoinAddress, MultiChainAddress, RoochAddress},

Choose a reason for hiding this comment

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

Isn't it included BitcoinAddress to the MultiChainAddress? We were using only two types of addresses before.

error::{RoochError, RoochResult},
multichain_id::RoochMultiChainID,
};

/// Binding a
#[derive(Debug, Parser)]
pub struct BindingCommand {
/// L1 chain id
#[clap(long, short = 'c')]
pub l1_chain: RoochMultiChainID,

/// L1 chain address
#[clap(long)]
pub l1_address: String,

#[clap(
long,
env = "BITCOIN_RPC_URL",

Choose a reason for hiding this comment

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

Is there a plan to include Bitcoin Lightning RPC URL?

requires = "btc-rpc-username",
requires = "btc-rpc-password"
)]
pub btc_rpc_url: String,

#[clap(long, id = "btc-rpc-username", env = "BTC_RPC_USERNAME")]
pub btc_rpc_username: String,

#[clap(long, id = "btc-rpc-password", env = "BTC_RPC_PASSWORD")]
pub btc_rpc_password: String,

#[clap(flatten)]
pub tx_options: TransactionOptions,

#[clap(flatten)]
pub context_options: WalletContextOptions,
}

impl BindingCommand {
pub async fn execute(self) -> RoochResult<()> {
let multichain_address = MultiChainAddress::try_from_str_with_multichain_id(
self.l1_chain,
self.l1_address.as_str(),
)?;
let wallet_context = self.context_options.build()?;
let sender = wallet_context.resolve_address(self.tx_options.sender)?;
match self.l1_chain {

Choose a reason for hiding this comment

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

Is there a binding for Ethereum L1?

RoochMultiChainID::Bitcoin => {
Self::binding_bitcoin_adddress(
self.btc_rpc_url,
self.btc_rpc_username,
self.btc_rpc_password,
sender.into(),
multichain_address.try_into()?,
)
.await?;
Ok(())
}
RoochMultiChainID::Rooch => Err(RoochError::CommandArgumentError(
"Can not binding Rooch address".to_owned(),
)),
_ => Err(RoochError::CommandArgumentError(
"Does not support this chain".to_owned(),
)),
}
}

async fn binding_bitcoin_adddress(
btc_rpc_url: String,
btc_rpc_user_name: String,
btc_rpc_password: String,
_sender: RoochAddress,
bitcoin_address: BitcoinAddress,
) -> anyhow::Result<String> {
let rpc = Client::new(
btc_rpc_url.as_str(),
Auth::UserPass(btc_rpc_user_name, btc_rpc_password),
)?;
let chain_info = rpc.get_blockchain_info()?;
let bitcoin_chain_network =
rooch_types::bitcoin::network::Network::try_from(chain_info.chain)?;
let bitcoin_address_str = bitcoin_address.format(bitcoin_chain_network.to_num())?;
let message = "rooch";
let result = rpc
.call::<String>(
"signmessage",
&[
serde_json::to_value(bitcoin_address_str)?,
serde_json::to_value(message)?,
],
)
.map_err(|e| RoochError::UnexpectedError(e.to_string()))?;
Ok(result)
}
}
1 change: 1 addition & 0 deletions crates/rooch/src/commands/account/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

pub mod balance;

Choose a reason for hiding this comment

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

I suggest this mod to be renamed to inquire? Never mind if it's only for inquire about account balance.

pub mod binding;
pub mod create;
pub mod list;
pub mod nullify;
Expand Down
5 changes: 4 additions & 1 deletion crates/rooch/src/commands/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::cli_types::CommandAction;
use crate::commands::account::commands::balance::BalanceCommand;
use async_trait::async_trait;
use commands::{
create::CreateCommand, list::ListCommand, nullify::NullifyCommand, switch::SwitchCommand,
binding::BindingCommand, create::CreateCommand, list::ListCommand, nullify::NullifyCommand,

Choose a reason for hiding this comment

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

Is there an option for updating the existing or created bitcoin address on Rooch?

switch::SwitchCommand,
};
use rooch_types::error::{RoochError, RoochResult};
use std::path::PathBuf;
Expand Down Expand Up @@ -33,6 +34,7 @@ impl CommandAction<String> for Account {
AccountCommand::Switch(switch) => switch.execute().await.map(|_| "".to_owned()),
AccountCommand::Nullify(nullify) => nullify.execute().await.map(|_| "".to_owned()),
AccountCommand::Balance(balance) => balance.execute().await.map(|_| "".to_owned()),
AccountCommand::Binding(binding) => binding.execute().await.map(|_| "".to_owned()),
}
.map_err(RoochError::from)
}
Expand All @@ -46,4 +48,5 @@ pub enum AccountCommand {
Switch(SwitchCommand),
Nullify(NullifyCommand),
Balance(BalanceCommand),
Binding(BindingCommand),
}
Loading