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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Authentication, identity, and node verification.

use std::{fmt, io::Write, path::Path, str::FromStr};

use anyhow::Context;
use bip39::Mnemonic;
use lexe_common::{
    ExposeSecret,
    api::user::{NodePk as UnstableNodePk, UserPk as UnstableUserPk},
    root_seed::RootSeed as UnstableRootSeed,
};
use lexe_crypto::rng::SysRng;
use lexe_enclave::enclave::Measurement as UnstableMeasurement;
use lexe_node_client::credentials::{
    ClientCredentials as UnstableClientCredentials,
    CredentialsRef as UnstableCredentialsRef,
};
use serde::{Deserialize, Serialize};

use crate::{
    config::WalletEnv,
    util::{ByteArray, hex},
};

// --- Credentials --- //

/// Credentials used to authenticate with a Lexe user node.
#[derive(Debug)]
pub enum Credentials {
    /// Authenticate with a [`RootSeed`].
    RootSeed(RootSeed),
    /// Authenticate with delegated [`ClientCredentials`].
    ClientCredentials(ClientCredentials),
}

impl Credentials {
    /// Borrow as a [`CredentialsRef`].
    pub fn as_ref(&self) -> CredentialsRef<'_> {
        match self {
            Self::RootSeed(root_seed) => CredentialsRef::RootSeed(root_seed),
            Self::ClientCredentials(cc) =>
                CredentialsRef::ClientCredentials(cc),
        }
    }
}

impl From<RootSeed> for Credentials {
    fn from(root_seed: RootSeed) -> Self {
        Self::RootSeed(root_seed)
    }
}

impl From<ClientCredentials> for Credentials {
    fn from(cc: ClientCredentials) -> Self {
        Self::ClientCredentials(cc)
    }
}

// --- CredentialsRef --- //

/// Borrowed version of [`Credentials`].
#[derive(Copy, Clone, Debug)]
pub enum CredentialsRef<'a> {
    /// Authenticate with a borrowed [`RootSeed`].
    RootSeed(&'a RootSeed),
    /// Authenticate with borrowed [`ClientCredentials`].
    ClientCredentials(&'a ClientCredentials),
}

impl<'a> CredentialsRef<'a> {
    /// Returns the user public key, if available.
    ///
    /// Always `Some(_)` if the credentials were created by `node-v0.8.11+`.
    pub(crate) fn user_pk(self) -> Option<UserPk> {
        self.to_unstable().user_pk().map(UserPk::from_unstable)
    }

    /// Convert to the inner [`UnstableCredentialsRef`] used by
    /// `lexe-node-client`.
    pub(crate) fn to_unstable(self) -> UnstableCredentialsRef<'a> {
        match self {
            Self::RootSeed(root_seed) =>
                UnstableCredentialsRef::RootSeed(root_seed.unstable()),
            Self::ClientCredentials(cc) =>
                UnstableCredentialsRef::ClientCredentials(cc.unstable()),
        }
    }
}

impl<'a> From<&'a RootSeed> for CredentialsRef<'a> {
    fn from(root_seed: &'a RootSeed) -> Self {
        Self::RootSeed(root_seed)
    }
}

impl<'a> From<&'a ClientCredentials> for CredentialsRef<'a> {
    fn from(cc: &'a ClientCredentials) -> Self {
        Self::ClientCredentials(cc)
    }
}

// --- RootSeed --- //

/// The root secret from which the user node's keys and credentials are derived.
#[derive(Serialize, Deserialize)]
pub struct RootSeed(UnstableRootSeed);

impl RootSeed {
    // --- Constructors & File I/O --- //

    /// Generate a new random [`RootSeed`] using the system CSPRNG.
    pub fn generate() -> Self {
        Self(UnstableRootSeed::from_rng(&mut SysRng::new()))
    }

    /// Read a [`RootSeed`] from the default seedphrase path for this
    /// environment (`~/.lexe/seedphrase[.env].txt`).
    ///
    /// Returns `Ok(None)` if the file doesn't exist.
    pub fn read(wallet_env: &WalletEnv) -> anyhow::Result<Option<Self>> {
        let lexe_data_dir = lexe_common::default_lexe_data_dir()
            .context("Could not get default lexe data dir")?;
        let path = wallet_env.seedphrase_path(&lexe_data_dir);
        Self::read_from_path(&path)
    }

