receipt_plans.rs (3813B)
1 //! Native storage tests use real synthetic NIP-44 data, never host custody. 2 #[path = "receipt_delivery.rs"] 3 mod delivery; 4 #[path = "receipt_installation.rs"] 5 mod installation; 6 #[path = "receipt_issuance.rs"] 7 mod issuance; 8 #[path = "receipt_snapshots.rs"] 9 mod snapshots; 10 use super::*; 11 use nostr::{JsonUtil, nips::nip44}; 12 use radroots_domain::EnvelopeEventId; 13 use radroots_protocol::chat::{ChatRumor, ChatSeal, ChatSigningPlan}; 14 use radroots_protocol::intent::SigningReplay; 15 use radroots_protocol::nip59::CapturedDmRoute; 16 use radroots_storage::command::{BoundSigningPlan, PlanBindingOutcome}; 17 fn keys(last: u8) -> nostr::Keys { 18 let mut bytes = [0; 32]; 19 bytes[31] = last; 20 nostr::Keys::new(nostr::SecretKey::from_slice(&bytes).unwrap()) 21 } 22 fn peer_request(binding: &WorkspaceBinding, id: u8) -> AdmissionRequest { 23 let request = request(binding, id, "protected complete private plan", "original"); 24 let mut value: serde_json::Value = 25 serde_json::from_slice(request.command().protected_wire()).unwrap(); 26 value["input"]["peer"] = keys(2).public_key().to_hex().into(); 27 AdmissionRequest::from_protected_wire(binding.clone(), &serde_json::to_vec(&value).unwrap()) 28 .unwrap() 29 } 30 fn plan( 31 command: &radroots_protocol::command::CapturedChatCommand, 32 created: u64, 33 suffix: &str, 34 ) -> ChatSigningPlan { 35 plan_with_routes(command, created, suffix, None) 36 } 37 fn plan_with_routes( 38 command: &radroots_protocol::command::CapturedChatCommand, 39 created: u64, 40 suffix: &str, 41 selected: Option<&[String]>, 42 ) -> ChatSigningPlan { 43 let rumor = ChatRumor::from_command(command, UnixSeconds::new(created).unwrap()).unwrap(); 44 let author = keys(1); 45 let mut seals = Vec::new(); 46 let mut routes = Vec::new(); 47 for recipient in rumor.recipients() { 48 let target = nostr::PublicKey::from_slice(recipient.as_bytes()).unwrap(); 49 let ciphertext = nip44::encrypt( 50 author.secret_key(), 51 &target, 52 rumor.json(), 53 nip44::Version::V2, 54 ) 55 .unwrap(); 56 seals.push( 57 ChatSeal::new( 58 &rumor, 59 *recipient, 60 UnixSeconds::new(created - 1).unwrap(), 61 &ciphertext, 62 ) 63 .unwrap(), 64 ); 65 let signer = if recipient == &key() { 66 keys(1) 67 } else { 68 keys(2) 69 }; 70 let urls = selected.map_or_else( 71 || vec![format!("wss://relay.example/{suffix}")], 72 <[String]>::to_vec, 73 ); 74 let source = nostr::UnsignedEvent { 75 id: None, 76 pubkey: signer.public_key(), 77 created_at: nostr::Timestamp::from_secs(created - 1), 78 kind: nostr::Kind::Custom(10050), 79 tags: nostr::Tags::from_list( 80 urls.iter() 81 .map(|url| nostr::Tag::parse(["relay", url]).unwrap()) 82 .collect(), 83 ), 84 content: "public signed route provenance ".repeat(1000), 85 } 86 .sign_with_keys(&signer) 87 .unwrap(); 88 source.verify().unwrap(); 89 routes.push( 90 CapturedDmRoute::from_observed_descriptor( 91 *recipient, 92 EnvelopeEventId::from_bytes(*source.id.as_bytes()), 93 &source.as_json(), 94 &urls, 95 ) 96 .unwrap(), 97 ); 98 } 99 ChatSigningPlan::new( 100 command, 101 UnixSeconds::new(created).unwrap(), 102 seals, 103 routes, 104 SigningReplay::NonReplayable, 105 ) 106 .unwrap() 107 } 108 fn bound(outcome: PlanBindingOutcome) -> BoundSigningPlan { 109 match outcome { 110 PlanBindingOutcome::Committed(bound) => bound, 111 PlanBindingOutcome::Unknown(_) => panic!("fixture must confirm T1"), 112 } 113 }