binding_fixture.rs (7580B)
1 //! Synthetic host assembly only; all JS command entry points are production. 2 use super::*; 3 use radroots_browser::bindings::{BrowserClient, Workspace}; 4 5 #[derive(Clone, Copy)] 6 #[wasm_bindgen] 7 pub enum FixtureRejection { 8 Malformed, 9 Oversized, 10 Foreign, 11 } 12 13 // Controlled callback responses through the production scoped issuer. 14 struct RejectedSigner { 15 local: LocalSigner, 16 mode: FixtureRejection, 17 calls: Rc<Cell<u32>>, 18 } 19 impl radroots_signing::Signer for RejectedSigner { 20 fn status( 21 &self, 22 ) -> BoxFuture<'_, Result<radroots_signing::SignerStatus, radroots_signing::Error>> { 23 radroots_signing::Signer::status(&self.local) 24 } 25 fn sign( 26 &self, 27 _: radroots_signing::SignRequest, 28 ) -> BoxFuture<'_, Result<radroots_signing::SignReceipt, radroots_signing::Error>> { 29 panic!("rejection fixture must not invoke legacy signing") 30 } 31 } 32 impl radroots_signing::provider::ParticipantProvider for RejectedSigner { 33 fn provider_state( 34 &self, 35 ) -> BoxFuture<'_, Result<radroots_signing::provider::ProviderState, ProviderError>> { 36 radroots_signing::provider::ParticipantProvider::provider_state(&self.local) 37 } 38 fn sign_exact( 39 &self, 40 intent: radroots_signing::issuance::SigningIntent, 41 ) -> BoxFuture<'_, radroots_signing::issuance::SigningOutcome> { 42 use radroots_signing::issuance::{ArtifactRole, IntentBinding, SigningOutcome}; 43 self.calls.set(self.calls.get() + 1); 44 Box::pin(async move { 45 match self.mode { 46 FixtureRejection::Malformed => SigningOutcome::capture( 47 &intent, 48 "synthetic private rejection canary".into(), 49 None, 50 ), 51 FixtureRejection::Oversized => { 52 SigningOutcome::capture(&intent, "X".repeat(65537), None) 53 } 54 FixtureRejection::Foreign => SigningOutcome::not_dispatched( 55 IntentBinding::from_components( 56 OperationId::from_bytes([0xf1; 32]), 57 [0xf2; 32], 58 [0xf3; 32], 59 ArtifactRole::PublicEvent, 60 None, 61 core::num::NonZeroU64::new(1).unwrap(), 62 ) 63 .unwrap(), 64 ProviderError::Cancelled, 65 ), 66 } 67 }) 68 } 69 fn encrypt_private( 70 &self, 71 call: radroots_signing::provider::ProviderCall, 72 recipient: PublicKey, 73 text: radroots_signing::provider::PrivateText, 74 ) -> BoxFuture<'_, Result<radroots_signing::provider::PrivateCiphertext, ProviderError>> { 75 radroots_signing::provider::ParticipantProvider::encrypt_private( 76 &self.local, 77 call, 78 recipient, 79 text, 80 ) 81 } 82 fn decrypt_private( 83 &self, 84 call: radroots_signing::provider::ProviderCall, 85 sender: PublicKey, 86 text: radroots_signing::provider::PrivateCiphertext, 87 ) -> BoxFuture<'_, Result<radroots_signing::provider::PrivateText, ProviderError>> { 88 radroots_signing::provider::ParticipantProvider::decrypt_private( 89 &self.local, 90 call, 91 sender, 92 text, 93 ) 94 } 95 } 96 97 #[wasm_bindgen] 98 pub struct BindingFixture { 99 existing: Rc<Existing>, 100 base: String, 101 client: Client, 102 scope: WorkspaceSession, 103 callbacks: Rc<Callbacks>, 104 rejection: Option<FixtureRejection>, 105 signing_calls: Rc<Cell<u32>>, 106 } 107 #[wasm_bindgen] 108 impl BindingFixture { 109 pub async fn create( 110 base: String, 111 label: String, 112 seller: bool, 113 ) -> Result<BindingFixture, JsValue> { 114 let existing = Rc::new(Existing::create_scope(&label, seller).await?); 115 Self::open(existing, base, None).await 116 } 117 pub async fn create_rejected( 118 base: String, 119 label: String, 120 mode: FixtureRejection, 121 ) -> Result<BindingFixture, JsValue> { 122 let existing = Rc::new(Existing::create_scope(&label, true).await?); 123 Self::open(existing, base, Some(mode)).await 124 } 125 pub fn signing_calls(&self) -> u32 { 126 self.signing_calls.get() 127 } 128 pub fn client(&self) -> BrowserClient { 129 BrowserClient::from_browser(self.client.clone()) 130 } 131 pub fn workspace(&self) -> Workspace { 132 Workspace::from_session(self.scope.clone()) 133 } 134 pub fn callbacks_closed(&self) -> u32 { 135 self.callbacks.closed.get() 136 } 137 pub async fn reopen(&self) -> Result<BindingFixture, JsValue> { 138 require( 139 self.client.state() == SessionState::Closed, 140 "fixture reopen requires explicit actual close", 141 )?; 142 Self::open(self.existing.clone(), self.base.clone(), self.rejection).await 143 } 144 } 145 impl BindingFixture { 146 async fn open( 147 existing: Rc<Existing>, 148 base: String, 149 rejection: Option<FixtureRejection>, 150 ) -> Result<Self, JsValue> { 151 let (client, scope) = existing.open(&base).await?; 152 let signing_calls = Rc::new(Cell::new(0)); 153 let callbacks = if let Some(mode) = rejection { 154 let callbacks = Rc::new(Callbacks { 155 quiesced: Cell::new(0), 156 closed: Cell::new(0), 157 }); 158 client 159 .attach_signer(SignerHandle::new( 160 Rc::new(RejectedSigner { 161 local: local_signer(), 162 mode, 163 calls: signing_calls.clone(), 164 }), 165 callbacks.clone(), 166 )) 167 .await 168 .map_err(fail)?; 169 callbacks 170 } else { 171 signer(&client).await? 172 }; 173 Ok(Self { 174 existing, 175 base, 176 client, 177 scope, 178 callbacks, 179 rejection, 180 signing_calls, 181 }) 182 } 183 } 184 185 /// Independent nested authentication of the original private JS command text. 186 #[wasm_bindgen] 187 pub async fn verify_binding_inquiry( 188 raw_events: String, 189 expected: String, 190 text: String, 191 ) -> Result<(), JsValue> { 192 let events: Vec<String> = serde_json::from_str(&raw_events).map_err(fail)?; 193 require(events.len() == 2, "one original self and peer envelope")?; 194 let mut recipients = Vec::new(); 195 for raw in events { 196 let event = nostr::Event::from_json(&raw).map_err(fail)?; 197 event.verify().map_err(fail)?; 198 let recipient = event 199 .tags 200 .iter() 201 .find(|tag| tag.as_slice()[0] == "p") 202 .ok_or_else(|| JsValue::from_str("missing private recipient"))? 203 .as_slice()[1] 204 .clone(); 205 let n = if recipient == key(1).public_key().to_hex() { 206 1 207 } else { 208 2 209 }; 210 require( 211 recipient == key(n).public_key().to_hex(), 212 "unexpected private recipient", 213 )?; 214 let message = nostr::nips::nip59::extract_rumor(&key(n), &event) 215 .await 216 .map_err(fail)?; 217 require( 218 message.sender == key(1).public_key() 219 && message.rumor.content == text 220 && message.rumor.id.map(|v| v.to_hex()).as_deref() == Some(&expected), 221 "private JS text and original rumor identity", 222 )?; 223 recipients.push(n); 224 } 225 recipients.sort(); 226 require(recipients == [1, 2], "independent self and peer originals") 227 }