lb-rs 26.4.13

The rust library for interacting with your lockbook.
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
use crate::model::account::{Account, MAX_USERNAME_LENGTH};
use crate::model::api::{
    DeleteAccountRequest, GetPublicKeyRequest, GetUsernameRequest, NewAccountRequestV2,
};
use crate::model::errors::{LbErrKind, LbResult, core_err_unexpected};
use crate::model::file_like::FileLike;
use crate::model::file_metadata::{FileType, Owner};
use crate::model::meta::Meta;
use crate::service::events::Actor;
use crate::{DEFAULT_API_LOCATION, Lb};
use libsecp256k1::SecretKey;
use qrcode_generator::QrCodeEcc;

use crate::io::network::ApiError;

impl Lb {
    /// CoreError::AccountExists,
    /// CoreError::UsernameTaken,
    /// CoreError::UsernameInvalid,
    /// CoreError::ServerDisabled,
    /// CoreError::ServerUnreachable,
    /// CoreError::ClientUpdateRequired,
    #[instrument(level = "debug", skip(self), err(Debug))]
    pub async fn create_account(
        &self, username: &str, api_url: &str, welcome_doc: bool,
    ) -> LbResult<Account> {
        let username = String::from(username).to_lowercase();

        if username.len() > MAX_USERNAME_LENGTH {
            return Err(LbErrKind::UsernameInvalid.into());
        }

        let mut tx = self.begin_tx().await;
        let db = tx.db();

        if db.account.get().is_some() {
            return Err(LbErrKind::AccountExists.into());
        }

        let account = Account::new(username.clone(), api_url.to_string());

        let root = Meta::create_root(&account)?.sign_with(&account)?;
        let root_id = *root.id();

        let last_synced = self
            .client
            .request(&account, NewAccountRequestV2::new(&account, &root))
            .await?
            .last_synced;

        db.account.insert(account.clone())?;
        db.base_metadata.insert(root_id, root)?;
        db.last_synced.insert(last_synced as i64)?;
        db.root.insert(root_id)?;
        db.pub_key_lookup
            .insert(Owner(account.public_key()), account.username.clone())?;

        self.keychain.cache_account(account.clone()).await?;

        tx.end();

        if welcome_doc {
            let welcome_doc = self
                .create_file("welcome.md", &root_id, FileType::Document)
                .await?;
            self.write_document(welcome_doc.id, Self::WELCOME_MESSAGE.as_bytes())
                .await?;
            self.sync().await?;
        }

        self.events.meta_changed(Actor::User);
        self.events.signed_in();

        Ok(account)
    }

    #[instrument(level = "debug", skip(self, key), err(Debug))]
    pub async fn import_account(&self, key: &str, api_url: Option<&str>) -> LbResult<Account> {
        if self.get_account().is_ok() {
            warn!("tried to import an account, but account exists already.");
            return Err(LbErrKind::AccountExists.into());
        }

        if let Ok(key) = base64::decode(key) {
            if let Ok(account) = bincode::deserialize(&key[..]) {
                return self.import_account_private_key_v1(account).await;
            } else if let Ok(key) = SecretKey::parse_slice(&key) {
                return self
                    .import_account_private_key_v2(key, api_url.unwrap_or(DEFAULT_API_LOCATION))
                    .await;
            }
        }

        let phrase: [&str; 24] = key
            .split([' ', ','])
            .filter(|maybe_word| !maybe_word.is_empty())
            .collect::<Vec<_>>()
            .try_into()
            .map_err(|_| LbErrKind::AccountStringCorrupted)?;

        self.import_account_phrase(phrase, api_url.unwrap_or(DEFAULT_API_LOCATION))
            .await
    }

    pub async fn import_account_private_key_v1(&self, account: Account) -> LbResult<Account> {
        let server_public_key = self
            .client
            .request(&account, GetPublicKeyRequest { username: account.username.clone() })
            .await?
            .key;

        let account_public_key = account.public_key();

        if account_public_key != server_public_key {
            return Err(LbErrKind::UsernamePublicKeyMismatch.into());
        }

        let mut tx = self.begin_tx().await;
        let db = tx.db();
        db.account.insert(account.clone())?;
        self.keychain.cache_account(account.clone()).await?;

        Ok(account)
    }

