harmoniis-wallet 0.1.111

Smart-contract wallet for the Harmoniis marketplace for agents and robots (RGB contracts, Witness-backed bearer state, Webcash fees)
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
//! SQLite implementation of [`HarmoniiStore`].
//!
//! Uses two connections: `master_conn` for metadata, PGP identities,
//! wallet slots, and payment audit tables; `rgb_conn` for contracts
//! and certificates (identity store).

use std::path::{Path, PathBuf};

use rusqlite::{params, Connection};

use super::schema::{
    ensure_default_pgp_identity, ensure_root_and_identity_materialized, migrate_identity_schema,
    migrate_identity_schema_if_present, migrate_rgb_state, row_to_contract, same_path,
    table_exists,
};
use super::store::*;
use crate::error::{Error, Result};
use crate::types::{Certificate, Contract};

const MASTER_DB_FILENAME: &str = "master.db";
const LEGACY_RGB_DB: &str = "rgb.db";
const LEGACY_WALLET_DB: &str = "wallet.db";
const RGB_SHARD_DIR: &str = "identities";

struct WalletDiskPaths {
    base_dir: PathBuf,
    master_path: PathBuf,
    rgb_path: PathBuf,
    wallet_migration_path: PathBuf,
}

/// SQLite-backed storage for the Harmoniis wallet engine.
pub struct SqliteHarmoniiStore {
    master_conn: Connection,
    rgb_conn: Connection,
}

// ── Constructors + schema init ──────────────────────────────────

impl SqliteHarmoniiStore {
    fn resolve_disk_paths(path: &Path) -> Result<WalletDiskPaths> {
        let normalized = if path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or_default()
            .is_empty()
        {
            PathBuf::from(MASTER_DB_FILENAME)
        } else {
            path.to_path_buf()
        };
        let file_name = normalized
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(MASTER_DB_FILENAME);
        let base_dir = normalized
            .parent()
            .map(ToOwned::to_owned)
            .unwrap_or_else(|| PathBuf::from("."));
        let master_path = if file_name.eq_ignore_ascii_case(MASTER_DB_FILENAME) {
            normalized.clone()
        } else if file_name.eq_ignore_ascii_case(LEGACY_RGB_DB)
            || file_name.eq_ignore_ascii_case("main_rgb.db")
            || file_name.eq_ignore_ascii_case(LEGACY_WALLET_DB)
        {
            base_dir.join(MASTER_DB_FILENAME)
        } else {
            normalized.clone()
        };
        let canonical_rgb = base_dir.join("main_rgb.db");
        let legacy_rgb = base_dir.join(LEGACY_RGB_DB);
        let rgb_path = if canonical_rgb.exists() {
            canonical_rgb
        } else if legacy_rgb.exists() {
            legacy_rgb
        } else {
            canonical_rgb
        };
        Ok(WalletDiskPaths {
            base_dir: base_dir.clone(),
            master_path,
            rgb_path,
            wallet_migration_path: base_dir.join(LEGACY_WALLET_DB),
        })
    }

