@reown/appkit-core-react-native 2.0.0-alpha.4 → 2.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/lib/commonjs/controllers/ApiController.js +102 -84
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -1
  3. package/lib/commonjs/controllers/BlockchainApiController.js +1 -1
  4. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
  5. package/lib/commonjs/controllers/ConnectionsController.js +34 -21
  6. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
  7. package/lib/commonjs/controllers/ModalController.js +4 -1
  8. package/lib/commonjs/controllers/ModalController.js.map +1 -1
  9. package/lib/commonjs/controllers/OnRampController.js +4 -1
  10. package/lib/commonjs/controllers/OnRampController.js.map +1 -1
  11. package/lib/commonjs/controllers/OptionsController.js +6 -3
  12. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  13. package/lib/commonjs/controllers/SendController.js +15 -1
  14. package/lib/commonjs/controllers/SendController.js.map +1 -1
  15. package/lib/commonjs/controllers/ThemeController.js +5 -0
  16. package/lib/commonjs/controllers/ThemeController.js.map +1 -1
  17. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +264 -0
  18. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
  19. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
  20. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  21. package/lib/commonjs/features/reown-authentication/index.js +28 -0
  22. package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
  23. package/lib/commonjs/index.js +12 -5
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/utils/ConstantsUtil.js +1 -5
  26. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  27. package/lib/commonjs/utils/CoreHelperUtil.js +13 -2
  28. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
  29. package/lib/commonjs/utils/FetchUtil.js.map +1 -1
  30. package/lib/commonjs/utils/StorageUtil.js +27 -27
  31. package/lib/commonjs/utils/StorageUtil.js.map +1 -1
  32. package/lib/module/controllers/ApiController.js +102 -84
  33. package/lib/module/controllers/ApiController.js.map +1 -1
  34. package/lib/module/controllers/BlockchainApiController.js +1 -1
  35. package/lib/module/controllers/BlockchainApiController.js.map +1 -1
  36. package/lib/module/controllers/ConnectionsController.js +35 -22
  37. package/lib/module/controllers/ConnectionsController.js.map +1 -1
  38. package/lib/module/controllers/ModalController.js +4 -1
  39. package/lib/module/controllers/ModalController.js.map +1 -1
  40. package/lib/module/controllers/OnRampController.js +4 -1
  41. package/lib/module/controllers/OnRampController.js.map +1 -1
  42. package/lib/module/controllers/OptionsController.js +6 -3
  43. package/lib/module/controllers/OptionsController.js.map +1 -1
  44. package/lib/module/controllers/SendController.js +15 -1
  45. package/lib/module/controllers/SendController.js.map +1 -1
  46. package/lib/module/controllers/ThemeController.js +5 -0
  47. package/lib/module/controllers/ThemeController.js.map +1 -1
  48. package/lib/module/features/reown-authentication/ReownAuthentication.js +260 -0
  49. package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
  50. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
  51. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  52. package/lib/module/features/reown-authentication/index.js +5 -0
  53. package/lib/module/features/reown-authentication/index.js.map +1 -0
  54. package/lib/module/index.js +4 -1
  55. package/lib/module/index.js.map +1 -1
  56. package/lib/module/utils/ConstantsUtil.js +1 -5
  57. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  58. package/lib/module/utils/CoreHelperUtil.js +13 -2
  59. package/lib/module/utils/CoreHelperUtil.js.map +1 -1
  60. package/lib/module/utils/FetchUtil.js.map +1 -1
  61. package/lib/module/utils/StorageUtil.js +28 -28
  62. package/lib/module/utils/StorageUtil.js.map +1 -1
  63. package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
  64. package/lib/typescript/controllers/ConnectionsController.d.ts +3 -2
  65. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
  66. package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
  67. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
  68. package/lib/typescript/controllers/OptionsController.d.ts +3 -3
  69. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  70. package/lib/typescript/controllers/RouterController.d.ts +1 -1
  71. package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
  72. package/lib/typescript/controllers/SendController.d.ts.map +1 -1
  73. package/lib/typescript/controllers/ThemeController.d.ts +2 -0
  74. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
  75. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
  76. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
  77. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
  78. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
  79. package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
  80. package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
  81. package/lib/typescript/index.d.ts +2 -1
  82. package/lib/typescript/index.d.ts.map +1 -1
  83. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  84. package/lib/typescript/utils/CoreHelperUtil.d.ts +2 -1
  85. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
  86. package/lib/typescript/utils/FetchUtil.d.ts +1 -1
  87. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
  88. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
  89. package/package.json +6 -5
  90. package/src/controllers/ApiController.ts +93 -73
  91. package/src/controllers/BlockchainApiController.ts +1 -1
  92. package/src/controllers/ConnectionsController.ts +60 -20
  93. package/src/controllers/ModalController.ts +5 -1
  94. package/src/controllers/OnRampController.ts +5 -1
  95. package/src/controllers/OptionsController.ts +11 -6
  96. package/src/controllers/RouterController.ts +3 -3
  97. package/src/controllers/SendController.ts +22 -2
  98. package/src/controllers/ThemeController.ts +7 -0
  99. package/src/features/reown-authentication/ReownAuthentication.ts +470 -0
  100. package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
  101. package/src/features/reown-authentication/index.ts +2 -0
  102. package/src/index.ts +4 -1
  103. package/src/utils/ConstantsUtil.ts +1 -5
  104. package/src/utils/CoreHelperUtil.ts +20 -2
  105. package/src/utils/FetchUtil.ts +1 -1
  106. package/src/utils/StorageUtil.ts +37 -53
  107. package/lib/commonjs/utils/RouterUtil.js +0 -25
  108. package/lib/commonjs/utils/RouterUtil.js.map +0 -1
  109. package/lib/module/utils/RouterUtil.js +0 -21
  110. package/lib/module/utils/RouterUtil.js.map +0 -1
  111. package/lib/typescript/utils/RouterUtil.d.ts +0 -5
  112. package/lib/typescript/utils/RouterUtil.d.ts.map +0 -1
  113. package/src/utils/RouterUtil.ts +0 -21
