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

Commit

Permalink
Merge #486 #501
Browse files Browse the repository at this point in the history
486: Problem:(CRO-479) no HD wallet support in client-rpc r=tomtau a=leejw51crypto

Solution: add hdwallet api to client-rpc


501: Problem: unrestricted drone exec pipeline r=tomtau a=tomtau

Solution: added trigger conditions, such that
"exec" pipelines are only executed after bors actions

Co-authored-by: Jongwhan Lee <jonghwan@crypto.com>
Co-authored-by: Tomas Tauber <2410580+tomtau@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 22, 2019
3 parents adb2b25 + 81d0a55 + 7271972 commit 6806343
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 27 deletions.
10 changes: 9 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ steps:
- cd chain-tx-enclave/tx-validation && make clean && SGX_TEST=1 make
- cd bin && ./tx-validation-app

trigger:
branch:
- master
- staging
- trying
event:
- push

---

kind: pipeline
Expand Down Expand Up @@ -80,6 +88,6 @@ trigger:
- push
---
kind: signature
hmac: 21e4a81d9785eeaa92154bda9daa8cc6cda13169e459849e919b473e0e2be725
hmac: bdc2684e182cf00ebf7a8a2e7971884e5ff9ee193d16398aec745bcd1260cf6f

...
3 changes: 2 additions & 1 deletion client-core/src/types/wallet_kind.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use client_common::{Error, ErrorKind, Result};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use unicase::eq_ascii;
/// Wallet kinds
/// Basic: default wallet
/// HD: HD wallet
/// Hardware: hardware based wallets
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum WalletKind {
/// Basic Wallet
Basic,
Expand Down
181 changes: 167 additions & 14 deletions client-rpc/src/rpc/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ use chain_core::tx::TxObfuscated;
use chain_core::tx::{TxAux, TxEnclaveAux};
use client_common::{PublicKey, Result as CommonResult};
use client_core::types::TransactionChange;
use client_core::types::WalletKind;
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")]
fn balance(&self, request: WalletRequest) -> Result<Coin>;

#[rpc(name = "wallet_create")]
fn create(&self, request: WalletRequest) -> Result<String>;
fn create(&self, request: WalletRequest, walletkind: WalletKind) -> Result<String>;

fn create_basic(&self, request: WalletRequest) -> Result<String>;

fn create_hd(&self, request: WalletRequest) -> Result<String>;

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

#[rpc(name = "wallet_createStakingAddress")]
fn create_staking_address(&self, request: WalletRequest) -> Result<String>;
Expand Down Expand Up @@ -87,7 +95,62 @@ where
}
}