    /// Write this [`RootSeed`] to the default seedphrase path for this
    /// environment (`~/.lexe/seedphrase[.env].txt`).
    ///
    /// Creates parent directories if needed. Fails if the file already exists.
    pub fn write(&self, wallet_env: &WalletEnv) -> anyhow::Result<()> {
        let lexe_data_dir = lexe_common::default_lexe_data_dir()
            .context("Could not get default lexe data dir")?;
        let path = wallet_env.seedphrase_path(&lexe_data_dir);
        self.write_to_path(&path)
    }

    /// Read a [`RootSeed`] from a seedphrase file at a specific path.
    ///
    /// Returns `Ok(None)` if the file doesn't exist.
    pub fn read_from_path(path: &Path) -> anyhow::Result<Option<Self>> {
        match std::fs::read_to_string(path) {
            Ok(contents) => {
                let mnemonic = bip39::Mnemonic::from_str(contents.trim())
                    .map_err(|e| anyhow::anyhow!("Invalid mnemonic: {e}"))?;
                Ok(Some(Self::try_from(mnemonic)?))
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) => Err(e).context("Failed to read seedphrase file"),
        }
    }

    /// Write this [`RootSeed`] to a seedphrase file at a specific path.
    ///
    /// Creates parent directories if needed. Returns an error if the file
    /// already exists. On Unix, the file is created with mode 0600 (owner
    /// read/write only).
    pub fn write_to_path(&self, path: &Path) -> anyhow::Result<()> {
        #[cfg(unix)]
        use std::os::unix::fs::OpenOptionsExt;

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .context("Failed to create data directory")?;
        }

        // Open with create_new to fail if file exists
        let mut opts = std::fs::OpenOptions::new();
        opts.write(true).create_new(true);

        // Set restrictive permissions on Unix (owner read/write only)
        #[cfg(unix)]
        opts.mode(0o600);

        let mut file = opts.open(path).with_context(|| {
            format!("Seedphrase file already exists: {}", path.display())
        })?;

        let mnemonic = self.to_mnemonic();
        writeln!(file, "{mnemonic}")
            .context("Failed to write seedphrase file")?;

        tracing::info!("Persisted seedphrase to {}", path.display());

        Ok(())
    }

    /// Construct a [`RootSeed`] from a BIP39 mnemonic.
    pub fn from_mnemonic(mnemonic: Mnemonic) -> anyhow::Result<Self> {
        Self::try_from(mnemonic)
    }

    /// Construct a [`RootSeed`] from a 32-byte slice.
    pub fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self> {
        Self::try_from(bytes)
    }

    /// Construct a [`RootSeed`] from a 64-character hex string.
    pub fn from_hex(hex: &str) -> anyhow::Result<Self> {
        Self::from_str(hex).map_err(anyhow::Error::from)
    }

    // --- Serialization --- //

    /// Convert this root secret to its BIP39 mnemonic.
    pub fn to_mnemonic(&self) -> Mnemonic {
        self.unstable().to_mnemonic()
    }

    /// Borrow the 32-byte root secret.
    pub fn as_bytes(&self) -> &[u8] {
        self.unstable().expose_secret()
    }

    /// Encode the root secret as a 64-character hex string.
    pub fn to_hex(&self) -> String {
        hex::encode(self.as_bytes())
    }

    // --- Derived Identity --- //
    /// Derive the user's public key.
    pub fn derive_user_pk(&self) -> UserPk {
        UserPk::from_unstable(self.unstable().derive_user_pk())
    }

    /// Derive the node public key.
    pub fn derive_node_pk(&self) -> NodePk {
        NodePk::from_unstable(self.unstable().derive_node_pk())
    }

    // --- Encryption --- //

    /// Encrypt this root secret under the given password.
    pub fn password_encrypt(&self, password: &str) -> anyhow::Result<Vec<u8>> {
        self.unstable()
            .password_encrypt(&mut SysRng::new(), password)
    }

    /// Decrypt a password-encrypted root secret.
    pub fn password_decrypt(
        password: &str,
        encrypted: Vec<u8>,
    ) -> anyhow::Result<Self> {
        UnstableRootSeed::password_decrypt(password, encrypted).map(Self)
    }

    // --- Internal Escape Hatches --- //

    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Returns the wrapped internal root-seed type.
            ///
            /// This is only exposed when the `unstable` feature is enabled.
            pub fn unstable(&self) -> &UnstableRootSeed {
                &self.0
            }
        } else {
            pub(crate) fn unstable(&self) -> &UnstableRootSeed {
                &self.0
            }
        }
    }

    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Destructure this SDK root seed into the internal root-seed type.
            ///
            /// This is only exposed when the `unstable` feature is enabled.
            pub fn into_unstable(self) -> UnstableRootSeed {
                self.0
            }
        } else {
            pub(crate) fn into_unstable(self) -> UnstableRootSeed {
                self.0
            }
        }
    }
}

