Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,53 @@ describe('requestSpendPermission', () => {
});
});

it('should return the wallet-substituted account in permission when mutableData substitution occurs', async () => {
const capabilities: WalletSignCapabilities = {
spendPermission: {
requireBalance: true,
},
};

// The request declares mutableData: { fields: ['message.account'] }, so the
// wallet is allowed to substitute account with a different (smart-wallet)
// address than the one originally requested.
const substitutedAccount = '0x00000000000000000000000000000000000000ab' as `0x${string}`;
const substitutedTypedData: SpendPermissionTypedData = {
...mockTypedData,
message: {
...mockTypedData.message,
account: substitutedAccount,
},
};

const mockWalletSignResponse = {
signature: mockSignature,
signedData: substitutedTypedData,
};

// createSpendPermissionTypedData returns the pre-substitution request data.
(createSpendPermissionTypedData as Mock).mockReturnValue(mockTypedData);
// The wallet returns the post-substitution signed data.
(mockProviderRequest as Mock).mockResolvedValue(mockWalletSignResponse);
(getHash as Mock).mockResolvedValue(mockPermissionHash);

const result = await requestSpendPermission({
...mockRequestData,
capabilities,
});

// permissionHash is derived from the post-substitution message, ...
expect(getHash).toHaveBeenCalledWith({
permission: substitutedTypedData.message,
chainId: mockRequestData.chainId,
});
// ... so the returned permission must describe the same (substituted)
// account rather than the original pre-substitution request, otherwise
// permission and permissionHash describe different permissions.
expect(result.permission).toEqual(substitutedTypedData.message);
expect(result.permission.account).toBe(substitutedAccount);
});

it('should handle invalid wallet_sign response', async () => {
const capabilities: WalletSignCapabilities = {
spendPermission: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ const requestSpendPermissionFn = async (
// Check if we should use wallet_sign (when capabilities are provided) or eth_signTypedData_v4
let signature: string;
let permissionHash: string;
// The permission message actually returned to the caller. It must match the
// data that permissionHash is derived from: when the wallet substitutes
// message.account via mutableData, this is the wallet-returned (post-
// substitution) message, not the original request.
let permissionMessage: typeof typedData.message;

if (capabilities) {
// Use wallet_sign with capabilities
Expand Down Expand Up @@ -126,6 +131,7 @@ const requestSpendPermissionFn = async (
permission: signResult.signedData.message,
chainId,
});
permissionMessage = signResult.signedData.message;
} else {
// Use the original eth_signTypedData_v4 method
[signature, permissionHash] = await Promise.all([
Expand All @@ -135,14 +141,15 @@ const requestSpendPermissionFn = async (
}) as Promise<string>,
getHash({ permission: typedData.message, chainId }),
]);
permissionMessage = typedData.message;
}

const permission: SpendPermission = {
createdAt: dateToTimestampInSeconds(new Date()),
permissionHash,
signature,
chainId,
permission: typedData.message,
permission: permissionMessage,
};

return permission;
Expand Down