upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/walletcmd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/walletcmd.cpp')
-rw-r--r--src/walletcmd.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/walletcmd.cpp b/src/walletcmd.cpp
index 6fb09a3..20e7e33 100644
--- a/src/walletcmd.cpp
+++ b/src/walletcmd.cpp
@@ -12,6 +12,7 @@
12#include "headers_core.h" 12#include "headers_core.h"
13#include "bip32.h" 13#include "bip32.h"
14#include "walletcmd.h" 14#include "walletcmd.h"
15#include "wallet_sqlite.h"
15 16
16#undef printf 17#undef printf
17 18
@@ -790,6 +791,102 @@ int CmdWalletStorageAudit(const std::string& strJsonOut)
790 return 0; 791 return 0;
791} 792}
792 793
794static vector<unsigned char> DataStreamBytesLocal(const CDataStream& ss)
795{
796 return vector<unsigned char>(ss.begin(), ss.end());
797}
798
799class CWalletSQLiteExportVisitor : public CWalletRecordVisitor
800{
801public:
802 CWalletDBSQLite& db;
803 int& nCopied;
804
805 CWalletSQLiteExportVisitor(CWalletDBSQLite& dbIn, int& nCopiedIn)
806 : db(dbIn), nCopied(nCopiedIn) { }
807
808 bool VisitWalletRecord(const CDataStream& ssKey,
809 const CDataStream& ssValue,
810 string& strErrorRet)
811 {
812 if (!db.WriteRecord(DataStreamBytesLocal(ssKey),
813 DataStreamBytesLocal(ssValue),
814 strErrorRet))
815 return false;
816 nCopied++;
817 return true;
818 }
819};
820
821int CmdWalletSQLiteExport(const std::string& strDest)
822{
823 AttachTerminal();
824
825 if (strDest.empty())
826 {
827 fprintf(stderr, "Missing SQLite wallet export path.\n");
828 return 1;
829 }
830 if (FileExists(strDest.c_str()))
831 {
832 fprintf(stderr, "Refusing to overwrite existing SQLite wallet export: %s\n",
833 strDest.c_str());
834 return 1;
835 }
836
837 string strError;
838 CWalletDBSQLite db;
839 if (!db.Open(strDest, strError))
840 {
841 fprintf(stderr, "Cannot open SQLite wallet export: %s\n", strError.c_str());
842 return 1;
843 }
844 if (!db.BeginTransaction(strError))
845 {
846 fprintf(stderr, "Cannot begin SQLite wallet export: %s\n", strError.c_str());
847 return 1;
848 }
849
850 int nCopied = 0;
851 CWalletSQLiteExportVisitor visitor(db, nCopied);
852 if (!ScanWalletRecords(visitor, strError))
853 {
854 string strRollbackError;
855 db.RollbackTransaction(strRollbackError);
856 fprintf(stderr, "Cannot export wallet.dat to SQLite: %s\n", strError.c_str());
857 return 1;
858 }
859 if (!db.CommitTransaction(strError))
860 {
861 fprintf(stderr, "Cannot commit SQLite wallet export: %s\n", strError.c_str());
862 return 1;
863 }
864 if (!db.Checkpoint(strError))
865 {
866 fprintf(stderr, "Cannot checkpoint SQLite wallet export: %s\n", strError.c_str());
867 return 1;
868 }
869
870 int nCount = 0;
871 if (!db.CountRecords(nCount, strError))
872 {
873 fprintf(stderr, "Cannot verify SQLite wallet export count: %s\n",
874 strError.c_str());
875 return 1;
876 }
877 if (nCount != nCopied)
878 {
879 fprintf(stderr, "SQLite wallet export count mismatch: copied %d, stored %d\n",
880 nCopied, nCount);
881 return 1;
882 }
883
884 printf("SQLite wallet export written to %s\n", strDest.c_str());
885 printf(" records copied: %d\n", nCopied);
886 fflush(stdout);
887 return 0;
888}
889
793int CmdEncryptWallet(const std::string& strPassphrase) 890int CmdEncryptWallet(const std::string& strPassphrase)
794{ 891{
795 AttachTerminal(); 892 AttachTerminal();