Skip to content

Commit

Permalink
Fix IsValidMessageSignature request and respective tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
BellringerQuinn committed Dec 13, 2023
1 parent 1eb955b commit 54a5e44
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 30 deletions.
8 changes: 7 additions & 1 deletion Assets/SequenceExamples/Scripts/MockTokenContentFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ public TokenElement CreateMockElement()
Sprite tokenIconSprite = Sprite.Create(tokenIconTexture, new Rect(0, 0, tokenIconTexture.width, tokenIconTexture.height),
new Vector2(.5f, .5f));

Chain network = Chain.None;
while (network == Chain.None)
{
network = EnumExtensions.GetRandomEnumValue<Chain>();
}

return new TokenElement(
new ERC20(potentialMockAddresses.GetRandomObjectFromArray()),
tokenIconSprite,
potentialNames.GetRandomObjectFromArray(),
EnumExtensions.GetRandomEnumValue<Chain>(),
network,
(uint)Random.Range(0, 10000),
potentialSymbols.GetRandomObjectFromArray(),
new MockCurrencyConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ private TransactionDetails CreateMockElement(string contractAddress = null)
contract = new Address(contractAddress);
}

Chain network = Chain.None;
while (network == Chain.None)
{
network = EnumExtensions.GetRandomEnumValue<Chain>();
}

