Skip to content

Commit

Permalink
Remove callbacks from transaction API functions (#22)
Browse files Browse the repository at this point in the history
* remove callbacks from transaction creation

* rustfmt

* add missing functions to owner api rpc

* comment fix
  • Loading branch information
yeastplume authored Mar 14, 2019
1 parent c38e2a4 commit 4d86b5a
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 184 deletions.
4 changes: 1 addition & 3 deletions api/src/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ where
None => None,
};

let (_, mut create_fn) =
tx::add_output_to_slate(&mut *w, slate, &parent_key_id, 1, message)?;
create_fn(&mut *w, &slate.tx, PhantomData, PhantomData)?;
tx::add_output_to_slate(&mut *w, slate, &parent_key_id, 1, message)?;
tx::update_message(&mut *w, slate)?;
w.close()?;
Ok(())
Expand Down
23 changes: 11 additions & 12 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ use crate::core::core::hash::Hashed;
use crate::core::core::Transaction;
use crate::core::ser;
use crate::keychain::{Identifier, Keychain};
use crate::libwallet::internal::{keys, tx, updater};
use crate::libwallet::internal::{keys, selection, tx, updater};
use crate::libwallet::slate::Slate;
use crate::libwallet::types::{
AcctPathMapping, NodeClient, OutputData, OutputLockFn, TxLogEntry, TxWrapper, WalletBackend,
WalletInfo,
AcctPathMapping, NodeClient, OutputData, TxLogEntry, TxWrapper, WalletBackend, WalletInfo,
};
use crate::libwallet::{Error, ErrorKind};
use crate::util;
Expand Down Expand Up @@ -625,7 +624,7 @@ where
selection_strategy_is_use_all: bool,
message: Option<String>,
target_slate_version: Option<u16>,
) -> Result<(Slate, OutputLockFn<W, C, K>), Error> {
) -> Result<Slate, Error> {
let mut w = self.wallet.lock();
w.open_with_credentials()?;
let parent_key_id = match src_acct_name {
Expand All @@ -649,7 +648,7 @@ where

let mut slate = tx::new_tx_slate(&mut *w, amount, 2)?;

let (context, lock_fn) = tx::add_inputs_to_slate(
let context = tx::add_inputs_to_slate(
&mut *w,
&mut slate,
minimum_confirmations,
Expand All @@ -674,7 +673,7 @@ where
if let Some(v) = target_slate_version {
slate.version_info.orig_version = v;
}
Ok((slate, lock_fn))
Ok(slate)
}

/// Estimates the amount to be locked and fee for the transaction without creating one
Expand Down Expand Up @@ -746,14 +745,14 @@ where
}

/// Lock outputs associated with a given slate/transaction
pub fn tx_lock_outputs(
&self,
slate: &Slate,
mut lock_fn: OutputLockFn<W, C, K>,
) -> Result<(), Error> {
/// and create any outputs needed
pub fn tx_lock_outputs(&self, slate: &Slate) -> Result<(), Error> {
let mut w = self.wallet.lock();
w.open_with_credentials()?;
lock_fn(&mut *w, &slate.tx, PhantomData, PhantomData)?;
let context = w.get_private_context(slate.id.as_bytes())?;
w.open_with_credentials()?;
selection::lock_tx_context(&mut *w, slate, &context)?;
w.close()?;
Ok(())
}

Expand Down
114 changes: 113 additions & 1 deletion api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! JSON-RPC Stub generation for the Foreign API
//! JSON-RPC Stub generation for the Owner API

use uuid::Uuid;

Expand Down Expand Up @@ -202,6 +202,42 @@ pub trait OwnerRpc {
minimum_confirmations: u64,
) -> Result<(bool, WalletInfo), ErrorKind>;

/**
Networked version of [Owner::estimate_initiate_tx](struct.Owner.html#method.initiate_tx).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
{
"jsonrpc": "2.0",
"method": "initiate_tx",
"params": [null, 0, 0, 10, 0, false, "my message", null],
"id": 1
},
{
"jsonrpc": "2.0",
"result": {
"Err": {
"CallbackImpl": "Error opening wallet"
}
},
"id": 1
}
# );
```
*/

fn initiate_tx(
&self,
src_acct_name: Option<String>,
amount: u64,
minimum_confirmations: u64,
max_outputs: usize,
num_change_outputs: usize,
selection_strategy_is_use_all: bool,
message: Option<String>,
target_slate_version: Option<u16>,
) -> Result<Slate, ErrorKind>;

/**
Networked version of [Owner::estimate_initiate_tx](struct.Owner.html#method.estimate_initiate_tx).
Expand Down Expand Up @@ -236,6 +272,53 @@ pub trait OwnerRpc {
selection_strategy_is_use_all: bool,
) -> Result<(/* total */ u64, /* fee */ u64), ErrorKind>;

/**
Networked version of [Owner::tx_lock_outputs](struct.Owner.html#method.tx_lock_outputs).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
{
"jsonrpc": "2.0",
"method": "tx_lock_outputs",
"params": [{
"version_info": {
"version": 2,
"orig_version": 2,
"min_compat_version": 0
},
"amount": 0,
"fee": 0,
"height": 0,
"id": "414bad48-3386-4fa7-8483-72384c886ba3",
"lock_height": 0,
"num_participants": 2,
"participant_data": [],
"tx": {
"body": {
"inputs": [],
"kernels": [],
"outputs": []
},
"offset": "0000000000000000000000000000000000000000000000000000000000000000"
}
}],
"id": 1
},
{
"jsonrpc": "2.0",
"result": {
"Err": {
"CallbackImpl": "Error opening wallet"
}
},
"id": 1
}
# );
```
*/
fn tx_lock_outputs(&self, slate: Slate) -> Result<(), ErrorKind>;

/**
Networked version of [Owner::finalize_tx](struct.Owner.html#method.finalize_tx).
Expand Down Expand Up @@ -555,6 +638,31 @@ where
.map_err(|e| e.kind())
}

fn initiate_tx(
&self,
src_acct_name: Option<String>,
amount: u64,
minimum_confirmations: u64,
max_outputs: usize,
num_change_outputs: usize,
selection_strategy_is_use_all: bool,
message: Option<String>,
target_slate_version: Option<u16>,
) -> Result<Slate, ErrorKind> {
Owner::initiate_tx(
self,
src_acct_name.as_ref().map(String::as_str),
amount,
minimum_confirmations,
max_outputs,
num_change_outputs,
selection_strategy_is_use_all,
message,
target_slate_version,
)
.map_err(|e| e.kind())
}

fn estimate_initiate_tx(
&self,
src_acct_name: Option<String>,
Expand All @@ -581,6 +689,10 @@ where
Ok(slate)
}

fn tx_lock_outputs(&self, mut slate: Slate) -> Result<(), ErrorKind> {
Owner::tx_lock_outputs(self, &mut slate).map_err(|e| e.kind())
}

fn cancel_tx(&self, tx_id: Option<u32>, tx_slate_id: Option<Uuid>) -> Result<(), ErrorKind> {
Owner::cancel_tx(self, tx_id, tx_slate_id).map_err(|e| e.kind())
}
Expand Down
Loading

0 comments on commit 4d86b5a

Please sign in to comment.