[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 {
+2 -3
View File
@@ -166,9 +166,8 @@ where
}
/// Creates a new output in the wallet for the recipient,
/// returning the key of the fresh output and a closure
/// that actually performs the addition of the output to the
/// wallet
/// returning the key of the fresh output
/// Also creates a new transaction containing the output
pub fn build_recipient_output<T: ?Sized, C, K>(
wallet: &mut T,
slate: &mut Slate,
+34 -11
View File
@@ -118,6 +118,7 @@ pub fn add_inputs_to_slate<T: ?Sized, C, K>(
parent_key_id: &Identifier,
participant_id: usize,
message: Option<String>,
is_initator: bool,
use_test_rng: bool,
) -> Result<Context, Error>
where
@@ -158,16 +159,27 @@ where
use_test_rng,
)?;
if !is_initator {
// perform partial sig
let _ = slate.fill_round_2(
wallet.keychain(),
&context.sec_key,
&context.sec_nonce,
participant_id,
)?;
}
Ok(context)
}
/// Add outputs to the slate, becoming the recipient
/// Add receiver output to the slate
pub fn add_output_to_slate<T: ?Sized, C, K>(
wallet: &mut T,
slate: &mut Slate,
parent_key_id: &Identifier,
participant_id: usize,
message: Option<String>,
is_initiator: bool,
use_test_rng: bool,
) -> Result<Context, Error>
where
@@ -189,18 +201,20 @@ where
use_test_rng,
)?;
// perform partial sig
let _ = slate.fill_round_2(
wallet.keychain(),
&context.sec_key,
&context.sec_nonce,
participant_id,
)?;
if !is_initiator {
// perform partial sig
let _ = slate.fill_round_2(
wallet.keychain(),
&context.sec_key,
&context.sec_nonce,
participant_id,
)?;
}
Ok(context)
}
/// Complete a transaction as the sender
/// Complete a transaction
pub fn complete_tx<T: ?Sized, C, K>(
wallet: &mut T,
slate: &mut Slate,
@@ -218,6 +232,7 @@ where
&context.sec_nonce,
participant_id,
)?;
// Final transaction can be built by anyone at this stage
slate.finalize(wallet.keychain())?;
Ok(())
@@ -260,7 +275,11 @@ where
}
/// Update the stored transaction (this update needs to happen when the TX is finalised)
pub fn update_stored_tx<T: ?Sized, C, K>(wallet: &mut T, slate: &Slate) -> Result<(), Error>
pub fn update_stored_tx<T: ?Sized, C, K>(
wallet: &mut T,
slate: &Slate,
is_invoiced: bool,
) -> Result<(), Error>
where
T: WalletBackend<C, K>,
C: NodeClient,
@@ -271,7 +290,11 @@ where
let mut tx = None;
// don't want to assume this is the right tx, in case of self-sending
for t in tx_vec {
if t.tx_type == TxLogEntryType::TxSent {
if t.tx_type == TxLogEntryType::TxSent && !is_invoiced {
tx = Some(t.clone());
break;
}
if t.tx_type == TxLogEntryType::TxReceived && is_invoiced {
tx = Some(t.clone());
break;
}
+2 -2
View File
@@ -50,8 +50,8 @@ pub use crate::error::{Error, ErrorKind};
pub use crate::slate::{ParticipantData, ParticipantMessageData, Slate};
pub use crate::slate_versions::{SlateVersion, VersionedSlate};
pub use api_impl::types::{
BlockFees, CbData, InitTxArgs, InitTxSendArgs, NodeHeightResult, OutputCommitMapping,
SendTXArgs, VersionInfo,
BlockFees, CbData, InitTxArgs, InitTxSendArgs, IssueInvoiceTxArgs, NodeHeightResult,
OutputCommitMapping, SendTXArgs, VersionInfo,
};
pub use internal::restore::{check_repair, restore};
pub use types::{
+6 -1
View File
@@ -333,7 +333,12 @@ impl Slate {
Some(&self.pub_blind_sum(keychain.secp())?),
&self.msg_to_sign()?,
)?;
self.participant_data[participant_id].part_sig = Some(sig_part);
for i in 0..self.num_participants {
if self.participant_data[i].id == participant_id as u64 {
self.participant_data[i].part_sig = Some(sig_part);
break;
}
}
Ok(())
}