@@ -0,0 +1,470 @@
1
+ import {
2
+ type CaipNetworkId,
3
+ type ChainNamespace,
4
+ type SafeStorageItems,
5
+ SafeStorageKeys,
6
+ type SIWXConfig,
7
+ type SIWXMessage,
8
+ type SIWXSession
9
+ } from '@reown/appkit-common-react-native';
10
+
11
+ import { ApiController } from '../../controllers/ApiController';
12
+ import { BlockchainApiController } from '../../controllers/BlockchainApiController';
13
+
14
+ import { ReownAuthenticationMessenger } from './ReownAuthenticationMessenger';
15
+ import { ConnectionsController } from '../../controllers/ConnectionsController';
16
+ import { CoreHelperUtil } from '../../utils/CoreHelperUtil';
17
+ import { OptionsController } from '../../controllers/OptionsController';
18
+ import { FetchUtil } from '../../utils/FetchUtil';
19
+
20
+ /**
21
+ * This is the configuration for using SIWX with Reown Authentication service.
22
+ * It allows you to authenticate and capture user sessions through the Reown Dashboard.
23
+ */
24
+ export class ReownAuthentication implements SIWXConfig {
25
+ private readonly localAuthStorageKey: keyof SafeStorageItems;
26
+ private readonly localNonceStorageKey: keyof SafeStorageItems;
27
+ private readonly messenger: ReownAuthenticationMessenger;
28
+ private readonly fetchUtil = new FetchUtil({ baseUrl: CoreHelperUtil.getApiUrl() });
29
+ public readonly signOutOnDisconnect: boolean;
30
+
31
+ private required: boolean;
32
+
33
+ private listeners: ReownAuthentication.EventListeners = {
34
+ sessionChanged: []
35
+ };
36
+
37
+ constructor(params: ReownAuthentication.ConstructorParams = {}) {
38
+ this.localAuthStorageKey =
39
+ (params.localAuthStorageKey as keyof SafeStorageItems) || SafeStorageKeys.SIWX_AUTH_TOKEN;
40
+ this.localNonceStorageKey =
41
+ (params.localNonceStorageKey as keyof SafeStorageItems) || SafeStorageKeys.SIWX_NONCE_TOKEN;
42
+ this.required = params.required ?? true;
43
+
44
+ this.messenger = new ReownAuthenticationMessenger({
45
+ getNonce: this.getNonce.bind(this)
46
+ });
47
+
48
+ this.signOutOnDisconnect = params.signOutOnDisconnect ?? true;
49
+ }
50
+
51
+ async createMessage(input: SIWXMessage.Input): Promise<SIWXMessage> {
52
+ return this.messenger.createMessage(input);
53
+ }
54
+
55
+ async addSession(session: SIWXSession): Promise<void> {
56
+ const response = await this.request({
57
+ method: 'POST',
58
+ key: 'authenticate',
59
+ body: {
60
+ data: session.cacao ? undefined : session.data,
61
+ message: session.message,
62
+ signature: session.signature,
63
+ clientId: this.getClientId(),
64
+ walletInfo: this.getWalletInfo()
65
+ },
66
+ headers: ['nonce']
67
+ });
68
+
69
+ this.setStorageToken(response.token, this.localAuthStorageKey);
70
+
71
+ this.emit('sessionChanged', session);
72
+ }
73
+
74
+ async getSessions(chainId: CaipNetworkId, address: string): Promise<SIWXSession[]> {
75
+ try {
76
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
77
+ if (!sessions) {
78
+ return [];
79
+ }
80
+
81
+ const account = await this.request({
82
+ method: 'GET',
83
+ key: 'me',
84
+ query: {},
85
+ headers: ['auth']
86
+ });
87
+
88
+ if (!account) {
89
+ return [];
90
+ }
91
+
92
+ const isSameAddress = account.address.toLowerCase() === address.toLowerCase();
93
+ const isSameNetwork = account.caip2Network === chainId;
94
+
95
+ if (!isSameAddress || !isSameNetwork) {
96
+ return [];
97
+ }
98
+
99
+ const session: SIWXSession = {
100
+ data: {
101
+ accountAddress: account.address,
102
+ chainId: account.caip2Network
103
+ } as SIWXMessage.Data,
104
+ message: '',
105
+ signature: ''
106
+ };
107
+
108
+ this.emit('sessionChanged', session);
109
+
110
+ return [session];
111
+ } catch {
112
+ return [];
113
+ }
114
+ }
115
+
116
+ async revokeSession(_chainId: CaipNetworkId, _address: string): Promise<void> {
117
+ return Promise.resolve(this.clearStorageTokens());
118
+ }
119
+
120
+ async setSessions(sessions: SIWXSession[]): Promise<void> {
121
+ if (sessions.length === 0) {
122
+ this.clearStorageTokens();
123
+ } else {
124
+ const session = (sessions.find(
125
+ s => s.data.chainId === ConnectionsController.state.activeCaipNetworkId
126
+ ) || sessions[0]) as SIWXSession;
127
+
128
+ await this.addSession(session);
129
+ }
130
+ }
131
+
132
+ getRequired() {
133
+ return this.required;
134
+ }
135
+
136
+ async getSessionAccount() {
137
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
138
+ if (!sessions) {
139
+ throw new Error('Not authenticated');
140
+ }
141
+
142
+ return this.request({
143
+ method: 'GET',
144
+ key: 'me',
145
+ body: undefined,
146
+ query: {
147
+ includeAppKitAccount: true
148
+ },
149
+ headers: ['auth']
150
+ });
151
+ }
152
+
153
+ async setSessionAccountMetadata(metadata: object | null = null) {
154
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
155
+ if (!sessions) {
156
+ throw new Error('Not authenticated');
157
+ }
158
+
159
+ return this.request({
160
+ method: 'PUT',
161
+ key: 'account-metadata',
162
+ body: { metadata },
163
+ headers: ['auth']
164
+ });
165
+ }
166
+
167
+ on<Event extends keyof ReownAuthentication.Events>(
168
+ event: Event,
169
+ callback: ReownAuthentication.Listener<Event>
170
+ ) {
171
+ this.listeners[event].push(callback);
172
+
173
+ return () => {
174
+ this.listeners[event] = this.listeners[event].filter(
175
+ cb => cb !== callback
176
+ ) as ReownAuthentication.EventListeners[Event];
177
+ };
178
+ }
179
+
180
+ removeAllListeners() {
181
+ const keys = Object.keys(this.listeners) as (keyof ReownAuthentication.Events)[];
182
+ keys.forEach(key => {
183
+ this.listeners[key] = [];
184
+ });
185
+ }
186
+
187
+ private async request<
188
+ Method extends ReownAuthentication.Methods,
189
+ Key extends ReownAuthentication.RequestKeys<Method>
190
+ >({
191
+ method,
192
+ key,
193
+ query,
194
+ body,
195
+ headers
196
+ }: ReownAuthentication.RequestParams<Key, Method>): Promise<
197
+ ReownAuthentication.RequestResponse<Method, Key>
198
+ > {
199
+ const { projectId, st, sv, domain } = this.getSDKProperties();
200
+
201
+ const url = this.fetchUtil.createUrl({
202
+ path: `/auth/v1/${String(key)}`,
203
+ params: { projectId, st, sv, domain, ...query }
204
+ });
205
+
206
+ const nonceJwt = await this.getStorageToken(this.localNonceStorageKey);
207
+ const auth = await this.getStorageToken(this.localAuthStorageKey);
208
+
209
+ const response = await fetch(url, {
210
+ method,
211
+ body: body ? JSON.stringify(body) : undefined,
212
+ headers: {
213
+ 'Content-Type': 'application/json',
214
+ ...(Array.isArray(headers)
215
+ ? headers.reduce((acc, header) => {
216
+ switch (header) {
217
+ case 'nonce':
218
+ acc['x-nonce-jwt'] = `Bearer ${nonceJwt}`;
219
+ break;
220
+ case 'auth':
221
+ acc['Authorization'] = `Bearer ${auth}`;
222
+ break;
223
+ default:
224
+ break;
225
+ }
226
+
227
+ return acc;
228
+ }, {})
229
+ : {})
230
+ }
231
+ });
232
+
233
+ if (!response.ok) {
234
+ throw new Error(await response.text());
235
+ }
236
+
237
+ if (response.headers.get('content-type')?.includes('application/json')) {
238
+ return response.json();
239
+ }
240
+
241
+ return null as ReownAuthentication.RequestResponse<Method, Key>;
242
+ }
243
+
244
+ private async getStorageToken(key: keyof SafeStorageItems): Promise<string | undefined> {
245
+ return OptionsController.getStorage().getItem(key);
246
+ }
247
+
248
+ private setStorageToken(token: string, key: keyof SafeStorageItems): void {
249
+ OptionsController.getStorage().setItem(key, token);
250
+ }
251
+
252
+ private clearStorageTokens(): void {
253
+ OptionsController.getStorage().removeItem(this.localAuthStorageKey);
254
+ OptionsController.getStorage().removeItem(this.localNonceStorageKey);
255
+ this.emit('sessionChanged', undefined);
256
+ }
257
+
258
+ private async getNonce(): Promise<string> {
259
+ const { nonce, token } = await this.request({
260
+ method: 'GET',
261
+ key: 'nonce'
262
+ });
263
+
264
+ this.setStorageToken(token, this.localNonceStorageKey);
265
+
266
+ return nonce;
267
+ }
268
+
269
+ private getClientId(): string | null {
270
+ return BlockchainApiController.state.clientId;
271
+ }
272
+
273
+ private getWalletInfo(): ReownAuthentication.WalletInfo | undefined {
274
+ const connectedWalletInfo = ConnectionsController.state.walletInfo;
275
+ const connectionProperties = ConnectionsController.state.connection?.properties;
276
+
277
+ if (!connectedWalletInfo || !connectionProperties) {
278
+ return undefined;
279
+ }
280
+
281
+ if (connectionProperties?.provider) {
282
+ const social = connectionProperties.provider;
283
+ const identifier = connectionProperties.email || 'unknown';
284
+
285
+ return { type: 'social', social, identifier };
286
+ }
287
+
288
+ let { name, icon, type } = connectedWalletInfo;
289
+
290
+ if (!type || type !== 'walletconnect') {
291
+ type = 'unknown';
292
+ }
293
+
294
+ return {
295
+ type,
296
+ name,
297
+ icon
298
+ };
299
+ }
300
+
301
+ private getSDKProperties(): { projectId: string; st: string; sv: string; domain: string } {
302
+ const headers = ApiController._getApiHeaders();
303
+
304
+ return {
305
+ projectId: headers['x-project-id'],
306
+ st: headers['x-sdk-type'],
307
+ sv: headers['x-sdk-version'],
308
+ domain: headers['origin']
309
+ };
310
+ }
311
+
312
+ private emit<Event extends keyof ReownAuthentication.Events>(
313
+ event: Event,
314
+ data: ReownAuthentication.Events[Event]
315
+ ) {
316
+ this.listeners[event].forEach(listener => listener(data));
317
+ }
318
+ }
319
+
320
+ export namespace ReownAuthentication {
321
+ export type ConstructorParams = {
322
+ /**
323
+ * The key to use for storing the session token in local storage.
324
+ * @default '@appkit/siwx-auth-token'
325
+ */
326
+ localAuthStorageKey?: string;
327
+ /**
328
+ * The key to use for storing the nonce token in local storage.
329
+ * @default '@appkit/siwx-nonce-token'
330
+ */
331
+ localNonceStorageKey?: string;
332
+ /**
333
+ * If false the wallet stays connected when user denies the signature request.
334
+ * @default true
335
+ */
336
+ required?: boolean;
337
+ /**
338
+ * This flag determines whether the session should be cleared when the user disconnects.
339
+ *
340
+ * @default true
341
+ */
342
+ signOutOnDisconnect?: boolean;
343
+ };
344
+
345
+ export type AvailableRequestHeaders = {
346
+ nonce: {
347
+ 'x-nonce-jwt': string;
348
+ };
349
+ auth: {
350
+ Authorization: string;
351
+ };
352
+ origin: {
353
+ origin: string;
354
+ };
355
+ };
356
+
357
+ export type RequestParams<Key extends keyof Requests[Method], Method extends Methods> = {
358
+ method: Method;
359
+ key: Key;
360
+ // @ts-expect-error - This is matching correctly already
361
+ } & Pick<Requests[Method][Key], 'query' | 'body' | 'headers'>;
362
+
363
+ export type RequestResponse<
364
+ Method extends Methods,
365
+ Key extends RequestKeys<Method>
366
+ // @ts-expect-error - This is matching correctly already
367
+ > = Requests[Method][Key]['response'];
368
+
369
+ export type Request<
370
+ Body,
371
+ Response,
372
+ Query extends Record<string, unknown> | undefined = undefined,
373
+ Headers extends (keyof AvailableRequestHeaders)[] | undefined = undefined
374
+ > = (Response extends undefined
375
+ ? {
376
+ response?: never;
377
+ }
378
+ : {
379
+ response: Response;
380
+ }) &
381
+ (Body extends undefined ? { body?: never } : { body: Body }) &
382
+ (Query extends undefined ? { query?: never } : { query: Query }) &
383
+ (Headers extends undefined ? { headers?: never } : { headers: Headers });
384
+
385
+ export type Requests = {
386
+ GET: {
387
+ nonce: Request<undefined, { nonce: string; token: string }>;
388
+ me: Request<
389
+ undefined,
390
+ Omit<SessionAccount, 'appKitAccount'>,
391
+ { includeAppKitAccount?: boolean },
392
+ ['auth']
393
+ >;
394
+ };
395
+ POST: {
396
+ 'authenticate': Request<
397
+ {
398
+ data?: SIWXMessage.Data;
399
+ message: string;
400
+ signature: string;
401
+ clientId?: string | null;
402
+ walletInfo?: WalletInfo;
403
+ },
404
+ {
405
+ token: string;
406
+ },
407
+ undefined,
408
+ ['nonce']
409
+ >;
410
+ 'sign-out': Request<undefined, never, never, ['auth']>;
411
+ };
412
+ PUT: {
413
+ 'account-metadata': Request<{ metadata: object | null }, unknown, undefined, ['auth']>;
414
+ };
415
+ };
416
+
417
+ export type Methods = 'GET' | 'POST' | 'PUT';
418
+
419
+ export type RequestKeys<Method extends Methods> = keyof Requests[Method];
420
+
421
+ export type WalletInfo =
422
+ | {
423
+ type: 'walletconnect' | 'external' | 'unknown';
424
+ name: string | undefined;
425
+ icon: string | undefined;
426
+ }
427
+ | { type: 'social'; social: string; identifier: string };
428
+
429
+ export type Events = {
430
+ sessionChanged: SIWXSession | undefined;
431
+ };
432
+
433
+ export type Listener<Event extends keyof Events> = (event: Events[Event]) => void;
434
+
435
+ export type EventListeners = {
436
+ [Key in keyof Events]: Listener<Key>[];
437
+ };
438
+
439
+ export type SessionAccount = {
440
+ aud: string;
441
+ iss: string;
442
+ exp: number;
443
+ projectIdKey: string;
444
+ sub: string;
445
+ address: string;
446
+ chainId: number | string;
447
+ chainNamespace: ChainNamespace;
448
+ caip2Network: string;
449
+ uri: string;
450
+ domain: string;
451
+ projectUuid: string;
452
+ profileUuid: string;
453
+ nonce: string;
454
+ email?: string;
455
+ appKitAccount?: {
456
+ uuid: string;
457
+ caip2_chain: string;
458
+ address: string;
459
+ profile_uuid: string;
460
+ created_at: string;
461
+ is_main_account: boolean;
462
+ verification_status: null;
463
+ connection_method: object | null;
464
+ metadata: object;
465
+ last_signed_in_at: string;
466
+ signed_up_at: string;
467
+ updated_at: string;
468
+ };
469
+ };
470
+ }
@@ -0,0 +1,80 @@
1
+ import type { SIWXMessage, CaipNetworkId } from '@reown/appkit-common-react-native';
2
+ import { ConnectionsController } from '../../controllers/ConnectionsController';
3
+ import { OptionsController } from '../../controllers/OptionsController';
4
+ import { CoreHelperUtil } from '../../utils/CoreHelperUtil';
5
+
6
+ export class ReownAuthenticationMessenger {
7
+ public resources?: SIWXMessage['resources'];
8
+
9
+ protected getNonce: (params: SIWXMessage.Input) => Promise<SIWXMessage['nonce']>;
10
+
11
+ constructor(params: ReownAuthenticationMessenger.ConstructorParams) {
12
+ this.getNonce = params.getNonce;
13
+ }
14
+
15
+ async createMessage(input: SIWXMessage.Input): Promise<SIWXMessage> {
16
+ const metadata = OptionsController.state.metadata;
17
+ const domain = CoreHelperUtil.getBundleId();
18
+ const params = {
19
+ accountAddress: input.accountAddress,
20
+ chainId: input.chainId,
21
+ version: '1',
22
+ domain: domain ?? 'Unknown Domain',
23
+ uri: metadata?.redirect?.universal ?? metadata?.redirect?.native ?? 'Unknown URI',
24
+ resources: this.resources,
25
+ nonce: await this.getNonce(input),
26
+ issuedAt: this.stringifyDate(new Date()),
27
+ statement: undefined,
28
+ expirationTime: undefined,
29
+ notBefore: undefined
30
+ };
31
+
32
+ const methods = {
33
+ toString: () => this.stringify(params)
34
+ } satisfies SIWXMessage.Methods;
35
+
36
+ return Object.assign(params, methods);
37
+ }
38
+
39
+ private stringify(params: SIWXMessage.Data): string {
40
+ const networkName = this.getNetworkName(params.chainId);
41
+
42
+ return [
43
+ `${params.domain} wants you to sign in with your ${networkName} account:`,
44
+ params.accountAddress,
45
+ params.statement ? `\n${params.statement}\n` : '',
46
+ `URI: ${params.uri}`,
47
+ `Version: ${params.version}`,
48
+ `Chain ID: ${params.chainId}`,
49
+ `Nonce: ${params.nonce}`,
50
+ params.issuedAt && `Issued At: ${params.issuedAt}`,
51
+ params.expirationTime && `Expiration Time: ${params.expirationTime}`,
52
+ params.notBefore && `Not Before: ${params.notBefore}`,
53
+ params.requestId && `Request ID: ${params.requestId}`,
54
+ params.resources?.length &&
55
+ params.resources.reduce((acc, resource) => `${acc}\n- ${resource}`, 'Resources:')
56
+ ]
57
+ .filter(line => typeof line === 'string')
58
+ .join('\n')
59
+ .trim();
60
+ }
61
+
62
+ private getNetworkName(chainId: CaipNetworkId): string | undefined {
63
+ const requestedNetworks = ConnectionsController.state.networks;
64
+
65
+ return (
66
+ requestedNetworks.find(network => network.caipNetworkId === chainId)?.name ||
67
+ 'Unknown Network'
68
+ );
69
+ }
70
+
71
+ private stringifyDate(date: Date): string {
72
+ return date.toISOString();
73
+ }
74
+ }
75
+
76
+ export namespace ReownAuthenticationMessenger {
77
+ export interface ConstructorParams {
78
+ getNonce: (params: SIWXMessage.Input) => Promise<SIWXMessage['nonce']>;
79
+ }
80
+ }
@@ -0,0 +1,2 @@
1
+ export * from './ReownAuthentication';
2
+ export * from './ReownAuthenticationMessenger';
package/src/index.ts CHANGED
@@ -53,7 +53,10 @@ export { ConstantsUtil } from './utils/ConstantsUtil';
53
53
  export { CoreHelperUtil } from './utils/CoreHelperUtil';