    pub async fn import_account_private_key_v2(
        &self, private_key: SecretKey, api_url: &str,
    ) -> LbResult<Account> {
        let mut account =
            Account { username: "".to_string(), api_url: api_url.to_string(), private_key };
        let public_key = account.public_key();

        account.username = self
            .client
            .request(&account, GetUsernameRequest { key: public_key })
            .await?
            .username;

        let mut tx = self.begin_tx().await;
        let db = tx.db();
        db.account.insert(account.clone())?;
        self.keychain.cache_account(account.clone()).await?;

        Ok(account)
    }

    pub async fn import_account_phrase(
        &self, phrase: [&str; 24], api_url: &str,
    ) -> LbResult<Account> {
        let private_key = Account::phrase_to_private_key(phrase)?;
        self.import_account_private_key_v2(private_key, api_url)
            .await
    }

    #[instrument(level = "debug", skip(self), err(Debug))]
    pub fn export_account_private_key(&self) -> LbResult<String> {
        self.export_account_private_key_v2()
    }

    #[allow(dead_code)]
    pub(crate) fn export_account_private_key_v1(&self) -> LbResult<String> {
        let account = self.get_account()?;
        let encoded: Vec<u8> = bincode::serialize(account).map_err(core_err_unexpected)?;
        Ok(base64::encode(encoded))
    }

    pub(crate) fn export_account_private_key_v2(&self) -> LbResult<String> {
        let account = self.get_account()?;
        Ok(base64::encode(account.private_key.serialize()))
    }

    pub fn export_account_phrase(&self) -> LbResult<String> {
        let account = self.get_account()?;
        Ok(account.get_phrase()?.join(" "))
    }

    pub fn export_account_qr(&self) -> LbResult<Vec<u8>> {
        let acct_secret = self.export_account_private_key_v2()?;
        qrcode_generator::to_png_to_vec(acct_secret, QrCodeEcc::Low, 1024)
            .map_err(|err| core_err_unexpected(err).into())
    }

    #[instrument(level = "debug", skip(self), err(Debug))]
    pub async fn delete_account(&self) -> LbResult<()> {
        let account = self.get_account()?;

        self.client
            .request(account, DeleteAccountRequest {})
            .await
            .map_err(|err| match err {
                ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
                ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
                _ => core_err_unexpected(err),
            })?;

        let mut tx = self.begin_tx().await;
        let db = tx.db();

        db.account.clear()?;
        db.last_synced.clear()?;
        db.base_metadata.clear()?;
        db.root.clear()?;
        db.local_metadata.clear()?;
        db.pub_key_lookup.clear()?;

        // todo: clear cache?

        Ok(())
    }

