diff --git a/libwallet/src/api_impl/foreign.rs b/libwallet/src/api_impl/foreign.rs index 7d299ab..6012a1f 100644 --- a/libwallet/src/api_impl/foreign.rs +++ b/libwallet/src/api_impl/foreign.rs @@ -19,7 +19,7 @@ use strum::IntoEnumIterator; use super::owner::tx_lock_outputs; use crate::api_impl::owner::contract_new as owner_contract_new; use crate::api_impl::owner::contract_sign as owner_contract_sign; -use crate::api_impl::owner::{check_ttl, post_tx}; +use crate::api_impl::owner::{check_ttl, payment_proof_kernel, post_tx}; use crate::api_impl::types::update_tx_slate_state; use crate::backend::WalletBackend; use crate::contract::types::{ContractNewArgsAPI, ContractSetupArgsAPI}; @@ -307,21 +307,7 @@ where Error::PaymentProof("Cannot verify early payment proof with no witness data".to_string()) })?; - let (retrieved_kernel, _) = match client.get_kernel(&wd.kernel_commitment, None, None) { - Err(e) => { - return Err(Error::PaymentProof(format!( - "Error retrieving kernel from chain: {}", - e - ))); - } - Ok(None) => { - return Err(Error::PaymentProof(format!( - "Transaction kernel with excess {:?} not found on chain", - wd.kernel_commitment - ))); - } - Ok(Some((k, _, index))) => (k, index), - }; + let (retrieved_kernel, _) = payment_proof_kernel(&mut client, &wd.kernel_commitment)?; // Now verify with retrieved data proof.verify_witness( diff --git a/libwallet/src/api_impl/owner.rs b/libwallet/src/api_impl/owner.rs index a011ddd..b01a02a 100644 --- a/libwallet/src/api_impl/owner.rs +++ b/libwallet/src/api_impl/owner.rs @@ -20,7 +20,7 @@ use uuid::Uuid; use crate::api_impl::foreign::finalize_tx as foreign_finalize; use crate::grin_core::core::amount_to_hr_string; use crate::grin_core::core::hash::Hashed; -use crate::grin_core::core::{FeeFields, Output, OutputFeatures, Transaction}; +use crate::grin_core::core::{FeeFields, Output, OutputFeatures, Transaction, TxKernel}; use crate::grin_core::libtx::proof; use crate::grin_keychain::ViewKey; use crate::grin_util::secp::key::SecretKey; @@ -542,6 +542,24 @@ where }) } +/// Look up the kernel used by a payment proof +pub(super) fn payment_proof_kernel( + client: &mut C, + excess: &Commitment, +) -> Result<(TxKernel, u64), Error> { + match client.get_kernel(excess, None, None) { + Err(e) => Err(Error::PaymentProof(format!( + "Error retrieving kernel from chain: {}", + e + ))), + Ok(None) => Err(Error::PaymentProof(format!( + "Transaction kernel with excess {:?} not found on chain", + excess + ))), + Ok(Some((kernel, _, index))) => Ok((kernel, index)), + } +} + /// Retrieve an early payment proof pub fn retrieve_payment_proof_early<'a, L, C, K>( wallet_inst: Arc>>>, @@ -613,30 +631,11 @@ where w.w2n_client().clone() }; - let kernel_excess = match tx.kernel_excess { - Some(k) => k, - None => { - return Err(Error::PaymentProofRetrieval(format!( - "Early payment proof transaction kernel excess missing", - ))) - } - }; + let kernel_excess = tx.kernel_excess.ok_or_else(|| { + Error::PaymentProofRetrieval("Early payment proof transaction kernel excess missing".into()) + })?; - let (retrieved_kernel, index) = match client.get_kernel(&kernel_excess, None, None) { - Err(e) => { - return Err(Error::PaymentProof(format!( - "Error retrieving kernel from chain: {}", - e - ))); - } - Ok(None) => { - return Err(Error::PaymentProof(format!( - "Transaction kernel with excess {:?} not found on chain", - kernel_excess - ))); - } - Ok(Some((k, _, index))) => (k, index), - }; + let (retrieved_kernel, index) = payment_proof_kernel(&mut client, &kernel_excess)?; proof.witness_data = Some(ProofWitness { kernel_index: index, @@ -1415,21 +1414,7 @@ where }; // Check kernel exists - match client.get_kernel(&proof.excess, None, None) { - Err(e) => { - return Err(Error::PaymentProof(format!( - "Error retrieving kernel from chain: {}", - e - ))); - } - Ok(None) => { - return Err(Error::PaymentProof(format!( - "Transaction kernel with excess {:?} not found on chain", - proof.excess - ))); - } - Ok(Some(_)) => {} - }; + payment_proof_kernel(&mut client, &proof.excess)?; // Check Sigs let recipient_pubkey = proof.recipient_address.pub_key;