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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use super::config::{Config, load_config};
use super::error::{Error, ErrorKind, Result};
use super::wallet::{
    discover_wallet_path, load_wallet, normalized_public_key_b64, signing_key_from_text,
};
use crate::protocol::target::{DatabaseTarget, ReadMode, parse_database_target};
use base64::{Engine as _, engine::general_purpose};
use ed25519_dalek::{Signer, SigningKey};
use std::collections::BTreeSet;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use zeroize::Zeroize;

const PUBLIC_VIEW_CALLER: &str = "oct11111111111111111111111111111111111111111111";

/// Explicit options for opening a client or database.
///
/// Prefer `wallet` for normal use. Inline `private_key` and `public_key`
/// fields exist for controlled services and tests that already manage secret
/// material outside the local wallet file.
///
/// `ClientOptions` intentionally does not implement `Debug` because it may
/// contain private key material.
///
/// ```compile_fail
/// let _ = format!("{:?}", octra_sqlite::ClientOptions::default());
/// ```
#[derive(Clone, Default)]
pub struct ClientOptions {
    /// Saved database name, raw Circle ID, or `oct://` database URI.
    pub target: Option<String>,
    /// Local wallet JSON path.
    pub wallet: Option<PathBuf>,
    /// Octra RPC URL override.
    pub rpc: Option<String>,
    /// Caller address override for read/session construction.
    pub caller: Option<String>,
    /// Inline private key material. Prefer `wallet` outside controlled tests.
    pub private_key: Option<String>,
    /// Optional public key material used to verify the private key.
    pub public_key: Option<String>,
}

/// Resolved Octra session used by the raw client layer.
#[derive(Clone)]
pub struct Session {
    target: DatabaseTarget,
    wallet_path: Option<PathBuf>,
    wallet_load_error: Option<String>,
    rpc: String,
    rpc_override: bool,
    caller: String,
    signer: Option<Arc<LocalSigner>>,
}

struct LocalSigner {
    key: SigningKey,
    public_key_b64: String,
}

impl LocalSigner {
    fn from_private_key_text(private_key: &str, public_key: Option<String>) -> Result<Self> {
        let key = signing_key_from_text(private_key)?;
        let derived_public_key = key.verifying_key().to_bytes();
        let public_key_b64 = match public_key {
            Some(text) => normalized_public_key_b64(&text, &derived_public_key)?,
            None => general_purpose::STANDARD.encode(derived_public_key),
        };
        Ok(Self {
            key,
            public_key_b64,
        })
    }

    fn public_key_b64(&self) -> &str {
        &self.public_key_b64
    }

    fn intent_public_key(&self) -> [u8; 32] {
        self.key.verifying_key().to_bytes()
    }

    fn sign_text_b64(&self, message: &str) -> String {
        general_purpose::STANDARD.encode(self.key.sign(message.as_bytes()).to_bytes())
    }

    fn sign_bytes_hex(&self, message: &[u8]) -> String {
        hex::encode(self.key.sign(message).to_bytes())
    }
}

impl Session {
    pub fn target(&self) -> &DatabaseTarget {
        &self.target
    }

    pub fn wallet_path(&self) -> Option<&Path> {
        self.wallet_path.as_deref()
    }

    pub fn wallet_load_error(&self) -> Option<&str> {
        self.wallet_load_error.as_deref()
    }

    pub fn rpc(&self) -> &str {
        &self.rpc
    }

    pub fn caller(&self) -> &str {
        &self.caller
    }

    pub fn public_key_b64(&self) -> Result<&str> {
        Ok(self.signer()?.public_key_b64())
    }

    pub fn with_database_target(&self, target: DatabaseTarget) -> Session {
        Session {
            target,
            wallet_path: self.wallet_path.clone(),
            wallet_load_error: self.wallet_load_error.clone(),
            rpc: self.rpc.clone(),
            rpc_override: self.rpc_override,
            caller: self.caller.clone(),
            signer: self.signer.clone(),
        }
    }

    pub fn open_database(&self, target: impl Into<String>) -> Result<Session> {
        let config = load_config().unwrap_or_default();
        let mut target = resolve_database_target(&target.into(), &config)?;
        if target.rpc.is_empty() {
            target.rpc = self.rpc.clone();
        }
        Ok(Session {
            rpc: open_database_rpc(&self.rpc, self.rpc_override, Some(target.rpc.clone())),
            target,
            wallet_path: self.wallet_path.clone(),
            wallet_load_error: self.wallet_load_error.clone(),
            rpc_override: self.rpc_override,
            caller: self.caller.clone(),
            signer: self.signer.clone(),
        })
    }

