Skip to content

Commit

Permalink
Make NEP6Wallet.path public (#1627)
Browse files Browse the repository at this point in the history
* wallet-backup

* make path public

* case

* Abstract Path

* Improve

* Revert UT

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: erikzhang <erik@neo.org>
  • Loading branch information
3 people authored May 6, 2020
1 parent 946fb73 commit d37416a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 3 additions & 6 deletions src/neo/Wallets/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
23 changes: 10 additions & 13 deletions src/neo/Wallets/SQLite/UserWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt160, UserWalletAccount> accounts;

public override string Name => Path.GetFileNameWithoutExtension(path);
public override string Name => GetFileNameWithoutExtension(Path);

public override Version Version
{
Expand All @@ -46,9 +45,8 @@ public override Version Version
/// </summary>
/// <param name="path">Path</param>
/// <param name="passwordKey">Password Key</param>
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()))
Expand All @@ -70,9 +68,8 @@ private UserWallet(string path, byte[] passwordKey)
/// <param name="path">Path</param>
/// <param name="passwordKey">Password Key</param>
/// <param name="scrypt">Scrypt initialization value</param>
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];
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -303,7 +300,7 @@ public override IEnumerable<WalletAccount> GetAccounts()

private Dictionary<UInt160, UserWalletAccount> LoadAccounts()
{
using (WalletDataContext ctx = new WalletDataContext(path))
using (WalletDataContext ctx = new WalletDataContext(Path))
{
string passphrase = Encoding.UTF8.GetString(masterKey);
Dictionary<UInt160, UserWalletAccount> accounts = ctx.Addresses.Select(p => p.ScriptHash).AsEnumerable().Select(p => new UserWalletAccount(new UInt160(p))).ToDictionary(p => p.ScriptHash);
Expand All @@ -320,7 +317,7 @@ private Dictionary<UInt160, UserWalletAccount> 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;
}
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -32,6 +33,15 @@ public abstract class Wallet
public abstract WalletAccount GetAccount(UInt160 scriptHash);
public abstract IEnumerable<WalletAccount> GetAccounts();

internal Wallet()
{
}

protected Wallet(string path)
{
this.Path = path;
}

public WalletAccount CreateAccount()
{
byte[] privateKey = new byte[32];
Expand Down

0 comments on commit d37416a

Please sign in to comment.