Skip to content

Commit

Permalink
[cli] Provide a util address command to resolve and convert address f…
Browse files Browse the repository at this point in the history
…ormat (#2618)
  • Loading branch information
jolestar authored Sep 13, 2024
1 parent 400c55a commit e3c77c2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
46 changes: 46 additions & 0 deletions crates/rooch/src/commands/util/commands/address.rs
Original file line number Diff line number Diff line change
@@ -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<AddressOutput> for AddressCommand {
async fn execute(self) -> RoochResult<AddressOutput> {
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(),
})
}
}
1 change: 1 addition & 0 deletions crates/rooch/src/commands/util/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

pub mod address;
pub mod hex;
4 changes: 3 additions & 1 deletion crates/rooch/src/commands/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,13 +19,15 @@ pub struct Util {
#[clap(name = "util")]
pub enum UtilCommand {
Hex(HexCommand),
Address(AddressCommand),
}

#[async_trait]
impl CommandAction<String> for Util {
async fn execute(self) -> RoochResult<String> {
match self.cmd {
UtilCommand::Hex(c) => c.execute().await,
UtilCommand::Address(c) => c.execute_serialized().await,
}
}
}

0 comments on commit e3c77c2

Please sign in to comment.