upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/wallet_sqlite.cpp
blob: 4104ac61b3b39437e4ce1410eb8167d8342d3459 (plain)
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
// Copyright (c) 2026 Bitflash developers
// Distributed under the MIT/X11 software license.

#include "headers.h"
#include "wallet_sqlite.h"

static void SetSQLiteError(sqlite3* pdb, const string& strContext,
                           string& strError)
{
    if (pdb)
        strError = strContext + ": " + sqlite3_errmsg(pdb);
    else
        strError = strContext;
}

CWalletDBSQLite::CWalletDBSQLite() : pdb(NULL)
{
}

CWalletDBSQLite::~CWalletDBSQLite()
{
    Close();
}

bool CWalletDBSQLite::Open(const string& strPath, string& strError)
{
    Close();

    int ret = sqlite3_open_v2(strPath.c_str(), &pdb,
                              SQLITE_OPEN_READWRITE |
                              SQLITE_OPEN_CREATE |
                              SQLITE_OPEN_FULLMUTEX,
                              NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot open SQLite wallet", strError);
        Close();
        return false;
    }

    sqlite3_busy_timeout(pdb, 5000);

    if (!Exec("PRAGMA journal_mode=WAL;", strError))
    {
        Close();
        return false;
    }
    if (!Exec("PRAGMA synchronous=FULL;", strError))
    {
        Close();
        return false;
    }
    if (!InitSchema(strError))
    {
        Close();
        return false;
    }
    return true;
}

void CWalletDBSQLite::Close()
{
    if (pdb)
    {
        sqlite3_close(pdb);
        pdb = NULL;
    }
}

bool CWalletDBSQLite::BeginTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("BEGIN IMMEDIATE;", strError);
}

bool CWalletDBSQLite::CommitTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("COMMIT;", strError);
}

bool CWalletDBSQLite::RollbackTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("ROLLBACK;", strError);
}

bool CWalletDBSQLite::Exec(const char* pszSql, string& strError)
{
    char* pszErr = NULL;
    int ret = sqlite3_exec(pdb, pszSql, NULL, NULL, &pszErr);
    if (ret != SQLITE_OK)
    {
        if (pszErr)
        {
            strError = pszErr;
            sqlite3_free(pszErr);
        }
        else
            SetSQLiteError(pdb, "SQLite exec failed", strError);
        return false;
    }
    return true;
}

bool CWalletDBSQLite::InitSchema(string& strError)
{
    return Exec("CREATE TABLE IF NOT EXISTS wallet_records ("
                "key BLOB PRIMARY KEY NOT NULL,"
                "value BLOB NOT NULL"
                ");",
                strError) &&
           Exec("PRAGMA user_version=1;", strError);
}

bool CWalletDBSQLite::WriteRecord(const vector<unsigned char>& vchKey,
                                  const vector<unsigned char>& vchValue,
                                  string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    if (vchKey.empty())
    {
        strError = "SQLite wallet record key is empty";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql =
        "INSERT OR REPLACE INTO wallet_records(key, value) VALUES(?, ?);";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet write", strError);
        return false;
    }

    ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(),
                            SQLITE_TRANSIENT);
    if (ret == SQLITE_OK)
        ret = sqlite3_bind_blob(stmt, 2,
                                vchValue.empty() ? NULL : &vchValue[0],
                                (int)vchValue.size(),
                                SQLITE_TRANSIENT);
    if (ret == SQLITE_OK)
        ret = sqlite3_step(stmt);

    bool fOk = (ret == SQLITE_DONE);
    if (!fOk)
        SetSQLiteError(pdb, "cannot write SQLite wallet record", strError);
    sqlite3_finalize(stmt);
    return fOk;
}

bool CWalletDBSQLite::ReadRecord(const vector<unsigned char>& vchKey,
                                 vector<unsigned char>& vchValueRet,
                                 string& strError)
{
    vchValueRet.clear();
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    if (vchKey.empty())
    {
        strError = "SQLite wallet record key is empty";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql = "SELECT value FROM wallet_records WHERE key = ?;";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet read", strError);
        return false;
    }

    ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(),
                            SQLITE_TRANSIENT);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot bind SQLite wallet key", strError);
        sqlite3_finalize(stmt);
        return false;
    }

    ret = sqlite3_step(stmt);
    if (ret == SQLITE_ROW)
    {
        const unsigned char* pch =
            (const unsigned char*)sqlite3_column_blob(stmt, 0);
        int nBytes = sqlite3_column_bytes(stmt, 0);
        if (nBytes > 0 && pch)
            vchValueRet.assign(pch, pch + nBytes);
        sqlite3_finalize(stmt);
        return true;
    }

    if (ret == SQLITE_DONE)
        strError = "SQLite wallet record not found";
    else
        SetSQLiteError(pdb, "cannot read SQLite wallet record", strError);
    sqlite3_finalize(stmt);
    return false;
}

bool CWalletDBSQLite::CountRecords(int& nRecordsRet, string& strError)
{
    nRecordsRet = 0;
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    int ret = sqlite3_prepare_v2(pdb,
                                 "SELECT COUNT(*) FROM wallet_records;",
                                 -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet count", strError);
        return false;
    }

    ret = sqlite3_step(stmt);
    if (ret != SQLITE_ROW)
    {
        SetSQLiteError(pdb, "cannot count SQLite wallet records", strError);
        sqlite3_finalize(stmt);
        return false;
    }

    nRecordsRet = sqlite3_column_int(stmt, 0);
    sqlite3_finalize(stmt);
    return true;
}