impl fmt::Debug for RootSeed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.unstable(), f)
    }
}

impl FromStr for RootSeed {
    type Err = <UnstableRootSeed as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UnstableRootSeed::from_str(s).map(Self)
    }
}

impl TryFrom<&[u8]> for RootSeed {
    type Error = anyhow::Error;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        UnstableRootSeed::try_from(bytes).map(Self)
    }
}

impl TryFrom<Mnemonic> for RootSeed {
    type Error = anyhow::Error;

    fn try_from(mnemonic: Mnemonic) -> Result<Self, Self::Error> {
        UnstableRootSeed::try_from(mnemonic).map(Self)
    }
}

// --- ClientCredentials --- //

/// Scoped and revocable credentials for controlling a Lexe user node.
///
/// These are useful when you want node access without exposing the user's
/// [`RootSeed`], which is irrevocable.
#[derive(Clone)]
pub struct ClientCredentials(UnstableClientCredentials);

impl ClientCredentials {
    /// Parse [`ClientCredentials`] from a string.
    pub fn from_string(s: &str) -> anyhow::Result<Self> {
        Self::from_str(s)
    }

    /// Export these credentials as a portable string.
    ///
    /// The returned string can be passed to [`ClientCredentials::from_string`]
    /// to reconstruct the credentials.
    pub fn export_string(&self) -> String {
        self.unstable().to_base64_blob()
    }

    /// Access the inner [`UnstableClientCredentials`].
    pub(crate) fn unstable(&self) -> &UnstableClientCredentials {
        &self.0
    }
}

impl FromStr for ClientCredentials {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UnstableClientCredentials::try_from_base64_blob(s).map(Self)
    }
}

impl fmt::Debug for ClientCredentials {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

// --- Measurement --- //

/// An SGX enclave measurement (MRENCLAVE).
///
/// This is the hash of the enclave binary, used to verify that a node is
/// running a specific version. Returned in
/// [`NodeInfo`](super::command::NodeInfo).
///
/// The measurement is a 32-byte value typically represented as a 64-character
/// hex string.
///
/// Implements [`ByteArray<32>`].
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Measurement(UnstableMeasurement);

impl ByteArray<32> for Measurement {
    fn from_array(array: [u8; 32]) -> Self {
        Self(UnstableMeasurement::from_array(array))
    }
    fn to_array(&self) -> [u8; 32] {
        self.0.to_array()
    }
    fn as_array(&self) -> &[u8; 32] {
        self.0.as_array()
    }
}

impl AsRef<[u8]> for Measurement {
    fn as_ref(&self) -> &[u8] {
        self.0.as_array().as_slice()
    }
}

impl AsRef<[u8; 32]> for Measurement {
    fn as_ref(&self) -> &[u8; 32] {
        self.0.as_array()
    }
}

impl fmt::Debug for Measurement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl fmt::Display for Measurement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl FromStr for Measurement {
    type Err = <UnstableMeasurement as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UnstableMeasurement::from_str(s).map(Self)
    }
}

impl Measurement {
    /// Destructure this SDK type into the internal type.
    #[cfg(feature = "unstable")]
    pub fn unstable(self) -> UnstableMeasurement {
        self.0
    }

    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Wraps an internal type into this SDK type.
            pub fn from_unstable(inner: UnstableMeasurement) -> Self {
                Self(inner)
            }
        } else {
            pub(crate) fn from_unstable(inner: UnstableMeasurement) -> Self {
                Self(inner)
            }
        }
    }
}

#[cfg(feature = "unstable")]
impl From<UnstableMeasurement> for Measurement {
    fn from(inner: UnstableMeasurement) -> Self {
        Self(inner)
    }
}

#[cfg(feature = "unstable")]
impl From<Measurement> for UnstableMeasurement {
    fn from(outer: Measurement) -> Self {
        outer.0
    }
}

// --- UserPk --- //

