Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: raw message signing #31

Merged
merged 1 commit into from
Dec 13, 2024
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
5 changes: 5 additions & 0 deletions sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ public async void OnPersonalSignButton()
var account = await AppKit.GetAccountAsync();

const string message = "Hello from Unity!";

// It's also possible to sign a message as a byte array
// var messageBytes = System.Text.Encoding.UTF8.GetBytes(message);
// var signature = await AppKit.Evm.SignMessageAsync(messageBytes);

var signature = await AppKit.Evm.SignMessageAsync(message);
var isValid = await AppKit.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);

Expand Down
9 changes: 9 additions & 0 deletions src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public Task<string> SignMessageAsync(string message)
return SignMessageAsyncCore(message);
}

public Task<string> SignMessageAsync(byte[] rawMessage)
{
if (rawMessage == null || rawMessage.Length == 0)
throw new ArgumentNullException(nameof(rawMessage));

return SignMessageAsyncCore(rawMessage);
}


// -- Sign Typed Data ------------------------------------------

Expand Down Expand Up @@ -157,6 +165,7 @@ public Task<BigInteger> GetGasPriceAsync()
protected abstract Task InitializeAsyncCore(SignClientUnity signClient);
protected abstract Task<BigInteger> GetBalanceAsyncCore(string address);
protected abstract Task<string> SignMessageAsyncCore(string message);
protected abstract Task<string> SignMessageAsyncCore(byte[] rawMessage);
protected abstract Task<bool> VerifyMessageSignatureAsyncCore(string address, string message, string signature);
protected abstract Task<string> SignTypedDataAsyncCore(string dataJson);
protected abstract Task<bool> VerifyTypedDataSignatureAsyncCore(string address, string dataJson, string signature);
Expand Down
6 changes: 6 additions & 0 deletions src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ protected override async Task<string> SignMessageAsyncCore(string message)
return await Web3.Client.SendRequestAsync<string>("personal_sign", null, encodedMessage);
}

protected override Task<string> SignMessageAsyncCore(byte[] rawMessage)
{
var encodedMessage = rawMessage.ToHex(true);
return Web3.Client.SendRequestAsync<string>("personal_sign", null, encodedMessage);
}


// -- Verify Message -------------------------------------------

Expand Down
5 changes: 5 additions & 0 deletions src/Reown.AppKit.Unity/Runtime/Evm/WagmiEvmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ protected override Task<string> SignMessageAsyncCore(string message)
return WagmiInterop.SignMessageAsync(message);
}

protected override Task<string> SignMessageAsyncCore(byte[] rawMessage)
{
return WagmiInterop.SignMessageAsync(rawMessage);
}

protected override Task<bool> VerifyMessageSignatureAsyncCore(string address, string message, string signature)
{
return WagmiInterop.VerifyMessageAsync(address, message, signature);
Expand Down
13 changes: 13 additions & 0 deletions src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ public static Task<string> SignMessageAsync(string message)
return SignMessageAsync(parameter);
}

public static Task<string> SignMessageAsync(byte[] rawMessage)
{
var parameter = new SignRawMessageParameter
{
message = new
{
raw = rawMessage
}
};

return InteropCallAsync<SignRawMessageParameter, string>(WagmiMethods.SignMessage, parameter);
}

public static Task<string> SignMessageAsync(SignMessageParameter parameter)
{
return InteropCallAsync<SignMessageParameter, string>(WagmiMethods.SignMessage, parameter);
Expand Down
6 changes: 6 additions & 0 deletions src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class SignMessageParameter
public string message;
}

[Serializable]
public class SignRawMessageParameter
{
public object message;
}

[Serializable]
public class VerifyMessageParameters
{
Expand Down
3 changes: 3 additions & 0 deletions src/Reown.Sign.Nethereum/Runtime/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Reown.Core.Common.Logging;
using Reown.Core.Common.Model.Errors;
using Reown.Sign.Interfaces;
using Reown.Sign.Nethereum.Model;
Expand Down Expand Up @@ -38,6 +39,8 @@ public static async Task SwitchEthereumChainAsync(this ISignClient signClient, E
var metaMaskError = JsonConvert.DeserializeObject<MetaMaskError>(e.Message);
if (metaMaskError is { Code: 4001 }) // If user rejected
throw;

ReownLogger.LogError($"[MetaMask Error] {metaMaskError.Message}");
}
catch (Exception)
{
Expand Down
Loading