octra-sqlite 0.5.2

Real SQLite inside an Octra Circle, with a Rust CLI and client library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#[cfg(feature = "http")]
use super::transport::HttpTransport;
use super::{
    error::{Error, ErrorKind, Result},
    results::{AuthInfo, ExecuteResult, ProgramInfo, QueryResult, SubmittedTransaction},
    rpc::{auth_info_with, program_info_with, query_typed_with, wait_for_receipt_with},
    safety::Operation,
    session::{ClientOptions, Session, build_session},
    transport::Transport,
    write::{
        PreparedWrite, SignedWrite, ensure_submit_mode, prepare_write_with, sign_write,
        submit_signed_write_with,
    },
};
#[cfg(feature = "http")]
use std::path::PathBuf;
use std::sync::Arc;

#[cfg(feature = "http")]
/// Configured client used to open Circle-backed SQLite databases.
#[derive(Clone)]
pub struct Client<T = HttpTransport> {
    options: ClientOptions,
    transport: Arc<T>,
}

#[cfg(not(feature = "http"))]
/// Configured client used to open Circle-backed SQLite databases.
#[derive(Clone)]
pub struct Client<T> {
    options: ClientOptions,
    transport: Arc<T>,
}

#[cfg(feature = "http")]
impl Default for Client<HttpTransport> {
    fn default() -> Self {
        Self {
            options: ClientOptions::default(),
            transport: Arc::new(HttpTransport::default()),
        }
    }
}

#[cfg(feature = "http")]
impl Client<HttpTransport> {
    /// Load the default local config and construct a client with the HTTP transport.
    ///
    /// This is fallible because it reads the configured octra-sqlite config
    /// file. Use [`Client::with_options`] when construction should be purely
    /// in-memory.
    pub fn from_default_config() -> Result<Self> {
        let config = super::config::load_config()?;
        let options = ClientOptions {
            target: config.default_database.clone(),
            wallet: config.wallet.as_ref().map(PathBuf::from),
            ..ClientOptions::default()
        };
        Ok(Self::with_options(options))
    }

    /// Construct a client from explicit options using the default HTTP transport.
    pub fn with_options(options: ClientOptions) -> Self {
        Self {
            options,
            transport: Arc::new(HttpTransport::default()),
        }
    }
}

impl<T: Transport> Client<T> {
    /// Construct a client from explicit options and a custom transport.
    pub fn with_transport(options: ClientOptions, transport: T) -> Self {
        Self {
            options,
            transport: Arc::new(transport),
        }
    }

    /// Open a database by saved name, Circle ID, or `oct://` URI.
    pub fn database(&self, target: impl Into<String>) -> Result<Database<T>> {
        let mut options = self.options.clone();
        options.target = Some(target.into());
        Database::open_with_shared_transport(options, Arc::clone(&self.transport))
    }
}

#[cfg(feature = "http")]
/// Opened Circle-backed SQLite database.
#[derive(Clone)]
pub struct Database<T = HttpTransport> {
    session: Session,
    transport: Arc<T>,
}

#[cfg(not(feature = "http"))]
/// Opened Circle-backed SQLite database.
#[derive(Clone)]
pub struct Database<T> {
    session: Session,
    transport: Arc<T>,
}

#[cfg(feature = "http")]
impl Database<HttpTransport> {
    /// Open a database directly with the default HTTP transport.
    pub fn open(options: ClientOptions) -> Result<Self> {
        Self::open_with_transport(options, HttpTransport::default())
    }
}

impl<T: Transport> Database<T> {
    /// Open a database directly with a custom transport.
    pub fn open_with_transport(options: ClientOptions, transport: T) -> Result<Self> {
        Self::open_with_shared_transport(options, Arc::new(transport))
    }

    fn open_with_shared_transport(options: ClientOptions, transport: Arc<T>) -> Result<Self> {
        Ok(Self {
            session: build_session(&options)?,
            transport,
        })
    }

    /// Run read-only SQL and return typed rows.
    pub fn query(&self, sql: &str) -> Result<QueryResult> {
        QueryResult::from_value(query_typed_with(
            self.transport.as_ref(),
            &self.session,
            sql,
        )?)
    }