/// A Lexe user's primary identifier, derived from the root seed.
///
/// Serialized as a 64-character hex string (32 bytes).
///
/// Implements [`ByteArray<32>`].
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct UserPk(UnstableUserPk);

impl ByteArray<32> for UserPk {
    fn from_array(array: [u8; 32]) -> Self {
        Self(UnstableUserPk::from_array(array))
    }
    fn to_array(&self) -> [u8; 32] {
        self.0.to_array()
    }
    fn as_array(&self) -> &[u8; 32] {
        self.0.as_array()
    }
}

impl AsRef<[u8]> for UserPk {
    fn as_ref(&self) -> &[u8] {
        self.0.as_array().as_slice()
    }
}

impl AsRef<[u8; 32]> for UserPk {
    fn as_ref(&self) -> &[u8; 32] {
        self.0.as_array()
    }
}

impl fmt::Debug for UserPk {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl fmt::Display for UserPk {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl FromStr for UserPk {
    type Err = <UnstableUserPk as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UnstableUserPk::from_str(s).map(Self)
    }
}

impl UserPk {
    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Destructure this SDK type into the internal type.
            pub fn unstable(self) -> UnstableUserPk {
                self.0
            }
        } else {
            pub(crate) fn unstable(self) -> UnstableUserPk {
                self.0
            }
        }
    }

    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Wraps an internal type into this SDK type.
            pub fn from_unstable(inner: UnstableUserPk) -> Self {
                Self(inner)
            }
        } else {
            pub(crate) fn from_unstable(inner: UnstableUserPk) -> Self {
                Self(inner)
            }
        }
    }
}

#[cfg(feature = "unstable")]
impl From<UnstableUserPk> for UserPk {
    fn from(inner: UnstableUserPk) -> Self {
        Self(inner)
    }
}

#[cfg(feature = "unstable")]
impl From<UserPk> for UnstableUserPk {
    fn from(outer: UserPk) -> Self {
        outer.0
    }
}

// --- NodePk --- //

/// A Lightning node's secp256k1 public key (the `node_id`).
///
/// Serialized as a 66-character hex string (33 bytes, compressed).
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NodePk(UnstableNodePk);

impl NodePk {
    /// Construct from a 66-character hex string.
    pub fn from_hex(hex_str: &str) -> anyhow::Result<Self> {
        Self::from_str(hex_str).map_err(anyhow::Error::from)
    }

    /// Encode as a 66-character hex string.
    pub fn to_hex(&self) -> String {
        self.to_string()
    }
}

impl fmt::Debug for NodePk {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl fmt::Display for NodePk {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl FromStr for NodePk {
    type Err = <UnstableNodePk as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UnstableNodePk::from_str(s).map(Self)
    }
}

impl NodePk {
    /// Destructure this SDK type into the internal type.
    #[cfg(feature = "unstable")]
    pub fn unstable(self) -> UnstableNodePk {
        self.0
    }

    cfg_if::cfg_if! {
        if #[cfg(feature = "unstable")] {
            /// Wraps an internal type into this SDK type.
            pub fn from_unstable(inner: UnstableNodePk) -> Self {
                Self(inner)
            }
        } else {
            pub(crate) fn from_unstable(inner: UnstableNodePk) -> Self {
                Self(inner)
            }
        }
    }
}

#[cfg(feature = "unstable")]
impl From<UnstableNodePk> for NodePk {
    fn from(inner: UnstableNodePk) -> Self {
        Self(inner)
    }
}

#[cfg(feature = "unstable")]
impl From<NodePk> for UnstableNodePk {
    fn from(outer: NodePk) -> Self {
        outer.0
    }
}

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

    #[test]
    fn seedphrase_file_roundtrip() {
        let root_seed1 = RootSeed::generate();

        let tempdir = tempfile::tempdir().unwrap();
        let path = tempdir.path().join("seedphrase.txt");

        // Write seedphrase to file
        root_seed1.write_to_path(&path).unwrap();

        // Read it back
        let root_seed2 = RootSeed::read_from_path(&path).unwrap().unwrap();
        assert_eq!(root_seed1.as_bytes(), root_seed2.as_bytes());

        // Writing again should fail (file exists)
        let err = root_seed1.write_to_path(&path).unwrap_err();
        assert!(err.to_string().contains("already exists"));

        // Reading non-existent file should return None
        let missing = tempdir.path().join("missing.txt");
        assert!(RootSeed::read_from_path(&missing).unwrap().is_none());
    }
}