From d97366a5b8d14f9369a52193ccdeb655951d4ee3 Mon Sep 17 00:00:00 2001 From: skibitsky Date: Fri, 6 Dec 2024 16:41:46 +0200 Subject: [PATCH] Raw message signing --- sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs | 5 +++++ src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs | 9 +++++++++ .../Runtime/Evm/NethereumEvmService.cs | 6 ++++++ .../Runtime/Evm/WagmiEvmService.cs | 5 +++++ .../Runtime/WebGL/Wagmi/WagmiInterop.cs | 13 +++++++++++++ .../Runtime/WebGL/Wagmi/WagmiModel.cs | 6 ++++++ src/Reown.Sign.Nethereum/Runtime/Extensions.cs | 3 +++ 7 files changed, 47 insertions(+) diff --git a/sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs b/sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs index f144544..0aa9e51 100644 --- a/sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs +++ b/sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs @@ -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); diff --git a/src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs b/src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs index ca0e466..9e21e61 100644 --- a/src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs +++ b/src/Reown.AppKit.Unity/Runtime/Evm/EvmService.cs @@ -33,6 +33,14 @@ public Task SignMessageAsync(string message) return SignMessageAsyncCore(message); } + public Task SignMessageAsync(byte[] rawMessage) + { + if (rawMessage == null || rawMessage.Length == 0) + throw new ArgumentNullException(nameof(rawMessage)); + + return SignMessageAsyncCore(rawMessage); + } + // -- Sign Typed Data ------------------------------------------ @@ -157,6 +165,7 @@ public Task GetGasPriceAsync() protected abstract Task InitializeAsyncCore(SignClientUnity signClient); protected abstract Task GetBalanceAsyncCore(string address); protected abstract Task SignMessageAsyncCore(string message); + protected abstract Task SignMessageAsyncCore(byte[] rawMessage); protected abstract Task VerifyMessageSignatureAsyncCore(string address, string message, string signature); protected abstract Task SignTypedDataAsyncCore(string dataJson); protected abstract Task VerifyTypedDataSignatureAsyncCore(string address, string dataJson, string signature); diff --git a/src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs b/src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs index 0c85124..24e999a 100644 --- a/src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs +++ b/src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs @@ -101,6 +101,12 @@ protected override async Task SignMessageAsyncCore(string message) return await Web3.Client.SendRequestAsync("personal_sign", null, encodedMessage); } + protected override Task SignMessageAsyncCore(byte[] rawMessage) + { + var encodedMessage = rawMessage.ToHex(true); + return Web3.Client.SendRequestAsync("personal_sign", null, encodedMessage); + } + // -- Verify Message ------------------------------------------- diff --git a/src/Reown.AppKit.Unity/Runtime/Evm/WagmiEvmService.cs b/src/Reown.AppKit.Unity/Runtime/Evm/WagmiEvmService.cs index 16b679e..95ccbe0 100644 --- a/src/Reown.AppKit.Unity/Runtime/Evm/WagmiEvmService.cs +++ b/src/Reown.AppKit.Unity/Runtime/Evm/WagmiEvmService.cs @@ -25,6 +25,11 @@ protected override Task SignMessageAsyncCore(string message) return WagmiInterop.SignMessageAsync(message); } + protected override Task SignMessageAsyncCore(byte[] rawMessage) + { + return WagmiInterop.SignMessageAsync(rawMessage); + } + protected override Task VerifyMessageSignatureAsyncCore(string address, string message, string signature) { return WagmiInterop.VerifyMessageAsync(address, message, signature); diff --git a/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiInterop.cs b/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiInterop.cs index 132ffb6..054422b 100644 --- a/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiInterop.cs +++ b/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiInterop.cs @@ -106,6 +106,19 @@ public static Task SignMessageAsync(string message) return SignMessageAsync(parameter); } + public static Task SignMessageAsync(byte[] rawMessage) + { + var parameter = new SignRawMessageParameter + { + message = new + { + raw = rawMessage + } + }; + + return InteropCallAsync(WagmiMethods.SignMessage, parameter); + } + public static Task SignMessageAsync(SignMessageParameter parameter) { return InteropCallAsync(WagmiMethods.SignMessage, parameter); diff --git a/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiModel.cs b/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiModel.cs index 9975beb..3b2af19 100644 --- a/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiModel.cs +++ b/src/Reown.AppKit.Unity/Runtime/WebGL/Wagmi/WagmiModel.cs @@ -21,6 +21,12 @@ public class SignMessageParameter public string message; } + [Serializable] + public class SignRawMessageParameter + { + public object message; + } + [Serializable] public class VerifyMessageParameters { diff --git a/src/Reown.Sign.Nethereum/Runtime/Extensions.cs b/src/Reown.Sign.Nethereum/Runtime/Extensions.cs index 4dde467..3059eb6 100644 --- a/src/Reown.Sign.Nethereum/Runtime/Extensions.cs +++ b/src/Reown.Sign.Nethereum/Runtime/Extensions.cs @@ -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; @@ -38,6 +39,8 @@ public static async Task SwitchEthereumChainAsync(this ISignClient signClient, E var metaMaskError = JsonConvert.DeserializeObject(e.Message); if (metaMaskError is { Code: 4001 }) // If user rejected throw; + + ReownLogger.LogError($"[MetaMask Error] {metaMaskError.Message}"); } catch (Exception) {