From 79211add4864c846b9ad16f409613f1bd2f40cb4 Mon Sep 17 00:00:00 2001 From: Julia Seward Date: Sun, 10 Sep 2023 13:59:45 -0400 Subject: [PATCH] fix: more unit tests, get rid of asdictionary --- .../Model/Errors/SdkErrors.cs | 10 +++---- .../Model/Errors/WalletConnectException.cs | 6 ++-- .../Utils/Extensions.cs | 28 ------------------- .../WalletConnectSharp.Crypto/Crypto.cs | 5 +++- .../WalletConnectSharp.Crypto/KeyChain.cs | 15 ++++++++-- .../Models/Error.cs | 13 ++++++++- .../Controllers/AuthEngine.cs | 4 +-- .../Controllers/JsonRpcHistory.cs | 5 +++- 8 files changed, 42 insertions(+), 44 deletions(-) diff --git a/Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs b/Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs index 5133bd4..db44bf6 100644 --- a/Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs +++ b/Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs @@ -27,9 +27,9 @@ public static class SdkErrors /// The error type message to generate /// A dictionary (or anonymous type) of parameters for the error message /// The error message as a string - public static string MessageFromType(ErrorType type, object @params = null) + public static string MessageFromType(ErrorType type, Dictionary @params = null) { - return MessageFromType(type, null, @params.AsDictionary()); + return MessageFromType(type, null, @params); } /// @@ -47,8 +47,8 @@ public static string MessageFromType(ErrorType type, string message = null, Dict @params = new Dictionary(); } - if (!string.IsNullOrWhiteSpace(message) && !@params.ContainsKey("message")) - @params.Add("message", message); + if (!string.IsNullOrWhiteSpace(message)) + @params.TryAdd("message", message); string errorMessage; switch (type) @@ -231,4 +231,4 @@ private static string FormatErrorText(string formattedText, DictionaryThe error type of the exception /// Additional (optional) parameters for the generated error message /// A new exception - public static WalletConnectException FromType(ErrorType type, object @params = null) + public static WalletConnectException FromType(ErrorType type, Dictionary @params = null) { - return FromType(type, null, @params.AsDictionary()); + return FromType(type, null, @params); } } -} \ No newline at end of file +} diff --git a/Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs b/Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs index 63b210e..e7ae344 100644 --- a/Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs +++ b/Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs @@ -11,34 +11,6 @@ namespace WalletConnectSharp.Common.Utils /// public static class Extensions { - /// - /// Convert an anonymous type to a Dictionary - /// - /// The anonymous type instance to convert to a dictionary - /// Enforce all keys to be lowercased - /// A dictionary where each key is the property name of the anonymous type - /// and each value is the property's value - public static Dictionary AsDictionary(this object obj, bool enforceLowercase = true) - { - if (obj is Dictionary objects) - return objects; - - var dict = new Dictionary(StringComparer.OrdinalIgnoreCase); - - if (obj != null) - { - foreach (PropertyInfo propertyDescriptor in obj.GetType().GetProperties()) - { - object value = propertyDescriptor.GetValue(obj, null); - var key = enforceLowercase ? propertyDescriptor.Name.ToLower() : propertyDescriptor.Name; - - dict.Add(key, value); - } - } - - return dict; - } - /// /// Returns true if the given object is a numeric type /// diff --git a/Core Modules/WalletConnectSharp.Crypto/Crypto.cs b/Core Modules/WalletConnectSharp.Crypto/Crypto.cs index 537c8a7..6b1ecc8 100644 --- a/Core Modules/WalletConnectSharp.Crypto/Crypto.cs +++ b/Core Modules/WalletConnectSharp.Crypto/Crypto.cs @@ -578,7 +578,10 @@ private void IsInitialized() { if (!this._initialized) { - throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name}); + throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary() + { + { "Name", Name } + }); } } diff --git a/Core Modules/WalletConnectSharp.Crypto/KeyChain.cs b/Core Modules/WalletConnectSharp.Crypto/KeyChain.cs index c3671af..511b57a 100644 --- a/Core Modules/WalletConnectSharp.Crypto/KeyChain.cs +++ b/Core Modules/WalletConnectSharp.Crypto/KeyChain.cs @@ -142,7 +142,10 @@ public async Task Get(string tag) if (!await Has(tag)) { - throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {tag}); + throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary() + { + {"tag", tag} + }); } return this._keyChain[tag]; @@ -160,7 +163,10 @@ public async Task Delete(string tag) if (!await Has(tag)) { - throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {tag}); + throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary() + { + {"tag", tag} + }); } _keyChain.Remove(tag); @@ -172,7 +178,10 @@ private void IsInitialized() { if (!this._initialized) { - throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name}); + throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary() + { + {"Name", Name} + }); } } diff --git a/Core Modules/WalletConnectSharp.Network/Models/Error.cs b/Core Modules/WalletConnectSharp.Network/Models/Error.cs index 2f4b699..aa68838 100644 --- a/Core Modules/WalletConnectSharp.Network/Models/Error.cs +++ b/Core Modules/WalletConnectSharp.Network/Models/Error.cs @@ -28,6 +28,17 @@ public class Error /// [JsonProperty("data")] public string Data; + + /// + /// Create an ErrorResponse with a given ErrorType and (optional) parameters + /// + /// The error type of the ErrorResponse to create + /// The message to attach to the error + /// A new ErrorResponse + public static Error FromErrorType(ErrorType type, string message) + { + return FromErrorType(type, new Dictionary() { { "message", message } }); + } /// /// Create an ErrorResponse with a given ErrorType and (optional) parameters @@ -36,7 +47,7 @@ public class Error /// Extra parameters for the error message /// Extra data that is stored in the Data field of the newly created ErrorResponse /// A new ErrorResponse - public static Error FromErrorType(ErrorType type, object @params = null, string extraData = null) + public static Error FromErrorType(ErrorType type, Dictionary @params = null, string extraData = null) { string message = SdkErrors.MessageFromType(type, @params); diff --git a/WalletConnectSharp.Auth/Controllers/AuthEngine.cs b/WalletConnectSharp.Auth/Controllers/AuthEngine.cs index 2fcdb53..94efde1 100644 --- a/WalletConnectSharp.Auth/Controllers/AuthEngine.cs +++ b/WalletConnectSharp.Auth/Controllers/AuthEngine.cs @@ -280,9 +280,9 @@ private async Task OnAuthResponse(string topic, JsonRpcResponse response) { this.Client.OnAuthResponse(new AuthErrorResponse() { - Id = id, Topic = topic, Error = Error.FromErrorType(ErrorType.GENERIC, new + Id = id, Topic = topic, Error = Error.FromErrorType(ErrorType.GENERIC, new Dictionary() { - Message = "Invalid signature" + {"Message", "Invalid signature"} }) }); } diff --git a/WalletConnectSharp.Core/Controllers/JsonRpcHistory.cs b/WalletConnectSharp.Core/Controllers/JsonRpcHistory.cs index 552954c..9385b64 100644 --- a/WalletConnectSharp.Core/Controllers/JsonRpcHistory.cs +++ b/WalletConnectSharp.Core/Controllers/JsonRpcHistory.cs @@ -272,7 +272,10 @@ private JsonRpcRecord GetRecord(long id) if (!_records.ContainsKey(id)) { - throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {Tag = $"{Name}: {id}"}); + throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary() + { + {"Tag", $"{Name}: {id}"} + }); } return _records[id];