diff --git a/NftFaucet/Pages/Step1Page.razor.cs b/NftFaucet/Pages/Step1Page.razor.cs index f5c08a9..62b8f2b 100644 --- a/NftFaucet/Pages/Step1Page.razor.cs +++ b/NftFaucet/Pages/Step1Page.razor.cs @@ -1,14 +1,24 @@ using AntDesign; using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using Nethereum.Hex.HexConvertors.Extensions; +using Nethereum.JsonRpc.Client; +using Nethereum.JsonRpc.Client.RpcMessages; +using Newtonsoft.Json; using NftFaucet.Components; using NftFaucet.Constants; using NftFaucet.Extensions; using NftFaucet.Models.Enums; +using Serilog; +using RpcError = Nethereum.JsonRpc.Client.RpcError; namespace NftFaucet.Pages; public class Step1Component : BasicComponent { + [Inject] + protected IJSRuntime JsRuntime { get; set; } + protected string NameErrorMessage { get; set; } protected string DescriptionErrorMessage { get; set; } protected string ImageErrorMessage { get; set; } @@ -122,4 +132,24 @@ public class Step1Component : BasicComponent return false; } + + public async Task SignAsync(string message) + { + var rpcJsonResponse = await JsRuntime.InvokeAsync("NethereumMetamaskInterop.Sign", message.ToHexUTF8()); + var response = JsonConvert.DeserializeObject(rpcJsonResponse); + if (response.HasError) + { + var rpcError = new RpcError(response.Error.Code, response.Error.Message, response.Error.Data); + throw new RpcResponseException(rpcError); + } + + try + { + return response.GetResult(); + } + catch (FormatException formatException) + { + throw new RpcResponseFormatException("Invalid format found in RPC response", formatException); + } + } } diff --git a/NftFaucet/wwwroot/NethereumMetamask.js b/NftFaucet/wwwroot/NethereumMetamask.js new file mode 100644 index 0000000..1e5c54e --- /dev/null +++ b/NftFaucet/wwwroot/NethereumMetamask.js @@ -0,0 +1,55 @@ +async function metamaskRequest(parsedMessage) { + try { + const response = await ethereum.request(parsedMessage); + let rpcResponse = { + jsonrpc: "2.0", + result: response, + id: parsedMessage.id, + error: null + } + + return rpcResponse; + } catch (e) { + console.log(e); + let rpcResonseError = { + jsonrpc: "2.0", + id: parsedMessage.id, + error: e + } + return rpcResonseError; + } +} + +async function getSelectedAddress() { + let accountsReponse = await getAddresses(); + if (accountsReponse.error !== null) throw accountsReponse.error; + return accountsReponse.result[0]; +} + +async function getAddresses() { + return await metamaskRequest({ method: 'eth_requestAccounts' }); +} + +window.NethereumMetamaskInterop = { + Sign: async (utf8HexMsg) => { + try { + const from = await getSelectedAddress(); + const params = [utf8HexMsg, from]; + const method = 'personal_sign'; + const rpcResponse = await metamaskRequest({ + method, + params, + from + }); + return JSON.stringify(rpcResponse); + } catch (e) { + console.log(e); + let rpcResponseError = { + jsonrpc: "2.0", + id: parsedMessage.id, + error: e + } + return JSON.stringify(rpcResponseError); + } + } +} diff --git a/NftFaucet/wwwroot/index.html b/NftFaucet/wwwroot/index.html index ad065dc..f622208 100644 --- a/NftFaucet/wwwroot/index.html +++ b/NftFaucet/wwwroot/index.html @@ -31,6 +31,7 @@ +