diff options
| author | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 17:40:20 -0300 |
|---|---|---|
| committer | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 17:59:25 -0300 |
| commit | 805c5757b801d80ea575c6a8b58e2855254f9046 (patch) | |
| tree | 5faf35daa569d987b87dbb161bb18ec170f944b0 /src/walletcmd.cpp | |
| parent | 8e62e362e2fbe6fd6c5d66ddb354ec544f8cf0db (diff) | |
wallet: export storage audit json
Diffstat (limited to 'src/walletcmd.cpp')
| -rw-r--r-- | src/walletcmd.cpp | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/src/walletcmd.cpp b/src/walletcmd.cpp index 896d593..6f8258d 100644 --- a/src/walletcmd.cpp +++ b/src/walletcmd.cpp | |||
| @@ -605,7 +605,60 @@ static bool ReadWalletStorageCounts(WalletStorageAuditCounts& counts, | |||
| 605 | return true; | 605 | return true; |
| 606 | } | 606 | } |
| 607 | 607 | ||
| 608 | int CmdWalletStorageAudit() | 608 | static std::string WalletStorageAuditJson(const WalletStorageAuditCounts& counts) |
| 609 | { | ||
| 610 | std::string strJson; | ||
| 611 | strJson += "{\n"; | ||
| 612 | strJson += " \"format\": \"bitflash-wallet-storage-audit-v1\",\n"; | ||
| 613 | strJson += strprintf(" \"records_total\": %u,\n", counts.nTotal); | ||
| 614 | strJson += strprintf(" \"malformed_records\": %u,\n", counts.nMalformed); | ||
| 615 | strJson += strprintf(" \"unknown_records\": %u,\n", counts.nUnknown); | ||
| 616 | strJson += strprintf(" \"plain_private_keys\": %u,\n", counts.nPlainKeys); | ||
| 617 | strJson += strprintf(" \"encrypted_private_keys\": %u,\n", counts.nEncryptedKeys); | ||
| 618 | strJson += strprintf(" \"encryption_master_keys\": %u,\n", counts.nMasterKeys); | ||
| 619 | strJson += strprintf(" \"default_public_key\": \"%s\",\n", | ||
| 620 | counts.nDefaultKey ? "present" : "none"); | ||
| 621 | strJson += strprintf(" \"plain_hd_seed\": \"%s\",\n", | ||
| 622 | HDSeedStorageState(counts.nPlainHDMaster, | ||
| 623 | counts.nPlainHDChainCode).c_str()); | ||
| 624 | strJson += strprintf(" \"encrypted_hd_seed\": \"%s\",\n", | ||
| 625 | HDSeedStorageState(counts.nCryptedHDMaster, | ||
| 626 | counts.nCryptedHDChainCode).c_str()); | ||
| 627 | strJson += strprintf(" \"wallet_minimum_version\": \"%s\",\n", | ||
| 628 | counts.nWalletMinVersion ? "present" : "none"); | ||
| 629 | strJson += strprintf(" \"keypool_entries\": %u,\n", counts.nPool); | ||
| 630 | strJson += strprintf(" \"wallet_transactions\": %u,\n", counts.nTransactions); | ||
| 631 | strJson += strprintf(" \"address_book_labels\": %u,\n", counts.nNames); | ||
| 632 | strJson += strprintf(" \"settings\": %u,\n", counts.nSettings); | ||
| 633 | strJson += strprintf(" \"hd_metadata_records\": %u,\n", | ||
| 634 | counts.nHDNext + counts.nHDSchema + counts.nHDCoinType + | ||
| 635 | counts.nHDReceiveNext + counts.nHDChangeNext); | ||
| 636 | strJson += strprintf(" \"database_version_records\": %u\n", counts.nVersion); | ||
| 637 | strJson += "}\n"; | ||
| 638 | return strJson; | ||
| 639 | } | ||
| 640 | |||
| 641 | static bool WriteAuditTextFile(const std::string& strPath, | ||
| 642 | const std::string& strText, | ||
| 643 | std::string& strError) | ||
| 644 | { | ||
| 645 | FILE* pf = fopen(strPath.c_str(), "wb"); | ||
| 646 | if (!pf) | ||
| 647 | { | ||
| 648 | strError = "cannot open output file"; | ||
| 649 | return false; | ||
| 650 | } | ||
| 651 | size_t nWritten = fwrite(strText.data(), 1, strText.size(), pf); | ||
| 652 | bool fCloseOk = fclose(pf) == 0; | ||
| 653 | if (nWritten != strText.size() || !fCloseOk) | ||
| 654 | { | ||
| 655 | strError = "could not write the complete output file"; | ||
| 656 | return false; | ||
| 657 | } | ||
| 658 | return true; | ||
| 659 | } | ||
| 660 | |||
| 661 | int CmdWalletStorageAudit(const std::string& strJsonOut) | ||
| 609 | { | 662 | { |
| 610 | AttachTerminal(); | 663 | AttachTerminal(); |
| 611 | 664 | ||
| @@ -617,6 +670,22 @@ int CmdWalletStorageAudit() | |||
| 617 | return 1; | 670 | return 1; |
| 618 | } | 671 | } |
| 619 | 672 | ||
| 673 | if (!strJsonOut.empty()) | ||
| 674 | { | ||
| 675 | std::string strWriteError; | ||
| 676 | if (!WriteAuditTextFile(strJsonOut, | ||
| 677 | WalletStorageAuditJson(counts), | ||
| 678 | strWriteError)) | ||
| 679 | { | ||
| 680 | fprintf(stderr, "Cannot write wallet storage audit JSON: %s\n", | ||
| 681 | strWriteError.c_str()); | ||
| 682 | return 1; | ||
| 683 | } | ||
| 684 | printf("Wallet storage audit written to %s\n", strJsonOut.c_str()); | ||
| 685 | fflush(stdout); | ||
| 686 | return counts.nMalformed > 0 ? 2 : 0; | ||
| 687 | } | ||
| 688 | |||
| 620 | printf("Wallet storage audit\n"); | 689 | printf("Wallet storage audit\n"); |
| 621 | printf(" records total: %u\n", counts.nTotal); | 690 | printf(" records total: %u\n", counts.nTotal); |
| 622 | printf(" malformed records: %u\n", counts.nMalformed); | 691 | printf(" malformed records: %u\n", counts.nMalformed); |