From 76826cf09fd32a6bf67058fb49df45b87be43049 Mon Sep 17 00:00:00 2001 From: jolestar Date: Thu, 12 Sep 2024 11:43:54 +0800 Subject: [PATCH 1/2] [cli] Provide a util address command to resolve and convert address format --- crates/rooch/src/commands/util/commands/mod.rs | 1 + crates/rooch/src/commands/util/mod.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/rooch/src/commands/util/commands/mod.rs b/crates/rooch/src/commands/util/commands/mod.rs index 0eca351d05..b44cb51aca 100644 --- a/crates/rooch/src/commands/util/commands/mod.rs +++ b/crates/rooch/src/commands/util/commands/mod.rs @@ -1,4 +1,5 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 +pub mod address; pub mod hex; diff --git a/crates/rooch/src/commands/util/mod.rs b/crates/rooch/src/commands/util/mod.rs index fd2675c309..b7c426922c 100644 --- a/crates/rooch/src/commands/util/mod.rs +++ b/crates/rooch/src/commands/util/mod.rs @@ -4,7 +4,7 @@ use crate::CommandAction; use async_trait::async_trait; use clap::{Parser, Subcommand}; -use commands::hex::HexCommand; +use commands::{address::AddressCommand, hex::HexCommand}; use rooch_types::error::RoochResult; pub mod commands; @@ -19,6 +19,7 @@ pub struct Util { #[clap(name = "util")] pub enum UtilCommand { Hex(HexCommand), + Address(AddressCommand), } #[async_trait] @@ -26,6 +27,7 @@ impl CommandAction for Util { async fn execute(self) -> RoochResult { match self.cmd { UtilCommand::Hex(c) => c.execute().await, + UtilCommand::Address(c) => c.execute_serialized().await, } } } From 752fd028f68b90711da89c7f6fe4f98348bb849a Mon Sep 17 00:00:00 2001 From: jolestar Date: Thu, 12 Sep 2024 11:44:42 +0800 Subject: [PATCH 2/2] fixup --- .../src/commands/util/commands/address.rs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 crates/rooch/src/commands/util/commands/address.rs diff --git a/crates/rooch/src/commands/util/commands/address.rs b/crates/rooch/src/commands/util/commands/address.rs new file mode 100644 index 0000000000..848583e225 --- /dev/null +++ b/crates/rooch/src/commands/util/commands/address.rs @@ -0,0 +1,46 @@ +// Copyright (c) RoochNetwork +// SPDX-License-Identifier: Apache-2.0 + +use crate::cli_types::{CommandAction, WalletContextOptions}; +use async_trait::async_trait; +use clap::Parser; +use rooch_types::{address::ParsedAddress, error::RoochResult}; +use serde::{Deserialize, Serialize}; + +/// Tool for convert address format +#[derive(Debug, Parser)] +pub struct AddressCommand { + /// Address to convert, any format which rooch supports + addr: ParsedAddress, + + #[clap(flatten)] + pub context_options: WalletContextOptions, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AddressOutput { + pub rooch_address: String, + pub hex_address: String, + pub bitcoin_main_address: String, + pub bitcoin_test_address: String, + pub bitcoin_regtest_address: String, + pub bitcoin_segtest_address: String, +} + +#[async_trait] +impl CommandAction for AddressCommand { + async fn execute(self) -> RoochResult { + let context = self.context_options.build()?; + let rooch_addr = context.resolve_rooch_address(self.addr.clone())?; + let bitcoin_addr = context.resolve_bitcoin_address(self.addr).await?; + + Ok(AddressOutput { + rooch_address: rooch_addr.to_string(), + hex_address: rooch_addr.to_hex_literal(), + bitcoin_main_address: bitcoin_addr.format(bitcoin::Network::Bitcoin)?.to_string(), + bitcoin_test_address: bitcoin_addr.format(bitcoin::Network::Testnet)?.to_string(), + bitcoin_regtest_address: bitcoin_addr.format(bitcoin::Network::Regtest)?.to_string(), + bitcoin_segtest_address: bitcoin_addr.format(bitcoin::Network::Signet)?.to_string(), + }) + } +}