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;
  • Loading branch information
lock9 committed Sep 18, 2018
1 parent 3d872c2 commit f4f5b7a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
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
12 changes: 12 additions & 0 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,41 @@ 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")]
public Object Extra { get; set; }

public NEP6Account()
{

}

public NEP6Account(Contract accountContract){
Contract = accountContract;
Address = accountContract.ScriptHash.ToAddress();
Expand Down
12 changes: 8 additions & 4 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@
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: Replace JObject
[JsonProperty("extra")]
public Object Extra { get; set; }

public NEP6Wallet()
{
Scrypt = ScryptParameters.Default;
Accounts = new HashSet<IWalletAccount>();
Accounts = new IWalletAccount[0];
}
}
}
21 changes: 14 additions & 7 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,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,7 +302,9 @@ 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);
}
Expand Down Expand Up @@ -357,9 +359,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 +470,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
2 changes: 1 addition & 1 deletion test/NeoSharp.Core.Test/Wallet/NEP6WalletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void TestImportScriptHash()
//TODO: Check & improve
//Assert.IsFalse(String.IsNullOrEmpty(walletAccount2.Address));

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

[TestMethod]
Expand Down

0 comments on commit f4f5b7a

Please sign in to comment.