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

[cli] Provide a util address command to resolve and convert address format #2618

Merged
merged 2 commits into from
Sep 13, 2024
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
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,
}
}
}
Loading