Skip to content

Commit

Permalink
Update snap and keyring api dependencies and fix tests (#4090)
Browse files Browse the repository at this point in the history
This pull request updates the dependencies in the project, including
bumping snap dependencies, keyring-api, and eth-snap-keyring. It also
fixes the failing tests.

Closes: MetaMask/accounts-planning#307

---------

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
  • Loading branch information
montelaidev and ccharly authored Apr 10, 2024
1 parent 2dbec7b commit e86b84e
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 294 deletions.
14 changes: 7 additions & 7 deletions packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"dependencies": {
"@ethereumjs/util": "^8.1.0",
"@metamask/base-controller": "^5.0.1",
"@metamask/eth-snap-keyring": "^2.1.1",
"@metamask/keyring-api": "^4.0.0",
"@metamask/snaps-sdk": "^1.3.2",
"@metamask/snaps-utils": "^5.1.2",
"@metamask/eth-snap-keyring": "^3.0.0",
"@metamask/keyring-api": "^5.1.0",
"@metamask/snaps-sdk": "^3.1.1",
"@metamask/snaps-utils": "^7.0.3",
"@metamask/utils": "^8.3.0",
"deepmerge": "^4.2.2",
"ethereum-cryptography": "^2.1.2",
Expand All @@ -56,7 +56,7 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@metamask/keyring-controller": "^14.0.1",
"@metamask/snaps-controllers": "^4.0.0",
"@metamask/snaps-controllers": "^6.0.3",
"@types/jest": "^27.4.1",
"@types/readable-stream": "^2.3.0",
"jest": "^27.5.1",
Expand All @@ -66,8 +66,8 @@
"typescript": "~4.9.5"
},
"peerDependencies": {
"@metamask/keyring-controller": "^14.0.0",
"@metamask/snaps-controllers": "^4.0.0"
"@metamask/keyring-controller": "^14.0.1",
"@metamask/snaps-controllers": "^6.0.3"
},
"engines": {
"node": ">=16.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@metamask/ethjs-provider-http": "^0.3.0",
"@metamask/keyring-api": "^4.0.0",
"@metamask/keyring-api": "^5.1.0",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.191",
"@types/node": "^16.18.54",
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@metamask/eth-hd-keyring": "^7.0.1",
"@metamask/eth-sig-util": "^7.0.1",
"@metamask/eth-simple-keyring": "^6.0.1",
"@metamask/keyring-api": "^4.0.0",
"@metamask/keyring-api": "^5.1.0",
"@metamask/message-manager": "^8.0.1",
"@metamask/utils": "^8.3.0",
"async-mutex": "^0.2.6",
Expand Down
55 changes: 50 additions & 5 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,10 @@ describe('KeyringController', () => {
});

describe('prepareUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};
describe('when the keyring for the given address supports prepareUserOperation', () => {
it('should prepare base user operation', async () => {
const address = '0x660265edc169bab511a40c0e049cc1e33774443d';
Expand Down Expand Up @@ -1674,20 +1678,23 @@ describe('KeyringController', () => {
data: '0x7064',
},
];

jest
.spyOn(mockKeyring, 'prepareUserOperation')
.mockResolvedValueOnce(baseUserOp);

const result = await controller.prepareUserOperation(
address,
baseTxs,
executionContext,
);

expect(result).toStrictEqual(baseUserOp);
expect(mockKeyring.prepareUserOperation).toHaveBeenCalledTimes(1);
expect(mockKeyring.prepareUserOperation).toHaveBeenCalledWith(
address,
baseTxs,
executionContext,
);
},
);
Expand All @@ -1704,7 +1711,7 @@ describe('KeyringController', () => {
await controller.addNewKeyring(MockKeyring.type);

await expect(
controller.prepareUserOperation(address, []),
controller.prepareUserOperation(address, [], executionContext),
).rejects.toThrow(
KeyringControllerError.UnsupportedPrepareUserOperation,
);
Expand All @@ -1715,6 +1722,11 @@ describe('KeyringController', () => {
});

describe('patchUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};

describe('when the keyring for the given address supports patchUserOperation', () => {
it('should patch an user operation', async () => {
const address = '0x660265edc169bab511a40c0e049cc1e33774443d';
Expand Down Expand Up @@ -1745,13 +1757,18 @@ describe('KeyringController', () => {
.spyOn(mockKeyring, 'patchUserOperation')
.mockResolvedValueOnce(patch);

const result = await controller.patchUserOperation(address, userOp);
const result = await controller.patchUserOperation(
address,
userOp,
executionContext,
);

expect(result).toStrictEqual(patch);
expect(mockKeyring.patchUserOperation).toHaveBeenCalledTimes(1);
expect(mockKeyring.patchUserOperation).toHaveBeenCalledWith(
address,
userOp,
executionContext,
);
},
);
Expand Down Expand Up @@ -1781,7 +1798,7 @@ describe('KeyringController', () => {
};

await expect(
controller.patchUserOperation(address, userOp),
controller.patchUserOperation(address, userOp, executionContext),
).rejects.toThrow(
KeyringControllerError.UnsupportedPatchUserOperation,
);
Expand All @@ -1792,6 +1809,10 @@ describe('KeyringController', () => {
});

describe('signUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};
describe('when the keyring for the given address supports signUserOperation', () => {
it('should sign an user operation', async () => {
const address = '0x660265edc169bab511a40c0e049cc1e33774443d';
Expand Down Expand Up @@ -1820,13 +1841,18 @@ describe('KeyringController', () => {
.spyOn(mockKeyring, 'signUserOperation')
.mockResolvedValueOnce(signature);

const result = await controller.signUserOperation(address, userOp);
const result = await controller.signUserOperation(
address,
userOp,
executionContext,
);

expect(result).toStrictEqual(signature);
expect(mockKeyring.signUserOperation).toHaveBeenCalledTimes(1);
expect(mockKeyring.signUserOperation).toHaveBeenCalledWith(
address,
userOp,
executionContext,
);
},
);
Expand Down Expand Up @@ -1856,7 +1882,7 @@ describe('KeyringController', () => {
};

await expect(
controller.signUserOperation(address, userOp),
controller.signUserOperation(address, userOp, executionContext),
).rejects.toThrow(
KeyringControllerError.UnsupportedSignUserOperation,
);
Expand Down Expand Up @@ -2902,6 +2928,11 @@ describe('KeyringController', () => {
});

describe('prepareUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};

it('should return a base UserOp', async () => {
await withController(
async ({ controller, messenger, initialState }) => {
Expand All @@ -2917,18 +2948,24 @@ describe('KeyringController', () => {
'KeyringController:prepareUserOperation',
initialState.keyrings[0].accounts[0],
baseTxs,
executionContext,
);

expect(controller.prepareUserOperation).toHaveBeenCalledWith(
initialState.keyrings[0].accounts[0],
baseTxs,
executionContext,
);
},
);
});
});

describe('patchUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};
it('should return an UserOp patch', async () => {
await withController(
async ({ controller, messenger, initialState }) => {
Expand All @@ -2950,18 +2987,24 @@ describe('KeyringController', () => {
'KeyringController:patchUserOperation',
initialState.keyrings[0].accounts[0],
userOp,
executionContext,
);

expect(controller.patchUserOperation).toHaveBeenCalledWith(
initialState.keyrings[0].accounts[0],
userOp,
executionContext,
);
},
);
});
});

describe('signUserOperation', () => {
const chainId = '0x1';
const executionContext = {
chainId,
};
it('should return an UserOp signature', async () => {
await withController(
async ({ controller, messenger, initialState }) => {
Expand All @@ -2983,11 +3026,13 @@ describe('KeyringController', () => {
'KeyringController:signUserOperation',
initialState.keyrings[0].accounts[0],
userOp,
executionContext,
);

expect(controller.signUserOperation).toHaveBeenCalledWith(
initialState.keyrings[0].accounts[0],
userOp,
executionContext,
);
},
);
Expand Down
17 changes: 14 additions & 3 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
EthKeyring,
EthUserOperation,
EthUserOperationPatch,
KeyringExecutionContext,
} from '@metamask/keyring-api';
import type {
PersonalMessageParams,
Expand Down Expand Up @@ -1235,11 +1236,13 @@ export class KeyringController extends BaseController<
*
* @param from - Address of the sender.
* @param transactions - Base transactions to include in the UserOperation.
* @param executionContext - The execution context to use for the UserOperation.
* @returns A pseudo-UserOperation that can be used to construct a real.
*/
async prepareUserOperation(
from: string,
transactions: EthBaseTransaction[],
executionContext: KeyringExecutionContext,
): Promise<EthBaseUserOperation> {
const address = normalize(from) as Hex;
const keyring = (await this.getKeyringForAccount(
Expand All @@ -1250,7 +1253,11 @@ export class KeyringController extends BaseController<
throw new Error(KeyringControllerError.UnsupportedPrepareUserOperation);
}

return await keyring.prepareUserOperation(address, transactions);
return await keyring.prepareUserOperation(
address,
transactions,
executionContext,
);
}

/**
Expand All @@ -1259,11 +1266,13 @@ export class KeyringController extends BaseController<
*
* @param from - Address of the sender.
* @param userOp - UserOperation to patch.
* @param executionContext - The execution context to use for the UserOperation.
* @returns A patch to apply to the UserOperation.
*/
async patchUserOperation(
from: string,
userOp: EthUserOperation,
executionContext: KeyringExecutionContext,
): Promise<EthUserOperationPatch> {
const address = normalize(from) as Hex;
const keyring = (await this.getKeyringForAccount(
Expand All @@ -1274,19 +1283,21 @@ export class KeyringController extends BaseController<
throw new Error(KeyringControllerError.UnsupportedPatchUserOperation);
}

return await keyring.patchUserOperation(address, userOp);
return await keyring.patchUserOperation(address, userOp, executionContext);
}

/**
* Signs an UserOperation.
*
* @param from - Address of the sender.
* @param userOp - UserOperation to sign.
* @param executionContext - The execution context to use for the UserOperation.
* @returns The signature of the UserOperation.
*/
async signUserOperation(
from: string,
userOp: EthUserOperation,
executionContext: KeyringExecutionContext,
): Promise<string> {
const address = normalize(from) as Hex;
const keyring = (await this.getKeyringForAccount(
Expand All @@ -1297,7 +1308,7 @@ export class KeyringController extends BaseController<
throw new Error(KeyringControllerError.UnsupportedSignUserOperation);
}

return await keyring.signUserOperation(address, userOp);
return await keyring.signUserOperation(address, userOp, executionContext);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ describe('UserOperationController', () => {
maxFeePerGas: '0x6',
maxPriorityFeePerGas: '0x7',
}),
chainId: CHAIN_ID_MOCK,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,13 @@ export class UserOperationController extends BaseController<
metadata: UserOperationMetadata,
smartContractAccount: SmartContractAccount,
) {
const { id, userOperation } = metadata;
const { id, userOperation, chainId } = metadata;

log('Requesting paymaster data', { id });

const response = await smartContractAccount.updateUserOperation({
userOperation,
chainId,
});

validateUpdateUserOperationResponse(response);
Expand Down
Loading

0 comments on commit e86b84e

Please sign in to comment.