public int Restore(WalletBackup entity) { int rows = 0; StringBuilder SQL_STATEMENT = new StringBuilder("BEGIN TRANSACTION;"); SQL_STATEMENT.Append("DELETE FROM AddressBook;DELETE FROM Settings;DELETE FROM TransactionComments;"); foreach (var item in entity.AccountList) { SQL_STATEMENT.Append($"REPLACE INTO Accounts VALUES('{item.Id}','{item.PrivateKey}','{item.PublicKey}',{item.Balance},{Convert.ToInt32(item.IsDefault)},{Convert.ToInt32(item.WatchedOnly)},{item.Timestamp},'{item.Tag}');"); } foreach (var item in entity.AddressBookItemList) { SQL_STATEMENT.Append($"INSERT INTO AddressBook (Address, Tag, Timestamp) VALUES('{item.Address}','{item.Tag}',{item.Timestamp});"); } foreach (var item in entity.SettingList) { SQL_STATEMENT.Append($"INSERT INTO Settings (Confirmations, FeePerKB, Encrypt, PassCiphertext) VALUES({item.Confirmations},{item.FeePerKB}, {Convert.ToInt32(item.Encrypt)}, '{item.PassCiphertext}');"); } foreach (var item in entity.TransactionCommentList) { SQL_STATEMENT.Append($"INSERT INTO TransactionComments (TransactionHash, OutputIndex, Comment, Timestamp) VALUES('{item.TransactionHash}',{item.OutputIndex},'{item.Comment}',{item.Timestamp});"); } SQL_STATEMENT.Append("END TRANSACTION;"); using (SqliteConnection con = new SqliteConnection(base.CacheConnectionString)) using (SqliteCommand cmd = new SqliteCommand(SQL_STATEMENT.ToString(), con)) { cmd.Connection.Open(); rows = cmd.ExecuteNonQuery(); } return(rows); }
public void BackupWallet(string filePath, string salt) { string extensionName = Path.GetExtension(filePath).ToLower(); if (string.IsNullOrWhiteSpace(salt)) { filePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + noEncryptExtensionName); } else { filePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + encryptExtensionName); } WalletBackup backup = null; try { backup = new WalletBackup() { AccountList = AccountDac.Default.SelectAll()?.ToList(), AddressBookItemList = AddressBookDac.Default.SelectAll()?.ToList(), SettingList = new List <Setting>() { SettingDac.Default.Setting }, TransactionCommentList = TransactionCommentDac.Default.SelectAll()?.ToList() }; } catch (Exception ex) { throw new CommonException(ErrorCode.Engine.Wallet.DB.LOAD_DATA_ERROR, ex); } try { if (backup != null) { if (extensionName == noEncryptExtensionName) { SaveFile(backup, filePath); } else { SaveFile(backup, filePath, salt); } } } catch (Exception ex) { throw new CommonException(ErrorCode.Engine.Wallet.DATA_SAVE_TO_FILE_ERROR, ex); } }
public virtual int Restore(WalletBackup entity) { try { AccountDac.Default.Insert(entity.AccountList); AddressBookDac.Default.InsertOrUpdate(entity.AddressBookItemList); SettingDac.Default.SetAppSetting(entity.SettingList.FirstOrDefault()); return(0); } catch { return(-1); } }
public bool RestoreWalletBackup(string filePath, string salt) { bool result = false; string extensionName = Path.GetExtension(filePath).ToLower(); if (extensionName != encryptExtensionName && extensionName != noEncryptExtensionName) { throw new CommonException(ErrorCode.Engine.Wallet.IO.EXTENSION_NAME_NOT_SUPPORT); } //Load backup file data. WalletBackup backup = null; if (extensionName == noEncryptExtensionName) { backup = LoadFile <WalletBackup>(filePath); } else { backup = LoadFile <WalletBackup>(filePath, salt); } //Create SQL Query Script and run try { if (backup != null) { WalletBackupDac dac = new WalletBackupDac(); int count = -1; count = dac.Restore(backup); if (count > -1) { result = true; } } } catch (Exception ex) { throw new CommonException(ErrorCode.Engine.Wallet.DB.EXECUTE_SQL_ERROR, ex); } return(result); }
public bool RestoreWalletBackup(string filePath, string salt) { bool result = false; string extensionName = Path.GetExtension(filePath).ToLower(); var setting = new SettingComponent().GetSetting(); if (setting.Encrypt && string.IsNullOrEmpty(salt)) { throw new CommonException(ErrorCode.Engine.Wallet.CHECK_PASSWORD_ERROR); } if (setting.Encrypt && (extensionName != encryptExtensionName)) { throw new CommonException(ErrorCode.Engine.Wallet.IO.EXTENSION_NAME_NOT_SUPPORT); } else if (!setting.Encrypt && extensionName != noEncryptExtensionName) { throw new CommonException(ErrorCode.Engine.Wallet.IO.EXTENSION_NAME_NOT_SUPPORT); } //Load backup file data. WalletBackup backup = null; if (extensionName == noEncryptExtensionName) { try { backup = LoadFile <WalletBackup>(filePath); } catch { var backup1 = LoadFile <WalletBackup1>(filePath); backup = backup1.ToBackup(); } } else { try { backup = LoadFile <WalletBackup>(filePath, salt); } catch { var backup1 = LoadFile <WalletBackup1>(filePath, salt); backup = backup1.ToBackup(); } backup.AccountList.RemoveAll(x => string.IsNullOrEmpty(x.PrivateKey)); } //Create SQL Query Script and run try { if (backup != null) { WalletBackupDac dac = WalletBackupDac.Default; dac.Restore(backup); result = true; } } catch (Exception ex) { throw new CommonException(ErrorCode.Engine.Wallet.DB.EXECUTE_SQL_ERROR, ex); } return(result); }