[WIP] Invoiced Transactions API Support (#90)

* basic invoiced tx working

* rustfmt

* teardown

* rustfmt

* rename, new struct for invoice args, begin to add new functions to RPC apis

* rustfmt

* add fns to rpc api

* rustfmt

* owner api functions RPC documentation in place

* rustfmt

* doctests for new invoicing functions

* rustfmt

* test fixes

* update documentation and doctests

* rustfmt

* invoice testing verification of tx log output

* rustfmt
This commit is contained in:
Yeastplume
2019-05-01 20:12:23 +01:00
committed by GitHub
parent 92be66672e
commit 3a3057defb
23 changed files with 1072 additions and 80 deletions
+21
View File
@@ -103,8 +103,29 @@ where
&parent_key_id,
1,
message,
false,
use_test_rng,
)?;
tx::update_message(&mut *w, &mut ret_slate)?;
Ok(ret_slate)
}
/// Receive an tx that this wallet has issued
pub fn finalize_invoice_tx<T: ?Sized, C, K>(w: &mut T, slate: &Slate) -> Result<Slate, Error>
where
T: WalletBackend<C, K>,
C: NodeClient,
K: Keychain,
{
let mut sl = slate.clone();
let context = w.get_private_context(sl.id.as_bytes())?;
tx::complete_tx(&mut *w, &mut sl, 1, &context)?;
tx::update_stored_tx(&mut *w, &mut sl, true)?;
tx::update_message(&mut *w, &mut sl)?;
{
let mut batch = w.batch()?;
batch.delete_private_context(sl.id.as_bytes())?;
batch.commit()?;
}
Ok(sl)
}
+132 -3
View File
@@ -26,7 +26,9 @@ use crate::internal::{keys, selection, tx, updater};
use crate::slate::Slate;
use crate::types::{AcctPathMapping, NodeClient, TxLogEntry, TxWrapper, WalletBackend, WalletInfo};
use crate::{Error, ErrorKind};
use crate::{InitTxArgs, NodeHeightResult, OutputCommitMapping};
use crate::{
InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult, OutputCommitMapping, TxLogEntryType,
};
const USER_MESSAGE_MAX_LEN: usize = 256;
@@ -133,7 +135,7 @@ where
}
/// Initiate tx as sender
pub fn initiate_tx<T: ?Sized, C, K>(
pub fn init_send_tx<T: ?Sized, C, K>(
w: &mut T,
args: InitTxArgs,
use_test_rng: bool,
@@ -191,6 +193,7 @@ where
&parent_key_id,
0,
message,
true,
use_test_rng,
)?;
@@ -207,6 +210,132 @@ where
Ok(slate)
}
/// Initiate a transaction as the recipient (invoicing)
pub fn issue_invoice_tx<T: ?Sized, C, K>(
w: &mut T,
args: IssueInvoiceTxArgs,
use_test_rng: bool,
) -> Result<Slate, Error>
where
T: WalletBackend<C, K>,
C: NodeClient,
K: Keychain,
{
let parent_key_id = match args.dest_acct_name {
Some(d) => {
let pm = w.get_acct_path(d)?;
match pm {
Some(p) => p.path,
None => w.parent_key_id(),
}
}
None => w.parent_key_id(),
};
let message = match args.message {
Some(mut m) => {
m.truncate(USER_MESSAGE_MAX_LEN);
Some(m)
}
None => None,
};
let mut slate = tx::new_tx_slate(&mut *w, args.amount, 2, use_test_rng)?;
let context = tx::add_output_to_slate(
&mut *w,
&mut slate,
&parent_key_id,
1,
message,
true,
use_test_rng,
)?;
// Save the aggsig context in our DB for when we
// recieve the transaction back
{
let mut batch = w.batch()?;
batch.save_private_context(slate.id.as_bytes(), &context)?;
batch.commit()?;
}
Ok(slate)
}
/// Receive an invoice tx, essentially adding inputs to whatever
/// output was specified
pub fn process_invoice_tx<T: ?Sized, C, K>(
w: &mut T,
slate: &Slate,
args: InitTxArgs,
use_test_rng: bool,
) -> Result<Slate, Error>
where
T: WalletBackend<C, K>,
C: NodeClient,
K: Keychain,
{
let mut ret_slate = slate.clone();
let parent_key_id = match args.src_acct_name {
Some(d) => {
let pm = w.get_acct_path(d.to_owned())?;
match pm {
Some(p) => p.path,
None => w.parent_key_id(),
}
}
None => w.parent_key_id(),
};
// Don't do this multiple times
let tx = updater::retrieve_txs(
&mut *w,
None,
Some(ret_slate.id),
Some(&parent_key_id),
use_test_rng,
)?;
for t in &tx {
if t.tx_type == TxLogEntryType::TxSent {
return Err(ErrorKind::TransactionAlreadyReceived(ret_slate.id.to_string()).into());
}
}
let message = match args.message {
Some(mut m) => {
m.truncate(USER_MESSAGE_MAX_LEN);
Some(m)
}
None => None,
};
let context = tx::add_inputs_to_slate(
&mut *w,
&mut ret_slate,
args.minimum_confirmations,
args.max_outputs as usize,
args.num_change_outputs as usize,
args.selection_strategy_is_use_all,
&parent_key_id,
0,
message,
false,
use_test_rng,
)?;
// Save the aggsig context in our DB for when we
// recieve the transaction back
{
let mut batch = w.batch()?;
batch.save_private_context(slate.id.as_bytes(), &context)?;
batch.commit()?;
}
// Always lock the context for now
selection::lock_tx_context(&mut *w, slate, &context)?;
tx::update_message(&mut *w, &mut ret_slate)?;
Ok(ret_slate)
}
/// Lock sender outputs
pub fn tx_lock_outputs<T: ?Sized, C, K>(w: &mut T, slate: &Slate) -> Result<(), Error>
where
@@ -228,7 +357,7 @@ where
let mut sl = slate.clone();
let context = w.get_private_context(sl.id.as_bytes())?;
tx::complete_tx(&mut *w, &mut sl, 0, &context)?;
tx::update_stored_tx(&mut *w, &mut sl)?;
tx::update_stored_tx(&mut *w, &mut sl, false)?;
tx::update_message(&mut *w, &mut sl)?;
{
let mut batch = w.batch()?;
+29
View File
@@ -129,6 +129,35 @@ impl Default for InitTxArgs {
}
}
/// V2 Issue Invoice Tx Args
#[derive(Clone, Serialize, Deserialize)]
pub struct IssueInvoiceTxArgs {
/// The human readable account name to which the received funds should be added
/// overriding whatever the active account is as set via the
/// [`set_active_account`](../grin_wallet_api/owner/struct.Owner.html#method.set_active_account) method.
pub dest_acct_name: Option<String>,
/// The invoice amount in nanogrins. (`1 G = 1_000_000_000nG`)
#[serde(with = "secp_ser::string_or_u64")]
pub amount: u64,
/// Optional message, that will be signed
pub message: Option<String>,
/// Optionally set the output target slate version (acceptable
/// down to the minimum slate version compatible with the current. If `None` the slate
/// is generated with the latest version.
pub target_slate_version: Option<u16>,
}
impl Default for IssueInvoiceTxArgs {
fn default() -> IssueInvoiceTxArgs {
IssueInvoiceTxArgs {
dest_acct_name: None,
amount: 0,
message: None,
target_slate_version: None,
}
}
}
/// Fees in block to use for coinbase amount calculation
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BlockFees {