Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Fixed Wallet Deserialization Bug;
Browse files Browse the repository at this point in the history
Set wallet properties to use lowercause;
Added new method CreateAndAddAccount; (+1 squashed commit)
Squashed commits:
[f4f5b7a] Fixed Wallet Deserialization Bug;
Set wallet properties to use lowercause;
  • Loading branch information
lock9 committed Sep 19, 2018
1 parent 2e62c66 commit b34b7ec
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void WalletCreateCommand(string fileName)
{
_walletManager.CreateWallet(fileName);
var secureString = _consoleReader.ReadPassword();
var walletAccount = _walletManager.CreateAccount(secureString);
var walletAccount = _walletManager.CreateAndAddAccount(secureString);

_consoleWriter.WriteLine("");
_consoleWriter.WriteLine("Address: " + walletAccount.Address, ConsoleOutputStyle.Information);
Expand Down Expand Up @@ -87,7 +87,7 @@ public void WalletSaveCommand(string fileName)
public void AccountCreateCommand()
{
var secureString = _consoleReader.ReadPassword();
var walletAccount = _walletManager.CreateAccount(secureString);
var walletAccount = _walletManager.CreateAndAddAccount(secureString);

_consoleWriter.WriteLine("");
_consoleWriter.WriteLine("Address: " + walletAccount.Address, ConsoleOutputStyle.Information);
Expand Down
2 changes: 1 addition & 1 deletion src/NeoSharp.Core/Wallet/IWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface IWallet
/// accounts is an list of Account objects which describe the details
/// of each account in the wallet.
/// </summary>
HashSet<IWalletAccount> Accounts { get; set; }
IWalletAccount[] Accounts { get; set; }

/// <summary>
/// extra is an object that is defined by the implementor of the client
Expand Down
10 changes: 9 additions & 1 deletion src/NeoSharp.Core/Wallet/IWalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ public interface IWalletManager
bool Contains(UInt160 scriptHash);

/// <summary>
/// Creates the account with random private key.
/// Creates the account with random private key.
/// It does not add it to the user Wallet.
/// </summary>
/// <returns>The account.</returns>
IWalletAccount CreateAccount(SecureString password);

/// <summary>
/// Creates the account with random private key
/// and adds it to the current Wallet.
/// </summary>
/// <returns>The account.</returns>
IWalletAccount CreateAndAddAccount(SecureString password);

/// <summary>
/// Remove the account.
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/NeoSharp.Core/Wallet/NEP6/NEP6Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,31 @@ namespace NeoSharp.Core.Wallet.NEP6
public class NEP6Account : IWalletAccount, IEquatable<NEP6Account>
{
/// <inheritdoc />
[JsonProperty("address")]
public string Address { get; set; }

/// <inheritdoc />
[JsonIgnore]
public UInt160 ScriptHash => Contract.ScriptHash;

/// <inheritdoc />>
[JsonProperty("label")]
public string Label { get; set; }

/// <inheritdoc />
[JsonProperty("isDefault")]
public bool IsDefault { get; set; }

/// <inheritdoc />
[JsonProperty("lock")]
public bool Lock { get; set; }

/// <inheritdoc />
[JsonProperty("key")]
public string Key { get; set; }

/// <inheritdoc />
[JsonProperty("contract")]
public Contract Contract { get; set; }

[JsonProperty("extra")]
Expand All @@ -37,7 +44,6 @@ public class NEP6Account : IWalletAccount, IEquatable<NEP6Account>
Address = accountContract.ScriptHash.ToAddress();
}


public override bool Equals(object obj)
{
if (obj is NEP6Account acc)
Expand Down
11 changes: 8 additions & 3 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
namespace NeoSharp.Core.Wallet.NEP6
{
public class NEP6Wallet : IWallet
{
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("scrypt")]
public ScryptParameters Scrypt { get; set; }

[JsonProperty("accounts")]
[JsonConverter(typeof(NEP6AccountConverter))]
public HashSet<IWalletAccount> Accounts { get; set; }
public IWalletAccount[] Accounts { get; set; }

//TODO #359: Replace JObject
[JsonProperty("extra")]
public Object Extra { get; set; }

public NEP6Wallet()
{
Scrypt = ScryptParameters.Default;
Accounts = new HashSet<IWalletAccount>();
Accounts = new IWalletAccount[0];
}
}
}
31 changes: 24 additions & 7 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public bool Contains(UInt160 scriptHash)
.FirstOrDefault() != null;
}

/// <inheritdoc />
public IWalletAccount CreateAndAddAccount(SecureString password)
{
var walletAccount = CreateAccount(password);
AddAccount(walletAccount);
return walletAccount;
}

/// <inheritdoc />
public IWalletAccount CreateAccount(SecureString password)
{
Expand All @@ -93,8 +101,8 @@ public void DeleteAccount(UInt160 scriptHash)
throw new ArgumentNullException();
}
CheckWalletIsOpen();
Wallet.Accounts = Wallet.Accounts.Where(x => !x.Contract.ScriptHash.Equals(scriptHash)).ToHashSet();

Wallet.Accounts = Wallet.Accounts.Where(x => !x.Contract.ScriptHash.Equals(scriptHash)).ToArray();
SaveWallet();
}

