chat_seal.rs (6278B)
1 #![cfg(feature = "chat")] 2 3 use nostr::{JsonUtil, nips::nip44}; 4 use radroots_domain::{ 5 ActorRef, ActorRole, AuthorityRef, CommandContextData, ExecutorId, Generation, GrantId, 6 OperationId, PublicKey, RouteDigest, ThreadId, UnixSeconds, WorkspaceId, 7 }; 8 use radroots_protocol::{ 9 chat::{ChatRumor, ChatSeal}, 10 command::{CapturedChatCommand, InquireInput}, 11 }; 12 use radroots_signing::{ 13 issuance::{ArtifactRole, OutcomeKind, SigningIntent, SigningOutcome}, 14 provider::{ProviderCall, ProviderError}, 15 request::CancellationSignal, 16 }; 17 use std::num::NonZeroU64; 18 19 const NOW: u64 = 1_700_000_000; 20 fn keys(last: u8) -> nostr::Keys { 21 // Published synthetic test identities, isolated from all host custody. 22 let mut bytes = [0; 32]; 23 bytes[31] = last; 24 nostr::Keys::new(nostr::SecretKey::from_slice(&bytes).unwrap()) 25 } 26 fn call(author: PublicKey) -> ProviderCall { 27 ProviderCall::new( 28 author, 29 NonZeroU64::new((NOW + 120) * 1000).unwrap(), 30 CancellationSignal::new(), 31 ) 32 } 33 fn command() -> CapturedChatCommand { 34 let author = PublicKey::from_hex(&keys(1).public_key().to_hex()).unwrap(); 35 let peer = PublicKey::from_hex(&keys(2).public_key().to_hex()).unwrap(); 36 let context = CommandContextData::new( 37 OperationId::from_bytes([1; 32]), 38 ActorRef::new( 39 author, 40 ActorRole::Buyer, 41 None, 42 Some(GrantId::from_bytes([2; 32])), 43 ), 44 WorkspaceId::from_bytes([3; 32]), 45 AuthorityRef::new(ExecutorId::from_bytes([4; 32]), Generation::new(1).unwrap()), 46 RouteDigest::from_bytes([5; 32]), 47 None, 48 None, 49 UnixSeconds::new(NOW + 120).unwrap(), 50 None, 51 ) 52 .unwrap(); 53 CapturedChatCommand::inquire( 54 context, 55 InquireInput::new( 56 ThreadId::from_bytes([6; 32]), 57 peer, 58 None, 59 "private 🥬 Å A\u{030a}\n", 60 ) 61 .unwrap(), 62 ) 63 .unwrap() 64 } 65 66 #[test] 67 fn known_kind_conversion_signs_and_authenticates_the_same_rumor_for_peer_and_self() { 68 let author = keys(1); 69 let peer = keys(2); 70 let command = command(); 71 let rumor = ChatRumor::from_command(&command, UnixSeconds::new(NOW).unwrap()).unwrap(); 72 let mut sealed = Vec::new(); 73 for recipient in rumor.recipients() { 74 let receiver = if recipient == &rumor.author() { 75 &author 76 } else { 77 &peer 78 }; 79 let encrypted = nip44::encrypt( 80 author.secret_key(), 81 &receiver.public_key(), 82 rumor.json(), 83 nip44::Version::V2, 84 ) 85 .unwrap(); 86 assert_eq!(encrypted.len(), rumor.budget().rumor_ciphertext_bytes()); 87 let seal = ChatSeal::new( 88 &rumor, 89 *recipient, 90 UnixSeconds::new(NOW - 10).unwrap(), 91 &encrypted, 92 ) 93 .unwrap(); 94 let intent = SigningIntent::from_chat_seal( 95 command.context().operation_id(), 96 command.digest(), 97 &seal, 98 call(rumor.author()), 99 ) 100 .unwrap(); 101 assert_eq!(intent.role(), ArtifactRole::BusinessSeal); 102 assert_eq!(intent.recipient(), Some(*recipient)); 103 assert_eq!(intent.author(), rumor.author()); 104 assert_eq!( 105 intent.binding().command_digest(), 106 command.digest().as_bytes() 107 ); 108 intent 109 .check_before_effect(rumor.author(), NOW * 1000) 110 .unwrap(); 111 let event = intent.with_event(|fields, event_id| { 112 nostr::UnsignedEvent { 113 id: Some(nostr::EventId::from_slice(event_id.as_bytes()).unwrap()), 114 pubkey: author.public_key(), 115 created_at: nostr::Timestamp::from_secs(fields.created_at), 116 kind: nostr::Kind::Custom(fields.kind as u16), 117 tags: nostr::Tags::new(), 118 content: fields.content.into(), 119 } 120 .sign_with_keys(&author) 121 .unwrap() 122 }); 123 event.verify().unwrap(); 124 let raw = event.as_json(); 125 assert_eq!(raw.len(), intent.signed_json_bytes()); 126 assert!(raw.len() <= rumor.budget().signed_seal_max_bytes()); 127 assert_eq!(event.id.as_bytes(), &seal.event_id()); 128 assert!(event.tags.is_empty()); 129 assert_eq!( 130 nip44::decrypt(receiver.secret_key(), &author.public_key(), &event.content).unwrap(), 131 rumor.json() 132 ); 133 let captured = SigningOutcome::capture(&intent, raw, None); 134 match captured.kind() { 135 OutcomeKind::Obtained(artifact) => { 136 assert_eq!(artifact.event_id().as_bytes(), &seal.event_id()); 137 assert_eq!(artifact.observed_at_unix_ms(), None); 138 sealed.push(artifact.event_id()); 139 } 140 _ => panic!("real exact seal must verify"), 141 } 142 } 143 assert_eq!(sealed.len(), 2); 144 assert_ne!(sealed[0], sealed[1]); 145 } 146 147 #[test] 148 fn known_seal_conversion_refuses_substituted_signer_identity() { 149 let command = command(); 150 let author = keys(1); 151 let peer = keys(2); 152 let rumor = ChatRumor::from_command(&command, UnixSeconds::new(NOW).unwrap()).unwrap(); 153 let encrypted = nip44::encrypt( 154 author.secret_key(), 155 &peer.public_key(), 156 rumor.json(), 157 nip44::Version::V2, 158 ) 159 .unwrap(); 160 let seal = ChatSeal::new( 161 &rumor, 162 command.peer(), 163 UnixSeconds::new(NOW - 10).unwrap(), 164 &encrypted, 165 ) 166 .unwrap(); 167 assert!(matches!( 168 SigningIntent::from_chat_seal( 169 command.context().operation_id(), 170 command.digest(), 171 &seal, 172 call(command.peer()) 173 ), 174 Err(ProviderError::InvalidInput) 175 )); 176 assert_eq!(seal.message_id(), rumor.message_id()); 177 for (operation, digest) in [ 178 (OperationId::from_bytes([99; 32]), command.digest()), 179 ( 180 command.context().operation_id(), 181 radroots_domain::CommandDigest::from_bytes([99; 32]), 182 ), 183 ] { 184 assert!(matches!( 185 SigningIntent::from_chat_seal(operation, digest, &seal, call(rumor.author())), 186 Err(ProviderError::InvalidInput) 187 )); 188 } 189 }