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

Add rooch account sign command #2431

Merged
merged 6 commits into from
Aug 14, 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
17 changes: 17 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/account_sign_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct AccountSignView {
pub msg: String,
pub signature: String,
}

impl AccountSignView {
pub fn new(msg: String, signature: String) -> Self {
Self { msg, signature }
}
}
2 changes: 2 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ pub use rpc_options::*;
pub use state_view::*;
pub use str_view::*;
pub use transaction_argument_view::*;

pub mod account_sign_view;
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 @@ -7,5 +7,6 @@ pub mod export;
pub mod import;
pub mod list;
pub mod nullify;
pub mod sign;
pub mod switch;
pub mod transfer;
62 changes: 62 additions & 0 deletions crates/rooch/src/commands/account/commands/sign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::{CommandAction, WalletContextOptions};
use async_trait::async_trait;
use clap::Parser;
use hex::ToHex;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_rpc_api::jsonrpc_types::account_sign_view::AccountSignView;
use rooch_types::{
address::ParsedAddress,
error::{RoochError, RoochResult},
};

/// Sign an msg with current account private key (sign_hashed)
///
/// This operation must be specified with -a or
/// --address to export only one address with a private key.
#[derive(Debug, Parser)]
pub struct SignCommand {
#[clap(short = 'a', long = "address", value_parser=ParsedAddress::parse, default_value = "")]
address: ParsedAddress,
#[clap(flatten)]
pub context_options: WalletContextOptions,

/// Return command outputs in json format
#[clap(long, default_value = "false")]
json: bool,

/// Msg command will sign
#[clap(long, default_value = "")]
msg: String,
}

#[async_trait]
impl CommandAction<Option<AccountSignView>> for SignCommand {
async fn execute(self) -> RoochResult<Option<AccountSignView>> {
let context = self.context_options.build_require_password()?;
let password = context.get_password();

let mapping = context.address_mapping();
let addrss = self.address.into_rooch_address(&mapping).map_err(|e| {
RoochError::CommandArgumentError(format!("Invalid Rooch address String: {}", e))
})?;

let signature =
context
.keystore
.sign_hashed(&addrss, &self.msg.clone().into_bytes(), password)?;

if self.json {
Ok(Some(AccountSignView::new(
self.msg.clone(),
signature.encode_hex(),
)))
} else {
println!("Msg you input : {}", &self.msg);
println!("Signature : {}", signature.encode_hex::<String>());
Ok(None)
}
}
}
10 changes: 5 additions & 5 deletions crates/rooch/src/commands/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::CommandAction;
use crate::commands::account::commands::balance::BalanceCommand;
use crate::commands::account::commands::export::ExportCommand;
use async_trait::async_trait;
use commands::{
create::CreateCommand, import::ImportCommand, list::ListCommand, nullify::NullifyCommand,
switch::SwitchCommand, transfer::TransferCommand,
balance::BalanceCommand, create::CreateCommand, export::ExportCommand, import::ImportCommand,
list::ListCommand, nullify::NullifyCommand, sign::SignCommand, switch::SwitchCommand,
transfer::TransferCommand,
};
use rooch_types::error::RoochResult;
use std::path::PathBuf;

pub mod commands;

/// Tool for interacting with accounts
#[derive(clap::Parser)]
pub struct Account {
Expand All @@ -36,6 +34,7 @@ impl CommandAction<String> for Account {
AccountCommand::Transfer(transfer) => transfer.execute_serialized().await,
AccountCommand::Export(export) => export.execute_serialized().await,
AccountCommand::Import(import) => import.execute_serialized().await,
AccountCommand::Sign(sign) => sign.execute_serialized().await,
}
}
}
Expand All @@ -51,4 +50,5 @@ pub enum AccountCommand {
Transfer(TransferCommand),
Export(ExportCommand),
Import(ImportCommand),
Sign(SignCommand),
}
Loading