Skip to content

Commit 9d3d2d2

Browse files
committed
Use network magic as sqlite wallet application ID
1 parent 9af5de3 commit 9d3d2d2

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/wallet/sqlite.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <wallet/sqlite.h>
66

7+
#include <chainparams.h>
8+
#include <crypto/common.h>
79
#include <logging.h>
810
#include <sync.h>
911
#include <util/memory.h>
@@ -113,6 +115,28 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
113115
{
114116
assert(m_db);
115117

118+
// Check the application ID matches our network magic
119+
sqlite3_stmt* app_id_stmt{nullptr};
120+
int ret = sqlite3_prepare_v2(m_db, "PRAGMA application_id", -1, &app_id_stmt, nullptr);
121+
if (ret != SQLITE_OK) {
122+
sqlite3_finalize(app_id_stmt);
123+
error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch the application id: %s"), sqlite3_errstr(ret));
124+
return false;
125+
}
126+
ret = sqlite3_step(app_id_stmt);
127+
if (ret != SQLITE_ROW) {
128+
sqlite3_finalize(app_id_stmt);
129+
error = strprintf(_("SQLiteDatabase: Failed to fetch the application id: %s"), sqlite3_errstr(ret));
130+
return false;
131+
}
132+
uint32_t app_id = static_cast<uint32_t>(sqlite3_column_int(app_id_stmt, 0));
133+
sqlite3_finalize(app_id_stmt);
134+
uint32_t net_magic = ReadBE32(Params().MessageStart());
135+
if (app_id != net_magic) {
136+
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
137+
return false;
138+
}
139+
116140
sqlite3_stmt* stmt{nullptr};
117141
ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
118142
if (ret != SQLITE_OK) {
@@ -214,6 +238,14 @@ void SQLiteDatabase::Open()
214238
if (ret != SQLITE_OK) {
215239
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
216240
}
241+
242+
// Set the application id
243+
uint32_t app_id = ReadBE32(Params().MessageStart());
244+
std::string set_app_id = strprintf("PRAGMA application_id = %d", static_cast<int32_t>(app_id));
245+
ret = sqlite3_exec(m_db, set_app_id.c_str(), nullptr, nullptr, nullptr);
246+
if (ret != SQLITE_OK) {
247+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret)));
248+
}
217249
}
218250
}
219251

@@ -544,9 +576,20 @@ bool IsSQLiteFile(const fs::path& path)
544576
// Magic is at beginning and is 16 bytes long
545577
char magic[16];
546578
file.read(magic, 16);
579+
580+
// Application id is at offset 68 and 4 bytes long
581+
file.seekg(68, std::ios::beg);
582+
char app_id[4];
583+
file.read(app_id, 4);
584+
547585
file.close();
548586

549587
// Check the magic, see https://sqlite.org/fileformat2.html
550588
std::string magic_str(magic);
551-
return magic_str == std::string("SQLite format 3");
589+
if (magic_str != std::string("SQLite format 3")) {
590+
return false;
591+
}
592+
593+
// Check the application id matches our network magic
594+
return memcmp(Params().MessageStart(), app_id, 4) == 0;
552595
}

0 commit comments

Comments
 (0)