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

Commit

Permalink
Test fixes (+1 squashed commit) (#440)
Browse files Browse the repository at this point in the history
Squashed commits:
[ee0bd01] Account alias
Code improvements (+1 squashed commit)
Squashed commits:
[438524e] Account alias
  • Loading branch information
lock9 authored and osmirnov committed Sep 25, 2018
1 parent d97733d commit 014f22a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 140 deletions.
33 changes: 21 additions & 12 deletions src/NeoSharp.Application/Controllers/PromptWalletController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NeoSharp.Core.Extensions;
using NeoSharp.Core.Types;
using NeoSharp.Core.Wallet;
using NeoSharp.Core.Wallet.Exceptions;
using NeoSharp.Core.Wallet.Helpers;

namespace NeoSharp.Application.Controllers
Expand Down Expand Up @@ -98,19 +99,18 @@ public void WalletSaveCommand(string fileName)
[PromptCommand("account create", Category = "Account", Help = "Create a new account")]
public void AccountCreateCommand()
{
var secureString = _consoleReader.ReadPassword();
var isUsingSameWalletPassword = _walletManager.CheckIfPasswordMatchesOpenWallet(secureString);

if (isUsingSameWalletPassword)
var secureString = _consoleReader.ReadPassword("Wallet password:");
try
{
_walletManager.CheckIfPasswordMatchesOpenWallet(secureString);
_consoleWriter.ApplyStyle(ConsoleOutputStyle.Prompt);
var walletAccount = _walletManager.CreateAndAddAccount(secureString);
_consoleWriter.WriteLine("\nAddress: " + walletAccount.Address, ConsoleOutputStyle.Information);
_consoleWriter.WriteLine("Public Key: " + _walletManager.GetPublicKeyFromNep2(walletAccount.Key, secureString), ConsoleOutputStyle.Information);
}
else
catch(AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nPasswords don't match.");
_consoleWriter.WriteLine("\nInvalid password.");
}
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public void AccountExportNep2(string address)
_consoleWriter.WriteLine("\nPasswords don't match.");
}
}
catch (Exception)
catch (AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nInvalid password.");
}
Expand All @@ -170,7 +170,7 @@ public void AccountExportWif(string address)
string wif = _walletManager.PrivateKeyToWif(accountPrivateKey);
_consoleWriter.WriteLine("\nExported wif: " + wif);
}
catch (Exception)
catch (AccountsPasswordMismatchException)
{
_consoleWriter.WriteLine("\nInvalid password.");
}
Expand All @@ -179,21 +179,30 @@ public void AccountExportWif(string address)
{
_consoleWriter.WriteLine("\nAccount not found.");
}
}

[PromptCommand("account alias", Category = "Account", Help = "Adds a label to an account")]
public void AddAccountAlias(string address, string alias)
{
UInt160 accountScriptHash = address.ToScriptHash();
if(_walletManager.Contains(accountScriptHash))
{
_walletManager.UpdateAccountAlias(accountScriptHash, alias);
}else
{
_consoleWriter.WriteLine("\nAccount not found.");
}
}


/*
TODO #404: Implement additional wallet features
wallet delete_addr {addr}
wallet delete_token {token_contract_hash}
wallet alias {addr} {title}
import multisig_addr {pubkey in wallet} {minimum # of signatures required} {signing pubkey 1} {signing pubkey 2}...
import watch_addr {address}
import token {token_contract_hash}
export wif {address}
export nep2 {address}
*/