    pub fn intent_public_key(&self) -> Result<[u8; 32]> {
        Ok(self.signer()?.intent_public_key())
    }

    pub(crate) fn sign_view_auth_b64(&self, message: &str) -> Result<String> {
        Ok(self.signer()?.sign_text_b64(message))
    }

    pub(crate) fn sign_program_info_b64(&self, message: &str) -> Result<String> {
        Ok(self.signer()?.sign_text_b64(message))
    }

    pub(crate) fn sign_transaction_b64(&self, message: &str) -> Result<String> {
        Ok(self.signer()?.sign_text_b64(message))
    }

    pub(crate) fn sign_owner_write_hex(&self, message: &[u8]) -> Result<String> {
        Ok(self.signer()?.sign_bytes_hex(message))
    }

    fn signer(&self) -> Result<&LocalSigner> {
        if let Some(error) = &self.wallet_load_error {
            return Err(Error::with_kind(
                ErrorKind::Wallet,
                format!(
                    "wallet failed to load; public reads can continue without it, but signed operations require a valid wallet: {error}"
                ),
            ));
        }
        self.signer.as_deref().ok_or_else(|| {
            Error::with_kind(
                ErrorKind::Wallet,
                "wallet private key is required for signed Octra operations",
            )
        })
    }
}

pub fn build_session(options: &ClientOptions) -> Result<Session> {
    let config = load_config().unwrap_or_default();
    let target_value = options
        .target
        .clone()
        .or_else(|| config.default_database.clone())
        .or_else(|| env::var("OCTRA_SQLITE_DATABASE").ok())
        .or_else(|| env::var("OCTRA_SQLITE_TARGET").ok())
        .or_else(|| env::var("OCTRA_CIRCLE_ID").ok())
        .ok_or_else(|| {
            Error::with_kind(
                ErrorKind::Config,
                "no database supplied and no default database is configured",
            )
        })?;
    let target = resolve_database_target(&target_value, &config)?;
    build_session_for_target(options, &config, target)
}

pub fn build_control_session(options: &ClientOptions, network: &str) -> Result<Session> {
    let config = load_config().unwrap_or_default();
    let target = DatabaseTarget {
        raw: format!("oct://{network}"),
        network: network.to_string(),
        circle: String::new(),
        rpc: config.rpc_for_network(network).unwrap_or_default(),
        read_mode: ReadMode::Sealed,
    };
    build_session_for_target(options, &config, target)
}

pub fn resolve_wallet_path(options: &ClientOptions, config: &Config) -> Option<PathBuf> {
    options
        .wallet
        .clone()
        .or_else(|| env::var("OCTRA_WALLET").ok().map(PathBuf::from))
        .or_else(|| config.wallet.as_ref().map(PathBuf::from))
        .or_else(discover_wallet_path)
}

pub fn resolve_database_target(value: &str, config: &Config) -> Result<DatabaseTarget> {
    let mut seen = BTreeSet::new();
    let mut chain = Vec::new();
    resolve_database_target_inner(value, config, &mut seen, &mut chain)
}

fn resolve_database_target_inner(
    value: &str,
    config: &Config,
    seen: &mut BTreeSet<String>,
    chain: &mut Vec<String>,
) -> Result<DatabaseTarget> {
    if let Some(database) = config.databases.get(value) {
        if !seen.insert(value.to_string()) {
            chain.push(value.to_string());
            return Err(Error::with_kind(
                ErrorKind::Config,
                format!("cyclic database alias: {}", chain.join(" -> ")),
            ));
        }
        chain.push(value.to_string());
        let mut target = resolve_database_target_inner(database, config, seen, chain)?;
        chain.pop();
        seen.remove(value);
        apply_target_metadata(value, config, &mut target);
        return Ok(target);
    }
    let mut target = parse_database_target(value, config.network.as_deref(), None)?;
    if target.rpc.is_empty() {
        target.rpc = config.rpc_for_network(&target.network).unwrap_or_default();
    }
    apply_target_metadata(value, config, &mut target);
    Ok(target)
}

fn apply_target_metadata(requested: &str, config: &Config, target: &mut DatabaseTarget) {
    if let Some(metadata) = config.metadata_for_target(requested, target) {
        target.read_mode = metadata.read_mode;
    }
}