    const WELCOME_MESSAGE: &'static str = r#"# Markdown Syntax
Markdown is a language for easily formatting your documents. This document can help you get started.

## Styled Text
To style text, wrap your text in the corresponding characters.
| Style         | Syntax              | Example           |
|---------------|---------------------|-------------------|
| emphasis      | `*emphasis*`        | *emphasis*        |
| strong        | `**strong**`        | **strong**        |
| strikethrough | `~~strikethrough~~` | ~~strikethrough~~ |
| underline     | `__underline__`     | __underline__     |
| code          | ``code``            | `code`            |
| highlight     | `==highlight==`     | ==highlight==     |
| spoiler       | `||spoiler||`       | ||spoiler||       |
| superscript   | `^superscript^`     | ^superscript^     |
| subscript     | `~subscript~`       | ~subscript~       |

## Links
To make text into a link, wrap it with `[` `]`, add a link destination to the end , and wrap the destination with `(` `)`. The link destination can be a web URL or a relative path to another Lockbook file.
```md
[Lockbook's website](https://lockbook.net)
```
> [Lockbook's website](https://lockbook.net)

## Images
To embed an image, add a `!` to the beginning of the link syntax.
```md
![Lockbook's logo](https://raw.githubusercontent.com/lockbook/lockbook/master/docs/graphics/logo.svg)
```
> ![Lockbook's logo](https://raw.githubusercontent.com/lockbook/lockbook/master/docs/graphics/logo.svg)

## Headings
To create a heading, add up to six `#`'s plus a space before your text. More `#`'s create a smaller heading.
```md
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6
```
> # Heading 1
> ## Heading 2
> ### Heading 3
> #### Heading 4
> ##### Heading 5
> ###### Heading 6

## Lists
Create a list item by adding `- `, `+ `, or `* ` for a bulleted list, `1. ` for a numbered list, or `- [ ] `, `+ [ ] `, or `* [ ] ` for a task list at the start of the line. The added characters are called the *list marker*.
```md
* bulleted list item
- bulleted list item
+ bulleted list item

1. numbered list item
1. numbered list item
1. numbered list item

- [ ] task list item
- [x] task list item
```
>* bulleted list item
>- bulleted list item
>+ bulleted list item
>
>1. numbered list item
>1. numbered list item
>1. numbered list item
>
>- [ ] task list item
>- [x] task list item

List items can be nested. To nest an inner item in an outer one, the inner item's line must start with at least one space for each character in the outer item's list marker: usually 2 for bulleted lists, 3 for numbered lists, or 2 for tasks lists (the trailing `[ ] ` is excluded).
```md
* This is a bulleted list
    * An inner item needs at least 2 spaces
1. This is a numbered list
    1. An inner item needs at least 3 spaces
* [ ] This is a task list
    * [ ] An inner item needs at least 2 spaces
```
> * This is a bulleted list
>   * An inner item needs at least 2 spaces
> 1. This is a numbered list
>    1. An inner item needs at least 3 spaces
> * [ ] This is a task list
>   * [ ] An inner item needs at least 2 spaces

List items can contain formatted content. For non-text content, each line must start with the same number of spaces as an inner list item would.
```md
* This item contains text,
    > a quote
    ### and a heading.
* This item contains two lines of text.
The second line doesn't need spaces.
```
> * This item contains text,
>   > a quote
>   ### and a heading.
> * This item contains two lines of text.
> The second line doesn't need spaces.

## Quotes
To create a block quote, add `> ` to each line.
```md
> This is a quote
```
> This is a quote

Like list items, block quotes can contain formatted content.
```md
> This quote contains some text,
> ```rust
> // some code
> fn main() { println!("Hello, world!"); }
> ```
> ### and a heading.

> This quote contains two lines of text.
The second line doesn't need added characters.
```
> This quote contains some text,
> ```rust
> // some code
> fn main() { println!("Hello, world!"); }
> ```
> ### and a heading.

> This quote contains two lines of text.
The second line doesn't need added characters.

## Alerts
To create an alert, add one of 5 tags to the first line of a quote: `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`, or `[!CAUTION]`. An alternate title can be added after the tag.
```md
> [!NOTE]
> This is a note.

> [!TIP]
> This is a tip.

> [!IMPORTANT]
> This is important.

> [!WARNING]
> This is a warning.

> [!CAUTION] Caution!!!!!
> This is a caution.
```
> [!NOTE]
> This is a note.

> [!TIP]
> This is a tip.

> [!IMPORTANT]
> This is important.

> [!WARNING]
> This is a warning.

> [!CAUTION] Caution!!!!!
> This is a caution.

## Tables
A table is written with `|`'s between columns and a row after the header row whose cell's contents are `-`'s.
```md
| Style         | Syntax              | Example           |
|---------------|---------------------|-------------------|
| emphasis      | `*emphasis*`        | *emphasis*        |
| strong        | `**strong**`        | **strong**        |
```
> | Style         | Syntax              | Example           |
> |---------------|---------------------|-------------------|
> | emphasis      | `*emphasis*`        | *emphasis*        |
> | strong        | `**strong**`        | **strong**        |

## Code
A code block is wrapped by two lines containing three backticks. A language can be added after the opening backticks.
```md
    ```rust
    // some code
    fn main() { println!("Hello, world!"); }
    ```
```
> ```rust
> // some code
> fn main() { println!("Hello, world!"); }
> ```

You can also create a code block by indenting each line with four spaces. Indented code blocks cannot have a language.
```md
    // some code
    fn main() { println!("Hello, world!"); }
```
>     // some code
>     fn main() { println!("Hello, world!"); }

## Thematic Breaks
A thematic break is written with `***`, `---`, or `___` and shows a horizontal line across the page.
```md
***
---
___
```
> ***
> ---
> ___
"#;
}