upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/wallet_sqlite.h
diff options
context:
space:
mode:
authorNsable-sh <nsable-sh@noreply.git.bitflash.network>2026-08-17 10:50:00 -0300
committerNsable-sh <nsable-sh@noreply.git.bitflash.network>2026-08-17 10:50:00 -0300
commita72fc5fa0c9811ff5a57af90bf4440023c94ac5a (patch)
treed511b288e1d0f555723114a9502c897b07cb048a /src/wallet_sqlite.h
parentf7033201689c6eb70f16b242b9f90be548f67aa5 (diff)
wallet: add typed SQLite wallet records
Diffstat (limited to 'src/wallet_sqlite.h')
-rw-r--r--src/wallet_sqlite.h77
1 files changed, 76 insertions, 1 deletions
diff --git a/src/wallet_sqlite.h b/src/wallet_sqlite.h
index 5e6467b..d27e13a 100644
--- a/src/wallet_sqlite.h
+++ b/src/wallet_sqlite.h
@@ -25,13 +25,88 @@ public:
25 25
26 bool WriteRecord(const vector<unsigned char>& vchKey, 26 bool WriteRecord(const vector<unsigned char>& vchKey,
27 const vector<unsigned char>& vchValue, 27 const vector<unsigned char>& vchValue,
28 string& strError); 28 string& strError,
29 bool fOverwrite=true);
29 bool ReadRecord(const vector<unsigned char>& vchKey, 30 bool ReadRecord(const vector<unsigned char>& vchKey,
30 vector<unsigned char>& vchValueRet, 31 vector<unsigned char>& vchValueRet,
31 string& strError); 32 string& strError);
33 bool EraseRecord(const vector<unsigned char>& vchKey,
34 string& strError);
32 bool ScanRecords(CWalletRecordVisitor& visitor, string& strError); 35 bool ScanRecords(CWalletRecordVisitor& visitor, string& strError);
33 bool CountRecords(int& nRecordsRet, string& strError); 36 bool CountRecords(int& nRecordsRet, string& strError);
34 37
38 template<typename K, typename T>
39 bool WriteTypedRecord(const K& key, const T& value, string& strError,
40 bool fOverwrite=true)
41 {
42 CDataStream ssKey(SER_DISK);
43 ssKey.reserve(1000);
44 ssKey << key;
45
46 CDataStream ssValue(SER_DISK);
47 ssValue.reserve(10000);
48 ssValue << value;
49
50 vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());
51 vector<unsigned char> vchValue(ssValue.begin(), ssValue.end());
52 bool fOk = WriteRecord(vchKey, vchValue, strError, fOverwrite);
53
54 if (!vchKey.empty())
55 memset(&vchKey[0], 0, vchKey.size());
56 if (!vchValue.empty())
57 memset(&vchValue[0], 0, vchValue.size());
58 if (!ssKey.empty())
59 memset(&ssKey[0], 0, ssKey.size());
60 if (!ssValue.empty())
61 memset(&ssValue[0], 0, ssValue.size());
62 return fOk;
63 }
64
65 template<typename K, typename T>
66 bool ReadTypedRecord(const K& key, T& valueRet, string& strError)
67 {
68 CDataStream ssKey(SER_DISK);
69 ssKey.reserve(1000);
70 ssKey << key;
71 vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());
72
73 vector<unsigned char> vchValue;
74 bool fOk = ReadRecord(vchKey, vchValue, strError);
75 if (!vchKey.empty())
76 memset(&vchKey[0], 0, vchKey.size());
77 if (!ssKey.empty())
78 memset(&ssKey[0], 0, ssKey.size());
79 if (!fOk)
80 return false;
81
82 CDataStream ssValue(vchValue, SER_DISK);
83 ssValue >> valueRet;
84 bool fReadOk = !ssValue.fail();
85 if (!vchValue.empty())
86 memset(&vchValue[0], 0, vchValue.size());
87 if (!ssValue.empty())
88 memset(&ssValue[0], 0, ssValue.size());
89 if (!fReadOk)
90 strError = "SQLite wallet record could not be deserialized";
91 return fReadOk;
92 }
93
94 template<typename K>
95 bool EraseTypedRecord(const K& key, string& strError)
96 {
97 CDataStream ssKey(SER_DISK);
98 ssKey.reserve(1000);
99 ssKey << key;
100 vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());
101
102 bool fOk = EraseRecord(vchKey, strError);
103 if (!vchKey.empty())
104 memset(&vchKey[0], 0, vchKey.size());
105 if (!ssKey.empty())
106 memset(&ssKey[0], 0, ssKey.size());
107 return fOk;
108 }
109
35private: 110private:
36 sqlite3* pdb; 111 sqlite3* pdb;
37 112