fn build_session_for_target(
    options: &ClientOptions,
    config: &Config,
    mut target: DatabaseTarget,
) -> Result<Session> {
    let explicit_rpc = first_string([options.rpc.clone(), env::var("OCTRA_RPC_URL").ok()]);
    if let Some(rpc) = explicit_rpc.clone() {
        target.rpc = rpc;
    }
    let rpc_override = explicit_rpc.is_some();
    let wallet_path = resolve_wallet_path(options, config);
    let mut wallet_load_error = None;
    let wallet = match load_wallet(wallet_path.as_deref()) {
        Ok(wallet) => wallet,
        Err(error) if target.read_mode.allows_unsigned_read() => {
            wallet_load_error = Some(error.to_string());
            Default::default()
        }
        Err(error) => return Err(error),
    };
    let wallet_rpc = wallet.rpc;
    let rpc = choose_session_rpc(
        explicit_rpc,
        Some(target.rpc.clone()),
        config.rpc.clone(),
        wallet_rpc,
    )
    .ok_or_else(|| {
        Error::with_kind(
            ErrorKind::Config,
            "RPC is required; run octra-sqlite setup, pass --rpc, or set OCTRA_RPC_URL",
        )
    })?;
    let caller = first_string([
        options.caller.clone(),
        wallet.addr,
        wallet.address,
        env::var("OCTRA_CALLER").ok(),
    ])
    .unwrap_or_else(|| PUBLIC_VIEW_CALLER.to_string());
    let private_key = first_secret_string([
        options.private_key.clone(),
        wallet.priv_field,
        wallet.priv_,
        wallet.private_key,
        wallet.private_key_b64,
        env::var("OCTRA_PRIVATE_KEY_B64").ok(),
    ]);
    let supplied_public_key = first_string([
        options.public_key.clone(),
        wallet.pub_field,
        wallet.pub_,
        wallet.public_key,
        wallet.public_key_b64,
        env::var("OCTRA_PUBLIC_KEY_B64").ok(),
    ]);
    let signer = match private_key {
        Some(mut private_key) => {
            let signer = LocalSigner::from_private_key_text(&private_key, supplied_public_key);
            private_key.zeroize();
            Some(Arc::new(signer?))
        }
        None if target.read_mode.allows_unsigned_read() => None,
        None => {
            return Err(Error::with_kind(
                ErrorKind::Wallet,
                "wallet private key is required; pass --wallet or OCTRA_PRIVATE_KEY_B64",
            ));
        }
    };
    Ok(Session {
        target,
        wallet_path,
        wallet_load_error,
        rpc,
        rpc_override,
        caller,
        signer,
    })
}

fn first_string(values: impl IntoIterator<Item = Option<String>>) -> Option<String> {
    values
        .into_iter()
        .find_map(|value| value.filter(|v| !v.is_empty()))
}

fn first_secret_string(values: impl IntoIterator<Item = Option<String>>) -> Option<String> {
    let mut selected = None;
    for mut value in values.into_iter().flatten() {
        if value.is_empty() {
            value.zeroize();
            continue;
        }
        if selected.is_none() {
            selected = Some(value);
        } else {
            value.zeroize();
        }
    }
    selected
}

fn open_database_rpc(current_rpc: &str, rpc_override: bool, target_rpc: Option<String>) -> String {
    if rpc_override {
        return current_rpc.to_string();
    }
    first_string([target_rpc, Some(current_rpc.to_string())]).unwrap()
}

