Skip to content

Commit

Permalink
Merge pull request #26 from reown-com/feat/aa-signing
Browse files Browse the repository at this point in the history
feat: AA signing
  • Loading branch information
skibitsky authored Nov 29, 2024
2 parents 6e7436d + 3d56125 commit c029952
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="8.0.1" />
<PackageVersion Include="Nethereum.HdWallet" Version="4.21.4" />
<PackageVersion Include="Nethereum.Web3" Version="4.21.4" />
<PackageVersion Include="Nethereum.HdWallet" Version="4.26.0" />
<PackageVersion Include="Nethereum.Web3" Version="4.26.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageVersion Include="Websocket.Client" Version="4.7.0" />
Expand All @@ -15,7 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Nethereum.Signer" Version="4.21.4" />
<PackageVersion Include="Nethereum.Signer" Version="4.26.0" />
<PackageVersion Include="ZXing.Net" Version="0.16.9" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions sample/Reown.AppKit.Unity/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"url": "https://package.openupm.com"
},
"com.nethereum.unity": {
"version": "4.19.1",
"version": "4.26.0",
"depth": 1,
"source": "registry",
"dependencies": {},
Expand Down Expand Up @@ -94,7 +94,7 @@
"source": "local",
"dependencies": {
"com.reown.sign": "1.1.1",
"com.nethereum.unity": "4.19.1"
"com.nethereum.unity": "4.26.0"
}
},
"com.reown.sign.nethereum.unity": {
Expand Down
63 changes: 61 additions & 2 deletions src/Reown.AppKit.Unity/Runtime/Evm/NethereumEvmService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.Contracts.Standards.ERC1271.ContractDefinition;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Signer;
Expand All @@ -9,6 +13,7 @@
using Nethereum.Web3;
using Reown.Sign.Nethereum.Unity;
using Reown.Sign.Unity;
using UnityEngine;
using HexBigInteger = Nethereum.Hex.HexTypes.HexBigInteger;

namespace Reown.AppKit.Unity
Expand Down Expand Up @@ -99,10 +104,64 @@ protected override async Task<string> SignMessageAsyncCore(string message)

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

protected override Task<bool> VerifyMessageSignatureAsyncCore(string address, string message, string signature)
protected override async Task<bool> VerifyMessageSignatureAsyncCore(string address, string message, string signature)
{

// -- ERC-6492
var erc6492Service = Web3.Eth.SignatureValidationPredeployContractERC6492;
if (erc6492Service.IsERC6492Signature(signature))
{
return await erc6492Service.IsValidSignatureMessageAsync(address, message, signature.HexToByteArray());
}

// -- EOA
var recoveredAddress = _ethereumMessageSigner.EncodeUTF8AndEcRecover(message, signature);
return Task.FromResult(recoveredAddress.IsTheSameAddress(address));
if (recoveredAddress.IsTheSameAddress(address))
{
return true;
}

// -- ERC-1271
var ethGetCode = await Web3.Eth.GetCode.SendRequestAsync(address);
if (ethGetCode is { Length: > 2 })
{
var hashedMessage = _ethereumMessageSigner.HashPrefixedMessage(Encoding.UTF8.GetBytes(message));

var isValidSignatureFunctionMessage = new IsValidSignatureFunction()
{
Hash = hashedMessage,
Signature = signature.HexToByteArray()
};

var handler = Web3.Eth.GetContractQueryHandler<IsValidSignatureFunction>();

try
{
var result = await handler.QueryAsync<byte[]>(address, isValidSignatureFunctionMessage);

// The magic value 0x1626ba7e
var magicValue = new byte[]
{
0x16,
0x26,
0xBA,
0x7E
};

return result != null && result.SequenceEqual(magicValue);
}
catch (SmartContractRevertException)
{
return false;
}
catch (Exception ex)
{
Debug.LogException(ex);
return false;
}
}

return false;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Reown.Sign.Nethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
],
"dependencies": {
"com.reown.sign": "1.1.1",
"com.nethereum.unity": "4.19.1"
"com.nethereum.unity": "4.26.0"
}
}

0 comments on commit c029952

Please sign in to comment.