upleb.uk

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

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

#ifndef BITFLASH_WALLET_SQLITE_H
#define BITFLASH_WALLET_SQLITE_H

#include <sqlite3.h>

class CWalletRecordVisitor;

class CWalletDBSQLite
{
public:
    CWalletDBSQLite();
    ~CWalletDBSQLite();

    bool Open(const string& strPath, string& strError);
    bool OpenReadOnly(const string& strPath, string& strError);
    void Close();

    bool BeginTransaction(string& strError);
    bool CommitTransaction(string& strError);
    bool RollbackTransaction(string& strError);
    bool Checkpoint(string& strError);

    bool WriteRecord(const vector<unsigned char>& vchKey,
                     const vector<unsigned char>& vchValue,
                     string& strError,
                     bool fOverwrite=true);
    bool ReadRecord(const vector<unsigned char>& vchKey,
                    vector<unsigned char>& vchValueRet,
                    string& strError);
    bool EraseRecord(const vector<unsigned char>& vchKey,
                     string& strError);
    bool ScanRecords(CWalletRecordVisitor& visitor, string& strError);
    bool CountRecords(int& nRecordsRet, string& strError);

    template<typename K, typename T>
    bool WriteTypedRecord(const K& key, const T& value, string& strError,
                          bool fOverwrite=true)
    {
        CDataStream ssKey(SER_DISK);
        ssKey.reserve(1000);
        ssKey << key;

        CDataStream ssValue(SER_DISK);
        ssValue.reserve(10000);
        ssValue << value;

        vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());
        vector<unsigned char> vchValue(ssValue.begin(), ssValue.end());
        bool fOk = WriteRecord(vchKey, vchValue, strError, fOverwrite);

        if (!vchKey.empty())
            memset(&vchKey[0], 0, vchKey.size());
        if (!vchValue.empty())
            memset(&vchValue[0], 0, vchValue.size());
        if (!ssKey.empty())
            memset(&ssKey[0], 0, ssKey.size());
        if (!ssValue.empty())
            memset(&ssValue[0], 0, ssValue.size());
        return fOk;
    }

    template<typename K, typename T>
    bool ReadTypedRecord(const K& key, T& valueRet, string& strError)
    {
        CDataStream ssKey(SER_DISK);
        ssKey.reserve(1000);
        ssKey << key;
        vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());

        vector<unsigned char> vchValue;
        bool fOk = ReadRecord(vchKey, vchValue, strError);
        if (!vchKey.empty())
            memset(&vchKey[0], 0, vchKey.size());
        if (!ssKey.empty())
            memset(&ssKey[0], 0, ssKey.size());
        if (!fOk)
            return false;

        CDataStream ssValue(vchValue, SER_DISK);
        ssValue >> valueRet;
        bool fReadOk = !ssValue.fail();
        if (!vchValue.empty())
            memset(&vchValue[0], 0, vchValue.size());
        if (!ssValue.empty())
            memset(&ssValue[0], 0, ssValue.size());
        if (!fReadOk)
            strError = "SQLite wallet record could not be deserialized";
        return fReadOk;
    }

    template<typename K>
    bool EraseTypedRecord(const K& key, string& strError)
    {
        CDataStream ssKey(SER_DISK);
        ssKey.reserve(1000);
        ssKey << key;
        vector<unsigned char> vchKey(ssKey.begin(), ssKey.end());

        bool fOk = EraseRecord(vchKey, strError);
        if (!vchKey.empty())
            memset(&vchKey[0], 0, vchKey.size());
        if (!ssKey.empty())
            memset(&ssKey[0], 0, ssKey.size());
        return fOk;
    }

private:
    sqlite3* pdb;

    bool Exec(const char* pszSql, string& strError);
    bool InitSchema(string& strError);

    CWalletDBSQLite(const CWalletDBSQLite&);
    void operator=(const CWalletDBSQLite&);
};

#endif