-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathReownWebGLProvider.cs
More file actions
227 lines (200 loc) · 9.18 KB
/
ReownWebGLProvider.cs
File metadata and controls
227 lines (200 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#if UNITY_WEBGL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChainSafe.Gaming.Reown.Methods;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core;
using ChainSafe.Gaming.Web3.Core.Chains;
using ChainSafe.Gaming.Web3.Core.Operations;
using ChainSafe.Gaming.Web3.Environment;
using ChainSafe.Gaming.Web3.Evm.Wallet;
using Nethereum.JsonRpc.Client.RpcMessages;
using Newtonsoft.Json;
using Reown.AppKit.Unity;
using Reown.AppKit.Unity.WebGl.Wagmi;
using UnityEngine;
using NativeCurrency = Reown.AppKit.Unity.WebGl.Wagmi.NativeCurrency;
using Object = UnityEngine.Object;
using W3AppKit = global::Reown.AppKit.Unity.AppKit;
namespace ChainSafe.Gaming.Reown.AppKit
{
public class ReownWebGLProvider : WalletProvider, ILifecycleParticipant, IConnectionHelper
{
private IReownConfig ReownConfig { get; set; }
private readonly ReownHttpClient _httpClient;
private readonly IChainConfigSet _chains;
private readonly ILogWriter _logWriter;
private readonly IChainManager _chainManager;
private Chain[] _appKitChains;
private readonly string[] TestnetSuffix = new[]
{
"Testnet",
"Goerli",
"Sepolia"
};
public ReownWebGLProvider(ReownHttpClient httpClient, IChainConfigSet chains, IReownConfig reownConfig,
ILogWriter logWriter, Web3Environment web3Environment, IChainManager chainManager,
IOperationTracker operationTracker, IOperatingSystemMediator operatingSystemMediator) : base(web3Environment, chainManager.Current, operationTracker, operatingSystemMediator)
{
_httpClient = httpClient;
_chains = chains;
_logWriter = logWriter;
_chainManager = chainManager;
ReownConfig = reownConfig;
}
private async Task Initialize()
{
if (W3AppKit.Instance != null)
return;
Object.Instantiate(Resources.Load<AppKitCore>("Reown AppKit"));
_appKitChains = _chains.Configs
.Select(x =>
{
if (!ReownConfig.ChainIdViemNameMap.TryGetValue(x.ChainId, out var viemName))
throw new ReownIntegrationException(
$"Viem Name is not set for chain: {x.Chain} {x.ChainId}. Make sure to populate the viem name for this chain id in Reown Connection Provider.");
return IChainConfigToAppKitChain(x);
}).ToArray();
var appKitConfig = new AppKitConfig()
{
projectId = ReownConfig.ProjectId,
metadata = new Metadata(ReownConfig.Metadata.Name, ReownConfig.Metadata.Description,
ReownConfig.Metadata.Url, ReownConfig.Metadata.Icons[0], new RedirectData()
{
Native = ReownConfig.Metadata.Redirect.Native,
Universal = ReownConfig.Metadata.Redirect.Universal
}),
supportedChains = _appKitChains,
enableEmail = false,
enableOnramp = false
};
await W3AppKit.InitializeAsync(appKitConfig);
}
private Chain IChainConfigToAppKitChain(IChainConfig x)
{
return new Chain(ChainConstants.Namespaces.Evm, x.ChainId,
x.Chain, new Currency(x.NativeCurrency.Name, x.NativeCurrency.Symbol,
x.NativeCurrency.Decimals), new BlockExplorer(x.Chain + " block explorer", x.BlockExplorerUrl),
x.Rpc, IsTestnet(x.Chain), "https://chainlist.org/unknown-logo.png", ReownConfig.ChainIdViemNameMap[x.ChainId]);
}
private bool IsTestnet(string argChain)
{
return TestnetSuffix.Any(x => argChain.Contains(x, StringComparison.InvariantCultureIgnoreCase));
}
public async ValueTask WillStartAsync()
{
await Initialize();
}
public ValueTask WillStopAsync()
{
W3AppKit.AccountConnected -= AppKitAccountConnected;
W3AppKit.ModalController.OpenStateChanged -= OpenStateChanged;
W3AppKit.ModalController.OpenStateChanged -= OpenStateChanged;
return new ValueTask();
}
public bool StoredSessionAvailable => false;
private readonly TaskCompletionSource<string> _accountConnected = new();
public override async Task<string> Connect()
{
await Initialize();
W3AppKit.AccountConnected += AppKitAccountConnected;
W3AppKit.NetworkController.ChainChanged += NetworkControllerOnChainChanged;
W3AppKit.ModalController.OpenStateChanged += OpenStateChanged;
W3AppKit.OpenModal();
var result = await _accountConnected.Task;
return result;
}
private void NetworkControllerOnChainChanged(object sender, NetworkController.ChainChangedEventArgs e)
{
W3AppKit.CloseModal();
}
private async void OpenStateChanged(object sender, ModalOpenStateChangedEventArgs e)
{
await Task.Delay(200);
if(!e.IsOpen && _accountConnected.Task.IsCompleted == false)
_accountConnected.SetException(new TaskCanceledException());
}
private async void AppKitAccountConnected(object sender, Connector.AccountConnectedEventArgs e)
{
var account = await e.GetAccount();
_accountConnected.SetResult(account.Address);
}
public override async Task Disconnect()
{
await W3AppKit.ConnectorController.DisconnectAsync();
}
public override async Task HandleChainSwitching()
{
var currentChain = _chainManager.Current;
await WagmiInterop.SwitchChainAsync(int.Parse(currentChain.ChainId), new AddEthereumChainParameter()
{
chainId = $"{ChainConstants.Namespaces.Evm}:{currentChain.ChainId}",
blockExplorerUrls = new [] { currentChain.BlockExplorerUrl },
chainName = currentChain.Chain,
nativeCurrency = new NativeCurrency()
{
decimals = currentChain.NativeCurrency.Decimals,
name = currentChain.NativeCurrency.Name,
symbol = currentChain.NativeCurrency.Symbol
},
rpcUrls = new [] { currentChain.Rpc },
iconUrls = new [] { "https://chainlist.org/unknown-logo.png"}
});
}
public override async Task<T> Request<T>(string method, params object[] parameters)
{
return await ReownRequest<T>(method, parameters);
}
private async Task<T> ReownRequest<T>(string method, params object[] parameters)
{
// Helper method to make a request using ReownSignClient.
async Task<T> MakeRequest<TRequest>(bool sendChainId = true)
{
var data = (TRequest)Activator.CreateInstance(typeof(TRequest), parameters);
try
{
return await WagmiInterop.InteropCallAsync<TRequest, T>(method, data);
}
catch (KeyNotFoundException e)
{
throw new ReownIntegrationException(
"Can't execute request. The session was most likely terminated on the wallet side.", e);
}
}
switch (method)
{
case "personal_sign":
method = "signMessage";
return await MakeRequest<EthSignMessageWagmi>();
case "eth_signTypedData":
method = "signTypedData";
return await MakeRequest<EthSignTypedData>();
case "eth_sendTransaction":
method = "sendTransaction";
return await MakeRequest<EthSendTransactionWagmi>();
case "wallet_addEthereumChain" :
return default;
default:
try
{
// Direct RPC request via http, Reown RPC url.
var chain = W3AppKit.NetworkController.ActiveChain.ChainId;
// Using Reown Blockchain API: https://docs.reown.com/cloud/blockchain-api
var url = $"https://rpc.walletconnect.com/v1?chainId={chain}&projectId={ReownConfig.ProjectId}";
var body = JsonConvert.SerializeObject(new RpcRequestMessage(Guid.NewGuid().ToString(), method,
parameters));
var rawResult = await _httpClient.PostRaw(url, body, "application/json");
var response = JsonConvert.DeserializeObject<RpcResponseMessage>(rawResult.Response);
return response.Result.ToObject<T>();
}
catch (Exception e)
{
throw new ReownIntegrationException($"{method} RPC method currently not implemented.", e);
}
}
}
}
}
#endif