fn create(&self, request: WalletRequest) -> Result<String> {
fn create(&self, request: WalletRequest, kind: WalletKind) -> Result<String> {
match kind {
WalletKind::Basic => self.create_basic(request),
WalletKind::HD => self.create_hd(request),
}
}

fn create_basic(&self, request: WalletRequest) -> Result<String> {
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_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(&self, request: WalletRequest, mnemonics: SecUtf8) -> Result<String> {
if let Err(err) = self
.client
.new_hdwallet(&request.name, &request.passphrase, &mnemonics)
{
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));
}
Expand Down Expand Up @@ -417,7 +480,10 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();

wallet_rpc
.create(create_wallet_request("Default", "123456"))
.create(
create_wallet_request("Default", "123456"),
WalletKind::Basic,
)
.unwrap();
assert_eq!(
Coin::zero(),
Expand All @@ -435,7 +501,10 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();

wallet_rpc
.create(create_wallet_request("Default", "123456"))
.create(
create_wallet_request("Default", "123456"),
WalletKind::Basic,
)
.unwrap();

assert_eq!(
Expand All @@ -444,7 +513,10 @@ pub mod tests {
"Wallet with name (Default) already exists"
)),
wallet_rpc
.create(create_wallet_request("Default", "123456"))
.create(
create_wallet_request("Default", "123456"),
WalletKind::Basic
)
.unwrap_err()
);
}
Expand All @@ -456,7 +528,10 @@ pub mod tests {
assert_eq!(
"Default".to_owned(),
wallet_rpc
.create(create_wallet_request("Default", "123456"))
.create(
create_wallet_request("Default", "123456"),
WalletKind::Basic
)
.unwrap()
);

Expand All @@ -468,7 +543,9 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();
let wallet_request = create_wallet_request("Default", "123456");

wallet_rpc.create(wallet_request.clone()).unwrap();
wallet_rpc
.create(wallet_request.clone(), WalletKind::Basic)
.unwrap();

assert_eq!(
1,
Expand All @@ -492,7 +569,9 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();
let wallet_request = create_wallet_request("Default", "123456");

wallet_rpc.create(wallet_request.clone()).unwrap();
wallet_rpc
.create(wallet_request.clone(), WalletKind::Basic)
.unwrap();
assert_eq!(
1,
wallet_rpc
Expand All @@ -519,7 +598,9 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();
let wallet_request = create_wallet_request("Default", "123456");

wallet_rpc.create(wallet_request.clone()).unwrap();
wallet_rpc
.create(wallet_request.clone(), WalletKind::Basic)
.unwrap();

assert_eq!(
1,
Expand Down Expand Up @@ -547,7 +628,9 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();
let wallet_request = create_wallet_request("Default", "123456");

wallet_rpc.create(wallet_request.clone()).unwrap();
wallet_rpc
.create(wallet_request.clone(), WalletKind::Basic)
.unwrap();

assert_eq!(
wallet_rpc
Expand All @@ -565,13 +648,19 @@ pub mod tests {
assert_eq!(0, wallet_rpc.list().unwrap().len());

wallet_rpc
.create(create_wallet_request("Default", "123456"))
.create(
create_wallet_request("Default", "123456"),
WalletKind::Basic,
)
.unwrap();

assert_eq!(vec!["Default"], wallet_rpc.list().unwrap());

wallet_rpc
.create(create_wallet_request("Personal", "123456"))
.create(
create_wallet_request("Personal", "123456"),
WalletKind::Basic,
)
.unwrap();

let wallet_list = wallet_rpc.list().unwrap();
Expand All @@ -585,7 +674,9 @@ pub mod tests {
let wallet_rpc = setup_wallet_rpc();
let wallet_request = create_wallet_request("Default", "123456");

wallet_rpc.create(wallet_request.clone()).unwrap();
wallet_rpc
.create(wallet_request.clone(), WalletKind::Basic)
.unwrap();
assert_eq!(
0,
wallet_rpc
Expand Down Expand Up @@ -620,4 +711,66 @@ pub mod tests {
passphrase: SecUtf8::from(passphrase),
}
}

#[test]
fn hdwallet_should_create_hd_wallet() {
let wallet_rpc = setup_wallet_rpc();

wallet_rpc
.create(create_wallet_request("Default", "123456"), WalletKind::HD)
.unwrap();
}

#[test]
fn hdwallet_should_recover_hd_wallet() {
let wallet_rpc = setup_wallet_rpc();

let result=wallet_rpc
.restore(
create_wallet_request("Default", "123456"),
SecUtf8::from("online hire print other clock like betray vote hollow bus insect meadow replace two tape worry quality disease cabin girl tree pudding issue radar")
)
.unwrap();
assert!("Default" == result);
}

#[test]
fn wallet_can_send_amount_should_fail_with_insufficient_amount() {
let wallet_rpc = setup_wallet_rpc();

let result=wallet_rpc
.restore(
create_wallet_request("Default", "123456"),
SecUtf8::from("online hire print other clock like betray vote hollow bus insect meadow replace two tape worry quality disease cabin girl tree pudding issue radar")
)
.unwrap();
assert!("Default" == result);

let wallet_request = create_wallet_request("Default", "123456");

let result = wallet_rpc
.create_transfer_address(wallet_request.clone())
.unwrap();
assert!(
"dcro1cxsz9ayc9a93j98l2dqjc8nxnr3hgjt2an9s79w2mpnusap353gqdswd75" == result.to_string()
);

let to_result = wallet_rpc
.create_transfer_address(wallet_request.clone())
.unwrap();
assert!(
"dcro1kgdm0vg9sfymdln44vmlteyly3v4gglusfkmjpr7j6vc33jcl9dsgjqmef"
== to_result.to_string()
);

let viewkey = wallet_rpc.get_view_key(wallet_request.clone()).unwrap();

let send_result = wallet_rpc.send_to_address(
wallet_request.clone(),
to_result,
Coin::from(1_0000u32),
vec![viewkey],
);
assert!(send_result.is_err());
}
}
2 changes: 1 addition & 1 deletion integration-tests/client-rpc/test/network-ops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe("Staking", () => {

const walletName = generateWalletName();
const walletRequest = newWalletRequest(walletName, "123456");
await rpcClient.request("wallet_create", [walletRequest]);
await rpcClient.request("wallet_create", [walletRequest, "Basic"]);
const stakingAddress = await asyncMiddleman(
rpcClient.request("wallet_createStakingAddress", [walletRequest]),
"Error when creating staking address",
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/client-rpc/test/wallet-auto-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("Wallet Auto-sync", () => {
const transferAmount = "1000";

await asyncMiddleman(
zeroFeeRpcClient.request("wallet_create", [receiverWalletRequest]),
zeroFeeRpcClient.request("wallet_create", [receiverWalletRequest, "Basic"]),
"Error when creating receiver wallet",
);

Expand Down
15 changes: 8 additions & 7 deletions integration-tests/client-rpc/test/wallet-management.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe("Wallet management", () => {
const walletRequest = newWalletRequest(walletName, "123456");

const walletCreateResult = await client.request("wallet_create", [
walletRequest,
walletRequest
, "Basic"
]);
expect(walletCreateResult).to.deep.eq(walletName);

Expand All @@ -71,7 +72,7 @@ describe("Wallet management", () => {
const walletRequest = newWalletRequest(walletName, "123456");

const walletCreateResponse = await client.request("wallet_create", [
walletRequest,
walletRequest, "Basic"
]);
expect(walletCreateResponse).to.deep.eq(walletName);

Expand All @@ -95,12 +96,12 @@ describe("Wallet management", () => {
const walletRequest = newWalletRequest(walletName, "123456");

const walletCreateResponse = await client.request("wallet_create", [
walletRequest,
walletRequest,"Basic"
]);
expect(walletCreateResponse).to.deep.eq(walletName);

return expect(
client.request("wallet_create", [walletRequest]),
client.request("wallet_create", [walletRequest,"Basic"]),
).to.eventually.rejectedWith(
`Invalid input: Wallet with name (${walletName}) already exists`,
);
Expand All @@ -112,7 +113,7 @@ describe("Wallet management", () => {
const walletRequest = newWalletRequest(walletName, walletPassphrase);

await expect(
client.request("wallet_create", [walletRequest]),
client.request("wallet_create", [walletRequest,"Basic"]),
).to.eventually.deep.eq(walletName);

const incorrectWalletPassphrase = "different_passphrase";
Expand Down Expand Up @@ -140,7 +141,7 @@ describe("Wallet management", () => {
const walletPassphrase = "passphrase";
const walletRequest = newWalletRequest(walletName, walletPassphrase);

await client.request("wallet_create", [walletRequest]);
await client.request("wallet_create", [walletRequest, "Basic"]);

const transferAddress = await client.request("wallet_createTransferAddress", [
walletRequest,
Expand All @@ -159,7 +160,7 @@ describe("Wallet management", () => {
const walletPassphrase = "passphrase";
const walletRequest = newWalletRequest(walletName, walletPassphrase);

await client.request("wallet_create", [walletRequest]);
await client.request("wallet_create", [walletRequest,"Basic"]);

const stakingAddress = await client.request("wallet_createStakingAddress", [
walletRequest,
Expand Down
Loading

0 comments on commit 6806343

Please sign in to comment.