mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-12-03 20:33:59 +01:00
Payment Proof Implementation (#259)
* refactor address generation code into libwallet, bool to flag whether to include proof, add sender address in init_send_tx * rustfmt * require payment proof addr as part of init_tx * rustfmt * store payment proof on sender transaction side * rustfmt * change sig to ed25519 sig * rustfmt * add message creation and signature * rustfmt * add payment proof verification function * rustfmt * validate proof on sender side, store proof * rustfmt * fix json tests * fixes and updates to tests * added API functions for converting and retrieving proof addresses * rustfmt * add payment proof to init_send_tx example * rustfmt * incorrect comment
This commit is contained in:
@@ -20,7 +20,7 @@ use crate::grin_util::secp::key::SecretKey;
|
||||
use crate::internal::{tx, updater};
|
||||
use crate::slate_versions::SlateVersion;
|
||||
use crate::{
|
||||
BlockFees, CbData, Error, ErrorKind, NodeClient, Slate, TxLogEntryType, VersionInfo,
|
||||
address, BlockFees, CbData, Error, ErrorKind, NodeClient, Slate, TxLogEntryType, VersionInfo,
|
||||
WalletBackend,
|
||||
};
|
||||
|
||||
@@ -113,6 +113,21 @@ where
|
||||
use_test_rng,
|
||||
)?;
|
||||
tx::update_message(&mut *w, keychain_mask, &mut ret_slate)?;
|
||||
|
||||
let keychain = w.keychain(keychain_mask)?;
|
||||
let excess = ret_slate.calc_excess(&keychain)?;
|
||||
|
||||
if let Some(ref mut p) = ret_slate.payment_proof {
|
||||
let sig = tx::create_payment_proof_signature(
|
||||
ret_slate.amount,
|
||||
&excess,
|
||||
p.sender_address,
|
||||
address::address_from_derivation_path(&keychain, &parent_key_id, 0)?,
|
||||
)?;
|
||||
|
||||
p.receiver_signature = Some(sig);
|
||||
}
|
||||
|
||||
Ok(ret_slate)
|
||||
}
|
||||
|
||||
@@ -130,7 +145,7 @@ where
|
||||
let mut sl = slate.clone();
|
||||
let context = w.get_private_context(keychain_mask, sl.id.as_bytes(), 1)?;
|
||||
tx::complete_tx(&mut *w, keychain_mask, &mut sl, 1, &context)?;
|
||||
tx::update_stored_tx(&mut *w, keychain_mask, &mut sl, true)?;
|
||||
tx::update_stored_tx(&mut *w, keychain_mask, &context, &mut sl, true)?;
|
||||
tx::update_message(&mut *w, keychain_mask, &mut sl)?;
|
||||
{
|
||||
let mut batch = w.batch(keychain_mask)?;
|
||||
|
||||
@@ -26,13 +26,15 @@ use crate::grin_util::Mutex;
|
||||
use crate::api_impl::owner_updater::StatusMessage;
|
||||
use crate::grin_keychain::{Identifier, Keychain};
|
||||
use crate::internal::{keys, scan, selection, tx, updater};
|
||||
use crate::slate::Slate;
|
||||
use crate::slate::{PaymentInfo, Slate};
|
||||
use crate::types::{AcctPathMapping, NodeClient, TxLogEntry, TxWrapper, WalletBackend, WalletInfo};
|
||||
use crate::{
|
||||
wallet_lock, InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult, OutputCommitMapping,
|
||||
address, wallet_lock, InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult, OutputCommitMapping,
|
||||
ScannedBlockInfo, TxLogEntryType, WalletInitStatus, WalletInst, WalletLCProvider,
|
||||
};
|
||||
use crate::{Error, ErrorKind};
|
||||
use ed25519_dalek::PublicKey as DalekPublicKey;
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -72,6 +74,26 @@ where
|
||||
w.set_parent_key_id_by_name(label)
|
||||
}
|
||||
|
||||
/// Retrieve the payment proof address for the current parent key at
|
||||
/// the given index
|
||||
/// set active account
|
||||
pub fn get_public_proof_address<'a, L, C, K>(
|
||||
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
||||
keychain_mask: Option<&SecretKey>,
|
||||
index: u32,
|
||||
) -> Result<DalekPublicKey, Error>
|
||||
where
|
||||
L: WalletLCProvider<'a, C, K>,
|
||||
C: NodeClient + 'a,
|
||||
K: Keychain + 'a,
|
||||
{
|
||||
wallet_lock!(wallet_inst, w);
|
||||
let parent_key_id = w.parent_key_id();
|
||||
let k = w.keychain(keychain_mask)?;
|
||||
let sec_addr_key = address::address_from_derivation_path(&k, &parent_key_id, index)?;
|
||||
Ok(address::ed25519_keypair(&sec_addr_key)?.1)
|
||||
}
|
||||
|
||||
/// retrieve outputs
|
||||
pub fn retrieve_outputs<'a, L, C, K>(
|
||||
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
||||
@@ -222,7 +244,7 @@ where
|
||||
return Ok(slate);
|
||||
}
|
||||
|
||||
let context = tx::add_inputs_to_slate(
|
||||
let mut context = tx::add_inputs_to_slate(
|
||||
&mut *w,
|
||||
keychain_mask,
|
||||
&mut slate,
|
||||
@@ -237,6 +259,26 @@ where
|
||||
use_test_rng,
|
||||
)?;
|
||||
|
||||
// Payment Proof, add addresses to slate and save address
|
||||
// TODO: Note we only use single derivation path for now,
|
||||
// probably want to allow sender to specify which one
|
||||
let deriv_path = 0u32;
|
||||
|
||||
if let Some(a) = args.payment_proof_recipient_address {
|
||||
let k = w.keychain(keychain_mask)?;
|
||||
|
||||
let sec_addr_key = address::address_from_derivation_path(&k, &parent_key_id, deriv_path)?;
|
||||
let sender_address = address::ed25519_keypair(&sec_addr_key)?.1;
|
||||
|
||||
slate.payment_proof = Some(PaymentInfo {
|
||||
sender_address,
|
||||
receiver_address: a,
|
||||
receiver_signature: None,
|
||||
});
|
||||
|
||||
context.payment_proof_derivation_index = Some(deriv_path);
|
||||
}
|
||||
|
||||
// Save the aggsig context in our DB for when we
|
||||
// recieve the transaction back
|
||||
{
|
||||
@@ -247,6 +289,7 @@ where
|
||||
if let Some(v) = args.target_slate_version {
|
||||
slate.version_info.orig_version = v;
|
||||
}
|
||||
|
||||
Ok(slate)
|
||||
}
|
||||
|
||||
@@ -417,8 +460,10 @@ where
|
||||
{
|
||||
let mut sl = slate.clone();
|
||||
let context = w.get_private_context(keychain_mask, sl.id.as_bytes(), 0)?;
|
||||
let parent_key_id = w.parent_key_id();
|
||||
tx::complete_tx(&mut *w, keychain_mask, &mut sl, 0, &context)?;
|
||||
tx::update_stored_tx(&mut *w, keychain_mask, &mut sl, false)?;
|
||||
tx::verify_payment_proof(&mut *w, keychain_mask, &parent_key_id, &context, &sl)?;
|
||||
tx::update_stored_tx(&mut *w, keychain_mask, &context, &mut sl, false)?;
|
||||
tx::update_message(&mut *w, keychain_mask, &mut sl)?;
|
||||
{
|
||||
let mut batch = w.batch(keychain_mask)?;
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
use crate::grin_core::libtx::secp_ser;
|
||||
use crate::grin_keychain::Identifier;
|
||||
use crate::grin_util::secp::pedersen;
|
||||
use crate::slate_versions::ser as dalek_ser;
|
||||
use crate::slate_versions::SlateVersion;
|
||||
use crate::types::OutputData;
|
||||
|
||||
use ed25519_dalek::PublicKey as DalekPublicKey;
|
||||
|
||||
/// Send TX API Args
|
||||
// TODO: This is here to ensure the legacy V1 API remains intact
|
||||
// remove this when v1 api is removed
|
||||
@@ -86,6 +89,9 @@ pub struct InitTxArgs {
|
||||
/// 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>,
|
||||
/// If set, require a payment proof for the particular recipient
|
||||
#[serde(with = "dalek_ser::option_dalek_pubkey_serde")]
|
||||
pub payment_proof_recipient_address: Option<DalekPublicKey>,
|
||||
/// If true, just return an estimate of the resulting slate, containing fees and amounts
|
||||
/// locked without actually locking outputs or creating the transaction. Note if this is set to
|
||||
/// 'true', the amount field in the slate will contain the total amount locked, not the provided
|
||||
@@ -124,6 +130,7 @@ impl Default for InitTxArgs {
|
||||
message: None,
|
||||
target_slate_version: None,
|
||||
estimate_only: Some(false),
|
||||
payment_proof_recipient_address: None,
|
||||
send_args: None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user