zakura-client-sqlite 0.1.0-rc0

An SQLite-based Zcash light client
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
use std::{collections::HashSet, rc::Rc};

use rusqlite::{OptionalExtension, Transaction, named_params};
use schemerz_rusqlite::RusqliteMigration;
use secrecy::{ExposeSecret, SecretVec};
use uuid::Uuid;

use zcash_client_backend::data_api::{AccountPurpose, AccountSource, Zip32Derivation};
use zcash_keys::keys::{UnifiedFullViewingKey, UnifiedSpendingKey};
use zcash_protocol::consensus;
use zip32::fingerprint::SeedFingerprint;

use super::{
    add_account_birthdays, receiving_key_scopes, v_transactions_note_uniqueness, wallet_summaries,
};
use crate::wallet::{account_kind_code, init::WalletMigrationError};

/// The migration that switched from presumed seed-derived account IDs to supporting
/// HD accounts and all sorts of imported keys.
pub const MIGRATION_ID: Uuid = Uuid::from_u128(0x6d02ec76_8720_4cc6_b646_c4e2ce69221c);

pub(crate) struct Migration<P: consensus::Parameters> {
    pub(super) seed: Option<Rc<SecretVec<u8>>>,
    pub(super) params: P,
}

const DEPENDENCIES: &[Uuid] = &[
    receiving_key_scopes::MIGRATION_ID,
    add_account_birthdays::MIGRATION_ID,
    v_transactions_note_uniqueness::MIGRATION_ID,
    wallet_summaries::MIGRATION_ID,
];

impl<P: consensus::Parameters> schemerz::Migration<Uuid> for Migration<P> {
    fn id(&self) -> Uuid {
        MIGRATION_ID
    }

    fn dependencies(&self) -> HashSet<Uuid> {
        DEPENDENCIES.iter().copied().collect()
    }

    fn description(&self) -> &'static str {
        "Replaces the `account` column in the `accounts` table with columns to support all kinds of
         account and key types."
    }
}

impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
    type Error = WalletMigrationError;

    fn up(&self, transaction: &Transaction) -> Result<(), WalletMigrationError> {
        let account_kind_derived = account_kind_code(&AccountSource::Derived {
            derivation: Zip32Derivation::new(
                SeedFingerprint::from_bytes([0; 32]),
                zip32::AccountId::ZERO,
                #[cfg(feature = "zcashd-compat")]
                None,
            ),
            key_source: None,
        });
        let account_kind_imported = account_kind_code(&AccountSource::Imported {
            // the purpose here is irrelevant; we just use it to get the correct code
            // for the account kind
            purpose: AccountPurpose::ViewOnly,
            key_source: None,
        });
        transaction.execute_batch(&format!(
            r#"
            CREATE TABLE accounts_new (
                id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                account_kind INTEGER NOT NULL DEFAULT {account_kind_derived},
                hd_seed_fingerprint BLOB,
                hd_account_index INTEGER,
                ufvk TEXT,
                uivk TEXT NOT NULL,
                orchard_fvk_item_cache BLOB,
                sapling_fvk_item_cache BLOB,
                p2pkh_fvk_item_cache BLOB,
                birthday_height INTEGER NOT NULL,
                birthday_sapling_tree_size INTEGER,
                birthday_orchard_tree_size INTEGER,
                recover_until_height INTEGER,
                CHECK (
                  (
                    account_kind = {account_kind_derived}
                    AND hd_seed_fingerprint IS NOT NULL
                    AND hd_account_index IS NOT NULL
                    AND ufvk IS NOT NULL
                  )
                  OR
                  (
                    account_kind = {account_kind_imported}
                    AND hd_seed_fingerprint IS NULL
                    AND hd_account_index IS NULL
                  )
                )
            );
            CREATE UNIQUE INDEX hd_account ON accounts_new (hd_seed_fingerprint, hd_account_index);
            CREATE UNIQUE INDEX accounts_uivk ON accounts_new (uivk);
            CREATE UNIQUE INDEX accounts_ufvk ON accounts_new (ufvk);
            "#
        ))?;

        // We require the seed *if* there are existing accounts in the table.
        if transaction.query_row("SELECT COUNT(*) FROM accounts", [], |row| {
            Ok(row.get::<_, u32>(0)? > 0)
        })? {
            if let Some(seed) = &self.seed {
                let seed_id = SeedFingerprint::from_seed(seed.expose_secret())
                    .expect("Seed is between 32 and 252 bytes in length.");

                // We track whether we have determined seed relevance or not, in order to
                // correctly report errors when checking the seed against an account:
                //
                // - If we encounter an error with the first account, we can assert that
                //   the seed is not relevant to the wallet by assuming that:
                //   - All accounts are from the same seed (which is historically the only
                //     use case that this migration supported), and
                //   - All accounts in the wallet must have been able to derive their USKs
                //     (in order to derive UIVKs).
                //
                // - Once the seed has been determined to be relevant (because it matched
                //   the first account), any subsequent account derivation failure is
                //   proving wrong our second assumption above, and we report this as
                //   corrupted data.
                let mut seed_is_relevant = false;

                let mut q = transaction.prepare("SELECT * FROM accounts")?;
                let mut rows = q.query([])?;
                while let Some(row) = rows.next()? {
                    let account_index: u32 = row.get("account")?;
                    let birthday_height: u32 = row.get("birthday_height")?;
                    let recover_until_height: Option<u32> = row.get("recover_until_height")?;

                    // Although 'id' is an AUTOINCREMENT column, we'll set it explicitly to match the old account value
                    // strictly as a matter of convenience to make this migration script easier,
                    // specifically around updating tables with foreign keys to this one.
                    let account_id = account_index;

                    // Verify that the UFVK is as expected by re-deriving it.
                    let ufvk: String = row.get("ufvk")?;
                    let ufvk_parsed = UnifiedFullViewingKey::decode(&self.params, &ufvk)
                        .map_err(|_| WalletMigrationError::CorruptedData("Bad UFVK".to_string()))?;
                    let usk = UnifiedSpendingKey::from_seed(
                        &self.params,
                        seed.expose_secret(),
                        zip32::AccountId::try_from(account_index).map_err(|_| {
                            WalletMigrationError::CorruptedData("Bad account index".to_string())
                        })?,
                    )
                    .map_err(|_| {
                        if seed_is_relevant {
                            WalletMigrationError::CorruptedData(
                                "Unable to derive spending key from seed.".to_string(),
                            )
                        } else {
                            WalletMigrationError::SeedNotRelevant
                        }
                    })?;
                    let expected_ufvk = usk.to_unified_full_viewing_key();
                    if ufvk != expected_ufvk.encode(&self.params) {
                        return Err(if seed_is_relevant {
                            WalletMigrationError::CorruptedData(
                                "UFVK does not match expected value.".to_string(),
                            )
                        } else {
                            WalletMigrationError::SeedNotRelevant
                        });
                    }

                    // We made it past one derived account, so the seed must be relevant.
                    seed_is_relevant = true;

                    let uivk = ufvk_parsed
                        .to_unified_incoming_viewing_key()
                        .encode(&self.params);

                    #[cfg(feature = "orchard")]
                    let orchard_item = ufvk_parsed.orchard().map(|k| k.to_bytes());
                    #[cfg(not(feature = "orchard"))]
                    let orchard_item: Option<Vec<u8>> = None;

                    let sapling_item = ufvk_parsed.sapling().map(|k| k.to_bytes());

                    #[cfg(feature = "transparent-inputs")]
                    let transparent_item = ufvk_parsed.transparent().map(|k| k.serialize());
                    #[cfg(not(feature = "transparent-inputs"))]
                    let transparent_item: Option<Vec<u8>> = None;

                    // Get the tree sizes for the birthday height from the blocks table, if
                    // available.
                    let (birthday_sapling_tree_size, birthday_orchard_tree_size) = transaction
                        .query_row(
                            "SELECT sapling_commitment_tree_size - sapling_output_count,
                                    orchard_commitment_tree_size - orchard_action_count
                             FROM blocks
                             WHERE height = :birthday_height",
                            named_params![":birthday_height": birthday_height],
                            |row| {
                                Ok(row
                                    .get::<_, Option<u32>>(0)?
                                    .zip(row.get::<_, Option<u32>>(1)?))
                            },
                        )
                        .optional()?
                        .flatten()
                        .map_or((None, None), |(s, o)| (Some(s), Some(o)));

                    transaction.execute(
                        r#"
                        INSERT INTO accounts_new (
                            id, account_kind, hd_seed_fingerprint, hd_account_index,
                            ufvk, uivk,
                            orchard_fvk_item_cache, sapling_fvk_item_cache, p2pkh_fvk_item_cache,
                            birthday_height, birthday_sapling_tree_size, birthday_orchard_tree_size,
                            recover_until_height
                        )
                        VALUES (
                            :account_id, :account_kind, :seed_id, :account_index,
                            :ufvk, :uivk,
                            :orchard_fvk_item_cache, :sapling_fvk_item_cache, :p2pkh_fvk_item_cache,
                            :birthday_height, :birthday_sapling_tree_size, :birthday_orchard_tree_size,
                            :recover_until_height
                        );
                        "#,
                        named_params![
                            ":account_id": account_id,
                            ":account_kind": account_kind_derived,
                            ":seed_id": seed_id.to_bytes(),
                            ":account_index": account_index,
                            ":ufvk": ufvk,
                            ":uivk": uivk,
                            ":orchard_fvk_item_cache": orchard_item,
                            ":sapling_fvk_item_cache": sapling_item,
                            ":p2pkh_fvk_item_cache": transparent_item,
                            ":birthday_height": birthday_height,
                            ":birthday_sapling_tree_size": birthday_sapling_tree_size,
                            ":birthday_orchard_tree_size": birthday_orchard_tree_size,
                            ":recover_until_height": recover_until_height,
                        ],
                    )?;
                }
            } else {
                return Err(WalletMigrationError::SeedRequired);
            }
        }

        transaction.execute_batch(r#"
            PRAGMA legacy_alter_table = ON;

            DROP TABLE accounts;
            ALTER TABLE accounts_new RENAME TO accounts;

            -- Migrate addresses table
            CREATE TABLE addresses_new (
                account_id INTEGER NOT NULL,
                diversifier_index_be BLOB NOT NULL,
                address TEXT NOT NULL,
                cached_transparent_receiver_address TEXT,
                FOREIGN KEY (account_id) REFERENCES accounts(id),
                CONSTRAINT diversification UNIQUE (account_id, diversifier_index_be)
            );
            CREATE INDEX "addresses_accounts" ON "addresses_new" (
                "account_id" ASC
            );
            INSERT INTO addresses_new (account_id, diversifier_index_be, address, cached_transparent_receiver_address)
            SELECT account, diversifier_index_be, address, cached_transparent_receiver_address
            FROM addresses;

            DROP TABLE addresses;
            ALTER TABLE addresses_new RENAME TO addresses;

            -- Migrate sapling_received_notes table
            CREATE TABLE sapling_received_notes_new (
                id INTEGER PRIMARY KEY,
                tx INTEGER NOT NULL,
                output_index INTEGER NOT NULL,
                account_id INTEGER NOT NULL,
                diversifier BLOB NOT NULL,
                value INTEGER NOT NULL,
                rcm BLOB NOT NULL,
                nf BLOB UNIQUE,
                is_change INTEGER NOT NULL,
                memo BLOB,
                commitment_tree_position INTEGER,
                recipient_key_scope INTEGER,
                FOREIGN KEY (tx) REFERENCES transactions(id_tx),
                FOREIGN KEY (account_id) REFERENCES accounts(id),
                CONSTRAINT tx_output UNIQUE (tx, output_index)
            );
            CREATE INDEX "sapling_received_notes_account" ON "sapling_received_notes_new" (
                "account_id" ASC
            );
            CREATE INDEX "sapling_received_notes_tx" ON "sapling_received_notes_new" (
                "tx" ASC
            );

            -- Replace the `spent` column in `sapling_received_notes` with a junction table between
            -- received notes and the transactions that spend them. This is necessary as otherwise
            -- we cannot compute the correct value of transactions that expire unmined.
            CREATE TABLE sapling_received_note_spends (
                sapling_received_note_id INTEGER NOT NULL,
                transaction_id INTEGER NOT NULL,
                FOREIGN KEY (sapling_received_note_id)
                    REFERENCES sapling_received_notes(id)
                    ON DELETE CASCADE,
                FOREIGN KEY (transaction_id)
                    -- We do not delete transactions, so this does not cascade
                    REFERENCES transactions(id_tx),
                UNIQUE (sapling_received_note_id, transaction_id)
            );

            INSERT INTO sapling_received_note_spends (sapling_received_note_id, transaction_id)
            SELECT id_note, spent
            FROM sapling_received_notes
            WHERE spent IS NOT NULL;

            INSERT INTO sapling_received_notes_new (
                id, tx, output_index, account_id,
                diversifier, value, rcm, nf, is_change, memo, commitment_tree_position,
                recipient_key_scope
            )
            SELECT
                id_note, tx, output_index, account,
                diversifier, value, rcm, nf, is_change, memo, commitment_tree_position,
                recipient_key_scope
            FROM sapling_received_notes;

            DROP TABLE sapling_received_notes;
            ALTER TABLE sapling_received_notes_new RENAME TO sapling_received_notes;

            -- Migrate sent_notes table
            CREATE TABLE sent_notes_new (
                id INTEGER PRIMARY KEY,
                tx INTEGER NOT NULL,
                output_pool INTEGER NOT NULL,
                output_index INTEGER NOT NULL,
                from_account_id INTEGER NOT NULL,
                to_address TEXT,
                to_account_id INTEGER,
                value INTEGER NOT NULL,
                memo BLOB,
                FOREIGN KEY (tx) REFERENCES transactions(id_tx),
                FOREIGN KEY (from_account_id) REFERENCES accounts(id),
                FOREIGN KEY (to_account_id) REFERENCES accounts(id),
                CONSTRAINT tx_output UNIQUE (tx, output_pool, output_index),
                CONSTRAINT note_recipient CHECK (
                    (to_address IS NOT NULL) OR (to_account_id IS NOT NULL)
                )
            );
            CREATE INDEX sent_notes_tx ON sent_notes_new (tx);
            CREATE INDEX sent_notes_from_account ON sent_notes_new (from_account_id);
            CREATE INDEX sent_notes_to_account ON sent_notes_new (to_account_id);
            INSERT INTO sent_notes_new (id, tx, output_pool, output_index, from_account_id, to_address, to_account_id, value, memo)
            SELECT id_note, tx, output_pool, output_index, from_account, to_address, to_account, value, memo
            FROM sent_notes;

            DROP TABLE sent_notes;
            ALTER TABLE sent_notes_new RENAME TO sent_notes;

            -- No one uses this table any more, and it contains a reference to columns we renamed.
            DROP TABLE sapling_witnesses;

            -- Migrate utxos table
            CREATE TABLE utxos_new (
                id INTEGER PRIMARY KEY,
                received_by_account_id INTEGER NOT NULL,
                address TEXT NOT NULL,
                prevout_txid BLOB NOT NULL,
                prevout_idx INTEGER NOT NULL,
                script BLOB NOT NULL,
                value_zat INTEGER NOT NULL,
                height INTEGER NOT NULL,
                FOREIGN KEY (received_by_account_id) REFERENCES accounts(id),
                CONSTRAINT tx_outpoint UNIQUE (prevout_txid, prevout_idx)
            );
            CREATE INDEX utxos_received_by_account ON utxos_new (received_by_account_id);

            INSERT INTO utxos_new (id, received_by_account_id, address, prevout_txid, prevout_idx, script, value_zat, height)
            SELECT id_utxo, received_by_account, address, prevout_txid, prevout_idx, script, value_zat, height
            FROM utxos;

            -- Replace the `spent_in_tx` column in `utxos` with a junction table between received
            -- outputs and the transactions that spend them. This is necessary as otherwise we
            -- cannot compute the correct value of transactions that expire unmined.
            CREATE TABLE transparent_received_output_spends (
                transparent_received_output_id INTEGER NOT NULL,
                transaction_id INTEGER NOT NULL,
                FOREIGN KEY (transparent_received_output_id)
                    REFERENCES utxos(id)
                    ON DELETE CASCADE,
                FOREIGN KEY (transaction_id)
                    -- We do not delete transactions, so this does not cascade
                    REFERENCES transactions(id_tx),
                UNIQUE (transparent_received_output_id, transaction_id)
            );

            INSERT INTO transparent_received_output_spends (transparent_received_output_id, transaction_id)
            SELECT id_utxo, spent_in_tx
            FROM utxos
            WHERE spent_in_tx IS NOT NULL;

            DROP TABLE utxos;
            ALTER TABLE utxos_new RENAME TO utxos;

            PRAGMA legacy_alter_table = OFF;
        "#)?;

        // Rewrite v_transactions view
        transaction.execute_batch("
            DROP VIEW v_transactions;
            CREATE VIEW v_transactions AS
            WITH
            notes AS (
                SELECT sapling_received_notes.id             AS id,
                       sapling_received_notes.account_id     AS account_id,
                       transactions.block                    AS block,
                       transactions.txid                     AS txid,
                       2                                     AS pool,
                       sapling_received_notes.value          AS value,
                       CASE
                            WHEN sapling_received_notes.is_change THEN 1
                            ELSE 0
                       END AS is_change,
                       CASE
                            WHEN sapling_received_notes.is_change THEN 0
                            ELSE 1
                       END AS received_count,
                       CASE
                         WHEN (sapling_received_notes.memo IS NULL OR sapling_received_notes.memo = X'F6')
                           THEN 0
                         ELSE 1
                       END AS memo_present
                FROM sapling_received_notes
                JOIN transactions
                     ON transactions.id_tx = sapling_received_notes.tx
                UNION
                SELECT utxos.id                      AS id,
                       utxos.received_by_account_id  AS account_id,
                       utxos.height                  AS block,
                       utxos.prevout_txid            AS txid,
                       0                             AS pool,
                       utxos.value_zat               AS value,
                       0                             AS is_change,
                       1                             AS received_count,
                       0                             AS memo_present
                FROM utxos
                UNION
                SELECT sapling_received_notes.id             AS id,
                       sapling_received_notes.account_id     AS account_id,
                       transactions.block                    AS block,
                       transactions.txid                     AS txid,
                       2                                     AS pool,
                       -sapling_received_notes.value         AS value,
                       0                             AS is_change,
                       0                             AS received_count,
                       0                             AS memo_present
                FROM sapling_received_notes
                JOIN sapling_received_note_spends
                     ON sapling_received_note_id = sapling_received_notes.id
                JOIN transactions
                     ON transactions.id_tx = sapling_received_note_spends.transaction_id
                UNION
                SELECT utxos.id                      AS id,
                       utxos.received_by_account_id  AS account_id,
                       transactions.block            AS block,
                       transactions.txid             AS txid,
                       0                             AS pool,
                       -utxos.value_zat              AS value,
                       0                             AS is_change,
                       0                             AS received_count,
                       0                             AS memo_present
                FROM utxos
            JOIN transparent_received_output_spends txo_spends
                 ON txo_spends.transparent_received_output_id = txos.id
            JOIN transactions
                 ON transactions.id_tx = txo_spends.transaction_id
            ),
            sent_note_counts AS (
                SELECT sent_notes.from_account_id AS account_id,
                       transactions.txid       AS txid,
                       COUNT(DISTINCT sent_notes.id) as sent_notes,
                       SUM(
                         CASE
                           WHEN (sent_notes.memo IS NULL OR sent_notes.memo = X'F6' OR sapling_received_notes.tx IS NOT NULL)
                             THEN 0
                           ELSE 1
                         END
                       ) AS memo_count
                FROM sent_notes
                JOIN transactions
                     ON transactions.id_tx = sent_notes.tx
                LEFT JOIN sapling_received_notes
                          ON (sent_notes.tx, sent_notes.output_pool, sent_notes.output_index) =
                             (sapling_received_notes.tx, 2, sapling_received_notes.output_index)
                WHERE COALESCE(sapling_received_notes.is_change, 0) = 0
                GROUP BY account_id, txid
            ),
            blocks_max_height AS (
                SELECT MAX(blocks.height) as max_height FROM blocks
            )
            SELECT notes.account_id                  AS account_id,
                   notes.block                       AS mined_height,
                   notes.txid                        AS txid,
                   transactions.tx_index             AS tx_index,
                   transactions.expiry_height        AS expiry_height,
                   transactions.raw                  AS raw,
                   SUM(notes.value)                  AS account_balance_delta,
                   transactions.fee                  AS fee_paid,
                   SUM(notes.is_change) > 0          AS has_change,
                   MAX(COALESCE(sent_note_counts.sent_notes, 0))  AS sent_note_count,
                   SUM(notes.received_count)         AS received_note_count,
                   SUM(notes.memo_present) + MAX(COALESCE(sent_note_counts.memo_count, 0)) AS memo_count,
                   blocks.time                       AS block_time,
                   (
                        blocks.height IS NULL
                        AND transactions.expiry_height BETWEEN 1 AND blocks_max_height.max_height
                   ) AS expired_unmined
            FROM notes
            LEFT JOIN transactions
                 ON notes.txid = transactions.txid
            JOIN blocks_max_height
            LEFT JOIN blocks ON blocks.height = notes.block
            LEFT JOIN sent_note_counts
                      ON sent_note_counts.account_id = notes.account_id
                      AND sent_note_counts.txid = notes.txid
            GROUP BY notes.account_id, notes.txid;

            DROP VIEW v_tx_outputs;
            CREATE VIEW v_tx_outputs AS
            SELECT transactions.txid                   AS txid,
                   2                                   AS output_pool,
                   sapling_received_notes.output_index AS output_index,
                   sent_notes.from_account_id          AS from_account_id,
                   sapling_received_notes.account_id   AS to_account_id,
                   NULL                                AS to_address,
                   sapling_received_notes.value        AS value,
                   sapling_received_notes.is_change    AS is_change,
                   sapling_received_notes.memo         AS memo
            FROM sapling_received_notes
            JOIN transactions
                 ON transactions.id_tx = sapling_received_notes.tx
            LEFT JOIN sent_notes
                      ON (sent_notes.tx, sent_notes.output_pool, sent_notes.output_index) =
                         (sapling_received_notes.tx, 2, sent_notes.output_index)
            UNION
            SELECT utxos.prevout_txid           AS txid,
                   0                            AS output_pool,
                   utxos.prevout_idx            AS output_index,
                   NULL                         AS from_account_id,
                   utxos.received_by_account_id AS to_account_id,
                   utxos.address                AS to_address,
                   utxos.value_zat              AS value,
                   0                            AS is_change,
                   NULL                         AS memo
            FROM utxos
            UNION
            SELECT transactions.txid                 AS txid,
                   sent_notes.output_pool            AS output_pool,
                   sent_notes.output_index           AS output_index,
                   sent_notes.from_account_id        AS from_account_id,
                   sapling_received_notes.account_id AS to_account_id,
                   sent_notes.to_address             AS to_address,
                   sent_notes.value                  AS value,
                   0                                 AS is_change,
                   sent_notes.memo                   AS memo
            FROM sent_notes
            JOIN transactions
                 ON transactions.id_tx = sent_notes.tx
            LEFT JOIN sapling_received_notes
                      ON (sent_notes.tx, sent_notes.output_pool, sent_notes.output_index) =
                         (sapling_received_notes.tx, 2, sapling_received_notes.output_index)
            WHERE COALESCE(sapling_received_notes.is_change, 0) = 0;
        ")?;

        Ok(())
    }

    fn down(&self, _transaction: &Transaction) -> Result<(), WalletMigrationError> {
        Err(WalletMigrationError::CannotRevert(MIGRATION_ID))
    }
}

#[cfg(test)]
mod tests {
    use crate::wallet::init::migrations::tests::test_migrate;

    #[test]
    fn migrate() {
        test_migrate(&[super::MIGRATION_ID]);
    }
}