From bc1fcd3fc9aa24094ecca43bfd6af79e3100b08b Mon Sep 17 00:00:00 2001 From: belane Date: Wed, 6 May 2020 13:19:59 +0200 Subject: [PATCH] Make NEP6Wallet.path public (#1627) * wallet-backup * make path public * case * Abstract Path * Improve * Revert UT Co-authored-by: Shargon Co-authored-by: erikzhang --- src/neo/Wallets/NEP6/NEP6Wallet.cs | 9 +++------ src/neo/Wallets/SQLite/UserWallet.cs | 23 ++++++++++------------- src/neo/Wallets/Wallet.cs | 10 ++++++++++ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/neo/Wallets/NEP6/NEP6Wallet.cs b/src/neo/Wallets/NEP6/NEP6Wallet.cs index 423e079dbf..3a5af6d7af 100644 --- a/src/neo/Wallets/NEP6/NEP6Wallet.cs +++ b/src/neo/Wallets/NEP6/NEP6Wallet.cs @@ -13,7 +13,6 @@ namespace Neo.Wallets.NEP6 { public class NEP6Wallet : Wallet { - private readonly string path; private string password; private string name; private Version version; @@ -24,9 +23,8 @@ public class NEP6Wallet : Wallet public override string Name => name; public override Version Version => version; - public NEP6Wallet(string path, string name = null) + public NEP6Wallet(string path, string name = null) : base(path) { - this.path = path; if (File.Exists(path)) { JObject wallet = JObject.Parse(File.ReadAllBytes(path)); @@ -42,9 +40,8 @@ public NEP6Wallet(string path, string name = null) } } - public NEP6Wallet(JObject wallet) + internal NEP6Wallet(JObject wallet) : base(null) { - this.path = ""; LoadFromJson(wallet, out Scrypt, out accounts, out extra); } @@ -262,7 +259,7 @@ public void Save() wallet["scrypt"] = Scrypt.ToJson(); wallet["accounts"] = new JArray(accounts.Values.Select(p => p.ToJson())); wallet["extra"] = extra; - File.WriteAllText(path, wallet.ToString()); + File.WriteAllText(Path, wallet.ToString()); } public IDisposable Unlock(string password) diff --git a/src/neo/Wallets/SQLite/UserWallet.cs b/src/neo/Wallets/SQLite/UserWallet.cs index bea3626d34..8d69d941c4 100644 --- a/src/neo/Wallets/SQLite/UserWallet.cs +++ b/src/neo/Wallets/SQLite/UserWallet.cs @@ -6,26 +6,25 @@ using System; using System.Buffers.Binary; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Reflection; using System.Security; using System.Security.Cryptography; using System.Text; +using static System.IO.Path; namespace Neo.Wallets.SQLite { public class UserWallet : Wallet { private readonly object db_lock = new object(); - private readonly string path; private readonly byte[] iv; private readonly byte[] salt; private readonly byte[] masterKey; private readonly ScryptParameters scrypt; private readonly Dictionary accounts; - public override string Name => Path.GetFileNameWithoutExtension(path); + public override string Name => GetFileNameWithoutExtension(Path); public override Version Version { @@ -46,9 +45,8 @@ public override Version Version /// /// Path /// Password Key - private UserWallet(string path, byte[] passwordKey) + private UserWallet(string path, byte[] passwordKey) : base(path) { - this.path = path; this.salt = LoadStoredData("Salt"); byte[] passwordHash = LoadStoredData("PasswordHash"); if (passwordHash != null && !passwordHash.SequenceEqual(passwordKey.Concat(salt).ToArray().Sha256())) @@ -70,9 +68,8 @@ private UserWallet(string path, byte[] passwordKey) /// Path /// Password Key /// Scrypt initialization value - private UserWallet(string path, byte[] passwordKey, ScryptParameters scrypt) + private UserWallet(string path, byte[] passwordKey, ScryptParameters scrypt) : base(path) { - this.path = path; this.iv = new byte[16]; this.salt = new byte[20]; this.masterKey = new byte[32]; @@ -110,7 +107,7 @@ private void AddAccount(UserWalletAccount account) accounts[account.ScriptHash] = account; } lock (db_lock) - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { if (account.HasKey) { @@ -163,7 +160,7 @@ private void AddAccount(UserWalletAccount account) private void BuildDatabase() { - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); @@ -259,7 +256,7 @@ public override bool DeleteAccount(UInt160 scriptHash) if (account != null) { lock (db_lock) - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { if (account.HasKey) { @@ -303,7 +300,7 @@ public override IEnumerable GetAccounts() private Dictionary LoadAccounts() { - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { string passphrase = Encoding.UTF8.GetString(masterKey); Dictionary accounts = ctx.Addresses.Select(p => p.ScriptHash).AsEnumerable().Select(p => new UserWalletAccount(new UInt160(p))).ToDictionary(p => p.ScriptHash); @@ -320,7 +317,7 @@ private Dictionary LoadAccounts() private byte[] LoadStoredData(string name) { - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { return ctx.Keys.FirstOrDefault(p => p.Name == name)?.Value; } @@ -339,7 +336,7 @@ public static UserWallet Open(string path, SecureString password) private void SaveStoredData(string name, byte[] value) { lock (db_lock) - using (WalletDataContext ctx = new WalletDataContext(path)) + using (WalletDataContext ctx = new WalletDataContext(Path)) { SaveStoredData(ctx, name, value); ctx.SaveChanges(); diff --git a/src/neo/Wallets/Wallet.cs b/src/neo/Wallets/Wallet.cs index 56d0076140..adf5dcde7a 100644 --- a/src/neo/Wallets/Wallet.cs +++ b/src/neo/Wallets/Wallet.cs @@ -21,6 +21,7 @@ namespace Neo.Wallets public abstract class Wallet { public abstract string Name { get; } + public string Path { get; } public abstract Version Version { get; } public abstract bool ChangePassword(string oldPassword, string newPassword); @@ -32,6 +33,15 @@ public abstract class Wallet public abstract WalletAccount GetAccount(UInt160 scriptHash); public abstract IEnumerable GetAccounts(); + internal Wallet() + { + } + + protected Wallet(string path) + { + this.Path = path; + } + public WalletAccount CreateAccount() { byte[] privateKey = new byte[32];