Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support changing password for nep6 wallet #1504

Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d0ef09
Merge pull request #13 from neo-project/master
Qiao-Jin Mar 19, 2020
33e0e26
Merge pull request #16 from neo-project/master
Qiao-Jin Mar 23, 2020
8988eda
Merge pull request #20 from neo-project/master
Qiao-Jin Mar 23, 2020
e67fab5
Support changing password for Nep6 wallet
Mar 23, 2020
f44b115
Code optimization
Mar 23, 2020
b724a0a
Merge branch 'master' into support_changing_password_for_Nep6_wallet
shargon Mar 23, 2020
8cb7f22
Opde optimization
Mar 24, 2020
7f47701
Merge branch 'master' into support_changing_password_for_Nep6_wallet
shargon Mar 24, 2020
dbeb6fa
Code optimization
Mar 24, 2020
6e5bf94
Merge branch 'support_changing_password_for_Nep6_wallet' of https://g…
Mar 24, 2020
0af889c
Merge branch 'master' into support_changing_password_for_Nep6_wallet
Qiao-Jin Mar 25, 2020
5b2c0bf
Merge branch 'master' into support_changing_password_for_Nep6_wallet
Qiao-Jin Mar 27, 2020
fcf102b
Merge branch 'master' into support_changing_password_for_Nep6_wallet
Qiao-Jin Mar 27, 2020
0488b66
Update src/neo/Wallets/NEP6/NEP6Account.cs
Qiao-Jin Mar 27, 2020
f787d23
Change Nep6 wallet and account password changing strategy
Mar 27, 2020
8a34e66
Follow comment standard
shargon Mar 27, 2020
45d129f
Fix indent
shargon Mar 27, 2020
acc09c1
Fix indent
shargon Mar 27, 2020
d8ac5e6
Fix indent
shargon Mar 27, 2020
586bec2
Follow comment standard
shargon Mar 27, 2020
f0020f2
Merge remote-tracking branch 'Qiao-Jin/support_changing_password_for_…
shargon Mar 27, 2020
53d082c
Code optimization
Mar 30, 2020
d856f7f
Update NEP6Wallet.cs
erikzhang Mar 30, 2020
be37eb4
Rename methods
Mar 30, 2020
598a94a
Merge branch 'master' into support_changing_password_for_Nep6_wallet
Qiao-Jin Mar 31, 2020
defb346
Code optimization
Mar 31, 2020
4f8af9a
Code optimization
Apr 1, 2020
c00c3d1
Code optimization
Apr 1, 2020
7ebdd05
Merge branch 'master' into support_changing_password_for_Nep6_wallet
shargon Apr 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/neo/Wallets/NEP6/NEP6Account.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Neo.IO.Json;
using System;
using System.Threading;

namespace Neo.Wallets.NEP6
{
internal class NEP6Account : WalletAccount
{
private readonly NEP6Wallet wallet;
private readonly string nep2key;
private string nep2key;
private string nep2KeyNew = null;
private KeyPair key;
public JObject Extra;

Expand Down Expand Up @@ -83,5 +85,49 @@ public bool VerifyPassword(string password)
return false;
}
}

/// <summary>
/// Cache draft nep2key during wallet password changing process. Should not be called alone for a single account
/// </summary>
internal bool ChangePasswordPrepare(string password_old, string password_new)
{
if (WatchOnly) return true;
shargon marked this conversation as resolved.
Show resolved Hide resolved
KeyPair keyTemplate = null;
if (nep2key == null)
{
if (key == null)
{
return true;
}
}
else
{
try
{
keyTemplate = new KeyPair(Wallet.GetPrivateKeyFromNEP2(nep2key, password_old, wallet.Scrypt.N, wallet.Scrypt.R, wallet.Scrypt.P));
}
catch
{
return false;
}
}
nep2KeyNew = keyTemplate.Export(password_new, wallet.Scrypt.N, wallet.Scrypt.R, wallet.Scrypt.P);
shargon marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

internal bool ChangePasswordCommit()
{
if (nep2KeyNew != null)
{
nep2key = Interlocked.Exchange(ref nep2KeyNew, null);
return true;
}
return false;
}

internal void ChangePasswordRoolback()
{
nep2KeyNew = null;
}
}
}
30 changes: 30 additions & 0 deletions src/neo/Wallets/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using UserWallet = Neo.Wallets.SQLite.UserWallet;

namespace Neo.Wallets.NEP6
Expand Down Expand Up @@ -300,5 +301,34 @@ public override bool VerifyPassword(string password)
}
}
}

public bool ChangePassword(string password_old, string password_new)
{
bool succeed = true;
lock (accounts)
{
Parallel.ForEach(accounts.Values, (account, state) =>
{
if (!account.ChangePasswordPrepare(password_old, password_new))
{
state.Stop();
succeed = false;
}
});
}
if (succeed)
{
foreach (NEP6Account account in accounts.Values)
account.ChangePasswordCommit();
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (password != null)
password = password_new;
}
else
{
foreach (NEP6Account account in accounts.Values)
account.ChangePasswordRoolback();
}
return succeed;
}
}
}
20 changes: 20 additions & 0 deletions tests/neo.UnitTests/Wallets/NEP6/UT_NEP6Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ public void TestSetup()
_account = new NEP6Account(_wallet, _hash);
}

[TestMethod]
public void TestChangePassword()
{
_account = new NEP6Account(_wallet, _hash, _nep2);
_account.ChangePasswordPrepare("b", "Satoshi").Should().BeTrue();
_account.ChangePasswordCommit().Should().BeFalse();
_account.Contract = new Contract();
_wallet.Unlock("Satoshi");
_account.ChangePasswordPrepare("b", "Satoshi").Should().BeFalse();
_account.ChangePasswordPrepare("Satoshi", "b").Should().BeTrue();
_account.ChangePasswordCommit().Should().BeTrue();
_account.VerifyPassword("b").Should().BeTrue();
_account.ChangePasswordPrepare("b", "Satoshi").Should().BeTrue();
_account.ChangePasswordCommit().Should().BeTrue();
_account.ChangePasswordPrepare("Satoshi", "b").Should().BeTrue();
_account.ChangePasswordRoolback();
_account.VerifyPassword("Satoshi").Should().BeTrue();
_wallet.Lock();
}

[TestMethod]
public void TestConstructorWithNep2Key()
{
Expand Down
20 changes: 20 additions & 0 deletions tests/neo.UnitTests/Wallets/NEP6/UT_NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ public void TestCleanUp()
if (Directory.Exists(rootPath)) Directory.Delete(rootPath);
}

[TestMethod]
public void TestChangePassword()
{
JObject wallet = new JObject();
wallet["name"] = "name";
wallet["version"] = new System.Version("3.0").ToString();
wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson();
wallet["accounts"] = new JArray();
wallet["extra"] = new JObject();
File.WriteAllText(wPath, wallet.ToString());
uut = new NEP6Wallet(wPath);
uut.Unlock("123");
uut.CreateAccount(keyPair.PrivateKey);
uut.ChangePassword("456", "123").Should().BeFalse();
uut.ChangePassword("123", "456").Should().BeTrue();
uut.VerifyPassword("456").Should().BeTrue();
uut.ChangePassword("456", "123").Should().BeTrue();
uut.Lock();
}

[TestMethod]
public void TestConstructorWithPathAndName()
{
Expand Down