Skip to content

Commit

Permalink
Merge branch '2.0' into dependabot/nuget/xunit.runner.visualstudio-2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gigajuwels authored Sep 12, 2023
2 parents da2844e + 9cbe13a commit 7de7d35
Show file tree
Hide file tree
Showing 206 changed files with 6,583 additions and 703 deletions.
4 changes: 2 additions & 2 deletions Core Modules/WalletConnectSharp.Common/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WalletConnectSharp.Common
/// <summary>
/// An interface that represents a module
/// </summary>
public interface IModule /* : IDisposable */
public interface IModule : IDisposable
{
/// <summary>
/// The name of this module
Expand All @@ -18,4 +18,4 @@ public interface IModule /* : IDisposable */
/// </summary>
string Context { get; }
}
}
}
11 changes: 11 additions & 0 deletions Core Modules/WalletConnectSharp.Common/Logging/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace WalletConnectSharp.Common.Logging
{
public interface ILogger
{
void Log(string message);

void LogError(string message);

void LogError(Exception e);
}
}
36 changes: 36 additions & 0 deletions Core Modules/WalletConnectSharp.Common/Logging/WCLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace WalletConnectSharp.Common.Logging
{
public class WCLogger
{
public static ILogger Logger;

public static ILogger WithContext(string context)
{
return new WrapperLogger(Logger, context);
}

public static void Log(string message)
{
if (Logger == null)
return;

Logger.Log(message);
}

public static void LogError(string message)
{
if (Logger == null)
return;

Logger.LogError(message);
}

public static void LogError(Exception e)
{
if (Logger == null)
return;

Logger.LogError(e);
}
}
}
31 changes: 31 additions & 0 deletions Core Modules/WalletConnectSharp.Common/Logging/WrapperLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace WalletConnectSharp.Common.Logging;

public class WrapperLogger : ILogger
{
private ILogger _logger;
private string prefix;

public WrapperLogger(ILogger logger, string prefix)
{
_logger = logger;
this.prefix = prefix;
}

public void Log(string message)
{
if (_logger == null) return;
_logger.Log($"[{prefix}] {message}");
}

public void LogError(string message)
{
if (_logger == null) return;
_logger.LogError($"[{prefix}] {message}");
}

public void LogError(Exception e)
{
if (_logger == null) return;
_logger.LogError(e);
}
}
10 changes: 5 additions & 5 deletions Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static class SdkErrors
/// <param name="type">The error type message to generate</param>
/// <param name="params">A dictionary (or anonymous type) of parameters for the error message</param>
/// <returns>The error message as a string</returns>
public static string MessageFromType(ErrorType type, object @params = null)
public static string MessageFromType(ErrorType type, Dictionary<string, object> @params = null)
{
return MessageFromType(type, null, @params.AsDictionary());
return MessageFromType(type, null, @params);
}

/// <summary>
Expand All @@ -47,8 +47,8 @@ public static string MessageFromType(ErrorType type, string message = null, Dict
@params = new Dictionary<string, object>();
}

if (!string.IsNullOrWhiteSpace(message) && !@params.ContainsKey("message"))
@params.Add("message", message);
if (!string.IsNullOrWhiteSpace(message))
@params.TryAdd("message", message);

string errorMessage;
switch (type)
Expand Down Expand Up @@ -231,4 +231,4 @@ private static string FormatErrorText(string formattedText, Dictionary<string, o
return text;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public static WalletConnectException FromType(ErrorType type, string message = n
/// <param name="type">The error type of the exception</param>
/// <param name="params">Additional (optional) parameters for the generated error message</param>
/// <returns>A new exception</returns>
public static WalletConnectException FromType(ErrorType type, object @params = null)
public static WalletConnectException FromType(ErrorType type, Dictionary<string, object> @params = null)
{
return FromType(type, null, @params.AsDictionary());
return FromType(type, null, @params);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ public IsolatedModule()

activeModules.Add(_guid);
}

public void Dispose() { }
}
}
}
16 changes: 15 additions & 1 deletion Core Modules/WalletConnectSharp.Common/Utils/Clock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,19 @@ public static long Now()
{
return ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds();
}

