Skip to content

Commit

Permalink
Problem: (Fix crypto-com#972) Missing MultiSig public keys methods in…
Browse files Browse the repository at this point in the history
… JSONRPC and ClientCli

Soltion: Add new and list methods to JSONRPC and ClientCLI
  • Loading branch information
calvinlauyh committed Jan 31, 2020
1 parent ef253f0 commit b451ce4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ __pycache__
*.egg-info

# Storage
/.storage*
/.storage
/.storage.bak
/.cro-storage
/.cro-storage*

# OS
.DS_Store
Expand Down
12 changes: 12 additions & 0 deletions client-cli/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod address_command;
mod multisig_command;
mod transaction_command;
mod wallet_command;

Expand Down Expand Up @@ -37,6 +38,7 @@ use client_network::network_ops::{DefaultNetworkOpsClient, NetworkOpsClient};
use log::warn;

use self::address_command::AddressCommand;
use self::multisig_command::MultiSigCommand;
use self::transaction_command::TransactionCommand;
use self::wallet_command::WalletCommand;
use crate::{ask_seckey, storage_path, tendermint_url};
Expand Down Expand Up @@ -167,6 +169,11 @@ pub enum Command {
)]
block_height_ensure: u64,
},
#[structopt(name = "multisig", about = "MultiSig operations")]
MultiSig {
#[structopt(subcommand)]
multisig_command: MultiSigCommand,
},
}

/// normal
Expand Down Expand Up @@ -312,6 +319,11 @@ impl Command {
);
Self::resync(config, name.clone(), enckey, *force)
}
Command::MultiSig { multisig_command } => {
let storage = SledStorage::new(storage_path())?;
let wallet_client = DefaultWalletClient::new_read_only(storage);
multisig_command.execute(wallet_client)
}
}
}

Expand Down
82 changes: 82 additions & 0 deletions client-cli/src/command/multisig_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use quest::success;
use structopt::StructOpt;

use client_common::{PublicKey, Result};
use client_core::types::AddressType;
use client_core::WalletClient;

use crate::ask_seckey;

#[derive(Debug, StructOpt)]
pub enum MultiSigCommand {
#[structopt(
name = "new-address-public-key",
about = "Creates a new public key for MultiSig address"
)]
NewAddressPublicKey {
#[structopt(
name = "wallet name",
short = "n",
long = "name",
help = "Name of wallet"
)]
name: String,
},

#[structopt(
name = "list-address-public-keys",
about = "List public keys for MultiSig address"
)]
ListAddressPublicKeys {
#[structopt(
name = "wallet name",
short = "n",
long = "name",
help = "Name of wallet"
)]
name: String,
},
}

impl MultiSigCommand {
pub fn execute<T: WalletClient>(&self, wallet_client: T) -> Result<()> {
match self {
MultiSigCommand::NewAddressPublicKey { name } => {
new_address_public_key(wallet_client, name)
}
MultiSigCommand::ListAddressPublicKeys { name } => {
list_address_public_keys(wallet_client, name)
}
}
}
}

fn new_address_public_key<T: WalletClient>(wallet_client: T, name: &str) -> Result<()> {
let enckey = ask_seckey(None)?;

let public_key = wallet_client
.new_public_key(name, &enckey, Some(AddressType::Transfer))
.map(|public_key| public_key.to_string())?;

success(&format!("Public key: {}", public_key));

Ok(())
}

fn list_address_public_keys<T: WalletClient>(wallet_client: T, name: &str) -> Result<()> {
let enckey = ask_seckey(None)?;

let public_keys: Vec<PublicKey> = wallet_client
.public_keys(name, &enckey)
.map(|keys| keys.into_iter().collect())?;

if public_keys.is_empty() {
success("No public key found!");
return Ok(());
}
for public_key in public_keys {
success(&format!("Public key: {}", public_key.to_string()))
}

Ok(())
}
21 changes: 21 additions & 0 deletions client-rpc/src/rpc/multisig_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ use jsonrpc_derive::rpc;
use chain_core::common::{H256, HASH_SIZE_256};
use chain_core::tx::data::Tx;
use client_common::{Error, ErrorKind, PublicKey, Result as CommonResult, ResultExt, SecKey};
use client_core::types::AddressType;
use client_core::{MultiSigWalletClient, WalletClient};