    fn init_master_schema(conn: &Connection, allow_generate: bool) -> Result<()> {
        conn.execute_batch(
            "
            PRAGMA journal_mode=WAL;
            PRAGMA foreign_keys=ON;

            CREATE TABLE IF NOT EXISTS wallet_metadata (
                key   TEXT PRIMARY KEY,
                value TEXT NOT NULL
            );

            CREATE TABLE IF NOT EXISTS pgp_identities (
                label           TEXT PRIMARY KEY,
                key_index       INTEGER NOT NULL UNIQUE,
                private_key_hex TEXT NOT NULL,
                public_key_hex  TEXT NOT NULL,
                created_at      TEXT NOT NULL,
                is_active       INTEGER NOT NULL DEFAULT 0
            );

            CREATE TABLE IF NOT EXISTS wallet_slots (
                family      TEXT NOT NULL,
                slot_index  INTEGER NOT NULL,
                descriptor  TEXT NOT NULL,
                db_rel_path TEXT,
                label       TEXT,
                created_at  TEXT NOT NULL,
                updated_at  TEXT NOT NULL,
                PRIMARY KEY (family, slot_index)
            );

            CREATE TABLE IF NOT EXISTS payment_attempts (
                attempt_id        TEXT PRIMARY KEY,
                created_at        TEXT NOT NULL,
                updated_at        TEXT NOT NULL,
                service_origin    TEXT NOT NULL,
                endpoint_path     TEXT NOT NULL,
                method            TEXT NOT NULL,
                rail              TEXT NOT NULL,
                action_hint       TEXT NOT NULL,
                required_amount   TEXT NOT NULL,
                payment_unit      TEXT NOT NULL,
                payment_reference TEXT,
                request_hash      TEXT NOT NULL,
                response_status   INTEGER,
                response_code     TEXT,
                response_body     TEXT,
                recovery_state    TEXT NOT NULL,
                final_state       TEXT NOT NULL
            );

            CREATE TABLE IF NOT EXISTS payment_losses (
                loss_id            TEXT PRIMARY KEY,
                attempt_id         TEXT NOT NULL,
                created_at         TEXT NOT NULL,
                service_origin     TEXT NOT NULL,
                endpoint_path      TEXT NOT NULL,
                method             TEXT NOT NULL,
                rail               TEXT NOT NULL,
                amount             TEXT NOT NULL,
                payment_reference  TEXT,
                failure_stage      TEXT NOT NULL,
                response_status    INTEGER,
                response_code      TEXT,
                response_body      TEXT
            );

            CREATE TABLE IF NOT EXISTS payment_blacklist (
                service_origin      TEXT NOT NULL,
                endpoint_path       TEXT NOT NULL,
                method              TEXT NOT NULL,
                rail                TEXT NOT NULL,
                blacklisted_until   TEXT,
                reason              TEXT NOT NULL,
                triggered_by_loss_id TEXT,
                created_at          TEXT NOT NULL,
                updated_at          TEXT NOT NULL,
                PRIMARY KEY (service_origin, endpoint_path, method, rail)
            );

            CREATE TABLE IF NOT EXISTS payment_transactions (
                txn_id          TEXT PRIMARY KEY,
                attempt_id      TEXT,
                created_at      TEXT NOT NULL,
                updated_at      TEXT NOT NULL,
                occurred_at     TEXT NOT NULL,
                direction       TEXT NOT NULL,
                role            TEXT NOT NULL,
                source_system   TEXT NOT NULL,
                service_origin  TEXT,
                frontend_kind   TEXT,
                transport_kind  TEXT,
                endpoint_path   TEXT,
                method          TEXT,
                session_id      TEXT,
                action_kind     TEXT NOT NULL,
                resource_ref    TEXT,
                contract_ref    TEXT,
                invoice_ref     TEXT,
                challenge_id    TEXT,
                rail            TEXT NOT NULL,
                payment_unit    TEXT NOT NULL,
                quoted_amount   TEXT,
                settled_amount  TEXT,
                fee_amount      TEXT,
                proof_ref       TEXT,
                proof_kind      TEXT,
                payer_ref       TEXT,
                payee_ref       TEXT,
                request_hash    TEXT,
                response_code   TEXT,
                status          TEXT NOT NULL,
                metadata_json   TEXT
            );

            CREATE TABLE IF NOT EXISTS payment_transaction_events (
                event_id       TEXT PRIMARY KEY,
                txn_id         TEXT NOT NULL,
                created_at     TEXT NOT NULL,
                event_type     TEXT NOT NULL,
                status         TEXT NOT NULL,
                actor          TEXT NOT NULL,
                details_json   TEXT NOT NULL DEFAULT '{}'
            );

            CREATE INDEX IF NOT EXISTS idx_payment_transactions_created_at
                ON payment_transactions(created_at DESC);
            CREATE INDEX IF NOT EXISTS idx_payment_transactions_status
                ON payment_transactions(status, created_at DESC);
            CREATE INDEX IF NOT EXISTS idx_payment_transactions_direction
                ON payment_transactions(direction, created_at DESC);
            CREATE INDEX IF NOT EXISTS idx_payment_transactions_action_kind
                ON payment_transactions(action_kind, created_at DESC);
            CREATE UNIQUE INDEX IF NOT EXISTS idx_payment_transactions_proof_ref
                ON payment_transactions(direction, rail, proof_ref)
                WHERE proof_ref IS NOT NULL AND proof_ref != '';
            CREATE INDEX IF NOT EXISTS idx_payment_transaction_events_txn_id
                ON payment_transaction_events(txn_id, created_at ASC);
            ",
        )?;
        ensure_root_and_identity_materialized(conn, allow_generate)?;
        ensure_default_pgp_identity(conn)?;
        Ok(())
    }

    fn init_identity_schema(conn: &Connection) -> Result<()> {
        conn.execute_batch(
            "
            PRAGMA journal_mode=WAL;
            PRAGMA foreign_keys=ON;

            CREATE TABLE IF NOT EXISTS contracts (
                contract_id        TEXT PRIMARY KEY,
                contract_type      TEXT NOT NULL DEFAULT 'service',
                status             TEXT NOT NULL DEFAULT 'issued',
                witness_secret     TEXT,
                witness_proof      TEXT,
                amount_units       INTEGER NOT NULL DEFAULT 0,
                work_spec          TEXT NOT NULL DEFAULT '',
                buyer_fingerprint  TEXT NOT NULL DEFAULT '',
                seller_fingerprint TEXT,
                reference_post     TEXT,
                delivery_deadline  TEXT,
                role               TEXT NOT NULL DEFAULT 'buyer',
                delivered_text     TEXT,
                certificate_id     TEXT,
                created_at         TEXT NOT NULL,
                updated_at         TEXT NOT NULL
            );

            CREATE TABLE IF NOT EXISTS certificates (
                certificate_id  TEXT PRIMARY KEY,
                contract_id     TEXT,
                witness_secret  TEXT,
                witness_proof   TEXT,
                created_at      TEXT NOT NULL
            );

            CREATE TABLE IF NOT EXISTS timeline_posts (
                post_id      TEXT PRIMARY KEY,
                created_at   TEXT NOT NULL,
                updated_at   TEXT NOT NULL,
                metadata_json TEXT NOT NULL DEFAULT '{}'
            );

            CREATE TABLE IF NOT EXISTS timeline_comments (
                comment_id    TEXT PRIMARY KEY,
                post_id       TEXT NOT NULL DEFAULT '',
                created_at    TEXT NOT NULL,
                updated_at    TEXT NOT NULL,
                metadata_json TEXT NOT NULL DEFAULT '{}'
            );

            CREATE TABLE IF NOT EXISTS timeline_bids (
                bid_post_id    TEXT PRIMARY KEY,
                contract_id    TEXT NOT NULL DEFAULT '',
                service_post_id TEXT NOT NULL DEFAULT '',
                created_at     TEXT NOT NULL,
                metadata_json  TEXT NOT NULL DEFAULT '{}'
            );
            ",
        )?;
        migrate_identity_schema(conn)?;
        Ok(())
    }

