-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherc7715RequestExecutionPermissionsAction.ts
More file actions
519 lines (477 loc) · 14.1 KB
/
Copy patherc7715RequestExecutionPermissionsAction.ts
File metadata and controls
519 lines (477 loc) · 14.1 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import type {
AccountSigner,
Erc20TokenPeriodicPermission,
Erc20TokenStreamPermission,
NativeTokenPeriodicPermission,
NativeTokenStreamPermission,
PermissionRequest,
PermissionResponse,
PermissionTypes,
Rule,
} from '@metamask/7715-permission-types';
import { isHex, toHex } from 'viem';
import type {
Client,
Account,
RpcSchema,
Transport,
Chain,
Address,
} from 'viem';
/**
* RPC schema for MetaMask related methods.
*
* Extends the base RPC schema with methods specific to interacting with EIP-7715:
* - `wallet_invokeSnap`: Invokes a method on a specific Snap.
*/
export type MetaMaskExtensionSchema = RpcSchema &
[
{
// eslint-disable-next-line @typescript-eslint/naming-convention
Method: 'wallet_requestExecutionPermissions';
// eslint-disable-next-line @typescript-eslint/naming-convention
Params: PermissionRequest<AccountSigner, PermissionTypes>[];
// eslint-disable-next-line @typescript-eslint/naming-convention
ReturnType: PermissionResponse<AccountSigner, PermissionTypes>[];
},
];
/**
* A Viem client extended with MetaMask Snap-specific RPC methods.
*
* This client type allows for interaction with MetaMask Snaps through
* the standard Viem client interface, with added type safety for
* Snap-specific methods.
*/
export type MetaMaskExtensionClient = Client<
Transport,
Chain | undefined,
Account | undefined,
MetaMaskExtensionSchema
>;
type PermissionParameter = {
type: string;
data: Record<string, unknown>;
};
/**
* Represents a native token stream permission.
* This allows for continuous token streaming with defined parameters.
*/
export type NativeTokenStreamPermissionParameter = PermissionParameter & {
type: 'native-token-stream';
data: {
amountPerSecond: bigint;
initialAmount?: bigint;
maxAmount?: bigint;
startTime?: number;
justification?: string;
};
};
/**
* Represents an ERC-20 token stream permission.
* This allows for continuous ERC-20 token streaming with defined parameters.
*/
export type Erc20TokenStreamPermissionParameter = PermissionParameter & {
type: 'erc20-token-stream';
data: {
tokenAddress: Address;
amountPerSecond: bigint;
initialAmount?: bigint;
maxAmount?: bigint;
startTime?: number;
justification?: string;
};
};
/**
* Represents a native token periodic permission.
* This allows for periodic native token transfers with defined parameters.
*/
export type NativeTokenPeriodicPermissionParameter = PermissionParameter & {
type: 'native-token-periodic';
data: {
periodAmount: bigint;
periodDuration: number;
startTime?: number;
justification?: string;
};
};
/**
* Represents an ERC-20 token periodic permission.
* This allows for periodic ERC-20 token transfers with defined parameters.
*/
export type Erc20TokenPeriodicPermissionParameter = PermissionParameter & {
type: 'erc20-token-periodic';
data: {
tokenAddress: Address;
periodAmount: bigint;
periodDuration: number;
startTime?: number;
justification?: string;
};
};
export type SupportedPermissionParams =
| NativeTokenStreamPermissionParameter
| Erc20TokenStreamPermissionParameter
| NativeTokenPeriodicPermissionParameter
| Erc20TokenPeriodicPermissionParameter;
export type SignerParam = Address | AccountSigner;
/**
* Represents a single permission request.
*/
export type PermissionRequestParameter = {
chainId: number;
// The permission to grant to the user.
permission: SupportedPermissionParams;
// Whether the caller allows the permission to be adjusted.
isAdjustmentAllowed: boolean;
// Account to assign the permission to.
signer: SignerParam;
// address from which the permission should be granted.
address?: Address;
// Timestamp (in seconds) that specifies the time by which this permission MUST expire.
expiry: number;
};
/**
* Parameters for the RequestExecutionPermissions action.
*
* @template Signer - The type of the signer, either an Address or Account.
*/
export type RequestExecutionPermissionsParameters =
PermissionRequestParameter[];
/**
* Return type for the request execution permissions action.
*/
export type RequestExecutionPermissionsReturnType = PermissionResponse<
AccountSigner,
PermissionTypes
>[];
/**
* Grants permissions according to EIP-7715 specification.
*
* @template Signer - The type of the signer, either an Address or Account.
* @param client - The client to use for the request.
* @param parameters - The permissions requests to grant.
* @returns A promise that resolves to the permission responses.
* @description
* This function formats the permissions requests and invokes the wallet method to grant permissions.
* It will throw an error if the permissions could not be granted.
*/
export async function erc7715RequestExecutionPermissionsAction(
client: MetaMaskExtensionClient,
parameters: RequestExecutionPermissionsParameters,
): Promise<RequestExecutionPermissionsReturnType> {
const formattedPermissionRequest = parameters.map(formatPermissionsRequest);
const result = await client.request(
{
method: 'wallet_requestExecutionPermissions',
params: formattedPermissionRequest,
},
{ retryCount: 0 },
);
if (!result) {
throw new Error('Failed to grant permissions');
}
return result;
}
/**
* Formats a permissions request for submission to the wallet.
*
* @param parameters - The permissions request to format.
* @returns The formatted permissions request.
* @internal
*/
function formatPermissionsRequest(
parameters: PermissionRequestParameter,
): PermissionRequest<AccountSigner, PermissionTypes> {
const { chainId, address, expiry, isAdjustmentAllowed } = parameters;
const permissionFormatter = getPermissionFormatter(
parameters.permission.type,
);
const signerAddress =
typeof parameters.signer === 'string'
? parameters.signer
: parameters.signer.data.address;
const rules: Rule[] = [
{
type: 'expiry',
isAdjustmentAllowed,
data: {
timestamp: expiry,
},
},
];
const optionalFields = {
...(address ? { address } : {}),
};
return {
...optionalFields,
chainId: toHex(chainId),
permission: permissionFormatter({
permission: parameters.permission,
isAdjustmentAllowed,
}),
signer: {
// MetaMask 7715 implementation only supports AccountSigner
type: 'account',
data: {
address: signerAddress,
},
},
rules,
};
}
/**
* Checks if a value is defined (not null or undefined).
*
* @param value - The value to check.
* @returns A boolean indicating whether the value is defined.
*/
function isDefined<TValue>(value: TValue | null | undefined): value is TValue {
return value !== undefined && value !== null;
}
/**
* Asserts that a value is defined (not null or undefined).
*
* @param value - The value to check.
* @param message - Optional custom error message to throw if the value is not defined.
* @throws {Error} If the value is null or undefined.
*/
function assertIsDefined<TValue>(
value: TValue | null | undefined,
message?: string,
): asserts value is TValue {
if (!isDefined(value)) {
throw new Error(message ?? 'Invalid parameters: value is required');
}
}
/**
* Converts a value to a hex string or throws an error if the value is invalid.
*
* @param value - The value to convert to hex.
* @param message - Optional custom error message.
* @returns The value as a hex string.
*/
function toHexOrThrow(
value: Parameters<typeof toHex>[0] | undefined,
message?: string,
) {
assertIsDefined(value, message);
if (typeof value === 'string') {
if (!isHex(value)) {
throw new Error('Invalid parameters: invalid hex value');
}
return value;
}
return toHex(value);
}
type PermissionFormatter = (params: {
permission: PermissionParameter;
isAdjustmentAllowed: boolean;
}) => PermissionTypes;
/**
* Gets the appropriate formatter function for a specific permission type.
*
* @param permissionType - The type of permission to format.
* @returns A formatter function for the specified permission type.
*/
function getPermissionFormatter(permissionType: string): PermissionFormatter {
switch (permissionType) {
case 'native-token-stream':
return ({ permission, isAdjustmentAllowed }) =>
formatNativeTokenStreamPermission({
permission: permission as NativeTokenStreamPermissionParameter,
isAdjustmentAllowed,
});
case 'erc20-token-stream':
return ({ permission, isAdjustmentAllowed }) =>
formatErc20TokenStreamPermission({
permission: permission as Erc20TokenStreamPermissionParameter,
isAdjustmentAllowed,
});
case 'native-token-periodic':
return ({ permission, isAdjustmentAllowed }) =>
formatNativeTokenPeriodicPermission({
permission: permission as NativeTokenPeriodicPermissionParameter,
isAdjustmentAllowed,
});
case 'erc20-token-periodic':
return ({ permission, isAdjustmentAllowed }) =>
formatErc20TokenPeriodicPermission({
permission: permission as Erc20TokenPeriodicPermissionParameter,
isAdjustmentAllowed,
});
default:
throw new Error(`Unsupported permission type: ${permissionType}`);
}
}
/**
* Formats a native token stream permission for the wallet.
*
* @param permission - The native token stream permission to format.
* @param permission.permission - The native token stream permission to format.
* @param permission.isAdjustmentAllowed - Whether the permission is allowed to be adjusted.
* @returns The formatted permission object.
*/
function formatNativeTokenStreamPermission({
permission,
isAdjustmentAllowed,
}: {
permission: NativeTokenStreamPermissionParameter;
isAdjustmentAllowed: boolean;
}): NativeTokenStreamPermission {
const {
data: {
initialAmount,
justification,
maxAmount,
startTime,
amountPerSecond,
},
} = permission;
const optionalFields = {
...(isDefined(initialAmount) && {
initialAmount: toHexOrThrow(initialAmount),
}),
...(isDefined(maxAmount) && {
maxAmount: toHexOrThrow(maxAmount),
}),
...(isDefined(startTime) && {
startTime: Number(startTime),
}),
...(justification ? { justification } : {}),
};
return {
type: 'native-token-stream',
data: {
amountPerSecond: toHexOrThrow(
amountPerSecond,
'Invalid parameters: amountPerSecond is required',
),
...optionalFields,
},
isAdjustmentAllowed,
};
}
/**
* Formats an ERC-20 token stream permission parameter into the required
* Erc20TokenStreamPermission object, converting numeric values to hex strings
* and including only specified optional fields.
*
* @param params - The parameters for formatting the ERC-20 token stream permission.
* @param params.permission - The ERC-20 token stream permission parameter to format.
* @param params.isAdjustmentAllowed - Whether adjustment of the stream is allowed.
* @returns The formatted Erc20TokenStreamPermission object.
*/
function formatErc20TokenStreamPermission({
permission,
isAdjustmentAllowed,
}: {
permission: Erc20TokenStreamPermissionParameter;
isAdjustmentAllowed: boolean;
}): Erc20TokenStreamPermission {
const {
data: {
tokenAddress,
amountPerSecond,
initialAmount,
startTime,
maxAmount,
justification,
},
} = permission;
const optionalFields = {
...(isDefined(initialAmount) && {
initialAmount: toHexOrThrow(initialAmount),
}),
...(isDefined(maxAmount) && {
maxAmount: toHexOrThrow(maxAmount),
}),
...(isDefined(startTime) && {
startTime: Number(startTime),
}),
...(justification ? { justification } : {}),
};
return {
type: 'erc20-token-stream',
data: {
tokenAddress: toHexOrThrow(tokenAddress),
amountPerSecond: toHexOrThrow(amountPerSecond),
...optionalFields,
},
isAdjustmentAllowed,
};
}
/**
* Formats a native token periodic permission for submission to the wallet.
*
* @param params - The parameters for formatting the native token periodic permission.
* @param params.permission - The native token periodic permission parameter to format.
* @param params.isAdjustmentAllowed - Whether the permission is allowed to be adjusted.
* @returns The formatted NativeTokenPeriodicPermission object.
*/
function formatNativeTokenPeriodicPermission({
permission,
isAdjustmentAllowed,
}: {
permission: NativeTokenPeriodicPermissionParameter;
isAdjustmentAllowed: boolean;
}): NativeTokenPeriodicPermission {
const {
data: { periodAmount, periodDuration, startTime, justification },
} = permission;
const optionalFields = {
...(isDefined(startTime) && {
startTime: Number(startTime),
}),
...(justification ? { justification } : {}),
};
return {
type: 'native-token-periodic',
data: {
periodAmount: toHexOrThrow(periodAmount),
periodDuration: Number(periodDuration),
...optionalFields,
},
isAdjustmentAllowed,
};
}
/**
* Formats an ERC20 token periodic permission for submission to the wallet.
*
* @param params - The parameters for formatting the ERC20 token periodic permission.
* @param params.permission - The ERC20 token periodic permission parameter to format.
* @param params.isAdjustmentAllowed - Whether the permission is allowed to be adjusted.
* @returns The formatted Erc20TokenPeriodicPermission object.
*/
function formatErc20TokenPeriodicPermission({
permission,
isAdjustmentAllowed,
}: {
permission: Erc20TokenPeriodicPermissionParameter;
isAdjustmentAllowed: boolean;
}): Erc20TokenPeriodicPermission {
const {
data: {
tokenAddress,
periodAmount,
periodDuration,
startTime,
justification,
},
} = permission;
const optionalFields = {
...(isDefined(startTime) && {
startTime: Number(startTime),
}),
...(justification ? { justification } : {}),
};
return {
type: 'erc20-token-periodic',
data: {
tokenAddress: toHexOrThrow(tokenAddress),
periodAmount: toHexOrThrow(periodAmount),
periodDuration: Number(periodDuration),
...optionalFields,
},
isAdjustmentAllowed,
};
}