fn choose_session_rpc(
    explicit_rpc: Option<String>,
    target_rpc: Option<String>,
    config_rpc: Option<String>,
    wallet_rpc: Option<String>,
) -> Option<String> {
    first_string([explicit_rpc, target_rpc, config_rpc, wallet_rpc])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn target_network_rpc_wins_over_wallet_rpc() {
        assert_eq!(
            choose_session_rpc(
                None,
                Some("https://devnet.octrascan.io/rpc".to_string()),
                Some("https://config.example/rpc".to_string()),
                Some("http://wallet.example/rpc".to_string()),
            )
            .as_deref(),
            Some("https://devnet.octrascan.io/rpc")
        );
    }

    #[test]
    fn explicit_rpc_wins_over_target_network_rpc() {
        assert_eq!(
            choose_session_rpc(
                Some("https://override.example/rpc".to_string()),
                Some("https://devnet.octrascan.io/rpc".to_string()),
                Some("https://config.example/rpc".to_string()),
                Some("http://wallet.example/rpc".to_string()),
            )
            .as_deref(),
            Some("https://override.example/rpc")
        );
    }

    #[test]
    fn wallet_rpc_is_only_a_fallback() {
        assert_eq!(
            choose_session_rpc(
                None,
                Some(String::new()),
                None,
                Some("http://wallet.example/rpc".to_string()),
            )
            .as_deref(),
            Some("http://wallet.example/rpc")
        );
    }

    #[test]
    fn open_database_rpc_uses_target_network_unless_rpc_was_explicit() {
        assert_eq!(
            open_database_rpc(
                "https://devnet.octrascan.io/rpc",
                false,
                Some("https://octra.network/rpc".to_string()),
            ),
            "https://octra.network/rpc"
        );
        assert_eq!(
            open_database_rpc(
                "http://127.0.0.1:8080/rpc",
                true,
                Some("https://octra.network/rpc".to_string()),
            ),
            "http://127.0.0.1:8080/rpc"
        );
    }

    #[test]
    fn resolve_database_target_follows_aliases() {
        let mut config = Config {
            network: Some("devnet".to_string()),
            ..Config::default()
        };
        config.databases.insert("a".to_string(), "b".to_string());
        config
            .databases
            .insert("b".to_string(), "oct://devnet/octABC".to_string());
        let target = resolve_database_target("a", &config).unwrap();
        assert_eq!(target.circle, "octABC");
    }

    #[test]
    fn resolve_database_target_rejects_self_alias_cycle() {
        let mut config = Config::default();
        config.databases.insert("a".to_string(), "a".to_string());
        let error = resolve_database_target("a", &config).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Config);
        assert!(error.to_string().contains("a -> a"));
    }

    #[test]
    fn resolve_database_target_rejects_multi_alias_cycle() {
        let mut config = Config::default();
        config.databases.insert("a".to_string(), "b".to_string());
        config.databases.insert("b".to_string(), "a".to_string());
        let error = resolve_database_target("a", &config).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Config);
        assert!(error.to_string().contains("a -> b -> a"));
    }

    #[test]
    fn supplied_public_key_must_match_private_key() {
        let error = match build_session(&ClientOptions {
            target: Some("oct://devnet/octABC".to_string()),
            rpc: Some("mock://rpc".to_string()),
            caller: Some("octCaller".to_string()),
            private_key: Some(
                "0101010101010101010101010101010101010101010101010101010101010101".to_string(),
            ),
            public_key: Some(general_purpose::STANDARD.encode([2u8; 32])),
            ..ClientOptions::default()
        }) {
            Ok(_) => panic!("mismatched public key should fail"),
            Err(error) => error,
        };
        assert_eq!(error.kind(), ErrorKind::Wallet);
        assert!(
            error
                .to_string()
                .contains("wallet public key does not match private key")
        );
    }

    #[test]
    fn accepts_explicit_64_byte_keypair_form() {
        let seed = [3u8; 32];
        let key = SigningKey::from_bytes(&seed);
        let public_key = key.verifying_key().to_bytes();
        let mut keypair = Vec::from(seed);
        keypair.extend_from_slice(&public_key);
        let session = build_session(&ClientOptions {
            target: Some("oct://devnet/octABC".to_string()),
            rpc: Some("mock://rpc".to_string()),
            caller: Some("octCaller".to_string()),
            private_key: Some(hex::encode(keypair)),
            public_key: Some(general_purpose::STANDARD.encode(public_key)),
            ..ClientOptions::default()
        })
        .unwrap();
        assert_eq!(
            session.public_key_b64().unwrap(),
            general_purpose::STANDARD.encode(public_key)
        );
    }

    #[test]
    fn public_read_preserves_wallet_load_error_for_signed_operations() {
        let path = std::env::temp_dir().join(format!(
            "octra-sqlite-invalid-wallet-{}.json",
            std::process::id()
        ));
        std::fs::write(&path, "{").unwrap();
        let session = build_session(&ClientOptions {
            target: Some("oct://devnet/octABC?read_mode=public".to_string()),
            wallet: Some(path.clone()),
            rpc: Some("mock://rpc".to_string()),
            ..ClientOptions::default()
        })
        .unwrap();
        let _ = std::fs::remove_file(&path);
        assert!(
            session
                .wallet_load_error()
                .is_some_and(|error| error.contains("parsing wallet"))
        );
        let error = session.intent_public_key().unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Wallet);
        assert!(error.to_string().contains("wallet failed to load"));
        assert!(error.to_string().contains("parsing wallet"));
    }

    #[test]
    fn rejects_private_keys_with_ambiguous_length() {
        let error = signing_key_from_text("0102").unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Wallet);
        assert!(
            error
                .to_string()
                .contains("32-byte seed or 64-byte keypair")
        );
    }
}