    fn import_previous_layout(paths: &WalletDiskPaths) -> Result<()> {
        let source_path = if paths.rgb_path.exists() {
            paths.rgb_path.clone()
        } else if paths.wallet_migration_path.exists() {
            paths.wallet_migration_path.clone()
        } else {
            return Err(Error::NotFound("no wallet data source found".to_string()));
        };

        let source_conn = Connection::open(&source_path)?;
        migrate_identity_schema_if_present(&source_conn)?;

        let master_conn = Connection::open(&paths.master_path)?;
        Self::init_master_schema(&master_conn, true)?;

        if table_exists(&source_conn, "wallet_metadata")? {
            let mut stmt = source_conn.prepare("SELECT key, value FROM wallet_metadata")?;
            let rows = stmt.query_map([], |row| {
                Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
            })?;
            for row in rows {
                let (key, value) = row?;
                master_conn.execute(
                    "INSERT OR REPLACE INTO wallet_metadata (key, value) VALUES (?1, ?2)",
                    params![key, value],
                )?;
            }
        }

        if table_exists(&source_conn, "pgp_identities")? {
            let mut stmt = source_conn.prepare(
                "SELECT label, key_index, private_key_hex, public_key_hex, created_at, is_active
                 FROM pgp_identities",
            )?;
            let rows = stmt.query_map([], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, String>(2)?,
                    row.get::<_, String>(3)?,
                    row.get::<_, String>(4)?,
                    row.get::<_, i64>(5)?,
                ))
            })?;
            master_conn.execute("DELETE FROM pgp_identities", [])?;
            for row in rows {
                let (label, key_index, private_key_hex, public_key_hex, created_at, is_active) =
                    row?;
                master_conn.execute(
                    "INSERT OR REPLACE INTO pgp_identities
                     (label, key_index, private_key_hex, public_key_hex, created_at, is_active)
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                    params![label, key_index, private_key_hex, public_key_hex, created_at, is_active],
                )?;
            }
        }
        ensure_root_and_identity_materialized(&master_conn, true)?;
        ensure_default_pgp_identity(&master_conn)?;
        drop(master_conn);

        let rgb_conn = Connection::open(&paths.rgb_path)?;
        Self::init_identity_schema(&rgb_conn)?;
        if !same_path(&source_path, &paths.rgb_path) {
            migrate_rgb_state(&source_conn, &rgb_conn)?;
        }
        Self::merge_sharded_rgb_data(&paths.base_dir, &rgb_conn)?;
        Ok(())
    }

    fn merge_sharded_rgb_data(base_dir: &Path, rgb_conn: &Connection) -> Result<()> {
        let shard_dir = base_dir.join(RGB_SHARD_DIR);
        if !shard_dir.exists() {
            return Ok(());
        }
        for entry in std::fs::read_dir(&shard_dir)
            .map_err(|e| Error::Other(anyhow::anyhow!("cannot read rgb shard dir: {e}")))?
        {
            let entry = entry
                .map_err(|e| Error::Other(anyhow::anyhow!("cannot read rgb shard entry: {e}")))?;
            let path = entry.path();
            let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
                continue;
            };
            if !name.starts_with("identity-") || !name.ends_with(".db") {
                continue;
            }
            let shard_conn = Connection::open(&path)?;
            migrate_identity_schema_if_present(&shard_conn)?;
            migrate_rgb_state(&shard_conn, rgb_conn)?;
        }
        Ok(())
    }

    fn open_from_disk(path: &Path, allow_create: bool) -> Result<Self> {
        let paths = Self::resolve_disk_paths(path)?;
        std::fs::create_dir_all(&paths.base_dir)
            .map_err(|e| Error::Other(anyhow::anyhow!("cannot create wallet dir: {e}")))?;

        if !paths.master_path.exists() {
            if paths.rgb_path.exists() || paths.wallet_migration_path.exists() {
                Self::import_previous_layout(&paths)?;
            } else if !allow_create {
                return Err(Error::NotFound(format!(
                    "master wallet database not found at {}",
                    paths.master_path.display()
                )));
            }
        }

        let master_conn = Connection::open(&paths.master_path)?;
        Self::init_master_schema(&master_conn, allow_create)?;
        let rgb_conn = Connection::open(&paths.rgb_path)?;
        Self::init_identity_schema(&rgb_conn)?;
        Self::merge_sharded_rgb_data(&paths.base_dir, &rgb_conn)?;
        Ok(Self {
            master_conn,
            rgb_conn,
        })
    }

    /// Create a new store at the given path, generating fresh key material.
    pub fn create(path: &Path) -> Result<Self> {
        Self::open_from_disk(path, true)
    }

    /// Open an existing store at the given path.
    pub fn open(path: &Path) -> Result<Self> {
        Self::open_from_disk(path, false)
    }

    /// Open an in-memory store (for tests).
    pub fn open_memory() -> Result<Self> {
        let master_conn = Connection::open_in_memory()?;
        Self::init_master_schema(&master_conn, true)?;
        let rgb_conn = Connection::open_in_memory()?;
        Self::init_identity_schema(&rgb_conn)?;
        Ok(Self {
            master_conn,
            rgb_conn,
        })
    }

}

// ── HarmoniiStore implementation ────────────────────────────────

impl HarmoniiStore for SqliteHarmoniiStore {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    // ── Metadata ────────────────────────────────────────────────

    fn get_meta(&self, key: &str) -> Result<Option<String>> {
        let mut stmt = self
            .master_conn
            .prepare("SELECT value FROM wallet_metadata WHERE key = ?1")?;
        let mut rows = stmt.query(params![key])?;
        let Some(row) = rows.next()? else {
            return Ok(None);
        };
        Ok(Some(row.get(0)?))
    }

    fn set_meta(&self, key: &str, value: &str) -> Result<()> {
        self.master_conn.execute(
            "INSERT OR REPLACE INTO wallet_metadata (key, value) VALUES (?1, ?2)",
            params![key, value],
        )?;
        Ok(())
    }

