commerce_signing.rs (11296B)
1 #![cfg(feature = "commerce")] 2 use radroots_domain::{CommandDigest, OperationId, PublicKey, RecordId, Sequence, UnixSeconds}; 3 use radroots_protocol::{ 4 command::CapturedOrderAcceptance, 5 commerce::{self, CommerceSeal}, 6 nip01::{UnsignedEvent, WireError}, 7 nip59::{PrivateEnvelopeBudget, SIGNED_MAX_BYTES}, 8 }; 9 use serde_json::{Value, json}; 10 fn fixture() -> Value { 11 serde_json::from_str(include_str!("fixtures/commerce_signing.v1.json")).unwrap() 12 } 13 fn command(value: &Value) -> CapturedOrderAcceptance { 14 CapturedOrderAcceptance::parse(value["command_wire"].as_str().unwrap().as_bytes()).unwrap() 15 } 16 17 #[test] 18 fn exact_initial_record_rumor_command_and_both_seal_intents_match_independent_crypto_vectors() { 19 let value = fixture(); 20 let command = command(&value); 21 assert_eq!(command.digest().to_string(), value["command_digest"]); 22 let request = commerce::decode(value["request_wire"].as_str().unwrap().as_bytes()).unwrap(); 23 let rumor = commerce::initial_commitment_rumor( 24 &command, 25 &request, 26 None, 27 UnixSeconds::new(451).unwrap(), 28 ) 29 .unwrap(); 30 assert_eq!(rumor.record_id().to_string(), value["record_id"]); 31 assert_eq!(rumor.event_id().to_string(), value["rumor_id"]); 32 assert_eq!( 33 rumor.protected_wire(), 34 value["rumor_wire"].as_str().unwrap() 35 ); 36 let parsed: Value = serde_json::from_str(rumor.protected_wire()).unwrap(); 37 assert_eq!(parsed["content"], value["record_wire"]); 38 assert_eq!(rumor.author(), command.context().actor().participant()); 39 assert_eq!(rumor.created_at().get(), 451); 40 assert_eq!( 41 *rumor.recipients(), 42 [ 43 value["seals"][0]["recipient"] 44 .as_str() 45 .unwrap() 46 .parse() 47 .unwrap(), 48 value["seals"][1]["recipient"] 49 .as_str() 50 .unwrap() 51 .parse() 52 .unwrap() 53 ] 54 ); 55 for row in value["seals"].as_array().unwrap() { 56 let recipient: PublicKey = row["recipient"].as_str().unwrap().parse().unwrap(); 57 let seal = CommerceSeal::new( 58 command.context().operation_id(), 59 command.digest(), 60 &rumor, 61 recipient, 62 UnixSeconds::new(450).unwrap(), 63 row["ciphertext"].as_str().unwrap(), 64 ) 65 .unwrap(); 66 assert_eq!(seal.record_id(), rumor.record_id()); 67 assert_eq!(seal.rumor_id(), rumor.event_id()); 68 assert_eq!(seal.operation_id(), command.context().operation_id()); 69 assert_eq!(seal.command_digest(), command.digest()); 70 let encoded = UnsignedEvent::encode(&seal.fields(), SIGNED_MAX_BYTES).unwrap(); 71 assert_eq!(encoded.event_id(), seal.event_id()); 72 let expected: Value = serde_json::from_str(row["raw"].as_str().unwrap()).unwrap(); 73 assert_eq!( 74 serde_json::from_str::<Value>(encoded.json()).unwrap()["id"], 75 expected["id"] 76 ); 77 assert_eq!(hex(&seal.event_id()), row["event_id"]); 78 assert!(row["raw"].as_str().unwrap().len() <= rumor.budget().signed_seal_max_bytes()); 79 assert_eq!(seal.fields().tags.len(), 0); 80 assert_eq!(seal.fields().kind, 13); 81 assert_eq!(format!("{seal:?}"), "CommerceSeal(<redacted>)"); 82 } 83 } 84 fn hex(bytes: &[u8]) -> String { 85 bytes.iter().map(|byte| format!("{byte:02x}")).collect() 86 } 87 88 #[test] 89 fn complete_peer_self_plan_matches_independent_binary_bytes_and_refuses_changed_originals() { 90 use radroots_protocol::{ 91 commerce::CommerceSigningPlan, intent::SigningReplay, nip59::CapturedDmRoute, 92 }; 93 let value = fixture(); 94 let command = command(&value); 95 let request = commerce::decode(value["request_wire"].as_str().unwrap().as_bytes()).unwrap(); 96 let rumor = commerce::initial_commitment_rumor( 97 &command, 98 &request, 99 None, 100 UnixSeconds::new(451).unwrap(), 101 ) 102 .unwrap(); 103 let seals = || { 104 value["seals"] 105 .as_array() 106 .unwrap() 107 .iter() 108 .map(|row| { 109 CommerceSeal::new( 110 command.context().operation_id(), 111 command.digest(), 112 &rumor, 113 row["recipient"].as_str().unwrap().parse().unwrap(), 114 UnixSeconds::new(450).unwrap(), 115 row["ciphertext"].as_str().unwrap(), 116 ) 117 .unwrap() 118 }) 119 .collect::<Vec<_>>() 120 .try_into() 121 .unwrap() 122 }; 123 let routes = || { 124 value["seals"] 125 .as_array() 126 .unwrap() 127 .iter() 128 .map(|row| { 129 CapturedDmRoute::from_observed_descriptor( 130 row["recipient"].as_str().unwrap().parse().unwrap(), 131 row["route"]["event_id"].as_str().unwrap().parse().unwrap(), 132 row["route"]["raw"].as_str().unwrap(), 133 &["wss://relay.example".into()], 134 ) 135 .unwrap() 136 }) 137 .collect::<Vec<_>>() 138 .try_into() 139 .unwrap() 140 }; 141 for replay in [ 142 SigningReplay::NonReplayable, 143 SigningReplay::LocalReplaySafe, 144 SigningReplay::ExactReplayByRequestId, 145 ] { 146 let plan = CommerceSigningPlan::new( 147 command.context().operation_id(), 148 command.digest(), 149 &rumor, 150 seals(), 151 routes(), 152 replay, 153 ) 154 .unwrap(); 155 if replay == SigningReplay::NonReplayable { 156 assert_eq!(hex(plan.protected_wire()), value["plan_hex"]); 157 assert_eq!(hex(plan.digest()), value["plan_digest"]); 158 } 159 let restored = CommerceSigningPlan::parse( 160 command.context().operation_id(), 161 command.digest(), 162 &rumor, 163 plan.protected_wire(), 164 ) 165 .unwrap(); 166 assert_eq!(restored.protected_wire(), plan.protected_wire()); 167 assert_eq!(restored.replay(), replay); 168 for end in (0..plan.protected_wire().len()).step_by(73) { 169 assert!( 170 CommerceSigningPlan::parse( 171 command.context().operation_id(), 172 command.digest(), 173 &rumor, 174 &plan.protected_wire()[..end] 175 ) 176 .is_err() 177 ); 178 } 179 let changed = commerce::initial_commitment_rumor( 180 &command, 181 &request, 182 None, 183 UnixSeconds::new(452).unwrap(), 184 ) 185 .unwrap(); 186 assert!( 187 CommerceSigningPlan::parse( 188 command.context().operation_id(), 189 command.digest(), 190 &changed, 191 plan.protected_wire() 192 ) 193 .is_err() 194 ); 195 let mut extra = plan.protected_wire().to_vec(); 196 extra.push(0); 197 assert!( 198 CommerceSigningPlan::parse( 199 command.context().operation_id(), 200 command.digest(), 201 &rumor, 202 &extra 203 ) 204 .is_err() 205 ); 206 } 207 let mut wrong = seals(); 208 wrong.swap(0, 1); 209 assert!( 210 CommerceSigningPlan::new( 211 command.context().operation_id(), 212 command.digest(), 213 &rumor, 214 wrong, 215 routes(), 216 SigningReplay::NonReplayable 217 ) 218 .is_err() 219 ); 220 } 221 222 #[test] 223 fn initial_intent_rejects_wrong_request_terms_actor_state_deadline_and_unusable_author_head_claims() 224 { 225 let value = fixture(); 226 let request = commerce::decode(value["request_wire"].as_str().unwrap().as_bytes()).unwrap(); 227 let original: Value = serde_json::from_str(value["command_wire"].as_str().unwrap()).unwrap(); 228 for (path, changed) in [ 229 ("/input/request", json!("fa".repeat(32))), 230 ("/input/terms_digest", json!("fb".repeat(32))), 231 ( 232 "/context/actor/participant", 233 json!(request.content().header().buyer()), 234 ), 235 ("/context/expected/agreement", json!("fc".repeat(32))), 236 ("/context/expected/seller_sequence", json!("1")), 237 ("/context/expected/buyer_head", Value::Null), 238 ("/context/not_after", json!("451")), 239 ] { 240 let mut wire = original.clone(); 241 *wire.pointer_mut(path).unwrap() = changed; 242 let command = CapturedOrderAcceptance::parse(&serde_json::to_vec(&wire).unwrap()).unwrap(); 243 assert!( 244 commerce::initial_commitment_rumor( 245 &command, 246 &request, 247 None, 248 UnixSeconds::new(451).unwrap() 249 ) 250 .is_err(), 251 "{path}" 252 ); 253 } 254 let command = command(&value); 255 for at in [0, 449, 600] { 256 assert!( 257 commerce::initial_commitment_rumor( 258 &command, 259 &request, 260 None, 261 UnixSeconds::new(at).unwrap() 262 ) 263 .is_err() 264 ); 265 } 266 assert!( 267 commerce::initial_commitment_rumor( 268 &command, 269 &request, 270 Some((RecordId::from_bytes([1; 32]), Sequence::new(0).unwrap())), 271 UnixSeconds::new(451).unwrap() 272 ) 273 .is_err() 274 ); 275 assert!( 276 commerce::initial_commitment_rumor( 277 &command, 278 &request, 279 Some(( 280 RecordId::from_bytes([1; 32]), 281 Sequence::new(i64::MAX as u64).unwrap() 282 )), 283 UnixSeconds::new(451).unwrap() 284 ) 285 .is_err() 286 ); 287 } 288 289 #[test] 290 fn commerce_uses_the_shared_nested_budget_and_refuses_wrong_recipient_ciphertext_and_time() { 291 let value = fixture(); 292 let command = command(&value); 293 let request = commerce::decode(value["request_wire"].as_str().unwrap().as_bytes()).unwrap(); 294 let rumor = commerce::initial_commitment_rumor( 295 &command, 296 &request, 297 None, 298 UnixSeconds::new(451).unwrap(), 299 ) 300 .unwrap(); 301 assert_eq!( 302 rumor.budget(), 303 PrivateEnvelopeBudget::for_rumor(rumor.protected_wire().len(), rumor.author()).unwrap() 304 ); 305 let row = &value["seals"][0]; 306 let recipient: PublicKey = row["recipient"].as_str().unwrap().parse().unwrap(); 307 let ciphertext = row["ciphertext"].as_str().unwrap(); 308 let other: PublicKey = "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9" 309 .parse() 310 .unwrap(); 311 assert_eq!( 312 CommerceSeal::new( 313 OperationId::from_bytes([1; 32]), 314 CommandDigest::from_bytes([2; 32]), 315 &rumor, 316 other, 317 UnixSeconds::new(450).unwrap(), 318 ciphertext 319 ) 320 .err(), 321 Some(WireError::InvalidInput) 322 ); 323 for (at, ciphertext) in [(0, ciphertext), (452, ciphertext), (450, "invalid")] { 324 assert!( 325 CommerceSeal::new( 326 command.context().operation_id(), 327 command.digest(), 328 &rumor, 329 recipient, 330 UnixSeconds::new(at).unwrap(), 331 ciphertext 332 ) 333 .is_err() 334 ); 335 } 336 }