    /// Submit a write and wait for its receipt.
    pub fn execute(&self, sql: &str) -> Result<ExecuteResult> {
        let prepared = self.prepare_write(sql)?;
        let signed = self.sign_write(&prepared)?;
        self.submit_signed_write_and_wait(signed)
    }

    /// Submit a write without waiting for confirmation.
    pub fn execute_no_wait(&self, sql: &str) -> Result<SubmittedTransaction> {
        let prepared = self.prepare_write_no_wait(sql)?;
        let signed = self.sign_write(&prepared)?;
        self.submit_signed_write(signed)
    }

    /// Prepare a write for later signing and no-wait submission.
    pub fn prepare_write_no_wait(&self, sql: &str) -> Result<PreparedWrite> {
        self.prepare_write_for(sql, Operation::ExecuteNoWait)
    }

    /// Prepare a write for later signing and confirmed execution.
    pub fn prepare_write(&self, sql: &str) -> Result<PreparedWrite> {
        self.prepare_write_for(sql, Operation::Execute)
    }

    fn prepare_write_for(&self, sql: &str, operation: Operation) -> Result<PreparedWrite> {
        prepare_write_with(self.transport.as_ref(), &self.session, sql, operation)
    }

    /// Sign a prepared write with the database session wallet.
    pub fn sign_write(&self, prepared: &PreparedWrite) -> Result<SignedWrite> {
        sign_write(&self.session, prepared)
    }

    /// Submit a signed write without waiting for confirmation.
    pub fn submit_signed_write(&self, signed: SignedWrite) -> Result<SubmittedTransaction> {
        ensure_submit_mode(&signed, Operation::ExecuteNoWait)?;
        SubmittedTransaction::from_value(submit_signed_write_with(
            self.transport.as_ref(),
            &self.session,
            signed,
            true,
        )?)
    }

    /// Submit a signed write and wait for its receipt.
    pub fn submit_signed_write_and_wait(&self, signed: SignedWrite) -> Result<ExecuteResult> {
        ensure_submit_mode(&signed, Operation::Execute)?;
        ExecuteResult::from_value(submit_signed_write_with(
            self.transport.as_ref(),
            &self.session,
            signed,
            false,
        )?)
    }

    /// Wait for a submitted transaction receipt.
    pub fn wait(&self, submitted: &SubmittedTransaction) -> Result<ExecuteResult> {
        let tx_hash = submitted.tx_hash.as_deref().ok_or_else(|| {
            Error::with_kind(
                ErrorKind::Config,
                "submitted transaction is missing tx_hash",
            )
        })?;
        let receipt = wait_for_receipt_with(self.transport.as_ref(), &self.session, tx_hash)?;
        let mut value = serde_json::Map::new();
        if let Some(circle) = &submitted.circle {
            value.insert(
                "circle".to_string(),
                serde_json::Value::String(circle.clone()),
            );
        }
        if let Some(wallet) = &submitted.wallet {
            value.insert(
                "wallet".to_string(),
                serde_json::Value::String(wallet.clone()),
            );
        }
        value.insert(
            "tx_hash".to_string(),
            serde_json::Value::String(tx_hash.to_string()),
        );
        value.insert("result".to_string(), submitted.result.clone());
        value.insert("receipt".to_string(), receipt);
        ExecuteResult::from_value(serde_json::Value::Object(value))
    }

    /// Read owner-write authorization metadata.
    pub fn auth_info(&self) -> Result<AuthInfo> {
        auth_info_with(self.transport.as_ref(), &self.session)
    }

    /// Read deployed Circle program metadata.
    pub fn program_info(&self) -> Result<ProgramInfo> {
        ProgramInfo::from_value(program_info_with(self.transport.as_ref(), &self.session)?)
    }
}

#[cfg(test)]
mod tests {
    use super::super::write::sign_and_submit_tx_with;
    use super::*;
    use crate::client::{Error, ErrorKind};
    use crate::protocol::tx::Tx;
    use serde_json::{Value, json};
    use std::sync::{Arc, Mutex};