    // ── PGP Identities ─────────────────────────────────────────

    fn list_pgp_raw(&self) -> Result<Vec<PgpIdentityRow>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT label, key_index, private_key_hex, public_key_hex, created_at, is_active
             FROM pgp_identities
             ORDER BY key_index ASC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(PgpIdentityRow {
                label: row.get(0)?,
                key_index: row.get(1)?,
                private_key_hex: row.get(2)?,
                public_key_hex: row.get(3)?,
                created_at: row.get(4)?,
                is_active: row.get::<_, i64>(5)? == 1,
            })
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn insert_pgp(&self, row: &PgpIdentityRow) -> Result<()> {
        self.master_conn.execute(
            "INSERT INTO pgp_identities (label, key_index, private_key_hex, public_key_hex, created_at, is_active)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                row.label,
                i64::from(row.key_index),
                row.private_key_hex,
                row.public_key_hex,
                row.created_at,
                if row.is_active { 1i64 } else { 0i64 },
            ],
        )?;
        Ok(())
    }

    fn rename_pgp(&self, from: &str, to: &str) -> Result<u64> {
        let changed = self.master_conn.execute(
            "UPDATE pgp_identities SET label = ?1 WHERE label = ?2",
            params![to, from],
        )?;
        Ok(changed as u64)
    }

    fn count_pgp_by_label(&self, label: &str) -> Result<i64> {
        let mut stmt = self
            .master_conn
            .prepare("SELECT COUNT(*) FROM pgp_identities WHERE label = ?1")?;
        let count: i64 = stmt.query_row(params![label], |row| row.get(0))?;
        Ok(count)
    }

    fn max_pgp_key_index(&self) -> Result<i64> {
        let mut stmt = self
            .master_conn
            .prepare("SELECT COALESCE(MAX(key_index), -1) FROM pgp_identities")?;
        let max: i64 = stmt.query_row([], |row| row.get(0))?;
        Ok(max)
    }

    fn pgp_index_for_label(&self, label: &str) -> Result<Option<u32>> {
        let mut stmt = self
            .master_conn
            .prepare("SELECT key_index FROM pgp_identities WHERE label = ?1 LIMIT 1")?;
        let mut rows = stmt.query(params![label])?;
        if let Some(row) = rows.next()? {
            let idx: u32 = row.get(0)?;
            Ok(Some(idx))
        } else {
            Ok(None)
        }
    }

    fn replace_all_pgp(&self, rows: &[PgpIdentityRow]) -> Result<()> {
        let tx = self.master_conn.unchecked_transaction()?;
        tx.execute("DELETE FROM pgp_identities", [])?;
        for row in rows {
            tx.execute(
                "INSERT INTO pgp_identities (label, key_index, private_key_hex, public_key_hex, created_at, is_active)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                params![
                    row.label,
                    i64::from(row.key_index),
                    row.private_key_hex,
                    row.public_key_hex,
                    row.created_at,
                    if row.is_active { 1i64 } else { 0i64 },
                ],
            )?;
        }
        if !rows.is_empty() && !rows.iter().any(|r| r.is_active) {
            tx.execute(
                "UPDATE pgp_identities SET is_active = 1 WHERE key_index = (
                    SELECT MIN(key_index) FROM pgp_identities
                )",
                [],
            )?;
        }
        tx.commit()?;
        Ok(())
    }

    fn replace_pgp_at(
        &self,
        key_index: u32,
        label: &str,
        row: &PgpIdentityRow,
        set_active: bool,
    ) -> Result<()> {
        let tx = self.master_conn.unchecked_transaction()?;
        tx.execute(
            "DELETE FROM pgp_identities WHERE key_index = ?1",
            params![i64::from(key_index)],
        )?;
        tx.execute(
            "DELETE FROM pgp_identities WHERE label = ?1",
            params![label],
        )?;
        if set_active {
            tx.execute("UPDATE pgp_identities SET is_active = 0", [])?;
        }
        let active_flag = if set_active {
            1i64
        } else if row.is_active {
            1i64
        } else {
            0i64
        };
        tx.execute(
            "INSERT INTO pgp_identities (label, key_index, private_key_hex, public_key_hex, created_at, is_active)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                row.label,
                i64::from(row.key_index),
                row.private_key_hex,
                row.public_key_hex,
                row.created_at,
                active_flag,
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    fn activate_pgp_exclusive(&self, label: &str) -> Result<()> {
        let tx = self.master_conn.unchecked_transaction()?;
        tx.execute("UPDATE pgp_identities SET is_active = 0", [])?;
        let changed = tx.execute(
            "UPDATE pgp_identities SET is_active = 1 WHERE label = ?1",
            params![label],
        )?;
        if changed == 0 {
            return Err(Error::NotFound(format!(
                "PGP identity label '{label}' not found"
            )));
        }
        tx.commit()?;
        Ok(())
    }

    // ── Wallet Slots ────────────────────────────────────────────

    fn list_wallet_slots(&self, family: Option<&str>) -> Result<Vec<WalletSlotRecord>> {
        let sql = if family.is_some() {
            "SELECT family, slot_index, descriptor, db_rel_path, label, created_at, updated_at
             FROM wallet_slots
             WHERE family = ?1
             ORDER BY family ASC, slot_index ASC"
        } else {
            "SELECT family, slot_index, descriptor, db_rel_path, label, created_at, updated_at
             FROM wallet_slots
             ORDER BY family ASC, slot_index ASC"
        };
        let mut stmt = self.master_conn.prepare(sql)?;
        let mapper = |row: &rusqlite::Row<'_>| -> rusqlite::Result<WalletSlotRecord> {
            Ok(WalletSlotRecord {
                family: row.get(0)?,
                slot_index: row.get(1)?,
                descriptor: row.get(2)?,
                db_rel_path: row.get(3)?,
                label: row.get(4)?,
                created_at: row.get(5)?,
                updated_at: row.get(6)?,
            })
        };
        let rows = match family {
            Some(name) => stmt.query_map(params![name], mapper)?,
            None => stmt.query_map([], mapper)?,
        };
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn upsert_wallet_slot(&self, row: &WalletSlotRecord) -> Result<()> {
        self.master_conn.execute(
            "INSERT OR REPLACE INTO wallet_slots
                (family, slot_index, descriptor, db_rel_path, label, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5,
                COALESCE((SELECT created_at FROM wallet_slots WHERE family=?1 AND slot_index=?2), ?6), ?7)",
            params![
                row.family,
                i64::from(row.slot_index),
                row.descriptor,
                row.db_rel_path,
                row.label,
                row.created_at,
                row.updated_at,
            ],
        )?;
        Ok(())
    }

    fn get_slot_index_by_label(&self, family: &str, label: &str) -> Result<Option<u32>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT slot_index FROM wallet_slots WHERE family = ?1 AND label = ?2 LIMIT 1",
        )?;
        let mut rows = stmt.query(params![family, label])?;
        if let Some(row) = rows.next()? {
            let idx: u32 = row.get(0)?;
            Ok(Some(idx))
        } else {
            Ok(None)
        }
    }

    fn max_slot_index(&self, family: &str) -> Result<i64> {
        let mut stmt = self
            .master_conn
            .prepare("SELECT COALESCE(MAX(slot_index), -1) FROM wallet_slots WHERE family = ?1")?;
        let max: i64 = stmt.query_row(params![family], |row| row.get(0))?;
        Ok(max)
    }

    fn replace_slot_at(
        &self,
        family: &str,
        slot_index: u32,
        label: &str,
        descriptor: &str,
        now: &str,
    ) -> Result<()> {
        let tx = self.master_conn.unchecked_transaction()?;
        tx.execute(
            "DELETE FROM wallet_slots WHERE family = ?1 AND slot_index = ?2",
            params![family, i64::from(slot_index)],
        )?;
        tx.execute(
            "DELETE FROM wallet_slots WHERE family = ?1 AND label = ?2",
            params![family, label],
        )?;
        tx.execute(
            "INSERT INTO wallet_slots (family, slot_index, descriptor, db_rel_path, label, created_at, updated_at)
             VALUES (?1, ?2, ?3, NULL, ?4, ?5, ?5)",
            params![family, i64::from(slot_index), descriptor, label, now],
        )?;
        tx.commit()?;
        Ok(())
    }

    // ── Payment Attempts ────────────────────────────────────────

    fn insert_payment_attempt(&self, record: &PaymentAttemptRecord) -> Result<()> {
        self.master_conn.execute(
            "INSERT INTO payment_attempts (
                attempt_id, created_at, updated_at, service_origin, endpoint_path,
                method, rail, action_hint, required_amount, payment_unit,
                payment_reference, request_hash, response_status, response_code,
                response_body, recovery_state, final_state
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17)",
            params![
                record.attempt_id,
                record.created_at,
                record.updated_at,
                record.service_origin,
                record.endpoint_path,
                record.method,
                record.rail,
                record.action_hint,
                record.required_amount,
                record.payment_unit,
                record.payment_reference,
                record.request_hash,
                record.response_status.map(i64::from),
                record.response_code,
                record.response_body,
                record.recovery_state,
                record.final_state,
            ],
        )?;
        Ok(())
    }

    fn update_payment_attempt(
        &self,
        attempt_id: &str,
        now: &str,
        update: &PaymentAttemptUpdate<'_>,
    ) -> Result<()> {
        self.master_conn.execute(
            "UPDATE payment_attempts
             SET updated_at = ?2,
                 payment_reference = COALESCE(?3, payment_reference),
                 response_status = ?4,
                 response_code = ?5,
                 response_body = ?6,
                 recovery_state = ?7,
                 final_state = ?8
             WHERE attempt_id = ?1",
            params![
                attempt_id,
                now,
                update.payment_reference,
                update.response_status.map(i64::from),
                update.response_code,
                update.response_body,
                update.recovery_state,
                update.final_state,
            ],
        )?;
        Ok(())
    }

    // ── Payment Losses ──────────────────────────────────────────

    fn insert_payment_loss(&self, record: &PaymentLossRecord) -> Result<()> {
        self.master_conn.execute(
            "INSERT INTO payment_losses (
                loss_id, attempt_id, created_at, service_origin, endpoint_path,
                method, rail, amount, payment_reference, failure_stage,
                response_status, response_code, response_body
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)",
            params![
                record.loss_id,
                record.attempt_id,
                record.created_at,
                record.service_origin,
                record.endpoint_path,
                record.method,
                record.rail,
                record.amount,
                record.payment_reference,
                record.failure_stage,
                record.response_status.map(i64::from),
                record.response_code,
                record.response_body,
            ],
        )?;
        Ok(())
    }

    fn list_payment_losses(&self) -> Result<Vec<PaymentLossRecord>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT loss_id, attempt_id, created_at, service_origin, endpoint_path,
                    method, rail, amount, payment_reference, failure_stage,
                    response_status, response_code, response_body
             FROM payment_losses
             ORDER BY created_at DESC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(PaymentLossRecord {
                loss_id: row.get(0)?,
                attempt_id: row.get(1)?,
                created_at: row.get(2)?,
                service_origin: row.get(3)?,
                endpoint_path: row.get(4)?,
                method: row.get(5)?,
                rail: row.get(6)?,
                amount: row.get(7)?,
                payment_reference: row.get(8)?,
                failure_stage: row.get(9)?,
                response_status: row
                    .get::<_, Option<i64>>(10)?
                    .and_then(|v| u16::try_from(v).ok()),
                response_code: row.get(11)?,
                response_body: row.get(12)?,
            })
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn count_recent_losses(
        &self,
        service_origin: &str,
        endpoint_path: &str,
        method: &str,
        rail: &str,
        cutoff: &str,
    ) -> Result<i64> {
        let mut stmt = self.master_conn.prepare(
            "SELECT COUNT(*)
             FROM payment_losses
             WHERE service_origin = ?1
               AND endpoint_path = ?2
               AND method = ?3
               AND rail = ?4
               AND created_at >= ?5",
        )?;
        let count: i64 = stmt.query_row(
            params![service_origin, endpoint_path, method, rail, cutoff],
            |row| row.get(0),
        )?;
        Ok(count)
    }

    // ── Payment Blacklist ───────────────────────────────────────

    fn upsert_payment_blacklist(&self, record: &PaymentBlacklistRecord) -> Result<()> {
        self.master_conn.execute(
            "INSERT OR REPLACE INTO payment_blacklist (
                service_origin, endpoint_path, method, rail, blacklisted_until,
                reason, triggered_by_loss_id, created_at, updated_at
             ) VALUES (
                ?1, ?2, ?3, ?4, ?5, ?6, ?7,
                COALESCE((SELECT created_at FROM payment_blacklist
                          WHERE service_origin = ?1 AND endpoint_path = ?2
                            AND method = ?3 AND rail = ?4), ?8),
                ?8
             )",
            params![
                record.service_origin,
                record.endpoint_path,
                record.method,
                record.rail,
                record.blacklisted_until,
                record.reason,
                record.triggered_by_loss_id,
                record.updated_at,
            ],
        )?;
        Ok(())
    }

    fn get_payment_blacklist_entry(
        &self,
        service_origin: &str,
        endpoint_path: &str,
        method: &str,
        rail: &str,
    ) -> Result<Option<PaymentBlacklistRecord>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT service_origin, endpoint_path, method, rail,
                    blacklisted_until, reason, triggered_by_loss_id, created_at, updated_at
             FROM payment_blacklist
             WHERE service_origin = ?1 AND endpoint_path = ?2 AND method = ?3 AND rail = ?4",
        )?;
        let mut rows = stmt.query(params![service_origin, endpoint_path, method, rail])?;
        let Some(row) = rows.next()? else {
            return Ok(None);
        };
        Ok(Some(PaymentBlacklistRecord {
            service_origin: row.get(0)?,
            endpoint_path: row.get(1)?,
            method: row.get(2)?,
            rail: row.get(3)?,
            blacklisted_until: row.get(4)?,
            reason: row.get(5)?,
            triggered_by_loss_id: row.get(6)?,
            created_at: row.get(7)?,
            updated_at: row.get(8)?,
        }))
    }

    fn list_payment_blacklist(&self) -> Result<Vec<PaymentBlacklistRecord>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT service_origin, endpoint_path, method, rail,
                    blacklisted_until, reason, triggered_by_loss_id, created_at, updated_at
             FROM payment_blacklist
             ORDER BY updated_at DESC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(PaymentBlacklistRecord {
                service_origin: row.get(0)?,
                endpoint_path: row.get(1)?,
                method: row.get(2)?,
                rail: row.get(3)?,
                blacklisted_until: row.get(4)?,
                reason: row.get(5)?,
                triggered_by_loss_id: row.get(6)?,
                created_at: row.get(7)?,
                updated_at: row.get(8)?,
            })
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn delete_payment_blacklist(
        &self,
        service_origin: &str,
        endpoint_path: &str,
        method: &str,
        rail: &str,
    ) -> Result<bool> {
        let changed = self.master_conn.execute(
            "DELETE FROM payment_blacklist
             WHERE service_origin = ?1 AND endpoint_path = ?2 AND method = ?3 AND rail = ?4",
            params![service_origin, endpoint_path, method, rail],
        )?;
        Ok(changed > 0)
    }

    // ── Payment Transactions ────────────────────────────────────

    fn insert_payment_transaction(&self, record: &PaymentTransactionRecord) -> Result<()> {
        self.master_conn.execute(
            "INSERT INTO payment_transactions (
                txn_id, attempt_id, created_at, updated_at, occurred_at, direction, role,
                source_system, service_origin, frontend_kind, transport_kind, endpoint_path,
                method, session_id, action_kind, resource_ref, contract_ref, invoice_ref,
                challenge_id, rail, payment_unit, quoted_amount, settled_amount, fee_amount,
                proof_ref, proof_kind, payer_ref, payee_ref, request_hash, response_code,
                status, metadata_json
             ) VALUES (
                ?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
             )",
            params![
                record.txn_id,
                record.attempt_id,
                record.created_at,
                record.updated_at,
                record.occurred_at,
                record.direction,
                record.role,
                record.source_system,
                record.service_origin,
                record.frontend_kind,
                record.transport_kind,
                record.endpoint_path,
                record.method,
                record.session_id,
                record.action_kind,
                record.resource_ref,
                record.contract_ref,
                record.invoice_ref,
                record.challenge_id,
                record.rail,
                record.payment_unit,
                record.quoted_amount,
                record.settled_amount,
                record.fee_amount,
                record.proof_ref,
                record.proof_kind,
                record.payer_ref,
                record.payee_ref,
                record.request_hash,
                record.response_code,
                record.status,
                record.metadata_json,
            ],
        )?;
        Ok(())
    }

    fn update_payment_transaction(
        &self,
        txn_id: &str,
        now: &str,
        update: &PaymentTransactionUpdate<'_>,
    ) -> Result<()> {
        self.master_conn.execute(
            "UPDATE payment_transactions
             SET updated_at = ?2,
                 occurred_at = COALESCE(?3, occurred_at),
                 service_origin = COALESCE(?4, service_origin),
                 frontend_kind = COALESCE(?5, frontend_kind),
                 transport_kind = COALESCE(?6, transport_kind),
                 endpoint_path = COALESCE(?7, endpoint_path),
                 method = COALESCE(?8, method),
                 session_id = COALESCE(?9, session_id),
                 action_kind = COALESCE(?10, action_kind),
                 resource_ref = COALESCE(?11, resource_ref),
                 contract_ref = COALESCE(?12, contract_ref),
                 invoice_ref = COALESCE(?13, invoice_ref),
                 challenge_id = COALESCE(?14, challenge_id),
                 quoted_amount = COALESCE(?15, quoted_amount),
                 settled_amount = COALESCE(?16, settled_amount),
                 fee_amount = COALESCE(?17, fee_amount),
                 proof_ref = COALESCE(?18, proof_ref),
                 proof_kind = COALESCE(?19, proof_kind),
                 payer_ref = COALESCE(?20, payer_ref),
                 payee_ref = COALESCE(?21, payee_ref),
                 request_hash = COALESCE(?22, request_hash),
                 response_code = COALESCE(?23, response_code),
                 status = ?24,
                 metadata_json = COALESCE(?25, metadata_json)
             WHERE txn_id = ?1",
            params![
                txn_id,
                now,
                update.occurred_at,
                update.service_origin,
                update.frontend_kind,
                update.transport_kind,
                update.endpoint_path,
                update.method,
                update.session_id,
                update.action_kind,
                update.resource_ref,
                update.contract_ref,
                update.invoice_ref,
                update.challenge_id,
                update.quoted_amount,
                update.settled_amount,
                update.fee_amount,
                update.proof_ref,
                update.proof_kind,
                update.payer_ref,
                update.payee_ref,
                update.request_hash,
                update.response_code,
                update.status,
                update.metadata_json,
            ],
        )?;
        Ok(())
    }

    fn list_payment_transactions(&self) -> Result<Vec<PaymentTransactionRecord>> {
        let mut stmt = self.master_conn.prepare(
            "SELECT txn_id, attempt_id, created_at, updated_at, occurred_at, direction, role,
                    source_system, service_origin, frontend_kind, transport_kind, endpoint_path,
                    method, session_id, action_kind, resource_ref, contract_ref, invoice_ref,
                    challenge_id, rail, payment_unit, quoted_amount, settled_amount, fee_amount,
                    proof_ref, proof_kind, payer_ref, payee_ref, request_hash, response_code,
                    status, metadata_json
             FROM payment_transactions
             ORDER BY occurred_at DESC, created_at DESC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(PaymentTransactionRecord {
                txn_id: row.get(0)?,
                attempt_id: row.get(1)?,
                created_at: row.get(2)?,
                updated_at: row.get(3)?,
                occurred_at: row.get(4)?,
                direction: row.get(5)?,
                role: row.get(6)?,
                source_system: row.get(7)?,
                service_origin: row.get(8)?,
                frontend_kind: row.get(9)?,
                transport_kind: row.get(10)?,
                endpoint_path: row.get(11)?,
                method: row.get(12)?,
                session_id: row.get(13)?,
                action_kind: row.get(14)?,
                resource_ref: row.get(15)?,
                contract_ref: row.get(16)?,
                invoice_ref: row.get(17)?,
                challenge_id: row.get(18)?,
                rail: row.get(19)?,
                payment_unit: row.get(20)?,
                quoted_amount: row.get(21)?,
                settled_amount: row.get(22)?,
                fee_amount: row.get(23)?,
                proof_ref: row.get(24)?,
                proof_kind: row.get(25)?,
                payer_ref: row.get(26)?,
                payee_ref: row.get(27)?,
                request_hash: row.get(28)?,
                response_code: row.get(29)?,
                status: row.get(30)?,
                metadata_json: row.get(31)?,
            })
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    // ── Payment Transaction Events ──────────────────────────────

    fn insert_payment_transaction_event(
        &self,
        record: &PaymentTransactionEventRecord,
    ) -> Result<()> {
        self.master_conn.execute(
            "INSERT INTO payment_transaction_events (
                event_id, txn_id, created_at, event_type, status, actor, details_json
             ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            params![
                record.event_id,
                record.txn_id,
                record.created_at,
                record.event_type,
                record.status,
                record.actor,
                record.details_json.as_deref().unwrap_or("{}"),
            ],
        )?;
        Ok(())
    }

    fn list_payment_transaction_events(
        &self,
        txn_id: Option<&str>,
    ) -> Result<Vec<PaymentTransactionEventRecord>> {
        let sql = if txn_id.is_some() {
            "SELECT event_id, txn_id, created_at, event_type, status, actor, details_json
             FROM payment_transaction_events
             WHERE txn_id = ?1
             ORDER BY created_at ASC"
        } else {
            "SELECT event_id, txn_id, created_at, event_type, status, actor, details_json
             FROM payment_transaction_events
             ORDER BY created_at ASC"
        };
        let mut stmt = self.master_conn.prepare(sql)?;
        let mapper = |row: &rusqlite::Row<'_>| {
            Ok(PaymentTransactionEventRecord {
                event_id: row.get(0)?,
                txn_id: row.get(1)?,
                created_at: row.get(2)?,
                event_type: row.get(3)?,
                status: row.get(4)?,
                actor: row.get(5)?,
                details_json: row
                    .get::<_, Option<String>>(6)?
                    .filter(|value| !value.is_empty() && value != "{}"),
            })
        };
        let rows = match txn_id {
            Some(id) => stmt.query_map(params![id], mapper)?,
            None => stmt.query_map([], mapper)?,
        };
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    // ── Contracts (identity store / rgb_conn) ───────────────────

    fn store_contract(&self, c: &Contract) -> Result<()> {
        self.rgb_conn.execute(
            "INSERT OR REPLACE INTO contracts (
                contract_id, contract_type, status, witness_secret, witness_proof,
                amount_units, work_spec, buyer_fingerprint, seller_fingerprint,
                reference_post, delivery_deadline, role, delivered_text,
                certificate_id, created_at, updated_at
            ) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16)",
            params![
                c.contract_id,
                c.contract_type.as_str(),
                c.status.as_str(),
                c.witness_secret,
                c.witness_proof,
                c.amount_units as i64,
                c.work_spec,
                c.buyer_fingerprint,
                c.seller_fingerprint,
                c.reference_post,
                c.delivery_deadline,
                c.role.as_str(),
                c.delivered_text,
                c.certificate_id,
                c.created_at,
                c.updated_at,
            ],
        )?;
        Ok(())
    }

    fn get_contract(&self, id: &str) -> Result<Option<Contract>> {
        let mut stmt = self.rgb_conn.prepare(
            "SELECT contract_id, contract_type, status, witness_secret, witness_proof,
                    amount_units, work_spec, buyer_fingerprint, seller_fingerprint,
                    reference_post, delivery_deadline, role, delivered_text,
                    certificate_id, created_at, updated_at
             FROM contracts WHERE contract_id = ?1",
        )?;
        let mut rows = stmt.query(params![id])?;
        if let Some(row) = rows.next()? {
            Ok(Some(row_to_contract(row)?))
        } else {
            Ok(None)
        }
    }

    fn list_contracts(&self) -> Result<Vec<Contract>> {
        let mut stmt = self.rgb_conn.prepare(
            "SELECT contract_id, contract_type, status, witness_secret, witness_proof,
                    amount_units, work_spec, buyer_fingerprint, seller_fingerprint,
                    reference_post, delivery_deadline, role, delivered_text,
                    certificate_id, created_at, updated_at
             FROM contracts ORDER BY created_at DESC",
        )?;
        let rows = stmt.query_map([], |row| {
            row_to_contract(row)
                .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn count_contracts(&self) -> Result<i64> {
        let mut stmt = self.rgb_conn.prepare("SELECT COUNT(*) FROM contracts")?;
        let count: i64 = stmt.query_row([], |row| row.get(0))?;
        Ok(count)
    }

    // ── Certificates (identity store / rgb_conn) ────────────────

    fn store_certificate(&self, cert: &Certificate) -> Result<()> {
        self.rgb_conn.execute(
            "INSERT OR REPLACE INTO certificates (
                certificate_id, contract_id, witness_secret, witness_proof, created_at
            ) VALUES (?1,?2,?3,?4,?5)",
            params![
                cert.certificate_id,
                cert.contract_id,
                cert.witness_secret,
                cert.witness_proof,
                cert.created_at,
            ],
        )?;
        Ok(())
    }

    fn list_certificates(&self) -> Result<Vec<Certificate>> {
        let mut stmt = self.rgb_conn.prepare(
            "SELECT certificate_id, contract_id, witness_secret, witness_proof, created_at
             FROM certificates ORDER BY created_at DESC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(Certificate {
                certificate_id: row.get(0)?,
                contract_id: row.get(1)?,
                witness_secret: row.get(2)?,
                witness_proof: row.get(3)?,
                created_at: row.get(4)?,
            })
        })?;
        rows.collect::<std::result::Result<Vec<_>, _>>()
            .map_err(Error::Storage)
    }

    fn count_certificates(&self) -> Result<i64> {
        let mut stmt = self
            .rgb_conn
            .prepare("SELECT COUNT(*) FROM certificates")?;
        let count: i64 = stmt.query_row([], |row| row.get(0))?;
        Ok(count)
    }

    // ── Bulk Operations ─────────────────────────────────────────

    fn replace_identity_data(
        &self,
        contracts: &[Contract],
        certificates: &[Certificate],
    ) -> Result<()> {
        let tx = self.rgb_conn.unchecked_transaction()?;
        tx.execute("DELETE FROM contracts", [])?;
        tx.execute("DELETE FROM certificates", [])?;
        for c in contracts {
            tx.execute(
                "INSERT OR REPLACE INTO contracts (
                    contract_id, contract_type, status, witness_secret, witness_proof,
                    amount_units, work_spec, buyer_fingerprint, seller_fingerprint,
                    reference_post, delivery_deadline, role, delivered_text,
                    certificate_id, created_at, updated_at
                ) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16)",
                params![
                    c.contract_id,
                    c.contract_type.as_str(),
                    c.status.as_str(),
                    c.witness_secret,
                    c.witness_proof,
                    c.amount_units as i64,
                    c.work_spec,
                    c.buyer_fingerprint,
                    c.seller_fingerprint,
                    c.reference_post,
                    c.delivery_deadline,
                    c.role.as_str(),
                    c.delivered_text,
                    c.certificate_id,
                    c.created_at,
                    c.updated_at,
                ],
            )?;
        }
        for cert in certificates {
            tx.execute(
                "INSERT OR REPLACE INTO certificates (
                    certificate_id, contract_id, witness_secret, witness_proof, created_at
                ) VALUES (?1,?2,?3,?4,?5)",
                params![
                    cert.certificate_id,
                    cert.contract_id,
                    cert.witness_secret,
                    cert.witness_proof,
                    cert.created_at,
                ],
            )?;
        }
        tx.commit()?;
        Ok(())
    }
}