-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathAddressProvider.cs
281 lines (237 loc) · 7.67 KB
/
AddressProvider.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using WalletConnectSharp.Common.Utils;
using WalletConnectSharp.Core;
using WalletConnectSharp.Sign.Interfaces;
using WalletConnectSharp.Sign.Models;
using WalletConnectSharp.Sign.Models.Engine.Events;
namespace WalletConnectSharp.Sign.Controllers;
public class AddressProvider : IAddressProvider
{
private bool _disposed;
public struct DefaultData
{
public SessionStruct Session;
public string Namespace;
public string ChainId;
}
public event EventHandler<DefaultsLoadingEventArgs> DefaultsLoaded;
public bool HasDefaultSession
{
get
{
return !string.IsNullOrWhiteSpace(DefaultSession.Topic) && DefaultSession.Namespaces != null;
}
}
public string Name
{
get
{
return $"{_client.Name}-address-provider";
}
}
public string Context
{
get
{
return Name;
}
}
private DefaultData _state;
public SessionStruct DefaultSession
{
get
{
return _state.Session;
}
set
{
_state.Session = value;
}
}
public string DefaultNamespace
{
get
{
return _state.Namespace;
}
set
{
_state.Namespace = value;
}
}
public string DefaultChainId
{
get
{
return _state.ChainId;
}
set
{
_state.ChainId = value;
}
}
public ISession Sessions { get; private set; }
private ISignClient _client;
public AddressProvider(ISignClient client)
{
this._client = client;
this.Sessions = client.Session;
// set the first connected session to the default one
client.SessionConnected += ClientOnSessionConnected;
client.SessionDeleted += ClientOnSessionDeleted;
client.SessionUpdateRequest += ClientOnSessionUpdated;
client.SessionApproved += ClientOnSessionConnected;
}
public virtual async Task SaveDefaults()
{
await _client.Core.Storage.SetItem($"{Context}-default-session", _state);
}
public virtual async Task LoadDefaultsAsync()
{
var key = $"{Context}-default-session";
if (await _client.Core.Storage.HasItem(key))
{
var state = await _client.Core.Storage.GetItem<DefaultData>(key);
var sessionExpiry = state.Session.Expiry;
_state = sessionExpiry != null && !Clock.IsExpired(sessionExpiry.Value)
? state
: new DefaultData();
}
else
{
_state = new DefaultData();
}
DefaultsLoaded?.Invoke(this, new DefaultsLoadingEventArgs(_state));
}
private async void ClientOnSessionUpdated(object sender, SessionEvent e)
{
if (DefaultSession.Topic == e.Topic)
{
DefaultSession = Sessions.Get(e.Topic);
await UpdateDefaultChainIdAndNamespaceAsync();
}
}
private async void ClientOnSessionDeleted(object sender, SessionEvent e)
{
if (DefaultSession.Topic == e.Topic)
{
DefaultSession = default;
await UpdateDefaultChainIdAndNamespaceAsync();
}
}
private async void ClientOnSessionConnected(object sender, SessionStruct e)
{
DefaultSession = e;
await UpdateDefaultChainIdAndNamespaceAsync();
}
private async Task UpdateDefaultChainIdAndNamespaceAsync()
{
if (HasDefaultSession)
{
// Check if current default namespace is still valid with the current session
var currentDefault = DefaultNamespace;
if (currentDefault != null && DefaultSession.Namespaces.ContainsKey(currentDefault))
{
if (!DefaultSession.Namespaces[DefaultNamespace].TryGetChains(out var approvedChains))
{
throw new InvalidOperationException("Could not get chains for current default namespace");
}
// Check if current default chain is still valid with the current session
var currentChain = DefaultChainId;
if (currentChain == null || !approvedChains.Contains(currentChain))
{
// If the current default chain is not valid, let's use the first one
DefaultChainId = approvedChains[0];
}
}
else
{
// If DefaultNamespace is null or not found in current available spaces, update it
DefaultNamespace = DefaultSession.Namespaces.Keys.FirstOrDefault();
if (DefaultNamespace != null)
{
if (!DefaultSession.Namespaces[DefaultNamespace].TryGetChains(out var approvedChains))
{
throw new InvalidOperationException("Could not get chains for current default namespace");
}
DefaultChainId = approvedChains[0];
}
else
{
throw new InvalidOperationException("Could not figure out default chain and namespace");
}
}
await SaveDefaults();
}
else
{
DefaultNamespace = null;
DefaultChainId = null;
}
}
public async Task SetDefaultNamespaceAsync(string @namespace)
{
if (string.IsNullOrWhiteSpace(@namespace))
{
throw new ArgumentNullException(nameof(@namespace));
}
if (!DefaultSession.Namespaces.ContainsKey(@namespace))
{
throw new InvalidOperationException($"Namespace {@namespace} is not available in the current session");
}
DefaultNamespace = @namespace;
await SaveDefaults();
}
public async Task SetDefaultChainIdAsync(string chainId)
{
if (string.IsNullOrWhiteSpace(chainId))
{
throw new ArgumentNullException(nameof(chainId));
}
if (!Utils.IsValidChainId(chainId))
{
throw new ArgumentException("The format of 'chainId' is invalid. Must be in the format of 'namespace:chainId' (e.g. 'eip155:10'). See CAIP-2 for more information.");
}
DefaultChainId = chainId;
await SaveDefaults();
}
public Caip25Address CurrentAddress(string chainId = null, SessionStruct session = default)
{
chainId ??= DefaultChainId;
if (string.IsNullOrWhiteSpace(session.Topic))
{
session = DefaultSession;
}
return session.CurrentAddress(chainId);
}
public IEnumerable<Caip25Address> AllAddresses(string @namespace = null, SessionStruct session = default)
{
@namespace ??= DefaultNamespace;
if (string.IsNullOrWhiteSpace(session.Topic)) // default
session = DefaultSession;
return session.AllAddresses(@namespace);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
_client.SessionConnected -= ClientOnSessionConnected;
_client.SessionDeleted -= ClientOnSessionDeleted;
_client.SessionUpdateRequest -= ClientOnSessionUpdated;
_client.SessionApproved -= ClientOnSessionConnected;
_client = null;
Sessions = null;
DefaultNamespace = null;
DefaultSession = default;
}
_disposed = true;
}
}