diff --git a/src/Plugins/SQLiteWallet/Account.cs b/src/Plugins/SQLiteWallet/Account.cs index facc3d112d..30ffc53700 100644 --- a/src/Plugins/SQLiteWallet/Account.cs +++ b/src/Plugins/SQLiteWallet/Account.cs @@ -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; } } } diff --git a/src/Plugins/SQLiteWallet/Address.cs b/src/Plugins/SQLiteWallet/Address.cs index 56642b081e..366a9acf63 100644 --- a/src/Plugins/SQLiteWallet/Address.cs +++ b/src/Plugins/SQLiteWallet/Address.cs @@ -13,6 +13,6 @@ namespace Neo.Wallets.SQLite { internal class Address { - public byte[] ScriptHash { get; set; } + public required byte[] ScriptHash { get; set; } } } diff --git a/src/Plugins/SQLiteWallet/Contract.cs b/src/Plugins/SQLiteWallet/Contract.cs index 3dbcf91a0a..e9ac6cb946 100644 --- a/src/Plugins/SQLiteWallet/Contract.cs +++ b/src/Plugins/SQLiteWallet/Contract.cs @@ -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; } } } diff --git a/src/Plugins/SQLiteWallet/Key.cs b/src/Plugins/SQLiteWallet/Key.cs index ed8614bb04..076e9ce1f1 100644 --- a/src/Plugins/SQLiteWallet/Key.cs +++ b/src/Plugins/SQLiteWallet/Key.cs @@ -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; } } } diff --git a/src/Plugins/SQLiteWallet/SQLiteWallet.cs b/src/Plugins/SQLiteWallet/SQLiteWallet.cs index 8d7d5093b8..14ea841d8d 100644 --- a/src/Plugins/SQLiteWallet/SQLiteWallet.cs +++ b/src/Plugins/SQLiteWallet/SQLiteWallet.cs @@ -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; @@ -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) @@ -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(); } @@ -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) @@ -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; @@ -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); @@ -320,7 +322,6 @@ public override bool DeleteAccount(UInt160 scriptHash) public override IEnumerable GetAccounts() { SQLiteWalletAccount[] accounts; - lock (_lock) { accounts = [.. _accounts.Values]; @@ -331,14 +332,20 @@ public override IEnumerable GetAccounts() private Dictionary 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(); 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; } @@ -447,5 +454,3 @@ internal static byte[] ToAesKey(string password) } } } - -#nullable disable diff --git a/src/Plugins/SQLiteWallet/SQLiteWallet.csproj b/src/Plugins/SQLiteWallet/SQLiteWallet.csproj index 400adc98aa..bdf1bbea91 100644 --- a/src/Plugins/SQLiteWallet/SQLiteWallet.csproj +++ b/src/Plugins/SQLiteWallet/SQLiteWallet.csproj @@ -5,6 +5,7 @@ Neo.Wallets.SQLite Neo.Wallets.SQLite enable + enable diff --git a/src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs b/src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs index bb713357c6..611d1436a7 100644 --- a/src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs +++ b/src/Plugins/SQLiteWallet/SQLiteWalletAccount.cs @@ -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; } } diff --git a/src/Plugins/SQLiteWallet/VerificationContract.cs b/src/Plugins/SQLiteWallet/VerificationContract.cs index 3857e6492b..d0160488ec 100644 --- a/src/Plugins/SQLiteWallet/VerificationContract.cs +++ b/src/Plugins/SQLiteWallet/VerificationContract.cs @@ -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); }