    #[derive(Clone)]
    struct MockTransport {
        calls: Arc<Mutex<Vec<String>>>,
        receipt: Arc<Mutex<Value>>,
        public_info: bool,
    }

    impl Default for MockTransport {
        fn default() -> Self {
            Self {
                calls: Arc::new(Mutex::new(Vec::new())),
                receipt: Arc::new(Mutex::new(json!({
                    "success": true,
                    "error": null,
                    "method": "exec",
                }))),
                public_info: false,
            }
        }
    }

    impl MockTransport {
        fn with_receipt(receipt: Value) -> Self {
            Self {
                receipt: Arc::new(Mutex::new(receipt)),
                ..Self::default()
            }
        }

        fn public_read_circle() -> Self {
            Self {
                public_info: true,
                ..Self::default()
            }
        }
    }

    impl Transport for MockTransport {
        fn call(&self, _rpc: &str, method: &str, params: Value) -> Result<Value> {
            self.calls.lock().unwrap().push(method.to_string());
            match method {
                "octra_circleInfo" => {
                    if self.public_info {
                        Ok(json!({
                            "privacy_class": "public",
                            "browser_mode": "gateway_allowed",
                            "resource_mode": "public_resources",
                        }))
                    } else {
                        Ok(json!({
                            "privacy_class": "sealed",
                            "browser_mode": "native_sealed",
                            "resource_mode": "sealed_read",
                        }))
                    }
                }
                "octra_circleViewAuth" => {
                    let circle_method = params
                        .as_array()
                        .and_then(|params| params.get(1))
                        .and_then(Value::as_str)
                        .unwrap_or_default();
                    if circle_method == "auth_info" {
                        return Ok(json!({
                            "configured": true,
                            "db_id": "1111111111111111111111111111111111111111111111111111111111111111",
                        }));
                    }
                    let vector: Value =
                        serde_json::from_str(include_str!("../../tests/fixtures/osr1/basic.json"))
                            .unwrap();
                    Ok(Value::String(format!(
                        "OSR1:{}",
                        vector["payload_b64"].as_str().unwrap()
                    )))
                }
                "octra_circleView" => {
                    let vector: Value =
                        serde_json::from_str(include_str!("../../tests/fixtures/osr1/basic.json"))
                            .unwrap();
                    Ok(Value::String(format!(
                        "OSR1:{}",
                        vector["payload_b64"].as_str().unwrap()
                    )))
                }
                "octra_balance" => Ok(json!({ "pending_nonce": 41 })),
                "octra_submit" => Ok(json!({ "tx_hash": "abc123" })),
                "contract_receipt" => Ok(self.receipt.lock().unwrap().clone()),
                _ => Err(Error::with_kind(
                    ErrorKind::Other,
                    format!("unexpected method {method}"),
                )),
            }
        }
    }

    struct ContractErrorTransport;

    impl Transport for ContractErrorTransport {
        fn call(&self, _rpc: &str, method: &str, _params: Value) -> Result<Value> {
            match method {
                "octra_circleViewAuth" => Ok(Value::String(
                    r#"{"ok":false,"error":"sqlite_prepare_failed","detail":"no such table: companion"}"#.to_string(),
                )),
                _ => Err(Error::with_kind(
                    ErrorKind::Other,
                    format!("unexpected method {method}"),
                )),
            }
        }
    }

    fn test_options() -> ClientOptions {
        ClientOptions {
            target: Some("oct://devnet/octABC?read_mode=sealed".to_string()),
            rpc: Some("mock://rpc".to_string()),
            caller: Some("octCaller".to_string()),
            private_key: Some(
                "0101010101010101010101010101010101010101010101010101010101010101".to_string(),
            ),
            ..ClientOptions::default()
        }
    }

    fn test_options_for(target: &str) -> ClientOptions {
        ClientOptions {
            target: Some(target.to_string()),
            ..test_options()
        }
    }

    #[test]
    fn database_query_uses_transport_and_returns_typed_rows() {
        let transport = MockTransport::default();
        let calls = transport.calls.clone();
        let db = Database::open_with_transport(test_options(), transport).unwrap();
        let result = db.query("select * from demo").unwrap();
        assert_eq!(result.columns[0], "nil");
        assert_eq!(result.row_count, 1);
        assert_eq!(calls.lock().unwrap().as_slice(), ["octra_circleViewAuth"]);
    }