return new TransactionDetails(potentialTypes.GetRandomObjectFromArray(),
EnumExtensions.GetRandomEnumValue<Chain>(),
network,
tokenIconSprite,
contract,
new Address(potentialMockAddresses.GetRandomObjectFromArray()),
Expand Down
3 changes: 3 additions & 0 deletions Assets/SequenceSDK/Ethereum/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public enum Chain
TestnetArbitrumGoerli = 421613,
TestnetBNBSmartChain = 97,
TestnetBaseGoerli = 84531,

// Null
None = 0
}
}
2 changes: 1 addition & 1 deletion Assets/SequenceSDK/Ethereum/ChainId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Sequence
{
public static class ChainId
{
public static string AsString(this Chain chain)
public static string AsHexString(this Chain chain)
{
BigInteger chainId = (BigInteger)(int)chain;
return chainId.BigIntegerToHexString();
Expand Down
4 changes: 2 additions & 2 deletions Assets/SequenceSDK/Ethereum/Tests/WalletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ public async Task TestWalletSignMessageWithChainId(IWallet wallet, string messag
string address = wallet.GetAddress();
Assert.NotNull(address);

string sig = await wallet.SignMessage(message, "0x89");
string sig = await wallet.SignMessage(message, Chain.Polygon.AsHexString());
Assert.NotNull(sig);

bool valid = await wallet.IsValidSignature(sig, message, chainId: "0x89");
bool valid = await wallet.IsValidSignature(sig, message, chain: Chain.Polygon);
Assert.IsTrue(valid);
}

Expand Down
7 changes: 6 additions & 1 deletion Assets/SequenceSDK/Ethereum/Utils/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ public static class ByteArrayExtensions
{
public static string ByteArrayToHexStringWithPrefix(this byte[] byteArray)
{
return "0x" + SequenceCoder.ByteArrayToHexString(byteArray);
return "0x" + byteArray.ByteArrayToHexString();
}

public static string ByteArrayToHexString(this byte[] byteArray)
{
return SequenceCoder.ByteArrayToHexString(byteArray);
}

public static bool HasPrefix(this byte[] b, byte[] prefix) {
Expand Down
6 changes: 3 additions & 3 deletions Assets/SequenceSDK/Ethereum/Wallet/EthWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ public string SignByteArray(string privateKey, byte[] byteArray)
/// <param name="signature">The signature to verify.</param>
/// <param name="message">The message that was signed.</param>
/// <returns><c>true</c> if the signature is valid, <c>false</c> otherwise.</returns>
public async Task<bool> IsValidSignature(string signature, string message, string chainId = "", uint accountIndex = 0)
public async Task<bool> IsValidSignature(string signature, string message, Chain chain = Chain.None)
{
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
if (chainId != null && chainId.Length > 0)
if (chain != Chain.None)
{
messageBytes = ByteArrayExtensions.ConcatenateByteArrays(messageBytes, Encoding.UTF8.GetBytes(chainId));
messageBytes = ByteArrayExtensions.ConcatenateByteArrays(messageBytes, Encoding.UTF8.GetBytes(chain.AsHexString()));
}
byte[] messagePrefix = PrefixedMessage(messageBytes);
byte[] hashedMessage = SequenceCoder.KeccakHash(messagePrefix);
Expand Down
4 changes: 1 addition & 3 deletions Assets/SequenceSDK/Ethereum/Wallet/IWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public Task<TransactionReceipt[]> SendTransactionBatchAndWaitForReceipts(IEthCli
/// <param name="accountIndex"></param>
/// <param name="chainId"></param>
/// <returns><c>true</c> if the signature is valid, <c>false</c> otherwise.</returns>
public Task<bool> IsValidSignature(string signature, string message, string chainId = "", uint accountIndex = 0);


public Task<bool> IsValidSignature(string signature, string message, Chain chain = Chain.None);

/// <summary>
/// Recovers the Ethereum address from a message and its signature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace Sequence.WaaS
[System.Serializable]
public class IsValidMessageSignatureArgs
{
public uint chainId;
public string chainId;
public string walletAddress;
public string message;
public string signature;

public IsValidMessageSignatureArgs(uint chainId, string walletAddress, string message, string signature)
public IsValidMessageSignatureArgs(Chain chain, string walletAddress, string message, string signature)
{
this.chainId = chainId;
this.chainId = ((int)chain).ToString();
this.walletAddress = walletAddress;
this.message = message;
this.signature = signature;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Text;
using Sequence.ABI;
using Sequence.Extensions;
using Sequence.Utils;
using SequenceSDK.WaaS;

namespace Sequence.WaaS
Expand All @@ -18,7 +22,7 @@ public SignMessageArgs(string wallet, Chain network, string message, uint timeBe
int networkId = (int)network;
this.wallet = wallet;
this.network = networkId.ToString();
this.message = message;
this.message = PrepareMessage(message);
this.issued = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
this.expires = this.issued + timeBeforeExpiry;
}
Expand All @@ -27,9 +31,14 @@ public SignMessageArgs(string wallet, string networkId, string message, uint tim
{
this.wallet = wallet;
this.network = networkId;
this.message = message;
this.message = PrepareMessage(message);
this.issued = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
this.expires = this.issued + timeBeforeExpiry;
}

private static string PrepareMessage(string message)
{
return Wallet.IWallet.PrefixedMessage(Encoding.UTF8.GetBytes(message)).ByteArrayToHexString().ToUpper().EnsureHexPrefix();
}
}
}
7 changes: 6 additions & 1 deletion Assets/SequenceSDK/WaaS/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Sequence.Authentication;
using Sequence.Provider;
using UnityEngine;
using UnityEngine.Networking;
Expand All @@ -32,9 +33,13 @@ public void AddDefaultHeader(string key, string value)
this._defaultHeaders[key] = value;
}

public async Task<T2> SendRequest<T, T2>(string path, T args, [CanBeNull] Dictionary<string, string> headers = null)
public async Task<T2> SendRequest<T, T2>(string path, T args, [CanBeNull] Dictionary<string, string> headers = null, string overrideUrl = null)
{
string url = _url + "/" + path;
if (overrideUrl != null)
{
url = overrideUrl.AppendTrailingSlashIfNeeded() + path;
}
string requestJson = JsonConvert.SerializeObject(args, serializerSettings);
UnityWebRequest request = UnityWebRequest.Get(url);
request.SetRequestHeader("Content-Type", "application/json");
Expand Down
5 changes: 3 additions & 2 deletions Assets/SequenceSDK/WaaS/Tests/WaaSToWalletAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public async Task TestSignMessage_withAdapter(string message, Chain network)
try
{
WaaSTestHarness.TestStarted?.Invoke();
string signature = await _wallet.SignMessage(message, network.AsString());
string signature = await _wallet.SignMessage(message, network.AsHexString());
CustomAssert.NotNull(signature, nameof(TestSignMessage_withAdapter), message, network);
CustomAssert.IsTrue(WaaSWalletTests.AppearsToBeValidSignature(signature), nameof(TestSignMessage_withAdapter), message, network); // If a signature appears valid and comes from WaaS, it most likely is valid - validity is tested on the WaaS side
bool isValid = await _wallet.IsValidSignature(signature, message, network);
CustomAssert.IsTrue(isValid, nameof(TestSignMessage_withAdapter), message, network);
WaaSTestHarness.TestPassed?.Invoke();
}
catch (Exception e)
Expand Down
9 changes: 3 additions & 6 deletions Assets/SequenceSDK/WaaS/Tests/WaaSWalletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public async Task TestMessageSigning(string message, Chain network)
var result = await _wallet.SignMessage(new SignMessageArgs(_address, network, message));
string signature = result.signature;
CustomAssert.NotNull(signature, nameof(TestMessageSigning), message, network);
CustomAssert.IsTrue(AppearsToBeValidSignature(signature), nameof(TestMessageSigning), message, network); // If a signature appears valid and comes from WaaS, it most likely is valid - validity is tested on the WaaS side
var isValid = await _wallet.IsValidMessageSignature(new IsValidMessageSignatureArgs(network, _address, message, signature));
bool isValidSignature = isValid.isValid;
CustomAssert.IsTrue(isValidSignature, nameof(TestMessageSigning), message, network);
WaaSTestHarness.TestPassed?.Invoke();
}
catch (Exception e)
Expand All @@ -61,11 +63,6 @@ public async Task TestMessageSigning(string message, Chain network)
}
}

public static bool AppearsToBeValidSignature(string signature)
{
return signature.StartsWith("0x") && signature.IsHexFormat();
}

public async Task TestTransfer()
{
try
Expand Down
4 changes: 2 additions & 2 deletions Assets/SequenceSDK/WaaS/WaaSToWalletAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public async Task<string> SignMessage(string message, string chainId)
return result.signature;
}

public async Task<bool> IsValidSignature(string signature, string message, string chainId, uint accountIndex = 0)
public async Task<bool> IsValidSignature(string signature, string message, Chain chain)
{
var args = new IsValidMessageSignatureArgs((uint)chainId.HexStringToInt(), GetAddress(), message, signature);
var args = new IsValidMessageSignatureArgs(chain, GetAddress(), message, signature);
var result = await _wallet.IsValidMessageSignature(args);
return result.isValid;
}
Expand Down
5 changes: 3 additions & 2 deletions Assets/SequenceSDK/WaaS/WaaSWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public async Task<SignMessageReturn> SignMessage(SignMessageArgs args)
return result;
}

public Task<IsValidMessageSignatureReturn> IsValidMessageSignature(IsValidMessageSignatureArgs args) // Todo figure out this intent is still supported
public Task<IsValidMessageSignatureReturn> IsValidMessageSignature(IsValidMessageSignatureArgs args)
{
return _intentSender.SendIntent<IsValidMessageSignatureReturn, IsValidMessageSignatureArgs>(args);
return _httpClient.SendRequest<IsValidMessageSignatureArgs, IsValidMessageSignatureReturn>(
"API/IsValidMessageSignature", args, null, "https://api.sequence.app/rpc/");
}

public event Action<SuccessfulTransactionReturn> OnSendTransactionComplete;
Expand Down

0 comments on commit 54a5e44

Please sign in to comment.