/*
Expand Down
25 changes: 17 additions & 8 deletions src/NeoSharp.Core/Wallet/IWalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ public interface IWalletManager
void DeleteAccount(UInt160 scriptHash);

/// <summary>
/// Gets the account.
/// Gets the account using the account <paramref name="alias"/>(label property).
/// </summary>
/// <returns>The account.</returns>
/// <param name="scriptHash">Scripthash's account.</param>
IWalletAccount GetAccount(string alias);

/// <summary>
/// Gets the account using the account scripthash.
/// </summary>
/// <returns>The account.</returns>
/// <param name="scriptHash">Scripthash's account.</param>
Expand Down Expand Up @@ -106,13 +113,9 @@ public interface IWalletManager
IWalletAccount ImportEncryptedWif(string nep2, SecureString password);

/// <summary>
/// Verifies the password.
/// Saves the current wallet into the HD
/// </summary>
/// <returns><c>true</c>, if password was verifyed, <c>false</c>
/// otherwise.</returns>
/// <param name="walletAccout">Wallet accout.</param>
/// <param name="password">Password.</param>
bool VerifyPassword(IWalletAccount walletAccout, SecureString password);
void SaveWallet();

/// <summary>
/// Checks the wallet is open.
Expand All @@ -122,7 +125,7 @@ public interface IWalletManager
/// <summary>
/// Check if the password provided is the same used in the first wallet account.
/// </summary>
bool CheckIfPasswordMatchesOpenWallet(SecureString password);
void CheckIfPasswordMatchesOpenWallet(SecureString password);

/// <summary>
/// save the open wallet into a specific filename
Expand Down Expand Up @@ -171,5 +174,11 @@ public interface IWalletManager
/// <param name="wif">Wif.</param>
byte[] GetPrivateKeyFromWIF(string wif);

/// <summary>
/// Adds/replaces an account alias (label)
/// </summary>
/// <param name="scripthash">Account scripthash.</param>
/// <param name="alias">Account label</param>
void UpdateAccountAlias(UInt160 scripthash, string alias);
}
}
116 changes: 46 additions & 70 deletions src/NeoSharp.Core/Wallet/NEP6/NEP6WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ public IWalletAccount CreateAndAddAccount(SecureString password)
public IWalletAccount CreateAccount(SecureString password)
{
CheckWalletIsOpen();
byte[] passwordHash;
ValidateAccountsPasswordMismatch(password, out passwordHash);
CheckIfPasswordMatchesOpenWallet(password);

var privateKey = Crypto.Default.GenerateRandomBytes(32);
var account = ImportPrivateKey(privateKey, password);
Array.Clear(privateKey, 0, privateKey.Length);
_accountPasswordHashCache = passwordHash;
_accountPasswordHashCache = GetPasswordHash(password);
return account;
}

Expand All @@ -108,6 +107,19 @@ public void DeleteAccount(UInt160 scriptHash)
SaveWallet();
}

public IWalletAccount GetAccount(string alias)
{
if (alias == null)
{
throw new ArgumentNullException();
}
CheckWalletIsOpen();

return Wallet.Accounts
.Where(x => x.Label.Equals(alias))
.FirstOrDefault();
}

/// <inheritdoc />
public IWalletAccount GetAccount(UInt160 scriptHash)
{
Expand Down Expand Up @@ -135,6 +147,14 @@ public IWalletAccount GetAccount(ECPoint pubkey)
return GetAccount(scriptHash);
}

/// <inheritdoc />
public void UpdateAccountAlias(UInt160 scripthash, string alias)
{
var account = GetAccount(scripthash);
account.Label = alias;
SaveWallet();
}

/// <inheritdoc />
public IWalletAccount ImportScriptHash(UInt160 scriptHash)
{
Expand Down Expand Up @@ -221,26 +241,6 @@ public IWalletAccount ImportEncryptedWif(string nep2, SecureString passphrase)
return account;
}

/// <inheritdoc />
public bool VerifyPassword(IWalletAccount walletAccout, SecureString password)
{
if (walletAccout == null)
{
throw new ArgumentException();
}

try
{
_walletHelper.DecryptWif(walletAccout.Key, password);
}

catch (FormatException)
{
return false;
}

return true;
}

/// <inheritdoc />
public void ExportWallet(String filename)
Expand Down Expand Up @@ -312,30 +312,40 @@ public void CheckWalletIsOpen()
}

/// <inheritdoc />
public bool CheckIfPasswordMatchesOpenWallet(SecureString password)
public void CheckIfPasswordMatchesOpenWallet(SecureString password)
{
CheckWalletIsOpen();
bool isSamePassword = true;
if (Wallet.Accounts.Length > 0)
byte[] passwordHash = GetPasswordHash(password);

if (_accountPasswordHashCache != null)
{
if (!passwordHash.SequenceEqual(_accountPasswordHashCache))
{
throw new AccountsPasswordMismatchException();
}
}

if (Wallet.Accounts != null && Wallet.Accounts.Length > 0)
{
var walletAccount = Wallet.Accounts[0];
try
{
byte[] decryptedKey = _walletHelper.DecryptWif(walletAccount.Key, password);
if (decryptedKey == null)
{
isSamePassword = false; ;
}
_walletHelper.DecryptWif(Wallet.Accounts.First().Key, password);
}
catch (FormatException)
{
isSamePassword = false;
throw new AccountsPasswordMismatchException();
}
}

return isSamePassword;
}

/// <summary>
/// save the open wallet into the same file
/// </summary>
public void SaveWallet()
{
CheckWalletIsOpen();
ExportWallet(_openWalletFilename);
}

/// <summary>
/// Adds the or replace an account in the system.
Expand Down Expand Up @@ -373,8 +383,6 @@ private void AddOrReplaceAccount(IWalletAccount account)
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 @@ -404,13 +412,7 @@ private IWalletAccount CloneAndUpdate(IWalletAccount newAccountInformation)
return clonedAccount;
}

/// <summary>
/// save the open wallet into the same file
/// </summary>
private void SaveWallet()
{
ExportWallet(_openWalletFilename);
}


/// <summary>
/// Adds the account to the Account list.
Expand Down Expand Up @@ -496,31 +498,5 @@ private void UnlockAccount(string nep2Key, ECPoint publicKey, byte[] privateKey)
_unlockedAccounts.Add(nep2Key, entry);
}
}

private void ValidateAccountsPasswordMismatch(SecureString password, out byte[] passwordHash)
{
CheckWalletIsOpen();

passwordHash = GetPasswordHash(password);

if (_accountPasswordHashCache != null) {
if (!passwordHash.SequenceEqual(_accountPasswordHashCache))
{
throw new AccountsPasswordMismatchException();
}
}
else if (Wallet.Accounts != null && Wallet.Accounts.Length > 0)
{
try
{
_walletHelper.DecryptWif(Wallet.Accounts.First().Key, password);
}
catch (FormatException)
{
throw new AccountsPasswordMismatchException();
}
}
}

}
}
Loading

0 comments on commit 014f22a

Please sign in to comment.