/// <summary>
/// Current DateTime.Now as unix timestamp milliseconds
/// </summary>
/// <returns></returns>
public static long NowMilliseconds()
{
return ((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds();
}

public static TimeSpan AsTimeSpan(long seconds)
{
return TimeSpan.FromSeconds(seconds);
}
}
}
}
30 changes: 30 additions & 0 deletions Core Modules/WalletConnectSharp.Common/Utils/DictionaryComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace WalletConnectSharp.Common.Utils
{
public class DictionaryComparer<TKey, TValue> :
IEqualityComparer<Dictionary<TKey, TValue>>
{
private IEqualityComparer<TValue> valueComparer;
public DictionaryComparer(IEqualityComparer<TValue> valueComparer = null)
{
this.valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;
}
public bool Equals(Dictionary<TKey, TValue> x, Dictionary<TKey, TValue> y)
{
if (x.Count != y.Count)
return false;
if (x.Keys.Except(y.Keys).Any())
return false;
if (y.Keys.Except(x.Keys).Any())
return false;
foreach (var pair in x)
if (!valueComparer.Equals(pair.Value, y[pair.Key]))
return false;
return true;
}

public int GetHashCode(Dictionary<TKey, TValue> obj)
{
throw new NotImplementedException();
}
}
}
37 changes: 8 additions & 29 deletions Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@ namespace WalletConnectSharp.Common.Utils
/// </summary>
public static class Extensions
{
/// <summary>
/// Convert an anonymous type to a Dictionary
/// </summary>
/// <param name="obj">The anonymous type instance to convert to a dictionary</param>
/// <param name="enforceLowercase">Enforce all keys to be lowercased</param>
/// <returns>A dictionary where each key is the property name of the anonymous type
/// and each value is the property's value</returns>
public static Dictionary<string, object> AsDictionary(this object obj, bool enforceLowercase = true)
{
if (obj is Dictionary<string, object> objects)
return objects;

var dict = new Dictionary<string, object>(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;
}

/// <summary>
/// Returns true if the given object is a numeric type
/// </summary>
Expand Down Expand Up @@ -129,5 +101,12 @@ public static async Task WithTimeout(this Task task, TimeSpan timeout, string me
throw new TimeoutException(message.Replace("%t", timeout.ToString()));
}
}

public static bool SetEquals<T>(this IEnumerable<T> first, IEnumerable<T> second,
IEqualityComparer<T> comparer)
{
return new HashSet<T>(second, comparer ?? EqualityComparer<T>.Default)
.SetEquals(first);
}
}
}
}
21 changes: 21 additions & 0 deletions Core Modules/WalletConnectSharp.Common/Utils/ListComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace WalletConnectSharp.Common.Utils
{
public class ListComparer<T> : IEqualityComparer<List<T>>
{
private IEqualityComparer<T> valueComparer;
public ListComparer(IEqualityComparer<T> valueComparer = null)
{
this.valueComparer = valueComparer ?? EqualityComparer<T>.Default;
}

public bool Equals(List<T> x, List<T> y)
{
return x.SetEquals(y, valueComparer);
}

public int GetHashCode(List<T> obj)
{
throw new NotImplementedException();
}
}
}
22 changes: 18 additions & 4 deletions Core Modules/WalletConnectSharp.Crypto/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class Crypto : ICrypto
private static readonly Encoding DATA_ENCODING = Encoding.UTF8;
private static readonly Encoding JSON_ENCODING = Encoding.UTF8;

private const int TYPE_0 = 0;
private const int TYPE_1 = 1;
public const int TYPE_0 = 0;
public const int TYPE_1 = 1;
private const int TYPE_LENGTH = 1;
private const int IV_LENGTH = 12;
private const int KEY_LENGTH = 32;
Expand Down Expand Up @@ -578,11 +578,20 @@ private void IsInitialized()
{
if (!this._initialized)
{
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name});
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary<string, object>()
{
{ "Name", Name }
});
}
}

private string HashKey(string key)
/// <summary>
/// Hash a hex key string using SHA256. The input key string must be a hex
/// string and the returned hash is represented as a hex string
/// </summary>
/// <param name="key">The input hex key string to hash using SHA256</param>
/// <returns>The hash of the given input as a hex string</returns>
public string HashKey(string key)
{
using (SHA256 sha256 = SHA256.Create())
{
Expand Down Expand Up @@ -675,5 +684,10 @@ private async Task<byte[]> GetClientSeed()

return seed.HexToByteArray();
}

public void Dispose()
{
KeyChain?.Dispose();
}
}
}
8 changes: 8 additions & 0 deletions Core Modules/WalletConnectSharp.Crypto/Interfaces/ICrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,13 @@ public interface ICrypto : IModule
/// </summary>
/// <returns>The client id as a string</returns>
Task<string> GetClientId();

/// <summary>
/// Hash a hex key string using SHA256. The input key string must be a hex
/// string and the returned hash is represented as a hex string
/// </summary>
/// <param name="key">The input hex key string to hash using SHA256</param>
/// <returns>The hash of the given input as a hex string</returns>
string HashKey(string key);
}
}
25 changes: 18 additions & 7 deletions Core Modules/WalletConnectSharp.Crypto/KeyChain.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using WalletConnectSharp.Common;
using WalletConnectSharp.Common.Model.Errors;
using WalletConnectSharp.Common.Utils;
using WalletConnectSharp.Crypto.Interfaces;
Expand Down Expand Up @@ -142,7 +138,10 @@ public async Task<string> 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<string, object>()
{
{"tag", tag}
});
}

return this._keyChain[tag];
Expand All @@ -160,7 +159,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<string, object>()
{
{"tag", tag}
});
}

_keyChain.Remove(tag);
Expand All @@ -172,7 +174,10 @@ private void IsInitialized()
{
if (!this._initialized)
{
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name});
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary<string, object>()
{
{"Name", Name}
});
}
}

Expand All @@ -190,5 +195,11 @@ private async Task SaveKeyChain()
{
await Storage.SetItem(StorageKey, this._keyChain);
}

public void Dispose()
{
_keyChain?.Clear();
Storage?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class DecodeOptions
/// The public key that received this encoded message
/// </summary>
[JsonProperty("receiverPublicKey")]
public string ReceiverPublicKey { get; set; }
public string ReceiverPublicKey;
}
}
Loading

0 comments on commit 7de7d35

Please sign in to comment.