    #[test]
    fn public_database_query_uses_unsigned_circle_view() {
        let transport = MockTransport::default();
        let calls = transport.calls.clone();
        let db = Database::open_with_transport(
            ClientOptions {
                target: Some("oct://devnet/octABC?read_mode=public".to_string()),
                rpc: Some("mock://rpc".to_string()),
                ..ClientOptions::default()
            },
            transport,
        )
        .unwrap();
        let result = db.query("select * from demo").unwrap();
        assert_eq!(result.row_count, 1);
        assert_eq!(calls.lock().unwrap().as_slice(), ["octra_circleView"]);
    }

    #[test]
    fn auto_database_query_detects_public_circle_without_wallet() {
        let transport = MockTransport::public_read_circle();
        let calls = transport.calls.clone();
        let db = Database::open_with_transport(
            ClientOptions {
                target: Some("oct://devnet/octABC".to_string()),
                rpc: Some("mock://rpc".to_string()),
                ..ClientOptions::default()
            },
            transport,
        )
        .unwrap();
        let result = db.query("select * from demo").unwrap();
        assert_eq!(result.row_count, 1);
        assert_eq!(
            calls.lock().unwrap().as_slice(),
            ["octra_circleInfo", "octra_circleView"]
        );
    }

    #[test]
    fn database_query_surfaces_contract_sql_errors() {
        let db = Database::open_with_transport(test_options(), ContractErrorTransport).unwrap();
        let error = db.query("select * from companion;").unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Rpc);
        assert!(error.to_string().contains("sqlite_prepare_failed"));
        assert!(error.to_string().contains("no such table: companion"));
    }

    #[test]
    fn database_execute_errors_on_failed_receipt() {
        let transport = MockTransport::with_receipt(json!({
            "success": false,
            "error": "near \"bad\": syntax error",
            "method": "exec",
        }));
        let db = Database::open_with_transport(test_options(), transport).unwrap();
        let error = db.execute("bad sql").unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Receipt);
        assert!(error.to_string().contains("syntax error"));
        assert!(error.to_string().contains("tx_hash: abc123"));
    }

    #[test]
    fn signed_write_submit_mode_must_match_prepare_mode() {
        let transport = MockTransport::default();
        let db = Database::open_with_transport(test_options(), transport).unwrap();
        let prepared = db.prepare_write("create table demo(id integer);").unwrap();
        let signed = db.sign_write(&prepared).unwrap();
        let error = db.submit_signed_write(signed).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Config);
    }

    #[test]
    fn generic_tx_submit_allows_deploy_destination() {
        let transport = MockTransport::default();
        let calls = transport.calls.clone();
        let session = build_session(&test_options()).unwrap();
        let tx = Tx {
            from: session.caller().to_string(),
            to_: "octNewCircle".to_string(),
            amount: "0".to_string(),
            nonce: 42,
            ou: "1000".to_string(),
            timestamp: 1000.0,
            op_type: "deploy_circle".to_string(),
            encrypted_data: String::new(),
            message: "{}".to_string(),
            signature: String::new(),
            public_key: session.public_key_b64().unwrap().to_string(),
        };
        let result = sign_and_submit_tx_with(&transport, &session, tx, true).unwrap();
        assert_eq!(result["circle"], "octNewCircle");
        assert_eq!(result["tx_hash"], "abc123");
        assert_eq!(calls.lock().unwrap().as_slice(), ["octra_submit"]);
    }

    #[test]
    fn prepared_write_must_match_signing_database() {
        let db_a = Database::open_with_transport(
            test_options_for("oct://devnet/octABC"),
            MockTransport::default(),
        )
        .unwrap();
        let db_b = Database::open_with_transport(
            test_options_for("oct://devnet/octDEF"),
            MockTransport::default(),
        )
        .unwrap();
        let prepared = db_a
            .prepare_write("create table demo(id integer);")
            .unwrap();
        let error = db_b.sign_write(&prepared).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Authorization);
    }
}