Expand Down Expand Up @@ -302,14 +310,18 @@ private void AddOrReplaceAccount(IWalletAccount account)
Lock = account.Lock
};

Wallet.Accounts.Remove(account);
List<IWalletAccount> accounts = new List<IWalletAccount>(Wallet.Accounts);
accounts.Remove(account);
Wallet.Accounts = accounts.ToArray();

AddAccount(clonedAccount);
}

SaveWallet();
}



/// <summary>
/// Retrieves the account from the Account list using the script hash/
/// Replaces the account information with those provided in the newAccountInformation parameter
Expand Down Expand Up @@ -357,9 +369,14 @@ private void AddAccount(IWalletAccount account)

account.ValidateAccount();

//Accounts is a set, it cannot contain duplicates.
Wallet.Accounts.Add(account);
SaveWallet();
var accountList = Wallet.Accounts.ToList();

if(!accountList.Contains(account))
{
accountList.Add(account);
Wallet.Accounts = accountList.ToArray();
SaveWallet();
}
}

/// <summary>
Expand Down Expand Up @@ -463,7 +480,7 @@ private void ValidateAccountsPasswordMismatch(SecureString password, out byte[]
throw new AccountsPasswordMismatchException();
}
}
else if (Wallet.Accounts != null && Wallet.Accounts.Count > 0)
else if (Wallet.Accounts != null && Wallet.Accounts.Length > 0)
{
try
{
Expand Down
18 changes: 9 additions & 9 deletions test/NeoSharp.Core.Test/Wallet/NEP6WalletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ public void TestDeleteAccount()
var mockWalletManager = GetAWalletManagerWithAnWallet();

// Act
IWalletAccount walletAccount = mockWalletManager.CreateAccount(_defaultPassword);
IWalletAccount walletAccount = mockWalletManager.CreateAndAddAccount(_defaultPassword);

Assert.IsTrue(mockWalletManager.Wallet.Accounts.ToList().Count == 1);
Assert.IsTrue(mockWalletManager.Wallet.Accounts.Length == 1);

mockWalletManager.DeleteAccount(walletAccount.Contract.ScriptHash);

Assert.IsTrue(mockWalletManager.Wallet.Accounts.ToList().Count == 0);
Assert.IsTrue(mockWalletManager.Wallet.Accounts.Length == 0);
}

[TestMethod]
Expand All @@ -263,9 +263,9 @@ public void TestDeleteAccountFalse()
var mockWalletManager = GetAWalletManagerWithAnWallet();

// Act
IWalletAccount walletAccount1 = mockWalletManager.CreateAccount(_defaultPassword);
IWalletAccount walletAccount1 = mockWalletManager.CreateAndAddAccount(_defaultPassword);

IWalletAccount walletAccount2 = mockWalletManager.CreateAccount(_defaultPassword);
IWalletAccount walletAccount2 = mockWalletManager.CreateAndAddAccount(_defaultPassword);

Assert.IsTrue(mockWalletManager.Wallet.Accounts.ToList().Count == 2);

Expand All @@ -279,7 +279,7 @@ public void TestDeleteAccountFalse()
public void TestDeleteAccountWalletIsNotOpened()
{
var mockWalletManager = GetAWalletManagerWithoutAnWallet();
IWalletAccount walletAccount = mockWalletManager.CreateAccount(_defaultPassword);
IWalletAccount walletAccount = mockWalletManager.CreateAndAddAccount(_defaultPassword);

Assert.IsTrue(mockWalletManager.Wallet.Accounts.ToList().Count == 1);

Expand Down Expand Up @@ -307,7 +307,7 @@ public void TestImportScriptHash()
var mockWalletManager = GetAWalletManagerWithAnWallet();

// Act
NEP6Account walletAccount1 = (NEP6Account)mockWalletManager.CreateAccount(_defaultPassword);
NEP6Account walletAccount1 = (NEP6Account)mockWalletManager.CreateAndAddAccount(_defaultPassword);

NEP6Account walletAccount2 = (NEP6Account)mockWalletManager.ImportScriptHash(walletAccount1.ScriptHash);

Expand All @@ -318,7 +318,7 @@ public void TestImportScriptHash()
//TODO #412: Check & improve address verification testing
//Assert.IsFalse(String.IsNullOrEmpty(walletAccount2.Address));

Assert.IsTrue(mockWalletManager.Wallet.Accounts.Count == 1);
Assert.IsTrue(mockWalletManager.Wallet.Accounts.Length == 1);
}

[TestMethod]
Expand Down Expand Up @@ -347,7 +347,7 @@ public void TestImportScriptHashWalletIsNotOpened()
{
var mockWalletManager = GetAWalletManagerWithoutAnWallet();
// Act
NEP6Account walletAccount1 = (NEP6Account)mockWalletManager.CreateAccount(_defaultPassword);
NEP6Account walletAccount1 = (NEP6Account)mockWalletManager.CreateAndAddAccount(_defaultPassword);

mockWalletManager = new Nep6WalletManager(new FileWrapper(), new JsonConverterWrapper());
NEP6Account walletAccount2 = (NEP6Account)mockWalletManager.ImportScriptHash(walletAccount1.ScriptHash);
Expand Down

0 comments on commit b34b7ec

Please sign in to comment.