Merge milestone/2.1.0 into master (#199)

* version bump for next potential release

* Merge master into milestone/2.1.0 (#182)

* Derive --version output dynamically from cargo package version (#174)

* add --txid to the `wallet txs` command (#176)

* add --txid to the `wallet txs` command

* add test for `wallet txs` command with `--txid` parameter

* Refactor - Split WalletCommAdapter into multiple traits (#180)

* Derive --version output dynamically from cargo package version (#174)

* add server auth argument to http client

* Revert "add server auth argument to http client"

This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.

* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing

* Split WalletCommAdapter into four traits, one for each of its intended behaviors.

* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used

* Remove unused "params" argument from listen method.

* Re-add previously existing TODO comment

* Fix non-test build

* completely Fix non-test build

* Full Lifecycle API Support (#184)

* refactoring wallet lib traits

* rustfmt

* rustfmt

* add new files

* explicit lifetime specifiers on all wallet traits

* rustfmt

* modify apis to use new walletinst

* rustfmt

* converting controller crate

* rustfmt

* controller crate compiling

* rustfmt

* compilation

* rustfmt

* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl

* rustfmt

* full compilation, changing recovery + init to new model

* rustfmt

* wallet initialisation working, init command output and flow identical to v2.0.0 wallet

* rustfmt

* fix listener and owner api startup

* rustfmt

* rustfmt

* move encryption test

* rustfmt

* fix api doctests

* rustfmt

* fix for most tests in controller crate

* rustfmt

* fix for check tests in controller crate

* fix main wallet tests

* rustfmt

* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow

* rustfmt

* update keybase adapter to use new wallet format

* rustfmt

* test fix

* remove debug output
This commit is contained in:
Yeastplume
2019-07-29 13:25:03 +01:00
committed by GitHub
parent 7b6d597d39
commit c40e8a915b
54 changed files with 2405 additions and 1942 deletions
+78 -48
View File
@@ -14,11 +14,13 @@
// Keybase Wallet Plugin
use crate::adapters::{SlateReceiver, SlateSender};
use crate::config::WalletConfig;
use crate::keychain::ExtKeychain;
use crate::libwallet::api_impl::foreign;
use crate::libwallet::{Error, ErrorKind, Slate};
use crate::{instantiate_wallet, HTTPNodeClient, WalletCommAdapter};
use failure::ResultExt;
use crate::libwallet::{Error, ErrorKind, Slate, WalletInst};
use crate::util::ZeroingString;
use crate::{DefaultLCProvider, DefaultWalletImpl, HTTPNodeClient};
use serde::Serialize;
use serde_json::{from_str, json, to_string, Value};
use std::collections::{HashMap, HashSet};
@@ -36,25 +38,40 @@ const SLATE_NEW: &str = "grin_slate_new";
const SLATE_SIGNED: &str = "grin_slate_signed";
#[derive(Clone)]
pub struct KeybaseWalletCommAdapter {}
pub struct KeybaseChannel(String);
impl KeybaseWalletCommAdapter {
impl KeybaseChannel {
/// Check if keybase is installed and return an adapter object.
pub fn new() -> Box<WalletCommAdapter> {
let mut proc = if cfg!(target_os = "windows") {
Command::new("where")
} else {
Command::new("which")
};
proc.arg("keybase")
.stdout(Stdio::null())
.status()
.expect("Keybase executable not found, make sure it is installed and in your PATH");
pub fn new(channel: String) -> Result<KeybaseChannel, Error> {
// Limit only one recipient
if channel.matches(",").count() > 0 {
return Err(
ErrorKind::GenericError("Only one recipient is supported!".to_owned()).into(),
);
}
Box::new(KeybaseWalletCommAdapter {})
if !keybase_installed() {
return Err(ErrorKind::GenericError(
"Keybase executable not found, make sure it is installed and in your PATH"
.to_owned(),
)
.into());
}
Ok(KeybaseChannel(channel))
}
}
/// Check if keybase executable exists in path
fn keybase_installed() -> bool {
let mut proc = if cfg!(target_os = "windows") {
Command::new("where")
} else {
Command::new("which")
};
proc.arg("keybase").stdout(Stdio::null()).status().is_ok()
}
/// Send a json object to the keybase process. Type `keybase chat api --help` for a list of available methods.
fn api_send(payload: &str) -> Result<Value, Error> {
let mut proc = Command::new("keybase");
@@ -280,23 +297,13 @@ fn poll(nseconds: u64, channel: &str) -> Option<Slate> {
None
}
impl WalletCommAdapter for KeybaseWalletCommAdapter {
fn supports_sync(&self) -> bool {
true
}
// Send a slate to a keybase username then wait for a response for TTL seconds.
fn send_tx_sync(&self, addr: &str, slate: &Slate) -> Result<Slate, Error> {
// Limit only one recipient
if addr.matches(",").count() > 0 {
error!("Only one recipient is supported!");
return Err(ErrorKind::GenericError("Tx rejected".to_owned()))?;
}
impl SlateSender for KeybaseChannel {
/// Send a slate to a keybase username then wait for a response for TTL seconds.
fn send_tx(&self, slate: &Slate) -> Result<Slate, Error> {
let id = slate.id;
// Send original slate to recipient with the SLATE_NEW topic
match send(&slate, addr, SLATE_NEW, TTL) {
match send(&slate, &self.0, SLATE_NEW, TTL) {
true => (),
false => {
return Err(ErrorKind::ClientCallback(
@@ -304,9 +311,9 @@ impl WalletCommAdapter for KeybaseWalletCommAdapter {
))?;
}
}
info!("tx request has been sent to @{}, tx uuid: {}", addr, id);
info!("tx request has been sent to @{}, tx uuid: {}", &self.0, id);
// Wait for response from recipient with SLATE_SIGNED topic
match poll(TTL as u64, addr) {
match poll(TTL as u64, &self.0) {
Some(slate) => return Ok(slate),
None => {
return Err(ErrorKind::ClientCallback(
@@ -315,30 +322,55 @@ impl WalletCommAdapter for KeybaseWalletCommAdapter {
}
}
}
}
/// Send a transaction asynchronously (result will be returned via the listener)
fn send_tx_async(&self, _addr: &str, _slate: &Slate) -> Result<(), Error> {
unimplemented!();
}
/// Receive a transaction async. (Actually just read it from wherever and return the slate)
fn receive_tx_async(&self, _params: &str) -> Result<Slate, Error> {
unimplemented!();
/// Receives slates on all channels with topic SLATE_NEW
pub struct KeybaseAllChannels {
_priv: (), // makes KeybaseAllChannels unconstructable without checking for existence of keybase executable
}
impl KeybaseAllChannels {
/// Create a KeybaseAllChannels, return error if keybase executable is not present
pub fn new() -> Result<KeybaseAllChannels, Error> {
if !keybase_installed() {
Err(ErrorKind::GenericError(
"Keybase executable not found, make sure it is installed and in your PATH"
.to_owned(),
)
.into())
} else {
Ok(KeybaseAllChannels { _priv: () })
}
}
}
impl SlateReceiver for KeybaseAllChannels {
/// Start a listener, passing received messages to the wallet api directly
#[allow(unreachable_code)]
fn listen(
&self,
_params: HashMap<String, String>,
config: WalletConfig,
passphrase: &str,
passphrase: ZeroingString,
account: &str,
node_api_secret: Option<String>,
) -> Result<(), Error> {
let node_client = HTTPNodeClient::new(&config.check_node_api_http_addr, node_api_secret);
let wallet = instantiate_wallet(config.clone(), node_client, passphrase, account)
.context(ErrorKind::WalletSeedDecryption)?;
let mut wallet = Box::new(
DefaultWalletImpl::<'static, HTTPNodeClient>::new(node_client.clone()).unwrap(),
)
as Box<
WalletInst<
'static,
DefaultLCProvider<HTTPNodeClient, ExtKeychain>,
HTTPNodeClient,
ExtKeychain,
>,
>;
let lc = wallet.lc_provider().unwrap();
lc.set_wallet_directory(&config.data_file_dir);
lc.open_wallet(None, passphrase)?;
let wallet_inst = lc.wallet_inst()?;
wallet_inst.set_parent_key_id_by_name(account)?;
info!("Listening for transactions on keybase ...");
loop {
@@ -379,10 +411,8 @@ impl WalletCommAdapter for KeybaseWalletCommAdapter {
return Err(e);
}
let res = {
let mut w = wallet.lock();
w.open_with_credentials()?;
let r = foreign::receive_tx(&mut *w, &slate, None, None, false);
w.close()?;
let r =
foreign::receive_tx(&mut **wallet_inst, &slate, None, None, false);
r
};
match res {