commerce.rs (4361B)
1 //! Crypto qualification for exact structured-commerce T1 data. No signing. 2 use crate::{ 3 issuance::VerifiedDmRelayAdvertisement, 4 provider::{ 5 ParticipantProvider, PrivateCiphertext, PrivateCryptoCapability, ProviderCall, 6 ProviderError, 7 }, 8 recovery::ReplayCapability, 9 }; 10 #[cfg(not(feature = "std"))] 11 use alloc::{string::String, vec::Vec}; 12 use core::fmt; 13 use radroots_protocol::{ 14 commerce::{CommerceSigningPlan, EncodedCommerceRumor}, 15 intent::SigningReplay, 16 }; 17 #[cfg(feature = "std")] 18 use std::{string::String, vec::Vec}; 19 20 /// Exact preimages whose encryption and original signed route advertisements 21 /// were checked through the selected participant provider. This proves no 22 /// current store consent, target network policy, dispatch or T1 commit. 23 pub struct VerifiedCommerceSigningPlan { 24 plan: CommerceSigningPlan, 25 deadline_ms: u64, 26 } 27 impl VerifiedCommerceSigningPlan { 28 /// Verifies all peer/self ciphertexts against the unchanged original rumor 29 /// before any business signing. Identity/capability changes fail closed. 30 /// The caller owns any encryption prompting/attempt lifecycle separately. 31 pub async fn verify( 32 provider: &dyn ParticipantProvider, 33 call: ProviderCall, 34 rumor: &EncodedCommerceRumor, 35 plan: CommerceSigningPlan, 36 ) -> Result<Self, ProviderError> { 37 let original = CommerceSigningPlan::parse( 38 plan.operation_id(), 39 plan.command_digest(), 40 rumor, 41 plan.protected_wire(), 42 ) 43 .map_err(|_| ProviderError::InvalidInput)?; 44 for (seal, route) in original.seals().iter().zip(original.routes()) { 45 let verified = 46 VerifiedDmRelayAdvertisement::verify(route.raw_json(), seal.recipient())?; 47 let targets: Vec<String> = route 48 .selected_relays() 49 .iter() 50 .map(|value| String::from(value.as_ref())) 51 .collect(); 52 let checked = verified.capture_route(&targets)?; 53 if checked.source_id() != route.source_id() { 54 return Err(ProviderError::AuthenticationFailed); 55 } 56 } 57 for seal in original.seals() { 58 state(provider, &call, rumor, original.replay()).await?; 59 let ciphertext = PrivateCiphertext::parse(seal.fields().content)?; 60 let clear = provider 61 .decrypt_private(call.clone(), seal.recipient(), ciphertext) 62 .await?; 63 state(provider, &call, rumor, original.replay()).await?; 64 if clear.expose() != rumor.protected_wire() { 65 return Err(ProviderError::AuthenticationFailed); 66 } 67 } 68 Ok(Self { 69 plan: original, 70 deadline_ms: call.deadline_unix_ms().get(), 71 }) 72 } 73 pub const fn plan(&self) -> &CommerceSigningPlan { 74 &self.plan 75 } 76 pub const fn deadline_unix_ms(&self) -> u64 { 77 self.deadline_ms 78 } 79 pub fn into_plan(self) -> CommerceSigningPlan { 80 self.plan 81 } 82 } 83 async fn state( 84 provider: &dyn ParticipantProvider, 85 call: &ProviderCall, 86 rumor: &EncodedCommerceRumor, 87 replay: SigningReplay, 88 ) -> Result<(), ProviderError> { 89 let state = provider.provider_state().await?; 90 let supported = match replay { 91 SigningReplay::NonReplayable => { 92 state.exact_issuance() == Some(ReplayCapability::NonReplayable) 93 } 94 SigningReplay::LocalReplaySafe => { 95 state.exact_issuance().is_some() 96 && state.exact_recovery() 97 == crate::issuance::ExactRecoveryCapability::LocalExactReplay 98 } 99 SigningReplay::ExactReplayByRequestId => { 100 state.exact_issuance() == Some(ReplayCapability::ExactReplayByRequestId) 101 } 102 }; 103 if call.identity() != rumor.author() 104 || state.identity() != rumor.author() 105 || state.encryption() != PrivateCryptoCapability::Nip44V2 106 || state.decryption() != PrivateCryptoCapability::Nip44V2 107 || !supported 108 { 109 return Err(ProviderError::CapabilityUnavailable); 110 } 111 Ok(()) 112 } 113 impl fmt::Debug for VerifiedCommerceSigningPlan { 114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 115 f.write_str("VerifiedCommerceSigningPlan(<redacted>)") 116 } 117 }