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
4 changes: 2 additions & 2 deletions src/Plugins/SQLiteWallet/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Neo.Wallets.SQLite
{
internal class Account
{
public byte[] PublicKeyHash { get; set; }
public string Nep2key { get; set; }
public required byte[] PublicKeyHash { get; set; }
public required string Nep2key { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Plugins/SQLiteWallet/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ namespace Neo.Wallets.SQLite
{
internal class Address
{
public byte[] ScriptHash { get; set; }
public required byte[] ScriptHash { get; set; }
}
}
10 changes: 5 additions & 5 deletions src/Plugins/SQLiteWallet/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace Neo.Wallets.SQLite
{
internal class Contract
{
public byte[] RawData { get; set; }
public byte[] ScriptHash { get; set; }
public byte[] PublicKeyHash { get; set; }
public Account Account { get; set; }
public Address Address { get; set; }
public required byte[] RawData { get; set; }
public required byte[] ScriptHash { get; set; }
public required byte[] PublicKeyHash { get; set; }
public Account? Account { get; set; }
public Address? Address { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Plugins/SQLiteWallet/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Neo.Wallets.SQLite
{
internal class Key
{
public string Name { get; set; }
public byte[] Value { get; set; }
public required string Name { get; set; }
public required byte[] Value { get; set; }
}
}
43 changes: 24 additions & 19 deletions src/Plugins/SQLiteWallet/SQLiteWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using Microsoft.EntityFrameworkCore;
using Neo.Cryptography;
using Neo.Extensions;
Expand Down Expand Up @@ -121,14 +119,14 @@ private void AddAccount(SQLiteWalletAccount account)
{
lock (_lock)
{
if (_accounts.TryGetValue(account.ScriptHash, out var account_old))
if (_accounts.TryGetValue(account.ScriptHash, out var accountOld))
{
account.Contract ??= account_old.Contract;
account.Contract ??= accountOld.Contract;
}
_accounts[account.ScriptHash] = account;

using var ctx = new WalletDataContext(Path);
if (account.HasKey)
if (account.Key is not null)
{
var dbAccount = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == account.Key.PublicKeyHash.ToArray());
if (dbAccount == null)
Expand All @@ -144,10 +142,13 @@ private void AddAccount(SQLiteWalletAccount account)
dbAccount.Nep2key = account.Key.Export(_masterKey, ProtocolSettings.AddressVersion, _scrypt.N, _scrypt.R, _scrypt.P);
}
}
if (account.Contract != null)
if (account.Contract is not null)
{
if (account.Key is null) // If no Key, cannot get PublicKeyHash
throw new InvalidOperationException("Account.Contract is not null when Account.Key is null");

var dbContract = ctx.Contracts.FirstOrDefault(p => p.ScriptHash == account.Contract.ScriptHash.ToArray());
if (dbContract != null)
if (dbContract is not null)
{
dbContract.PublicKeyHash = account.Key.PublicKeyHash.ToArray();
}
Expand All @@ -161,7 +162,8 @@ private void AddAccount(SQLiteWalletAccount account)
});
}
}
//add address

// add address
{
var dbAddress = ctx.Addresses.FirstOrDefault(p => p.ScriptHash == account.ScriptHash.ToArray());
if (dbAddress == null)
Expand Down Expand Up @@ -246,18 +248,18 @@ public override WalletAccount CreateAccount(byte[] privateKey)

public override WalletAccount CreateAccount(SmartContract.Contract contract, KeyPair? key = null)
{
if (contract is not VerificationContract verification_contract)
if (contract is not VerificationContract verificationContract)
{
verification_contract = new VerificationContract
verificationContract = new()
{
Script = contract.Script,
ParameterList = contract.ParameterList
};
}
var account = new SQLiteWalletAccount(verification_contract.ScriptHash, ProtocolSettings)
var account = new SQLiteWalletAccount(verificationContract.ScriptHash, ProtocolSettings)
{
Key = key,
Contract = verification_contract
Contract = verificationContract
};
AddAccount(account);
return account;
Expand Down Expand Up @@ -286,12 +288,12 @@ public override bool DeleteAccount(UInt160 scriptHash)
if (_accounts.Remove(scriptHash, out var account))
{
using var ctx = new WalletDataContext(Path);
if (account.HasKey)
if (account.Key is not null)
{
var dbAccount = ctx.Accounts.First(p => p.PublicKeyHash == account.Key.PublicKeyHash.ToArray());
ctx.Accounts.Remove(dbAccount);
}
if (account.Contract != null)
if (account.Contract is not null)
{
var dbContract = ctx.Contracts.First(p => p.ScriptHash == scriptHash.ToArray());
ctx.Contracts.Remove(dbContract);
Expand Down Expand Up @@ -320,7 +322,6 @@ public override bool DeleteAccount(UInt160 scriptHash)
public override IEnumerable<WalletAccount> GetAccounts()
{
SQLiteWalletAccount[] accounts;

lock (_lock)
{
accounts = [.. _accounts.Values];
Expand All @@ -331,14 +332,20 @@ public override IEnumerable<WalletAccount> GetAccounts()

private Dictionary<UInt160, SQLiteWalletAccount> LoadAccounts(WalletDataContext ctx)
{
var accounts = ctx.Addresses.Select(p => new SQLiteWalletAccount(p.ScriptHash, ProtocolSettings))
var accounts = ctx.Addresses
.Select(p => new SQLiteWalletAccount(p.ScriptHash, ProtocolSettings))
.ToDictionary(p => p.ScriptHash);
foreach (var dbContract in ctx.Contracts.Include(p => p.Account))
{
var contract = dbContract.RawData.AsSerializable<VerificationContract>();
var account = accounts[contract.ScriptHash];
account.Contract = contract;
account.Key = new KeyPair(GetPrivateKeyFromNEP2(dbContract.Account.Nep2key, _masterKey, ProtocolSettings.AddressVersion, _scrypt.N, _scrypt.R, _scrypt.P));
if (dbContract.Account is not null)
{
var privateKey = GetPrivateKeyFromNEP2(dbContract.Account.Nep2key, _masterKey,
ProtocolSettings.AddressVersion, _scrypt.N, _scrypt.R, _scrypt.P);
account.Key = new KeyPair(privateKey);
}
}
return accounts;
}
Expand Down Expand Up @@ -447,5 +454,3 @@ internal static byte[] ToAesKey(string password)
}
}
}

#nullable disable
1 change: 1 addition & 0 deletions src/Plugins/SQLiteWallet/SQLiteWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<RootNamespace>Neo.Wallets.SQLite</RootNamespace>
<PackageId>Neo.Wallets.SQLite</PackageId>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 3 additions & 8 deletions src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ namespace Neo.Wallets.SQLite
{
internal sealed class SQLiteWalletAccount : WalletAccount
{
public KeyPair Key;
public KeyPair? Key;

public override bool HasKey => Key != null;

public SQLiteWalletAccount(UInt160 scriptHash, ProtocolSettings settings)
: base(scriptHash, settings)
{
}
: base(scriptHash, settings) { }

public override KeyPair GetKey()
{
return Key;
}
public override KeyPair? GetKey() => Key;
}
}
4 changes: 2 additions & 2 deletions src/Plugins/SQLiteWallet/VerificationContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public void Deserialize(ref MemoryReader reader)
Script = reader.ReadVarMemory().ToArray();
}

public bool Equals(VerificationContract other)
public bool Equals(VerificationContract? other)
{
if (ReferenceEquals(this, other)) return true;
if (other is null) return false;
return ScriptHash.Equals(other.ScriptHash);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return Equals(obj as VerificationContract);
}
Expand Down