-
Notifications
You must be signed in to change notification settings - Fork 94
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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}, | ||
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod balance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest this mod to be renamed to |
||
pub mod binding; | ||
pub mod create; | ||
pub mod list; | ||
pub mod nullify; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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) | ||
} | ||
|
@@ -46,4 +48,5 @@ pub enum AccountCommand { | |
Switch(SwitchCommand), | ||
Nullify(NullifyCommand), | ||
Balance(BalanceCommand), | ||
Binding(BindingCommand), | ||
} |
There was a problem hiding this comment.
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.