Replace failure with thiserror (#654)

* updated util and libwallet with thiserror

* update impl crate to thiserror

* api crate converted to thiserror

* update of controller crate to thiserror

* update final bin + tests to thiserror

* update unused import

* remove failure derive

* reset import of grin to master

* update cargo lock

* update from master
This commit is contained in:
Yeastplume
2022-07-28 10:21:45 +01:00
committed by GitHub
parent 67f0e2b0e0
commit 64cab53b1b
63 changed files with 1066 additions and 1586 deletions
-2
View File
@@ -10,8 +10,6 @@ exclude = ["**/*.grin", "**/*.grin2"]
edition = "2018"
[dependencies]
failure = "0.1"
failure_derive = "0.1"
log = "0.4"
uuid = { version = "0.8", features = ["serde", "v4"] }
serde = "1"
+17 -20
View File
@@ -16,9 +16,8 @@
use crate::keychain::Keychain;
use crate::libwallet::{
self, BlockFees, CbData, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
NodeVersionInfo, Slate, SlateVersion, VersionInfo, VersionedCoinbase, VersionedSlate,
WalletLCProvider,
self, BlockFees, CbData, Error, InitTxArgs, IssueInvoiceTxArgs, NodeClient, NodeVersionInfo,
Slate, SlateVersion, VersionInfo, VersionedCoinbase, VersionedSlate, WalletLCProvider,
};
use crate::{Foreign, ForeignCheckMiddlewareFn};
use easy_jsonrpc_mw;
@@ -62,7 +61,7 @@ pub trait ForeignRpc {
# ,false, 0, false, false);
```
*/
fn check_version(&self) -> Result<VersionInfo, ErrorKind>;
fn check_version(&self) -> Result<VersionInfo, Error>;
/**
Networked Legacy (non-secure token) version of [Foreign::build_coinbase](struct.Foreign.html#method.build_coinbase).
@@ -111,7 +110,7 @@ pub trait ForeignRpc {
```
*/
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, ErrorKind>;
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, Error>;
/**
;Networked version of [Foreign::receive_tx](struct.Foreign.html#method.receive_tx).
@@ -190,7 +189,7 @@ pub trait ForeignRpc {
slate: VersionedSlate,
dest_acct_name: Option<String>,
dest: Option<String>,
) -> Result<VersionedSlate, ErrorKind>;
) -> Result<VersionedSlate, Error>;
/**
@@ -284,7 +283,7 @@ pub trait ForeignRpc {
# ,false, 5, false, true);
```
*/
fn finalize_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind>;
fn finalize_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, Error>;
}
impl<'a, L, C, K> ForeignRpc for Foreign<'a, L, C, K>
@@ -293,12 +292,12 @@ where
C: NodeClient + 'a,
K: Keychain + 'a,
{
fn check_version(&self) -> Result<VersionInfo, ErrorKind> {
Foreign::check_version(self).map_err(|e| e.kind())
fn check_version(&self) -> Result<VersionInfo, Error> {
Foreign::check_version(self)
}
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, ErrorKind> {
let cb: CbData = Foreign::build_coinbase(self, block_fees).map_err(|e| e.kind())?;
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, Error> {
let cb: CbData = Foreign::build_coinbase(self, block_fees)?;
Ok(VersionedCoinbase::into_version(cb, SlateVersion::V4))
}
@@ -307,7 +306,7 @@ where
in_slate: VersionedSlate,
dest_acct_name: Option<String>,
dest: Option<String>,
) -> Result<VersionedSlate, ErrorKind> {
) -> Result<VersionedSlate, Error> {
let version = in_slate.version();
let slate_from = Slate::from(in_slate);
let out_slate = Foreign::receive_tx(
@@ -315,16 +314,14 @@ where
&slate_from,
dest_acct_name.as_ref().map(String::as_str),
dest,
)
.map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
)?;
Ok(VersionedSlate::into_version(out_slate, version)?)
}
fn finalize_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind> {
fn finalize_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, Error> {
let version = in_slate.version();
let out_slate =
Foreign::finalize_tx(self, &Slate::from(in_slate), true).map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
let out_slate = Foreign::finalize_tx(self, &Slate::from(in_slate), true)?;
Ok(VersionedSlate::into_version(out_slate, version)?)
}
}
@@ -334,7 +331,7 @@ fn test_check_middleware(
_slate: Option<&Slate>,
) -> Result<(), libwallet::Error> {
// TODO: Implement checks
// return Err(ErrorKind::GenericError("Test Rejection".into()))?
// return Err(Error::GenericError("Test Rejection".into()))?
Ok(())
}
-1
View File
@@ -29,7 +29,6 @@ use grin_wallet_util::grin_util as util;
extern crate grin_wallet_impls as impls;
extern crate grin_wallet_libwallet as libwallet;
extern crate failure_derive;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
+110 -155
View File
@@ -20,7 +20,7 @@ use crate::core::core::OutputFeatures;
use crate::core::global;
use crate::keychain::{Identifier, Keychain};
use crate::libwallet::{
AcctPathMapping, Amount, BuiltOutput, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
AcctPathMapping, Amount, BuiltOutput, Error, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
NodeHeightResult, OutputCommitMapping, PaymentProof, Slate, SlateVersion, Slatepack,
SlatepackAddress, StatusMessage, TxLogEntry, VersionedSlate, ViewWallet, WalletInfo,
WalletLCProvider,
@@ -78,7 +78,7 @@ pub trait OwnerRpc {
# , 4, false, false, false, false);
```
*/
fn accounts(&self, token: Token) -> Result<Vec<AcctPathMapping>, ErrorKind>;
fn accounts(&self, token: Token) -> Result<Vec<AcctPathMapping>, Error>;
/**
Networked version of [Owner::create_account_path](struct.Owner.html#method.create_account_path).
@@ -111,7 +111,7 @@ pub trait OwnerRpc {
# , 4, false, false, false, false);
```
*/
fn create_account_path(&self, token: Token, label: &String) -> Result<Identifier, ErrorKind>;
fn create_account_path(&self, token: Token, label: &String) -> Result<Identifier, Error>;
/**
Networked version of [Owner::set_active_account](struct.Owner.html#method.set_active_account).
@@ -144,7 +144,7 @@ pub trait OwnerRpc {
# , 4, false, false, false, false);
```
*/
fn set_active_account(&self, token: Token, label: &String) -> Result<(), ErrorKind>;
fn set_active_account(&self, token: Token, label: &String) -> Result<(), Error>;
/**
Networked version of [Owner::retrieve_outputs](struct.Owner.html#method.retrieve_outputs).
@@ -221,7 +221,7 @@ pub trait OwnerRpc {
include_spent: bool,
refresh_from_node: bool,
tx_id: Option<u32>,
) -> Result<(bool, Vec<OutputCommitMapping>), ErrorKind>;
) -> Result<(bool, Vec<OutputCommitMapping>), Error>;
/**
Networked version of [Owner::retrieve_txs](struct.Owner.html#method.retrieve_txs).
@@ -307,7 +307,7 @@ pub trait OwnerRpc {
refresh_from_node: bool,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<(bool, Vec<TxLogEntry>), ErrorKind>;
) -> Result<(bool, Vec<TxLogEntry>), Error>;
/**
Networked version of [Owner::retrieve_summary_info](struct.Owner.html#method.retrieve_summary_info).
@@ -358,7 +358,7 @@ pub trait OwnerRpc {
token: Token,
refresh_from_node: bool,
minimum_confirmations: u64,
) -> Result<(bool, WalletInfo), ErrorKind>;
) -> Result<(bool, WalletInfo), Error>;
/**
;Networked version of [Owner::init_send_tx](struct.Owner.html#method.init_send_tx).
@@ -417,7 +417,7 @@ pub trait OwnerRpc {
```
*/
fn init_send_tx(&self, token: Token, args: InitTxArgs) -> Result<VersionedSlate, ErrorKind>;
fn init_send_tx(&self, token: Token, args: InitTxArgs) -> Result<VersionedSlate, Error>;
/**
;Networked version of [Owner::issue_invoice_tx](struct.Owner.html#method.issue_invoice_tx).
@@ -468,7 +468,7 @@ pub trait OwnerRpc {
&self,
token: Token,
args: IssueInvoiceTxArgs,
) -> Result<VersionedSlate, ErrorKind>;
) -> Result<VersionedSlate, Error>;
/**
;Networked version of [Owner::process_invoice_tx](struct.Owner.html#method.process_invoice_tx).
@@ -552,7 +552,7 @@ pub trait OwnerRpc {
token: Token,
slate: VersionedSlate,
args: InitTxArgs,
) -> Result<VersionedSlate, ErrorKind>;
) -> Result<VersionedSlate, Error>;
/**
Networked version of [Owner::tx_lock_outputs](struct.Owner.html#method.tx_lock_outputs).
@@ -597,7 +597,7 @@ pub trait OwnerRpc {
```
*/
fn tx_lock_outputs(&self, token: Token, slate: VersionedSlate) -> Result<(), ErrorKind>;
fn tx_lock_outputs(&self, token: Token, slate: VersionedSlate) -> Result<(), Error>;
/**
Networked version of [Owner::finalize_tx](struct.Owner.html#method.finalize_tx).
@@ -683,8 +683,7 @@ pub trait OwnerRpc {
# , 5, true, true, false, false);
```
*/
fn finalize_tx(&self, token: Token, slate: VersionedSlate)
-> Result<VersionedSlate, ErrorKind>;
fn finalize_tx(&self, token: Token, slate: VersionedSlate) -> Result<VersionedSlate, Error>;
/**
Networked version of [Owner::post_tx](struct.Owner.html#method.post_tx).
@@ -753,7 +752,7 @@ pub trait OwnerRpc {
```
*/
fn post_tx(&self, token: Token, slate: VersionedSlate, fluff: bool) -> Result<(), ErrorKind>;
fn post_tx(&self, token: Token, slate: VersionedSlate, fluff: bool) -> Result<(), Error>;
/**
Networked version of [Owner::cancel_tx](struct.Owner.html#method.cancel_tx).
@@ -791,7 +790,7 @@ pub trait OwnerRpc {
token: Token,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<(), ErrorKind>;
) -> Result<(), Error>;
/**
Networked version of [Owner::get_stored_tx](struct.Owner.html#method.get_stored_tx).
@@ -840,7 +839,7 @@ pub trait OwnerRpc {
token: Token,
id: Option<u32>,
slate_id: Option<Uuid>,
) -> Result<Option<VersionedSlate>, ErrorKind>;
) -> Result<Option<VersionedSlate>, Error>;
/**
Networked version of [Owner::get_rewind_hash](struct.Owner.html#method.get_rewind_hash).
@@ -869,7 +868,7 @@ pub trait OwnerRpc {
# , 0, false, false, false, false);
```
*/
fn get_rewind_hash(&self, token: Token) -> Result<String, ErrorKind>;
fn get_rewind_hash(&self, token: Token) -> Result<String, Error>;
/**
Networked version of [Owner::scan_rewind_hash](struct.Owner.html#method.scan_rewind_hash).
@@ -949,7 +948,7 @@ pub trait OwnerRpc {
&self,
rewind_hash: String,
start_height: Option<u64>,
) -> Result<ViewWallet, ErrorKind>;
) -> Result<ViewWallet, Error>;
/**
Networked version of [Owner::scan](struct.Owner.html#method.scan).
@@ -987,7 +986,7 @@ pub trait OwnerRpc {
token: Token,
start_height: Option<u64>,
delete_unconfirmed: bool,
) -> Result<(), ErrorKind>;
) -> Result<(), Error>;
/**
Networked version of [Owner::node_height](struct.Owner.html#method.node_height).
@@ -1021,7 +1020,7 @@ pub trait OwnerRpc {
# , 5, false, false, false, false);
```
*/
fn node_height(&self, token: Token) -> Result<NodeHeightResult, ErrorKind>;
fn node_height(&self, token: Token) -> Result<NodeHeightResult, Error>;
/**
Initializes the secure JSON-RPC API. This function must be called and a shared key
@@ -1075,7 +1074,7 @@ pub trait OwnerRpc {
*/
fn init_secure_api(&self, ecdh_pubkey: ECDHPubkey) -> Result<ECDHPubkey, ErrorKind>;
fn init_secure_api(&self, ecdh_pubkey: ECDHPubkey) -> Result<ECDHPubkey, Error>;
/**
Networked version of [Owner::get_top_level_directory](struct.Owner.html#method.get_top_level_directory).
@@ -1105,7 +1104,7 @@ pub trait OwnerRpc {
```
*/
fn get_top_level_directory(&self) -> Result<String, ErrorKind>;
fn get_top_level_directory(&self) -> Result<String, Error>;
/**
Networked version of [Owner::set_top_level_directory](struct.Owner.html#method.set_top_level_directory).
@@ -1135,7 +1134,7 @@ pub trait OwnerRpc {
```
*/
fn set_top_level_directory(&self, dir: String) -> Result<(), ErrorKind>;
fn set_top_level_directory(&self, dir: String) -> Result<(), Error>;
/**
Networked version of [Owner::create_config](struct.Owner.html#method.create_config).
@@ -1205,7 +1204,7 @@ pub trait OwnerRpc {
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
tor_config: Option<TorConfig>,
) -> Result<(), ErrorKind>;
) -> Result<(), Error>;
/**
Networked version of [Owner::create_wallet](struct.Owner.html#method.create_wallet).
@@ -1244,7 +1243,7 @@ pub trait OwnerRpc {
mnemonic: Option<String>,
mnemonic_length: u32,
password: String,
) -> Result<(), ErrorKind>;
) -> Result<(), Error>;
/**
Networked version of [Owner::open_wallet](struct.Owner.html#method.open_wallet).
@@ -1275,7 +1274,7 @@ pub trait OwnerRpc {
```
*/
fn open_wallet(&self, name: Option<String>, password: String) -> Result<Token, ErrorKind>;
fn open_wallet(&self, name: Option<String>, password: String) -> Result<Token, Error>;
/**
Networked version of [Owner::close_wallet](struct.Owner.html#method.close_wallet).
@@ -1305,7 +1304,7 @@ pub trait OwnerRpc {
```
*/
fn close_wallet(&self, name: Option<String>) -> Result<(), ErrorKind>;
fn close_wallet(&self, name: Option<String>) -> Result<(), Error>;
/**
Networked version of [Owner::get_mnemonic](struct.Owner.html#method.get_mnemonic).
@@ -1336,7 +1335,7 @@ pub trait OwnerRpc {
```
*/
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, ErrorKind>;
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, Error>;
/**
Networked version of [Owner::change_password](struct.Owner.html#method.change_password).
@@ -1367,12 +1366,7 @@ pub trait OwnerRpc {
# , 0, false, false, false, false);
```
*/
fn change_password(
&self,
name: Option<String>,
old: String,
new: String,
) -> Result<(), ErrorKind>;
fn change_password(&self, name: Option<String>, old: String, new: String) -> Result<(), Error>;
/**
Networked version of [Owner::delete_wallet](struct.Owner.html#method.delete_wallet).
@@ -1401,7 +1395,7 @@ pub trait OwnerRpc {
# , 0, false, false, false, false);
```
*/
fn delete_wallet(&self, name: Option<String>) -> Result<(), ErrorKind>;
fn delete_wallet(&self, name: Option<String>) -> Result<(), Error>;
/**
Networked version of [Owner::start_updated](struct.Owner.html#method.start_updater).
@@ -1432,7 +1426,7 @@ pub trait OwnerRpc {
```
*/
fn start_updater(&self, token: Token, frequency: u32) -> Result<(), ErrorKind>;
fn start_updater(&self, token: Token, frequency: u32) -> Result<(), Error>;
/**
Networked version of [Owner::stop_updater](struct.Owner.html#method.stop_updater).
@@ -1459,7 +1453,7 @@ pub trait OwnerRpc {
# , 0, false, false, false, false);
```
*/
fn stop_updater(&self) -> Result<(), ErrorKind>;
fn stop_updater(&self) -> Result<(), Error>;
/**
Networked version of [Owner::get_updater_messages](struct.Owner.html#method.get_updater_messages).
@@ -1489,7 +1483,7 @@ pub trait OwnerRpc {
```
*/
fn get_updater_messages(&self, count: u32) -> Result<Vec<StatusMessage>, ErrorKind>;
fn get_updater_messages(&self, count: u32) -> Result<Vec<StatusMessage>, Error>;
/**
Networked version of [Owner::get_slatepack_address](struct.Owner.html#method.get_slatepack_address).
@@ -1524,7 +1518,7 @@ pub trait OwnerRpc {
&self,
token: Token,
derivation_index: u32,
) -> Result<SlatepackAddress, ErrorKind>;
) -> Result<SlatepackAddress, Error>;
/**
Networked version of [Owner::get_slatepack_secret_key](struct.Owner.html#method.get_slatepack_secret_key).
@@ -1559,7 +1553,7 @@ pub trait OwnerRpc {
&self,
token: Token,
derivation_index: u32,
) -> Result<Ed25519SecretKey, ErrorKind>;
) -> Result<Ed25519SecretKey, Error>;
/**
Networked version of [Owner::create_slatepack_message](struct.Owner.html#method.create_slatepack_message).
@@ -1611,7 +1605,7 @@ pub trait OwnerRpc {
slate: VersionedSlate,
sender_index: Option<u32>,
recipients: Vec<SlatepackAddress>,
) -> Result<String, ErrorKind>;
) -> Result<String, Error>;
/**
Networked version of [Owner::slate_from_slatepack_message](struct.Owner.html#method.slate_from_slatepack_message).
@@ -1665,7 +1659,7 @@ pub trait OwnerRpc {
token: Token,
message: String,
secret_indices: Vec<u32>,
) -> Result<VersionedSlate, ErrorKind>;
) -> Result<VersionedSlate, Error>;
/**
Networked version of [Owner::decode_slatepack_message](struct.Owner.html#method.decode_slatepack_message).
@@ -1707,7 +1701,7 @@ pub trait OwnerRpc {
token: Token,
message: String,
secret_indices: Vec<u32>,
) -> Result<Slatepack, ErrorKind>;
) -> Result<Slatepack, Error>;
/**
Networked version of [Owner::retrieve_payment_proof](struct.Owner.html#method.retrieve_payment_proof).
@@ -1753,7 +1747,7 @@ pub trait OwnerRpc {
refresh_from_node: bool,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<PaymentProof, ErrorKind>;
) -> Result<PaymentProof, Error>;
/**
Networked version of [Owner::verify_payment_proof](struct.Owner.html#method.verify_payment_proof).
@@ -1798,7 +1792,7 @@ pub trait OwnerRpc {
&self,
token: Token,
proof: PaymentProof,
) -> Result<(bool, bool), ErrorKind>;
) -> Result<(bool, bool), Error>;
/**
Networked version of [Owner::set_tor_config](struct.Owner.html#method.set_tor_config).
@@ -1831,7 +1825,7 @@ pub trait OwnerRpc {
# , 0, false, false, false, false);
```
*/
fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), ErrorKind>;
fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), Error>;
/**
Networked version of [Owner::build_output](struct.Owner.html#method.build_output).
@@ -1875,7 +1869,7 @@ pub trait OwnerRpc {
token: Token,
features: OutputFeatures,
amount: Amount,
) -> Result<BuiltOutput, ErrorKind>;
) -> Result<BuiltOutput, Error>;
}
impl<L, C, K> OwnerRpc for Owner<L, C, K>
@@ -1884,18 +1878,16 @@ where
C: NodeClient + 'static,
K: Keychain + 'static,
{
fn accounts(&self, token: Token) -> Result<Vec<AcctPathMapping>, ErrorKind> {
Owner::accounts(self, (&token.keychain_mask).as_ref()).map_err(|e| e.kind())
fn accounts(&self, token: Token) -> Result<Vec<AcctPathMapping>, Error> {
Owner::accounts(self, (&token.keychain_mask).as_ref())
}
fn create_account_path(&self, token: Token, label: &String) -> Result<Identifier, ErrorKind> {
fn create_account_path(&self, token: Token, label: &String) -> Result<Identifier, Error> {
Owner::create_account_path(self, (&token.keychain_mask).as_ref(), label)
.map_err(|e| e.kind())
}
fn set_active_account(&self, token: Token, label: &String) -> Result<(), ErrorKind> {
fn set_active_account(&self, token: Token, label: &String) -> Result<(), Error> {
Owner::set_active_account(self, (&token.keychain_mask).as_ref(), label)
.map_err(|e| e.kind())
}
fn retrieve_outputs(
@@ -1904,7 +1896,7 @@ where
include_spent: bool,
refresh_from_node: bool,
tx_id: Option<u32>,
) -> Result<(bool, Vec<OutputCommitMapping>), ErrorKind> {
) -> Result<(bool, Vec<OutputCommitMapping>), Error> {
Owner::retrieve_outputs(
self,
(&token.keychain_mask).as_ref(),
@@ -1912,7 +1904,6 @@ where
refresh_from_node,
tx_id,
)
.map_err(|e| e.kind())
}
fn retrieve_txs(
@@ -1921,7 +1912,7 @@ where
refresh_from_node: bool,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<(bool, Vec<TxLogEntry>), ErrorKind> {
) -> Result<(bool, Vec<TxLogEntry>), Error> {
Owner::retrieve_txs(
self,
(&token.keychain_mask).as_ref(),
@@ -1929,7 +1920,6 @@ where
tx_id,
tx_slate_id,
)
.map_err(|e| e.kind())
}
fn retrieve_summary_info(
@@ -1937,32 +1927,29 @@ where
token: Token,
refresh_from_node: bool,
minimum_confirmations: u64,
) -> Result<(bool, WalletInfo), ErrorKind> {
) -> Result<(bool, WalletInfo), Error> {
Owner::retrieve_summary_info(
self,
(&token.keychain_mask).as_ref(),
refresh_from_node,
minimum_confirmations,
)
.map_err(|e| e.kind())
}
fn init_send_tx(&self, token: Token, args: InitTxArgs) -> Result<VersionedSlate, ErrorKind> {
let slate = Owner::init_send_tx(self, (&token.keychain_mask).as_ref(), args)
.map_err(|e| e.kind())?;
fn init_send_tx(&self, token: Token, args: InitTxArgs) -> Result<VersionedSlate, Error> {
let slate = Owner::init_send_tx(self, (&token.keychain_mask).as_ref(), args)?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(slate, version).map_err(|e| e.kind())?)
VersionedSlate::into_version(slate, version)
}
fn issue_invoice_tx(
&self,
token: Token,
args: IssueInvoiceTxArgs,
) -> Result<VersionedSlate, ErrorKind> {
let slate = Owner::issue_invoice_tx(self, (&token.keychain_mask).as_ref(), args)
.map_err(|e| e.kind())?;
) -> Result<VersionedSlate, Error> {
let slate = Owner::issue_invoice_tx(self, (&token.keychain_mask).as_ref(), args)?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(slate, version).map_err(|e| e.kind())?)
VersionedSlate::into_version(slate, version)
}
fn process_invoice_tx(
@@ -1970,40 +1957,33 @@ where
token: Token,
in_slate: VersionedSlate,
args: InitTxArgs,
) -> Result<VersionedSlate, ErrorKind> {
) -> Result<VersionedSlate, Error> {
let out_slate = Owner::process_invoice_tx(
self,
(&token.keychain_mask).as_ref(),
&Slate::from(in_slate),
args,
)
.map_err(|e| e.kind())?;
)?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
VersionedSlate::into_version(out_slate, version)
}
fn finalize_tx(
&self,
token: Token,
in_slate: VersionedSlate,
) -> Result<VersionedSlate, ErrorKind> {
fn finalize_tx(&self, token: Token, in_slate: VersionedSlate) -> Result<VersionedSlate, Error> {
let out_slate = Owner::finalize_tx(
self,
(&token.keychain_mask).as_ref(),
&Slate::from(in_slate),
)
.map_err(|e| e.kind())?;
)?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
VersionedSlate::into_version(out_slate, version)
}
fn tx_lock_outputs(&self, token: Token, in_slate: VersionedSlate) -> Result<(), ErrorKind> {
fn tx_lock_outputs(&self, token: Token, in_slate: VersionedSlate) -> Result<(), Error> {
Owner::tx_lock_outputs(
self,
(&token.keychain_mask).as_ref(),
&Slate::from(in_slate),
)
.map_err(|e| e.kind())
}
fn cancel_tx(
@@ -2011,9 +1991,8 @@ where
token: Token,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<(), ErrorKind> {
) -> Result<(), Error> {
Owner::cancel_tx(self, (&token.keychain_mask).as_ref(), tx_id, tx_slate_id)
.map_err(|e| e.kind())
}
fn get_stored_tx(
@@ -2021,45 +2000,41 @@ where
token: Token,
id: Option<u32>,
slate_id: Option<Uuid>,
) -> Result<Option<VersionedSlate>, ErrorKind> {
) -> Result<Option<VersionedSlate>, Error> {
let out_slate = Owner::get_stored_tx(
self,
(&token.keychain_mask).as_ref(),
id,
(&slate_id).as_ref(),
)
.map_err(|e| e.kind())?;
)?;
match out_slate {
Some(s) => {
let version = SlateVersion::V4;
Ok(Some(
VersionedSlate::into_version(s, version).map_err(|e| e.kind())?,
))
Ok(Some(VersionedSlate::into_version(s, version)?))
}
None => Ok(None),
}
}
fn post_tx(&self, token: Token, slate: VersionedSlate, fluff: bool) -> Result<(), ErrorKind> {
fn post_tx(&self, token: Token, slate: VersionedSlate, fluff: bool) -> Result<(), Error> {
Owner::post_tx(
self,
(&token.keychain_mask).as_ref(),
&Slate::from(slate),
fluff,
)
.map_err(|e| e.kind())
}
fn get_rewind_hash(&self, token: Token) -> Result<String, ErrorKind> {
Owner::get_rewind_hash(self, (&token.keychain_mask).as_ref()).map_err(|e| e.kind())
fn get_rewind_hash(&self, token: Token) -> Result<String, Error> {
Owner::get_rewind_hash(self, (&token.keychain_mask).as_ref())
}
fn scan_rewind_hash(
&self,
rewind_hash: String,
start_height: Option<u64>,
) -> Result<ViewWallet, ErrorKind> {
Owner::scan_rewind_hash(self, rewind_hash, start_height).map_err(|e| e.kind())
) -> Result<ViewWallet, Error> {
Owner::scan_rewind_hash(self, rewind_hash, start_height)
}
fn scan(
@@ -2067,21 +2042,20 @@ where
token: Token,
start_height: Option<u64>,
delete_unconfirmed: bool,
) -> Result<(), ErrorKind> {
) -> Result<(), Error> {
Owner::scan(
self,
(&token.keychain_mask).as_ref(),
start_height,
delete_unconfirmed,
)
.map_err(|e| e.kind())
}
fn node_height(&self, token: Token) -> Result<NodeHeightResult, ErrorKind> {
Owner::node_height(self, (&token.keychain_mask).as_ref()).map_err(|e| e.kind())
fn node_height(&self, token: Token) -> Result<NodeHeightResult, Error> {
Owner::node_height(self, (&token.keychain_mask).as_ref())
}
fn init_secure_api(&self, ecdh_pubkey: ECDHPubkey) -> Result<ECDHPubkey, ErrorKind> {
fn init_secure_api(&self, ecdh_pubkey: ECDHPubkey) -> Result<ECDHPubkey, Error> {
let secp_inst = static_secp_instance();
let secp = secp_inst.lock();
let sec_key = SecretKey::new(&secp, &mut thread_rng());
@@ -2089,28 +2063,28 @@ where
let mut shared_pubkey = ecdh_pubkey.ecdh_pubkey;
shared_pubkey
.mul_assign(&secp, &sec_key)
.map_err(ErrorKind::Secp)?;
.map_err(Error::Secp)?;
let x_coord = shared_pubkey.serialize_vec(&secp, true);
let shared_key = SecretKey::from_slice(&secp, &x_coord[1..]).map_err(ErrorKind::Secp)?;
let shared_key = SecretKey::from_slice(&secp, &x_coord[1..]).map_err(Error::Secp)?;
{
let mut s = self.shared_key.lock();
*s = Some(shared_key);
}
let pub_key = PublicKey::from_secret_key(&secp, &sec_key).map_err(ErrorKind::Secp)?;
let pub_key = PublicKey::from_secret_key(&secp, &sec_key).map_err(Error::Secp)?;
Ok(ECDHPubkey {
ecdh_pubkey: pub_key,
})
}
fn get_top_level_directory(&self) -> Result<String, ErrorKind> {
Owner::get_top_level_directory(self).map_err(|e| e.kind())
fn get_top_level_directory(&self) -> Result<String, Error> {
Owner::get_top_level_directory(self)
}
fn set_top_level_directory(&self, dir: String) -> Result<(), ErrorKind> {
Owner::set_top_level_directory(self, &dir).map_err(|e| e.kind())
fn set_top_level_directory(&self, dir: String) -> Result<(), Error> {
Owner::set_top_level_directory(self, &dir)
}
fn create_config(
@@ -2119,9 +2093,8 @@ where
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
tor_config: Option<TorConfig>,
) -> Result<(), ErrorKind> {
) -> Result<(), Error> {
Owner::create_config(self, &chain_type, wallet_config, logging_config, tor_config)
.map_err(|e| e.kind())
}
fn create_wallet(
@@ -2130,90 +2103,78 @@ where
mnemonic: Option<String>,
mnemonic_length: u32,
password: String,
) -> Result<(), ErrorKind> {
) -> Result<(), Error> {
let n = name.as_ref().map(|s| s.as_str());
let m = match mnemonic {
Some(s) => Some(ZeroingString::from(s)),
None => None,
};
Owner::create_wallet(self, n, m, mnemonic_length, ZeroingString::from(password))
.map_err(|e| e.kind())
}
fn open_wallet(&self, name: Option<String>, password: String) -> Result<Token, ErrorKind> {
fn open_wallet(&self, name: Option<String>, password: String) -> Result<Token, Error> {
let n = name.as_ref().map(|s| s.as_str());
let sec_key = Owner::open_wallet(self, n, ZeroingString::from(password), true)
.map_err(|e| e.kind())?;
let sec_key = Owner::open_wallet(self, n, ZeroingString::from(password), true)?;
Ok(Token {
keychain_mask: sec_key,
})
}
fn close_wallet(&self, name: Option<String>) -> Result<(), ErrorKind> {
fn close_wallet(&self, name: Option<String>) -> Result<(), Error> {
let n = name.as_ref().map(|s| s.as_str());
Owner::close_wallet(self, n).map_err(|e| e.kind())
Owner::close_wallet(self, n)
}
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, ErrorKind> {
fn get_mnemonic(&self, name: Option<String>, password: String) -> Result<String, Error> {
let n = name.as_ref().map(|s| s.as_str());
let res =
Owner::get_mnemonic(self, n, ZeroingString::from(password)).map_err(|e| e.kind())?;
let res = Owner::get_mnemonic(self, n, ZeroingString::from(password))?;
Ok((&*res).to_string())
}
fn change_password(
&self,
name: Option<String>,
old: String,
new: String,
) -> Result<(), ErrorKind> {
fn change_password(&self, name: Option<String>, old: String, new: String) -> Result<(), Error> {
let n = name.as_ref().map(|s| s.as_str());
Owner::change_password(self, n, ZeroingString::from(old), ZeroingString::from(new))
.map_err(|e| e.kind())
}
fn delete_wallet(&self, name: Option<String>) -> Result<(), ErrorKind> {
fn delete_wallet(&self, name: Option<String>) -> Result<(), Error> {
let n = name.as_ref().map(|s| s.as_str());
Owner::delete_wallet(self, n).map_err(|e| e.kind())
Owner::delete_wallet(self, n)
}
fn start_updater(&self, token: Token, frequency: u32) -> Result<(), ErrorKind> {
fn start_updater(&self, token: Token, frequency: u32) -> Result<(), Error> {
Owner::start_updater(
self,
(&token.keychain_mask).as_ref(),
Duration::from_millis(frequency as u64),
)
.map_err(|e| e.kind())
}
fn stop_updater(&self) -> Result<(), ErrorKind> {
Owner::stop_updater(self).map_err(|e| e.kind())
fn stop_updater(&self) -> Result<(), Error> {
Owner::stop_updater(self)
}
fn get_updater_messages(&self, count: u32) -> Result<Vec<StatusMessage>, ErrorKind> {
Owner::get_updater_messages(self, count as usize).map_err(|e| e.kind())
fn get_updater_messages(&self, count: u32) -> Result<Vec<StatusMessage>, Error> {
Owner::get_updater_messages(self, count as usize)
}
fn get_slatepack_address(
&self,
token: Token,
derivation_index: u32,
) -> Result<SlatepackAddress, ErrorKind> {
) -> Result<SlatepackAddress, Error> {
Owner::get_slatepack_address(self, (&token.keychain_mask).as_ref(), derivation_index)
.map_err(|e| e.kind())
}
fn get_slatepack_secret_key(
&self,
token: Token,
derivation_index: u32,
) -> Result<Ed25519SecretKey, ErrorKind> {
) -> Result<Ed25519SecretKey, Error> {
let key = Owner::get_slatepack_secret_key(
self,
(&token.keychain_mask).as_ref(),
derivation_index,
)
.map_err(|e| e.kind())?;
)?;
Ok(Ed25519SecretKey { key })
}
@@ -2223,15 +2184,14 @@ where
slate: VersionedSlate,
sender_index: Option<u32>,
recipients: Vec<SlatepackAddress>,
) -> Result<String, ErrorKind> {
) -> Result<String, Error> {
let res = Owner::create_slatepack_message(
self,
(&token.keychain_mask).as_ref(),
&Slate::from(slate),
sender_index,
recipients,
)
.map_err(|e| e.kind())?;
)?;
Ok(res.trim().into())
}
@@ -2240,16 +2200,15 @@ where
token: Token,
message: String,
secret_indices: Vec<u32>,
) -> Result<VersionedSlate, ErrorKind> {
) -> Result<VersionedSlate, Error> {
let slate = Owner::slate_from_slatepack_message(
self,
(&token.keychain_mask).as_ref(),
message,
secret_indices,
)
.map_err(|e| e.kind())?;
)?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(slate, version).map_err(|e| e.kind())?)
VersionedSlate::into_version(slate, version)
}
fn decode_slatepack_message(
@@ -2257,14 +2216,13 @@ where
token: Token,
message: String,
secret_indices: Vec<u32>,
) -> Result<Slatepack, ErrorKind> {
) -> Result<Slatepack, Error> {
Owner::decode_slatepack_message(
self,
(&token.keychain_mask).as_ref(),
message,
secret_indices,
)
.map_err(|e| e.kind())
}
fn retrieve_payment_proof(
@@ -2273,7 +2231,7 @@ where
refresh_from_node: bool,
tx_id: Option<u32>,
tx_slate_id: Option<Uuid>,
) -> Result<PaymentProof, ErrorKind> {
) -> Result<PaymentProof, Error> {
Owner::retrieve_payment_proof(
self,
(&token.keychain_mask).as_ref(),
@@ -2281,19 +2239,17 @@ where
tx_id,
tx_slate_id,
)
.map_err(|e| e.kind())
}
fn verify_payment_proof(
&self,
token: Token,
proof: PaymentProof,
) -> Result<(bool, bool), ErrorKind> {
) -> Result<(bool, bool), Error> {
Owner::verify_payment_proof(self, (&token.keychain_mask).as_ref(), &proof)
.map_err(|e| e.kind())
}
fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), ErrorKind> {
fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), Error> {
Owner::set_tor_config(self, tor_config);
Ok(())
}
@@ -2303,9 +2259,8 @@ where
token: Token,
features: OutputFeatures,
amount: Amount,
) -> Result<BuiltOutput, ErrorKind> {
) -> Result<BuiltOutput, Error> {
Owner::build_output(self, (&token.keychain_mask).as_ref(), features, amount.0)
.map_err(|e| e.kind())
}
}
+38 -46
View File
@@ -13,11 +13,10 @@
use crate::core::libtx::secp_ser;
use crate::libwallet::slate_versions::ser as dalek_ser;
use crate::libwallet::{Error, ErrorKind};
use crate::libwallet::Error;
use crate::util::secp::key::{PublicKey, SecretKey};
use crate::util::{from_hex, ToHex};
use ed25519_dalek::SecretKey as DalekSecretKey;
use failure::ResultExt;
use base64;
use rand::{thread_rng, Rng};
@@ -75,9 +74,9 @@ impl EncryptedBody {
/// Encrypts and encodes json as base 64
pub fn from_json(json_in: &Value, enc_key: &SecretKey) -> Result<Self, Error> {
let mut to_encrypt = serde_json::to_string(&json_in)
.context(ErrorKind::APIEncryption(
"EncryptedBody Enc: Unable to encode JSON".to_owned(),
))?
.map_err(|_| {
Error::APIEncryption("EncryptedBody Enc: Unable to encode JSON".to_owned())
})?
.as_bytes()
.to_vec();
@@ -92,9 +91,7 @@ impl EncryptedBody {
&mut to_encrypt,
);
if let Err(_) = res {
return Err(
ErrorKind::APIEncryption("EncryptedBody: encryption failed".to_owned()).into(),
);
return Err(Error::APIEncryption("EncryptedBody: encryption failed".to_owned()).into());
}
Ok(EncryptedBody {
@@ -105,31 +102,32 @@ impl EncryptedBody {
/// return serialize JSON self
pub fn as_json_value(&self) -> Result<Value, Error> {
let res = serde_json::to_value(self).context(ErrorKind::APIEncryption(
"EncryptedBody: JSON serialization failed".to_owned(),
))?;
let res = serde_json::to_value(self).map_err(|_| {
Error::APIEncryption("EncryptedBody: JSON serialization failed".to_owned())
})?;
Ok(res)
}
/// return serialized JSON self as string
pub fn as_json_str(&self) -> Result<String, Error> {
let res = self.as_json_value()?;
let res = serde_json::to_string(&res).context(ErrorKind::APIEncryption(
"EncryptedBody: JSON String serialization failed".to_owned(),
))?;
let res = serde_json::to_string(&res).map_err(|_| {
Error::APIEncryption("EncryptedBody: JSON String serialization failed".to_owned())
})?;
Ok(res)
}
/// Return original request
pub fn decrypt(&self, dec_key: &SecretKey) -> Result<Value, Error> {
let mut to_decrypt = base64::decode(&self.body_enc).context(ErrorKind::APIEncryption(
"EncryptedBody Dec: Encrypted request contains invalid Base64".to_string(),
))?;
let nonce = from_hex(&self.nonce).map_err(|_| {
ErrorKind::APIEncryption("EncryptedBody Dec: Invalid Nonce".to_string())
let mut to_decrypt = base64::decode(&self.body_enc).map_err(|_| {
Error::APIEncryption(
"EncryptedBody Dec: Encrypted request contains invalid Base64".to_string(),
)
})?;
let nonce = from_hex(&self.nonce)
.map_err(|_| Error::APIEncryption("EncryptedBody Dec: Invalid Nonce".to_string()))?;
if nonce.len() < 12 {
return Err(ErrorKind::APIEncryption(
return Err(Error::APIEncryption(
"EncryptedBody Dec: Invalid Nonce length".to_string(),
)
.into());
@@ -142,22 +140,16 @@ impl EncryptedBody {
let res =
opening_key.open_in_place(aead::Nonce::assume_unique_for_key(n), aad, &mut to_decrypt);
if let Err(_) = res {
return Err(
ErrorKind::APIEncryption("EncryptedBody: decryption failed".to_owned()).into(),
);
return Err(Error::APIEncryption("EncryptedBody: decryption failed".to_owned()).into());
}
for _ in 0..aead::AES_256_GCM.tag_len() {
to_decrypt.pop();
}
let decrypted = String::from_utf8(to_decrypt).context(ErrorKind::APIEncryption(
"EncryptedBody Dec: Invalid UTF-8".to_string(),
))?;
Ok(
serde_json::from_str(&decrypted).context(ErrorKind::APIEncryption(
"EncryptedBody Dec: Invalid JSON".to_string(),
))?,
)
let decrypted = String::from_utf8(to_decrypt)
.map_err(|_| Error::APIEncryption("EncryptedBody Dec: Invalid UTF-8".to_string()))?;
Ok(serde_json::from_str(&decrypted)
.map_err(|_| Error::APIEncryption("EncryptedBody Dec: Invalid JSON".to_string()))?)
}
}
@@ -187,18 +179,18 @@ impl EncryptedRequest {
/// return serialize JSON self
pub fn as_json_value(&self) -> Result<Value, Error> {
let res = serde_json::to_value(self).context(ErrorKind::APIEncryption(
"EncryptedRequest: JSON serialization failed".to_owned(),
))?;
let res = serde_json::to_value(self).map_err(|_| {
Error::APIEncryption("EncryptedRequest: JSON serialization failed".to_owned())
})?;
Ok(res)
}
/// return serialized JSON self as string
pub fn as_json_str(&self) -> Result<String, Error> {
let res = self.as_json_value()?;
let res = serde_json::to_string(&res).context(ErrorKind::APIEncryption(
"EncryptedRequest: JSON String serialization failed".to_owned(),
))?;
let res = serde_json::to_string(&res).map_err(|_| {
Error::APIEncryption("EncryptedRequest: JSON String serialization failed".to_owned())
})?;
Ok(res)
}
@@ -236,18 +228,18 @@ impl EncryptedResponse {
/// return serialize JSON self
pub fn as_json_value(&self) -> Result<Value, Error> {
let res = serde_json::to_value(self).context(ErrorKind::APIEncryption(
"EncryptedResponse: JSON serialization failed".to_owned(),
))?;
let res = serde_json::to_value(self).map_err(|_| {
Error::APIEncryption("EncryptedResponse: JSON serialization failed".to_owned())
})?;
Ok(res)
}
/// return serialized JSON self as string
pub fn as_json_str(&self) -> Result<String, Error> {
let res = self.as_json_value()?;
let res = serde_json::to_string(&res).context(ErrorKind::APIEncryption(
"EncryptedResponse: JSON String serialization failed".to_owned(),
))?;
let res = serde_json::to_string(&res).map_err(|_| {
Error::APIEncryption("EncryptedResponse: JSON String serialization failed".to_owned())
})?;
Ok(res)
}
@@ -293,9 +285,9 @@ impl EncryptionErrorResponse {
/// return serialized JSON self
pub fn as_json_value(&self) -> Value {
let res = serde_json::to_value(self).context(ErrorKind::APIEncryption(
"EncryptedResponse: JSON serialization failed".to_owned(),
));
let res = serde_json::to_value(self).map_err(|_| {
Error::APIEncryption("EncryptedResponse: JSON serialization failed".to_owned())
});
match res {
Ok(r) => r,
// proverbial "should never happen"