Skip to content

Commit 951a367

Browse files
authored
Merge pull request #169 from MetaMask/feat/sign-typed-data-metadata
Provide metadata when signing delegation
2 parents 8488537 + af905fe commit 951a367

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

packages/gator-permissions-snap/src/core/accountController.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class AccountController {
6363
): Promise<Delegation> {
6464
logger.debug('AccountController:signDelegation()');
6565

66-
const { chainId, delegation, address } = options;
66+
const { chainId, delegation, address, origin, justification } = options;
6767

6868
const selectedChain = await this.#ethereumProvider.request<Hex>({
6969
method: 'eth_chainId',
@@ -96,6 +96,8 @@ export class AccountController {
9696
chainId,
9797
delegationManager,
9898
delegation,
99+
origin,
100+
justification,
99101
});
100102

101103
const signature = await this.#ethereumProvider.request<Hex>({
@@ -117,10 +119,14 @@ export class AccountController {
117119
chainId,
118120
delegationManager,
119121
delegation,
122+
origin,
123+
justification,
120124
}: {
121125
chainId: number;
122126
delegationManager: Hex;
123127
delegation: Omit<Delegation, 'signature'>;
128+
origin: string;
129+
justification: string;
124130
}) {
125131
logger.debug('AccountController:#getSignDelegationArgs()');
126132

@@ -151,10 +157,15 @@ export class AccountController {
151157
],
152158
};
153159

160+
const metadata = {
161+
origin,
162+
justification,
163+
};
164+
154165
const primaryType = 'Delegation';
155166

156167
const message = { ...delegation, salt: bigIntToHex(delegation.salt) };
157168

158-
return { domain, types, primaryType, message };
169+
return { domain, types, primaryType, message, metadata };
159170
}
160171
}

packages/gator-permissions-snap/src/core/permissionRequestLifecycleOrchestrator.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class PermissionRequestLifecycleOrchestrator {
252252
lifecycleHandlers,
253253
isAdjustmentAllowed,
254254
chainId,
255+
origin,
255256
});
256257

257258
return {
@@ -286,9 +287,10 @@ export class PermissionRequestLifecycleOrchestrator {
286287
* @param params - Parameters for resolving the response.
287288
* @param params.originalRequest - The original unmodified permission request.
288289
* @param params.modifiedContext - The possibly modified context after user interaction.
289-
* @param params.lifecycleHandlers - Handlers for the permission lifecycle.
290290
* @param params.isAdjustmentAllowed - Whether the permission can be adjusted.
291291
* @param params.chainId - The chain ID for the permission.
292+
* @param params.origin - The origin of the permission request.
293+
* @param params.lifecycleHandlers - Handlers for the permission lifecycle.
292294
* @returns The resolved permission response.
293295
*/
294296
async #resolveResponse<
@@ -300,14 +302,16 @@ export class PermissionRequestLifecycleOrchestrator {
300302
>({
301303
originalRequest,
302304
modifiedContext,
303-
lifecycleHandlers,
304305
isAdjustmentAllowed,
305306
chainId,
307+
origin,
308+
lifecycleHandlers,
306309
}: {
307310
originalRequest: TRequest;
308311
modifiedContext: TContext;
309312
isAdjustmentAllowed: boolean;
310313
chainId: number;
314+
origin: string;
311315
lifecycleHandlers: LifecycleOrchestrationHandlers<
312316
TRequest,
313317
TContext,
@@ -397,11 +401,15 @@ export class PermissionRequestLifecycleOrchestrator {
397401
salt: BigInt(salt),
398402
} as const;
399403

404+
const { justification } = modifiedContext;
405+
400406
const signedDelegation: Delegation =
401407
await this.#accountController.signDelegation({
402408
chainId,
403409
delegation,
404410
address,
411+
origin,
412+
justification,
405413
});
406414

407415
const context = encodeDelegations([signedDelegation], { out: 'hex' });

packages/gator-permissions-snap/src/core/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ export type SignDelegationOptions = {
336336
chainId: number;
337337
delegation: Omit<Delegation, 'signature'>;
338338
address: Hex;
339+
origin: string;
340+
justification: string;
339341
};
340342

341343
/**

packages/gator-permissions-snap/test/core/accountController.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe('AccountController', () => {
1212
const mockSignature =
1313
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef11';
1414
const expectedBalance = '0x1000000000000000000';
15+
const origin = 'https://example.com';
16+
const justification = 'Test justification';
1517

1618
let accountController: AccountController;
1719
let mockSnapsProvider: ReturnType<typeof createMockSnapsProvider>;
@@ -103,6 +105,8 @@ describe('AccountController', () => {
103105
chainId: sepolia,
104106
delegation: unsignedDelegation,
105107
address: mockAddress,
108+
origin,
109+
justification,
106110
});
107111

108112
expect(signedDelegation).toStrictEqual({
@@ -123,6 +127,8 @@ describe('AccountController', () => {
123127
chainId: sepolia,
124128
delegation: unsignedDelegation,
125129
address: mockAddress,
130+
origin,
131+
justification,
126132
});
127133

128134
expect(signature).toStrictEqual({
@@ -145,6 +151,8 @@ describe('AccountController', () => {
145151
chainId: sepolia,
146152
delegation: unsignedDelegation,
147153
address: mockAddress,
154+
origin,
155+
justification,
148156
}),
149157
).rejects.toThrow('Failed to sign delegation');
150158
});
@@ -154,6 +162,8 @@ describe('AccountController', () => {
154162
chainId: sepolia,
155163
delegation: unsignedDelegation,
156164
address: mockAddress,
165+
origin,
166+
justification,
157167
});
158168

159169
expect(mockEthereumProvider.request).toHaveBeenCalledWith({
@@ -218,6 +228,10 @@ describe('AccountController', () => {
218228
...unsignedDelegation,
219229
salt: '0x1',
220230
},
231+
metadata: {
232+
origin,
233+
justification,
234+
},
221235
},
222236
],
223237
});

0 commit comments

Comments
 (0)