use crate::server::{to_rpc_error, WalletRequest};

#[rpc]
pub trait MultiSigRpc: Send + Sync {
#[rpc(name = "multiSig_newAddressPublicKey")]
fn new_address_public_key(&self, request: WalletRequest) -> Result<String>;

#[rpc(name = "multiSig_listAddressPublicKeys")]
fn list_address_public_keys(&self, request: WalletRequest) -> Result<Vec<PublicKey>>;

#[rpc(name = "multiSig_createAddress")]
fn create_address(
&self,
Expand Down Expand Up @@ -97,6 +104,20 @@ impl<T> MultiSigRpc for MultiSigRpcImpl<T>
where
T: WalletClient + MultiSigWalletClient + 'static,
{
fn new_address_public_key(&self, request: WalletRequest) -> Result<String> {
self.client
.new_public_key(&request.name, &request.enckey, Some(AddressType::Transfer))
.map(|public_key| public_key.to_string())
.map_err(to_rpc_error)
}

fn list_address_public_keys(&self, request: WalletRequest) -> Result<Vec<PublicKey>> {
self.client
.public_keys(&request.name, &request.enckey)
.map(|keys| keys.into_iter().collect())
.map_err(to_rpc_error)
}

fn create_address(
&self,
request: WalletRequest,
Expand Down
22 changes: 1 addition & 21 deletions client-rpc/src/rpc/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use secstr::SecUtf8;
use chain_core::init::coin::Coin;
use chain_core::tx::data::address::ExtendedAddr;
use client_common::{PrivateKey, PublicKey, Result as CommonResult, SecKey};
use client_core::types::TransactionChange;
use client_core::types::WalletBalance;
use client_core::types::WalletKind;
use client_core::types::{AddressType, TransactionChange};
use client_core::{Mnemonic, MultiSigWalletClient, UnspentTransactions, WalletClient};

use crate::server::{rpc_error_from_string, to_rpc_error, CreateWalletRequest, WalletRequest};
Expand All @@ -35,9 +35,6 @@ pub trait WalletRpc: Send + Sync {
#[rpc(name = "wallet_delete")]
fn delete(&self, request: CreateWalletRequest) -> Result<()>;

#[rpc(name = "wallet_newMultiSigAddressPublicKey")]
fn new_multi_sig_address_public_key(&self, request: WalletRequest) -> Result<String>;

#[rpc(name = "wallet_createStakingAddress")]
fn create_staking_address(&self, request: WalletRequest) -> Result<String>;

Expand All @@ -64,9 +61,6 @@ pub trait WalletRpc: Send + Sync {
#[rpc(name = "wallet_list")]
fn list(&self) -> Result<Vec<String>>;

#[rpc(name = "wallet_listPublicKeys")]
fn list_public_keys(&self, request: WalletRequest) -> Result<Vec<PublicKey>>;

#[rpc(name = "wallet_listStakingAddresses")]
fn list_staking_addresses(&self, request: WalletRequest) -> Result<Vec<String>>;

Expand Down Expand Up @@ -194,13 +188,6 @@ where
.map_err(to_rpc_error)
}

fn new_multi_sig_address_public_key(&self, request: WalletRequest) -> Result<String> {
self.client
.new_public_key(&request.name, &request.enckey, Some(AddressType::Transfer))
.map(|public_key| public_key.to_string())
.map_err(to_rpc_error)
}

fn create_staking_address(&self, request: WalletRequest) -> Result<String> {
self.client
.new_staking_address(&request.name, &request.enckey)
Expand Down Expand Up @@ -263,13 +250,6 @@ where
self.client.wallets().map_err(to_rpc_error)
}

fn list_public_keys(&self, request: WalletRequest) -> Result<Vec<PublicKey>> {
self.client
.public_keys(&request.name, &request.enckey)
.map(|keys| keys.into_iter().collect())
.map_err(to_rpc_error)
}

fn list_staking_addresses(&self, request: WalletRequest) -> Result<Vec<String>> {
self.client
.staking_addresses(&request.name, &request.enckey)
Expand Down

0 comments on commit b451ce4

Please sign in to comment.