Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions neo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestServer", "src\Plugins\R
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Plugins.StateService.Tests", "tests\Neo.Plugins.StateService.Tests\Neo.Plugins.StateService.Tests.csproj", "{229C7877-C0FA-4399-A0DB-96E714A59481}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Plugins.SQLiteWallet.Tests", "tests\Neo.Plugins.SQLiteWallet.Tests\Neo.Plugins.SQLiteWallet.Tests.csproj", "{92E091FE-C7E0-4526-8352-779B73F55F13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -277,6 +279,10 @@ Global
{229C7877-C0FA-4399-A0DB-96E714A59481}.Debug|Any CPU.Build.0 = Debug|Any CPU
{229C7877-C0FA-4399-A0DB-96E714A59481}.Release|Any CPU.ActiveCfg = Release|Any CPU
{229C7877-C0FA-4399-A0DB-96E714A59481}.Release|Any CPU.Build.0 = Release|Any CPU
{92E091FE-C7E0-4526-8352-779B73F55F13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92E091FE-C7E0-4526-8352-779B73F55F13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92E091FE-C7E0-4526-8352-779B73F55F13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92E091FE-C7E0-4526-8352-779B73F55F13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -326,6 +332,7 @@ Global
{A7FE2B30-11F8-E88D-D5BF-AF1B11EFEC8E} = {7F257712-D033-47FF-B439-9D4320D06599}
{4865C487-C1A1-4E36-698D-1EC4CCF08FDB} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{229C7877-C0FA-4399-A0DB-96E714A59481} = {7F257712-D033-47FF-B439-9D4320D06599}
{92E091FE-C7E0-4526-8352-779B73F55F13} = {7F257712-D033-47FF-B439-9D4320D06599}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCBA19D9-F868-4C6D-8061-A2B91E06E3EC}
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.Wallets.SQLite
{
class Account
internal class Account
{
public byte[] PublicKeyHash { get; set; }
public string Nep2key { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.Wallets.SQLite
{
class Address
internal class Address
{
public byte[] ScriptHash { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.Wallets.SQLite
{
class Contract
internal class Contract
{
public byte[] RawData { get; set; }
public byte[] ScriptHash { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.Wallets.SQLite
{
class Key
internal class Key
{
public string Name { get; set; }
public byte[] Value { get; set; }
Expand Down
27 changes: 17 additions & 10 deletions src/Plugins/SQLiteWallet/SQLiteWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Neo.Wallets.SQLite
/// <summary>
/// A wallet implementation that uses SQLite as the underlying storage.
/// </summary>
class SQLiteWallet : Wallet
internal class SQLiteWallet : Wallet
{
private readonly Lock _lock = new();
private readonly byte[] _iv;
Expand Down Expand Up @@ -57,28 +57,35 @@ public override Version Version
}
}

/// <summary>
/// Opens a wallet at the specified path.
/// </summary>
private SQLiteWallet(string path, byte[] passwordKey, ProtocolSettings settings) : base(path, settings)
{
if (!File.Exists(path)) throw new InvalidOperationException($"Wallet file {path} not found");

using var ctx = new WalletDataContext(Path);
_salt = LoadStoredData(ctx, "Salt")
?? throw new FormatException("Salt was not found");
var passwordHash = LoadStoredData(ctx, "PasswordHash")
?? throw new FormatException("PasswordHash was not found");
if (!passwordHash.SequenceEqual(passwordKey.Concat(_salt).ToArray().Sha256()))
throw new CryptographicException();
_iv = LoadStoredData(ctx, "IV")
?? throw new FormatException("IV was not found");
throw new CryptographicException("Invalid password");

_iv = LoadStoredData(ctx, "IV") ?? throw new FormatException("IV was not found");
_masterKey = Decrypt(LoadStoredData(ctx, "MasterKey")
?? throw new FormatException("MasterKey was not found"), passwordKey, _iv);
_scrypt = new ScryptParameters
(
_scrypt = new ScryptParameters(
BinaryPrimitives.ReadInt32LittleEndian(LoadStoredData(ctx, "ScryptN") ?? throw new FormatException("ScryptN was not found")),
BinaryPrimitives.ReadInt32LittleEndian(LoadStoredData(ctx, "ScryptR") ?? throw new FormatException("ScryptR was not found")),
BinaryPrimitives.ReadInt32LittleEndian(LoadStoredData(ctx, "ScryptP") ?? throw new FormatException("ScryptP was not found"))
);
);
_accounts = LoadAccounts(ctx);
}

/// <summary>
/// Creates a new wallet at the specified path.
/// </summary>
private SQLiteWallet(string path, byte[] passwordKey, ProtocolSettings settings, ScryptParameters scrypt) : base(path, settings)
{
_iv = new byte[16];
Expand Down Expand Up @@ -397,7 +404,7 @@ public override bool VerifyPassword(string password)
return ToAesKey(password).Concat(_salt).ToArray().Sha256().SequenceEqual(hash);
}

private static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
internal static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
ArgumentNullException.ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(key, nameof(key));
Expand All @@ -413,7 +420,7 @@ private static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
return encryptor.TransformFinalBlock(data, 0, data.Length);
}

private static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
internal static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
{
ArgumentNullException.ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(key, nameof(key));
Expand All @@ -429,7 +436,7 @@ private static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
return decryptor.TransformFinalBlock(data, 0, data.Length);
}

private static byte[] ToAesKey(string password)
internal static byte[] ToAesKey(string password)
{
var passwordBytes = Encoding.UTF8.GetBytes(password);
var passwordHash = SHA256.HashData(passwordBytes);
Expand Down
4 changes: 4 additions & 0 deletions src/Plugins/SQLiteWallet/SQLiteWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.7" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Neo.Plugins.SQLiteWallet.Tests" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Neo.Wallets.SQLite
{
sealed class SQLiteWalletAccount : WalletAccount
internal sealed class SQLiteWalletAccount : WalletAccount
{
public KeyPair Key;

Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/VerificationContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Neo.Wallets.SQLite
{
class VerificationContract : SmartContract.Contract, IEquatable<VerificationContract>, ISerializable
internal class VerificationContract : SmartContract.Contract, IEquatable<VerificationContract>, ISerializable
{
public int Size => ParameterList.GetVarSize() + Script.GetVarSize();

Expand Down
8 changes: 4 additions & 4 deletions src/Plugins/SQLiteWallet/WalletDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@

namespace Neo.Wallets.SQLite
{
class WalletDataContext : DbContext
internal class WalletDataContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Contract> Contracts { get; set; }
public DbSet<Key> Keys { get; set; }

private readonly string filename;
private readonly string _filename;

public WalletDataContext(string filename)
{
this.filename = filename;
_filename = filename;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var sb = new SqliteConnectionStringBuilder()
{
DataSource = filename
DataSource = _filename
};
optionsBuilder.UseSqlite(sb.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest" Version="$(MSTestVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
<ProjectReference Include="..\..\src\Plugins\SQLiteWallet\SQLiteWallet.csproj" />
</ItemGroup>

</Project>
Loading
Loading