54
54
  export { StorageUtil } from './utils/StorageUtil';
55
55
  export { EventUtil } from './utils/EventUtil';
56
- export { RouterUtil } from './utils/RouterUtil';
57
56
  export { WalletUtil } from './utils/WalletUtil';
58
57
 
58
+ // -- Features ----------------------------------------------------------------
59
+ export { ReownAuthentication } from './features/reown-authentication/ReownAuthentication';
60
+ export { ReownAuthenticationMessenger } from './features/reown-authentication/ReownAuthenticationMessenger';
61
+
59
62
  // Types are now exported from @reown/appkit-common-react-native
@@ -219,8 +219,6 @@ export const ConstantsUtil = {
219
219
  'eip155:1499',
220
220
  // Sonic
221
221
  'eip155:7007',
222
- // Swellchain
223
- 'eip155:7777777',
224
222
  // Taiko
225
223
  'eip155:167000',
226
224
  // Viction
@@ -238,9 +236,7 @@ export const ConstantsUtil = {
238
236
  // ZkLink Nova
239
237
  'eip155:810180',
240
238
  // re.al
241
- 'eip155:666',
242
- // Zora
243
- 'eip155:7777777'
239
+ 'eip155:666'
244
240
  ],
245
241
 
246
242
  SWAP_SUPPORTED_NETWORKS: [
@@ -53,6 +53,20 @@ export const CoreHelperUtil = {
53
53
  return Date.now() - lastRetry >= ConstantsUtil.ONE_SEC_MS;
54
54
  },
55
55
 
56
+ isCaipAddress(address?: unknown): address is CaipAddress {
57
+ if (typeof address !== 'string') {
58
+ return false;
59
+ }
60
+
61
+ const sections = address.split(':');
62
+ const namespace = sections[0];
63
+
64
+ return (
65
+ sections.filter(Boolean).length === 3 &&
66
+ (namespace as string) in CommonConstants.CHAIN_NAME_MAP
67
+ );
68
+ },
69
+
56
70
  getPairingExpiry() {
57
71
  return Date.now() + ConstantsUtil.FOUR_MINUTES_MS;
58
72
  },
@@ -61,8 +75,12 @@ export const CoreHelperUtil = {
61
75
  return caipAddress?.split(':')[1];
62
76
  },
63
77
 
64
- getPlainAddress(caipAddress: CaipAddress | undefined) {
65
- return caipAddress?.split(':')[2];
78
+ getPlainAddress(address: CaipAddress | string | undefined) {
79
+ if (this.isCaipAddress(address)) {
80
+ return address?.split(':')[2];
81
+ }
82
+
83
+ return address;
66
84
  },
67
85
 
68
86
  async wait(milliseconds: number) {
@@ -87,7 +87,7 @@ export class FetchUtil {
87
87
  }
88
88
  }
89
89
 
90
- private createUrl({ path, params }: RequestArguments) {
90
+ public createUrl({ path, params }: RequestArguments) {
91
91
  let fullUrl: string;
92
92
 
93
93
  const isAbsoluteUrl = path.startsWith('http://') || path.startsWith('https://');