Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Problem:(CRO-479) no HD wallet support in client-rpc
Browse files Browse the repository at this point in the history
fix hdwallet creation
  • Loading branch information
leejw51crypto committed Oct 18, 2019
1 parent 38ad160 commit 60a8bfd
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion client-rpc/src/rpc/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use client_core::types::TransactionChange;
use client_core::{MultiSigWalletClient, WalletClient};

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

use secstr::*;
#[rpc]
pub trait WalletRpc: Send + Sync {
#[rpc(name = "wallet_balance")]
Expand All @@ -25,6 +25,12 @@ pub trait WalletRpc: Send + Sync {
#[rpc(name = "wallet_create")]
fn create(&self, request: WalletRequest) -> Result<String>;

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

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

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

Expand Down Expand Up @@ -101,6 +107,55 @@ where
Ok(request.name)
}

fn create_hd(&self, request: WalletRequest) -> Result<String> {
let mnemonics = self.client.new_mnemonics().map_err(to_rpc_error)?;
let mnemonics_phrase = SecUtf8::from(mnemonics.to_string());

// make seed for hd-wallet
if let Err(err) =
self.client
.new_hdwallet(&request.name, &request.passphrase, &mnemonics_phrase)
{
return Err(to_rpc_error(err));
}
// make basic wallet
// only generation is different with basic wallet
if let Err(err) = self.client.new_wallet(&request.name, &request.passphrase) {
return Err(to_rpc_error(err));
}

self.client
.new_staking_address(&request.name, &request.passphrase)
.map_err(to_rpc_error)?;
self.client
.new_transfer_address(&request.name, &request.passphrase)
.map_err(to_rpc_error)?;
Ok(mnemonics.to_string())
}

fn restore_hd(&self, request: WalletRequest, mnemonics: String) -> Result<String> {
let mnemonics_phrase = SecUtf8::from(mnemonics.to_string());
if let Err(err) =
self.client
.new_hdwallet(&request.name, &request.passphrase, &mnemonics_phrase)
{
return Err(to_rpc_error(err));
}
// make basic wallet
// only generation is different with basic wallet
if let Err(err) = self.client.new_wallet(&request.name, &request.passphrase) {
return Err(to_rpc_error(err));
}

self.client
.new_staking_address(&request.name, &request.passphrase)
.map_err(to_rpc_error)?;
self.client
.new_transfer_address(&request.name, &request.passphrase)
.map_err(to_rpc_error)?;
Ok(request.name)
}

fn create_staking_address(&self, request: WalletRequest) -> Result<String> {
self.client
.new_staking_address(&request.name, &request.passphrase)
Expand Down

0 comments on commit 60a8bfd

Please sign in to comment.