diff options
| author | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-17 11:32:45 -0300 |
|---|---|---|
| committer | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-17 11:32:45 -0300 |
| commit | d082210c5d080c0a91412b94a109d8e73422eb31 (patch) | |
| tree | 5372e845b7c6cf75fb9aaab2caaaf15eaadd0dd3 /src/db.h | |
| parent | 0175bb9205d7e3dc474aede8000a05f1d70ed0e6 (diff) | |
wallet: route CWalletDB through SQLite runtime
Diffstat (limited to 'src/db.h')
| -rw-r--r-- | src/db.h | 145 |
1 files changed, 116 insertions, 29 deletions
| @@ -356,6 +356,16 @@ private: | |||
| 356 | // Legacy IP-based peer address database (CAddrDB / addr.dat) removed -- | 356 | // Legacy IP-based peer address database (CAddrDB / addr.dat) removed -- |
| 357 | // peer discovery is entirely Nostr/.btf-based now. | 357 | // peer discovery is entirely Nostr/.btf-based now. |
| 358 | 358 | ||
| 359 | bool WalletSQLiteRuntimeActive(); | ||
| 360 | bool WalletSQLiteRuntimeOpen(const string& strPath, string& strErrorRet); | ||
| 361 | void WalletSQLiteRuntimeClose(); | ||
| 362 | bool WalletSQLiteRuntimeReadRecord(const vector<unsigned char>& vchKey, | ||
| 363 | vector<unsigned char>& vchValueRet); | ||
| 364 | bool WalletSQLiteRuntimeWriteRecord(const vector<unsigned char>& vchKey, | ||
| 365 | const vector<unsigned char>& vchValue, | ||
| 366 | bool fOverwrite); | ||
| 367 | bool WalletSQLiteRuntimeEraseRecord(const vector<unsigned char>& vchKey); | ||
| 368 | |||
| 359 | 369 | ||
| 360 | 370 | ||
| 361 | 371 | ||
| @@ -363,64 +373,141 @@ private: | |||
| 363 | class CWalletDB : public CDB | 373 | class CWalletDB : public CDB |
| 364 | { | 374 | { |
| 365 | public: | 375 | public: |
| 366 | CWalletDB(const char* pszMode="r+", bool fTxn=false) : CDB("wallet.dat", pszMode, fTxn) { } | 376 | CWalletDB(const char* pszMode="r+", bool fTxn=false); |
| 367 | private: | 377 | private: |
| 368 | CWalletDB(const CWalletDB&); | 378 | CWalletDB(const CWalletDB&); |
| 369 | void operator=(const CWalletDB&); | 379 | void operator=(const CWalletDB&); |
| 380 | |||
| 381 | template<typename K, typename T> | ||
| 382 | bool ReadWalletRecord(const K& key, T& value) | ||
| 383 | { | ||
| 384 | if (!WalletSQLiteRuntimeActive()) | ||
| 385 | return Read(key, value); | ||
| 386 | |||
| 387 | CDataStream ssKey(SER_DISK); | ||
| 388 | ssKey.reserve(1000); | ||
| 389 | ssKey << key; | ||
| 390 | vector<unsigned char> vchKey(ssKey.begin(), ssKey.end()); | ||
| 391 | vector<unsigned char> vchValue; | ||
| 392 | |||
| 393 | bool fOk = WalletSQLiteRuntimeReadRecord(vchKey, vchValue); | ||
| 394 | if (!vchKey.empty()) | ||
| 395 | memset(&vchKey[0], 0, vchKey.size()); | ||
| 396 | if (!ssKey.empty()) | ||
| 397 | memset(&ssKey[0], 0, ssKey.size()); | ||
| 398 | if (!fOk) | ||
| 399 | return false; | ||
| 400 | |||
| 401 | CDataStream ssValue(vchValue, SER_DISK); | ||
| 402 | ssValue >> value; | ||
| 403 | bool fReadOk = !ssValue.fail(); | ||
| 404 | if (!vchValue.empty()) | ||
| 405 | memset(&vchValue[0], 0, vchValue.size()); | ||
| 406 | if (!ssValue.empty()) | ||
| 407 | memset(&ssValue[0], 0, ssValue.size()); | ||
| 408 | return fReadOk; | ||
| 409 | } | ||
| 410 | |||
| 411 | template<typename K, typename T> | ||
| 412 | bool WriteWalletRecord(const K& key, const T& value, bool fOverwrite=true) | ||
| 413 | { | ||
| 414 | if (!WalletSQLiteRuntimeActive()) | ||
| 415 | return Write(key, value, fOverwrite); | ||
| 416 | |||
| 417 | CDataStream ssKey(SER_DISK); | ||
| 418 | ssKey.reserve(1000); | ||
| 419 | ssKey << key; | ||
| 420 | CDataStream ssValue(SER_DISK); | ||
| 421 | ssValue.reserve(10000); | ||
| 422 | ssValue << value; | ||
| 423 | |||
| 424 | vector<unsigned char> vchKey(ssKey.begin(), ssKey.end()); | ||
| 425 | vector<unsigned char> vchValue(ssValue.begin(), ssValue.end()); | ||
| 426 | bool fOk = WalletSQLiteRuntimeWriteRecord(vchKey, vchValue, fOverwrite); | ||
| 427 | |||
| 428 | if (!vchKey.empty()) | ||
| 429 | memset(&vchKey[0], 0, vchKey.size()); | ||
| 430 | if (!vchValue.empty()) | ||
| 431 | memset(&vchValue[0], 0, vchValue.size()); | ||
| 432 | if (!ssKey.empty()) | ||
| 433 | memset(&ssKey[0], 0, ssKey.size()); | ||
| 434 | if (!ssValue.empty()) | ||
| 435 | memset(&ssValue[0], 0, ssValue.size()); | ||
| 436 | return fOk; | ||
| 437 | } | ||
| 438 | |||
| 439 | template<typename K> | ||
| 440 | bool EraseWalletRecord(const K& key) | ||
| 441 | { | ||
| 442 | if (!WalletSQLiteRuntimeActive()) | ||
| 443 | return Erase(key); | ||
| 444 | |||
| 445 | CDataStream ssKey(SER_DISK); | ||
| 446 | ssKey.reserve(1000); | ||
| 447 | ssKey << key; | ||
| 448 | vector<unsigned char> vchKey(ssKey.begin(), ssKey.end()); | ||
| 449 | bool fOk = WalletSQLiteRuntimeEraseRecord(vchKey); | ||
| 450 | |||
| 451 | if (!vchKey.empty()) | ||
| 452 | memset(&vchKey[0], 0, vchKey.size()); | ||
| 453 | if (!ssKey.empty()) | ||
| 454 | memset(&ssKey[0], 0, ssKey.size()); | ||
| 455 | return fOk; | ||
| 456 | } | ||
| 370 | public: | 457 | public: |
| 371 | bool ReadName(const string& strAddress, string& strName) | 458 | bool ReadName(const string& strAddress, string& strName) |
| 372 | { | 459 | { |
| 373 | strName = ""; | 460 | strName = ""; |
| 374 | return Read(make_pair(string("name"), strAddress), strName); | 461 | return ReadWalletRecord(make_pair(string("name"), strAddress), strName); |
| 375 | } | 462 | } |
| 376 | 463 | ||
| 377 | bool WriteName(const string& strAddress, const string& strName) | 464 | bool WriteName(const string& strAddress, const string& strName) |
| 378 | { | 465 | { |
| 379 | mapAddressBook[strAddress] = strName; | 466 | mapAddressBook[strAddress] = strName; |
| 380 | return Write(make_pair(string("name"), strAddress), strName); | 467 | return WriteWalletRecord(make_pair(string("name"), strAddress), strName); |
| 381 | } | 468 | } |
| 382 | 469 | ||
| 383 | bool EraseName(const string& strAddress) | 470 | bool EraseName(const string& strAddress) |
| 384 | { | 471 | { |
| 385 | mapAddressBook.erase(strAddress); | 472 | mapAddressBook.erase(strAddress); |
| 386 | return Erase(make_pair(string("name"), strAddress)); | 473 | return EraseWalletRecord(make_pair(string("name"), strAddress)); |
| 387 | } | 474 | } |
| 388 | 475 | ||
| 389 | bool ReadTx(uint256 hash, CWalletTx& wtx) | 476 | bool ReadTx(uint256 hash, CWalletTx& wtx) |
| 390 | { | 477 | { |
| 391 | return Read(make_pair(string("tx"), hash), wtx); | 478 | return ReadWalletRecord(make_pair(string("tx"), hash), wtx); |
| 392 | } | 479 | } |
| 393 | 480 | ||
| 394 | bool WriteTx(uint256 hash, const CWalletTx& wtx) | 481 | bool WriteTx(uint256 hash, const CWalletTx& wtx) |
| 395 | { | 482 | { |
| 396 | return Write(make_pair(string("tx"), hash), wtx); | 483 | return WriteWalletRecord(make_pair(string("tx"), hash), wtx); |
| 397 | } | 484 | } |
| 398 | 485 | ||
| 399 | bool EraseTx(uint256 hash) | 486 | bool EraseTx(uint256 hash) |
| 400 | { | 487 | { |
| 401 | return Erase(make_pair(string("tx"), hash)); | 488 | return EraseWalletRecord(make_pair(string("tx"), hash)); |
| 402 | } | 489 | } |
| 403 | 490 | ||
| 404 | bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey) | 491 | bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey) |
| 405 | { | 492 | { |
| 406 | vchPrivKey.clear(); | 493 | vchPrivKey.clear(); |
| 407 | return Read(make_pair(string("key"), vchPubKey), vchPrivKey); | 494 | return ReadWalletRecord(make_pair(string("key"), vchPubKey), vchPrivKey); |
| 408 | } | 495 | } |
| 409 | 496 | ||
| 410 | bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey) | 497 | bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey) |
| 411 | { | 498 | { |
| 412 | return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false); | 499 | return WriteWalletRecord(make_pair(string("key"), vchPubKey), vchPrivKey, false); |
| 413 | } | 500 | } |
| 414 | 501 | ||
| 415 | bool ReadDefaultKey(vector<unsigned char>& vchPubKey) | 502 | bool ReadDefaultKey(vector<unsigned char>& vchPubKey) |
| 416 | { | 503 | { |
| 417 | vchPubKey.clear(); | 504 | vchPubKey.clear(); |
| 418 | return Read(string("defaultkey"), vchPubKey); | 505 | return ReadWalletRecord(string("defaultkey"), vchPubKey); |
| 419 | } | 506 | } |
| 420 | 507 | ||
| 421 | bool WriteDefaultKey(const vector<unsigned char>& vchPubKey) | 508 | bool WriteDefaultKey(const vector<unsigned char>& vchPubKey) |
| 422 | { | 509 | { |
| 423 | return Write(string("defaultkey"), vchPubKey); | 510 | return WriteWalletRecord(string("defaultkey"), vchPubKey); |
| 424 | } | 511 | } |
| 425 | 512 | ||
| 426 | // --- Deterministic (BIP32) seed --------------------------------------- | 513 | // --- Deterministic (BIP32) seed --------------------------------------- |
| @@ -438,62 +525,62 @@ public: | |||
| 438 | { | 525 | { |
| 439 | vchMasterRet.clear(); | 526 | vchMasterRet.clear(); |
| 440 | vchChainCodeRet.clear(); | 527 | vchChainCodeRet.clear(); |
| 441 | return Read(string("hdmaster"), vchMasterRet) | 528 | return ReadWalletRecord(string("hdmaster"), vchMasterRet) |
| 442 | && Read(string("hdchaincode"), vchChainCodeRet); | 529 | && ReadWalletRecord(string("hdchaincode"), vchChainCodeRet); |
| 443 | } | 530 | } |
| 444 | 531 | ||
| 445 | bool WriteHDMaster(const vector<unsigned char>& vchMaster, const vector<unsigned char>& vchChainCode) | 532 | bool WriteHDMaster(const vector<unsigned char>& vchMaster, const vector<unsigned char>& vchChainCode) |
| 446 | { | 533 | { |
| 447 | return Write(string("hdmaster"), vchMaster) | 534 | return WriteWalletRecord(string("hdmaster"), vchMaster) |
| 448 | && Write(string("hdchaincode"), vchChainCode); | 535 | && WriteWalletRecord(string("hdchaincode"), vchChainCode); |
| 449 | } | 536 | } |
| 450 | 537 | ||
| 451 | bool WriteWalletMinVersion(int nVersion) | 538 | bool WriteWalletMinVersion(int nVersion) |
| 452 | { | 539 | { |
| 453 | return Write(string("walletminversion"), nVersion); | 540 | return WriteWalletRecord(string("walletminversion"), nVersion); |
| 454 | } | 541 | } |
| 455 | 542 | ||
| 456 | bool WriteMasterKey(unsigned int nID, const CWalletMasterKey& kMasterKey) | 543 | bool WriteMasterKey(unsigned int nID, const CWalletMasterKey& kMasterKey) |
| 457 | { | 544 | { |
| 458 | return Write(make_pair(string("mkey"), nID), kMasterKey, true); | 545 | return WriteWalletRecord(make_pair(string("mkey"), nID), kMasterKey, true); |
| 459 | } | 546 | } |
| 460 | 547 | ||
| 461 | bool WriteCryptedKey(const vector<unsigned char>& vchPubKey, | 548 | bool WriteCryptedKey(const vector<unsigned char>& vchPubKey, |
| 462 | const vector<unsigned char>& vchCryptedSecret) | 549 | const vector<unsigned char>& vchCryptedSecret) |
| 463 | { | 550 | { |
| 464 | return Write(make_pair(string("ckey"), vchPubKey), vchCryptedSecret, false); | 551 | return WriteWalletRecord(make_pair(string("ckey"), vchPubKey), vchCryptedSecret, false); |
| 465 | } | 552 | } |
| 466 | 553 | ||
| 467 | bool WriteCryptedHDMaster(const vector<unsigned char>& vchCryptedMaster, | 554 | bool WriteCryptedHDMaster(const vector<unsigned char>& vchCryptedMaster, |
| 468 | const vector<unsigned char>& vchCryptedChainCode) | 555 | const vector<unsigned char>& vchCryptedChainCode) |
| 469 | { | 556 | { |
| 470 | return Write(string("cryptedhdmaster"), vchCryptedMaster) | 557 | return WriteWalletRecord(string("cryptedhdmaster"), vchCryptedMaster) |
| 471 | && Write(string("cryptedhdchaincode"), vchCryptedChainCode); | 558 | && WriteWalletRecord(string("cryptedhdchaincode"), vchCryptedChainCode); |
| 472 | } | 559 | } |
| 473 | 560 | ||
| 474 | bool WriteHDNext(unsigned int nNext) | 561 | bool WriteHDNext(unsigned int nNext) |
| 475 | { | 562 | { |
| 476 | return Write(string("hdnext"), nNext); | 563 | return WriteWalletRecord(string("hdnext"), nNext); |
| 477 | } | 564 | } |
| 478 | 565 | ||
| 479 | bool WriteHDSchema(int nSchema) | 566 | bool WriteHDSchema(int nSchema) |
| 480 | { | 567 | { |
| 481 | return Write(string("hdschema"), nSchema); | 568 | return WriteWalletRecord(string("hdschema"), nSchema); |
| 482 | } | 569 | } |
| 483 | 570 | ||
| 484 | bool WriteHDCoinType(unsigned int nCoinType) | 571 | bool WriteHDCoinType(unsigned int nCoinType) |
| 485 | { | 572 | { |
| 486 | return Write(string("hdcointype"), nCoinType); | 573 | return WriteWalletRecord(string("hdcointype"), nCoinType); |
| 487 | } | 574 | } |
| 488 | 575 | ||
| 489 | bool WriteHDReceiveNext(unsigned int nNext) | 576 | bool WriteHDReceiveNext(unsigned int nNext) |
| 490 | { | 577 | { |
| 491 | return Write(string("hdreceivenext"), nNext); | 578 | return WriteWalletRecord(string("hdreceivenext"), nNext); |
| 492 | } | 579 | } |
| 493 | 580 | ||
| 494 | bool WriteHDChangeNext(unsigned int nNext) | 581 | bool WriteHDChangeNext(unsigned int nNext) |
| 495 | { | 582 | { |
| 496 | return Write(string("hdchangenext"), nNext); | 583 | return WriteWalletRecord(string("hdchangenext"), nNext); |
| 497 | } | 584 | } |
| 498 | 585 | ||
| 499 | // Keys generated ahead of time and not yet handed out. The private key of | 586 | // Keys generated ahead of time and not yet handed out. The private key of |
| @@ -501,24 +588,24 @@ public: | |||
| 501 | // record only says that this one is still unspoken for. | 588 | // record only says that this one is still unspoken for. |
| 502 | bool WritePool(int64 nIndex, const vector<unsigned char>& vchPubKey) | 589 | bool WritePool(int64 nIndex, const vector<unsigned char>& vchPubKey) |
| 503 | { | 590 | { |
| 504 | return Write(make_pair(string("pool"), nIndex), vchPubKey); | 591 | return WriteWalletRecord(make_pair(string("pool"), nIndex), vchPubKey); |
| 505 | } | 592 | } |
| 506 | 593 | ||
| 507 | bool ErasePool(int64 nIndex) | 594 | bool ErasePool(int64 nIndex) |
| 508 | { | 595 | { |
| 509 | return Erase(make_pair(string("pool"), nIndex)); | 596 | return EraseWalletRecord(make_pair(string("pool"), nIndex)); |
| 510 | } | 597 | } |
| 511 | 598 | ||
| 512 | template<typename T> | 599 | template<typename T> |
| 513 | bool ReadSetting(const string& strKey, T& value) | 600 | bool ReadSetting(const string& strKey, T& value) |
| 514 | { | 601 | { |
| 515 | return Read(make_pair(string("setting"), strKey), value); | 602 | return ReadWalletRecord(make_pair(string("setting"), strKey), value); |
| 516 | } | 603 | } |
| 517 | 604 | ||
| 518 | template<typename T> | 605 | template<typename T> |
| 519 | bool WriteSetting(const string& strKey, const T& value) | 606 | bool WriteSetting(const string& strKey, const T& value) |
| 520 | { | 607 | { |
| 521 | return Write(make_pair(string("setting"), strKey), value); | 608 | return WriteWalletRecord(make_pair(string("setting"), strKey), value); |
| 522 | } | 609 | } |
| 523 | 610 | ||
| 524 | bool LoadWallet(vector<unsigned char>& vchDefaultKeyRet); | 611 | bool LoadWallet(vector<unsigned char>& vchDefaultKeyRet); |