mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-12-03 20:33:59 +01:00
Payment Proof export + validation (#289)
* add payment proof struct and deser * rustfmt * adding proof export functions * rustfmt * add payment proof validation function * rustfmt * add RPC version of retrieve_payment_proof + doctest * rustfmt * add verify_proof rpc function and documentation for new functions * rustfmt * add export and verify commands * rustfmt * test + test framework fixes * rustfmt * check whether addresses belong to this wallet, output such when checking * rustfmt * remove raw pubkey address and replace with ov3 address in user-facing contexts * merge from master and rustfmt * doctests
This commit is contained in:
@@ -30,10 +30,11 @@ use crate::slate::{PaymentInfo, Slate};
|
||||
use crate::types::{AcctPathMapping, NodeClient, TxLogEntry, TxWrapper, WalletBackend, WalletInfo};
|
||||
use crate::{
|
||||
address, wallet_lock, InitTxArgs, IssueInvoiceTxArgs, NodeHeightResult, OutputCommitMapping,
|
||||
ScannedBlockInfo, TxLogEntryType, WalletInitStatus, WalletInst, WalletLCProvider,
|
||||
PaymentProof, ScannedBlockInfo, TxLogEntryType, WalletInitStatus, WalletInst, WalletLCProvider,
|
||||
};
|
||||
use crate::{Error, ErrorKind};
|
||||
use ed25519_dalek::PublicKey as DalekPublicKey;
|
||||
use ed25519_dalek::SecretKey as DalekSecretKey;
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
@@ -193,6 +194,103 @@ where
|
||||
Ok((validated, wallet_info))
|
||||
}
|
||||
|
||||
/// Retrieve payment proof
|
||||
pub fn retrieve_payment_proof<'a, L, C, K>(
|
||||
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
||||
keychain_mask: Option<&SecretKey>,
|
||||
status_send_channel: &Option<Sender<StatusMessage>>,
|
||||
refresh_from_node: bool,
|
||||
tx_id: Option<u32>,
|
||||
tx_slate_id: Option<Uuid>,
|
||||
) -> Result<PaymentProof, Error>
|
||||
where
|
||||
L: WalletLCProvider<'a, C, K>,
|
||||
C: NodeClient + 'a,
|
||||
K: Keychain + 'a,
|
||||
{
|
||||
if tx_id.is_none() && tx_slate_id.is_none() {
|
||||
return Err(ErrorKind::PaymentProofRetrieval(
|
||||
"Transaction ID or Slate UUID must be specified".into(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let mut _validated = false;
|
||||
if refresh_from_node {
|
||||
_validated = update_wallet_state(
|
||||
wallet_inst.clone(),
|
||||
keychain_mask,
|
||||
status_send_channel,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
let txs = retrieve_txs(
|
||||
wallet_inst.clone(),
|
||||
keychain_mask,
|
||||
status_send_channel,
|
||||
refresh_from_node,
|
||||
tx_id,
|
||||
tx_slate_id,
|
||||
)?;
|
||||
if txs.1.len() != 1 {
|
||||
return Err(ErrorKind::PaymentProofRetrieval("Transaction doesn't exist".into()).into());
|
||||
}
|
||||
// Pull out all needed fields, returning an error if they're not present
|
||||
let tx = txs.1[0].clone();
|
||||
let proof = match tx.payment_proof {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return Err(ErrorKind::PaymentProofRetrieval(
|
||||
"Transaction does not contain a payment proof".into(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
let amount = if tx.amount_credited >= tx.amount_debited {
|
||||
tx.amount_credited - tx.amount_debited
|
||||
} else {
|
||||
let fee = match tx.fee {
|
||||
Some(f) => f,
|
||||
None => 0,
|
||||
};
|
||||
tx.amount_debited - tx.amount_credited - fee
|
||||
};
|
||||
let excess = match tx.kernel_excess {
|
||||
Some(e) => e,
|
||||
None => {
|
||||
return Err(ErrorKind::PaymentProofRetrieval(
|
||||
"Transaction does not contain kernel excess".into(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
let r_sig = match proof.receiver_signature {
|
||||
Some(e) => e,
|
||||
None => {
|
||||
return Err(ErrorKind::PaymentProofRetrieval(
|
||||
"Proof does not contain receiver signature ".into(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
let s_sig = match proof.sender_signature {
|
||||
Some(e) => e,
|
||||
None => {
|
||||
return Err(ErrorKind::PaymentProofRetrieval(
|
||||
"Proof does not contain sender signature ".into(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
Ok(PaymentProof {
|
||||
amount: amount,
|
||||
excess: excess,
|
||||
recipient_address: address::onion_v3_from_pubkey(&proof.receiver_address)?,
|
||||
recipient_sig: r_sig,
|
||||
sender_address: address::onion_v3_from_pubkey(&proof.sender_address)?,
|
||||
sender_sig: s_sig,
|
||||
})
|
||||
}
|
||||
|
||||
/// Initiate tx as sender
|
||||
pub fn init_send_tx<'a, T: ?Sized, C, K>(
|
||||
w: &mut T,
|
||||
@@ -469,7 +567,7 @@ where
|
||||
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::verify_payment_proof(&mut *w, keychain_mask, &parent_key_id, &context, &sl)?;
|
||||
tx::verify_slate_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)?;
|
||||
{
|
||||
@@ -779,6 +877,80 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Verify/validate arbitrary payment proof
|
||||
/// Returns (whether this wallet is the sender, whether this wallet is the recipient)
|
||||
pub fn verify_payment_proof<'a, L, C, K>(
|
||||
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
||||
keychain_mask: Option<&SecretKey>,
|
||||
proof: &PaymentProof,
|
||||
) -> Result<(bool, bool), Error>
|
||||
where
|
||||
L: WalletLCProvider<'a, C, K>,
|
||||
C: NodeClient + 'a,
|
||||
K: Keychain + 'a,
|
||||
{
|
||||
let sender_pubkey = address::pubkey_from_onion_v3(&proof.sender_address)?;
|
||||
let msg = tx::payment_proof_message(proof.amount, &proof.excess, sender_pubkey)?;
|
||||
|
||||
let (mut client, parent_key_id, keychain) = {
|
||||
wallet_lock!(wallet_inst, w);
|
||||
(
|
||||
w.w2n_client().clone(),
|
||||
w.parent_key_id(),
|
||||
w.keychain(keychain_mask)?,
|
||||
)
|
||||
};
|
||||
|
||||
// Check kernel exists
|
||||
match client.get_kernel(&proof.excess, None, None) {
|
||||
Err(e) => {
|
||||
return Err(ErrorKind::PaymentProof(
|
||||
format!("Error retrieving kernel from chain: {}", e).to_owned(),
|
||||
))?;
|
||||
}
|
||||
Ok(None) => {
|
||||
return Err(ErrorKind::PaymentProof(
|
||||
format!(
|
||||
"Transaction kernel with excess {:?} not found on chain",
|
||||
proof.excess
|
||||
)
|
||||
.to_owned(),
|
||||
))?;
|
||||
}
|
||||
Ok(Some(_)) => {}
|
||||
};
|
||||
|
||||
// Check Sigs
|
||||
let recipient_pubkey = address::pubkey_from_onion_v3(&proof.recipient_address)?;
|
||||
if let Err(_) = recipient_pubkey.verify(&msg, &proof.recipient_sig) {
|
||||
return Err(ErrorKind::PaymentProof(
|
||||
"Invalid recipient signature".to_owned(),
|
||||
))?;
|
||||
};
|
||||
|
||||
let sender_pubkey = address::pubkey_from_onion_v3(&proof.sender_address)?;
|
||||
if let Err(_) = sender_pubkey.verify(&msg, &proof.sender_sig) {
|
||||
return Err(ErrorKind::PaymentProof(
|
||||
"Invalid sender signature".to_owned(),
|
||||
))?;
|
||||
};
|
||||
|
||||
// for now, simple test as to whether one of the addresses belongs to this wallet
|
||||
let sec_key = address::address_from_derivation_path(&keychain, &parent_key_id, 0)?;
|
||||
let d_skey = match DalekSecretKey::from_bytes(&sec_key.0) {
|
||||
Ok(k) => k,
|
||||
Err(e) => {
|
||||
return Err(ErrorKind::ED25519Key(format!("{}", e)).to_owned())?;
|
||||
}
|
||||
};
|
||||
let my_address_pubkey: DalekPublicKey = (&d_skey).into();
|
||||
|
||||
let sender_mine = my_address_pubkey == sender_pubkey;
|
||||
let recipient_mine = my_address_pubkey == recipient_pubkey;
|
||||
|
||||
Ok((sender_mine, recipient_mine))
|
||||
}
|
||||
|
||||
/// Attempt to update outputs in wallet, return whether it was successful
|
||||
fn update_outputs<'a, L, C, K>(
|
||||
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
||||
|
||||
@@ -22,6 +22,7 @@ use crate::slate_versions::SlateVersion;
|
||||
use crate::types::OutputData;
|
||||
|
||||
use ed25519_dalek::PublicKey as DalekPublicKey;
|
||||
use ed25519_dalek::Signature as DalekSignature;
|
||||
|
||||
/// Send TX API Args
|
||||
// TODO: This is here to ensure the legacy V1 API remains intact
|
||||
@@ -222,3 +223,27 @@ pub struct VersionInfo {
|
||||
/// Slate version
|
||||
pub supported_slate_versions: Vec<SlateVersion>,
|
||||
}
|
||||
|
||||
/// Packaged Payment Proof
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct PaymentProof {
|
||||
/// Amount
|
||||
#[serde(with = "secp_ser::string_or_u64")]
|
||||
pub amount: u64,
|
||||
/// Kernel Excess
|
||||
#[serde(
|
||||
serialize_with = "secp_ser::as_hex",
|
||||
deserialize_with = "secp_ser::commitment_from_hex"
|
||||
)]
|
||||
pub excess: pedersen::Commitment,
|
||||
/// Recipient Wallet Address (Onion V3)
|
||||
pub recipient_address: String,
|
||||
/// Recipient Signature
|
||||
#[serde(with = "dalek_ser::dalek_sig_serde")]
|
||||
pub recipient_sig: DalekSignature,
|
||||
/// Sender Wallet Address (Onion V3)
|
||||
pub sender_address: String,
|
||||
/// Sender Signature
|
||||
#[serde(with = "dalek_ser::dalek_sig_serde")]
|
||||
pub sender_sig: DalekSignature,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user