Skip to content
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ linkStyle default opacity:0.5
money_account_upgrade_controller --> chomp_api_service;
money_account_upgrade_controller --> keyring_controller;
money_account_upgrade_controller --> messenger;
money_account_upgrade_controller --> network_controller;
multichain_account_service --> accounts_controller;
multichain_account_service --> base_controller;
multichain_account_service --> keyring_controller;
Expand Down
4 changes: 4 additions & 0 deletions packages/money-account-upgrade-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add EIP-7702 authorization step to the upgrade sequence.

## [1.0.0]

### Added
Expand Down
5 changes: 3 additions & 2 deletions packages/money-account-upgrade-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@
"@metamask/base-controller": "^9.1.0",
"@metamask/chomp-api-service": "^1.0.0",
"@metamask/keyring-controller": "^25.2.0",
"@metamask/messenger": "^1.1.1"
"@metamask/messenger": "^1.1.1",
"@metamask/network-controller": "^30.0.1",
"@metamask/utils": "^11.9.0"
},
"devDependencies": {
"@metamask/auto-changelog": "^6.1.0",
"@metamask/utils": "^11.9.0",
"@ts-bridge/cli": "^0.6.4",
"@types/jest": "^29.5.14",
"deepmerge": "^4.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type Mocks = {
getServiceDetails: jest.Mock;
signPersonalMessage: jest.Mock;
associateAddress: jest.Mock;
getUpgrade: jest.Mock;
createUpgrade: jest.Mock;
signEip7702Authorization: jest.Mock;
findNetworkClientIdByChainId: jest.Mock;
getNetworkClientById: jest.Mock;
};

function setup(): {
Expand All @@ -71,6 +76,9 @@ function setup(): {
messenger: MoneyAccountUpgradeControllerMessenger;
mocks: Mocks;
} {
// 65-byte signature — r (32 bytes) + s (32 bytes) + v = 0x1c (28).
const signature = `0x${'1'.repeat(64)}${'2'.repeat(64)}1c`;

const mocks: Mocks = {
getServiceDetails: jest
.fn()
Expand All @@ -81,6 +89,21 @@ function setup(): {
address: MOCK_ACCOUNT_ADDRESS,
status: 'CREATED',
}),
getUpgrade: jest.fn().mockResolvedValue(null),
createUpgrade: jest.fn().mockResolvedValue({
signerAddress: MOCK_ACCOUNT_ADDRESS,
status: 'pending',
createdAt: '2026-04-21T12:00:00.000Z',
}),
signEip7702Authorization: jest.fn().mockResolvedValue(signature),
findNetworkClientIdByChainId: jest
.fn()
.mockReturnValue('network-client-id'),
getNetworkClientById: jest.fn().mockReturnValue({
provider: {
request: jest.fn().mockResolvedValue('0x0'),
},
}),
};

const rootMessenger = new Messenger<MockAnyNamespace, AllActions, AllEvents>({
Expand All @@ -99,6 +122,26 @@ function setup(): {
'ChompApiService:associateAddress',
mocks.associateAddress,
);
rootMessenger.registerActionHandler(
'ChompApiService:getUpgrade',
mocks.getUpgrade,
);
rootMessenger.registerActionHandler(
'ChompApiService:createUpgrade',
mocks.createUpgrade,
);
rootMessenger.registerActionHandler(
'KeyringController:signEip7702Authorization',
mocks.signEip7702Authorization,
);
rootMessenger.registerActionHandler(
'NetworkController:findNetworkClientIdByChainId',
mocks.findNetworkClientIdByChainId,
);
rootMessenger.registerActionHandler(
'NetworkController:getNetworkClientById',
mocks.getNetworkClientById,
);

const messenger: MoneyAccountUpgradeControllerMessenger = new Messenger({
namespace: 'MoneyAccountUpgradeController',
Expand All @@ -110,6 +153,11 @@ function setup(): {
'ChompApiService:getServiceDetails',
'KeyringController:signPersonalMessage',
'ChompApiService:associateAddress',
'ChompApiService:getUpgrade',
'ChompApiService:createUpgrade',
'KeyringController:signEip7702Authorization',
'NetworkController:findNetworkClientIdByChainId',
'NetworkController:getNetworkClientById',
],
events: [],
messenger,
Expand Down Expand Up @@ -242,6 +290,15 @@ describe('MoneyAccountUpgradeController', () => {
expect(mocks.associateAddress).toHaveBeenCalledWith(
expect.objectContaining({ address: MOCK_ACCOUNT_ADDRESS }),
);
expect(mocks.signEip7702Authorization).toHaveBeenCalledWith(
expect.objectContaining({
from: MOCK_ACCOUNT_ADDRESS,
contractAddress: MOCK_CONFIG.delegatorImplAddress,
}),
);
expect(mocks.createUpgrade).toHaveBeenCalledWith(
expect.objectContaining({ address: MOCK_ACCOUNT_ADDRESS }),
);
});

it('is callable via the messenger', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ import type {
import { BaseController } from '@metamask/base-controller';
import type {
ChompApiServiceAssociateAddressAction,
ChompApiServiceCreateUpgradeAction,
ChompApiServiceGetServiceDetailsAction,
ChompApiServiceGetUpgradeAction,
} from '@metamask/chomp-api-service';
import type { KeyringControllerSignPersonalMessageAction } from '@metamask/keyring-controller';
import type {
KeyringControllerSignEip7702AuthorizationAction,
KeyringControllerSignPersonalMessageAction,
} from '@metamask/keyring-controller';
import type { Messenger } from '@metamask/messenger';
import type {
NetworkControllerFindNetworkClientIdByChainIdAction,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { Hex } from '@metamask/utils';

import { associateAddressStep } from './associate-address';
import type { MoneyAccountUpgradeControllerMethodActions } from './MoneyAccountUpgradeController-method-action-types';
import type { Step } from './step';
import { associateAddressStep } from './steps/associate-address';
import { eip7702AuthorizationStep } from './steps/eip-7702-authorization';
import type { Step } from './steps/step';
import type { InitConfig } from './types';

export const controllerName = 'MoneyAccountUpgradeController';
Expand All @@ -38,8 +48,13 @@ export type MoneyAccountUpgradeControllerActions =

type AllowedActions =
| ChompApiServiceAssociateAddressAction
| ChompApiServiceCreateUpgradeAction
| ChompApiServiceGetServiceDetailsAction
| KeyringControllerSignPersonalMessageAction;
| ChompApiServiceGetUpgradeAction
| KeyringControllerSignEip7702AuthorizationAction
| KeyringControllerSignPersonalMessageAction
| NetworkControllerFindNetworkClientIdByChainIdAction
| NetworkControllerGetNetworkClientByIdAction;

export type MoneyAccountUpgradeControllerStateChangedEvent =
ControllerStateChangedEvent<
Expand All @@ -66,9 +81,9 @@ export class MoneyAccountUpgradeController extends BaseController<
MoneyAccountUpgradeControllerState,
MoneyAccountUpgradeControllerMessenger
> {
#initialized: boolean;
#config?: { chainId: Hex; delegatorImplAddress: Hex };

readonly #steps: Step[] = [associateAddressStep];
readonly #steps: Step[] = [associateAddressStep, eip7702AuthorizationStep];

/**
* Constructor for the MoneyAccountUpgradeController.
Expand All @@ -88,8 +103,6 @@ export class MoneyAccountUpgradeController extends BaseController<
state: {},
});

this.#initialized = false;

this.messenger.registerMethodActionHandlers(
this,
MESSENGER_EXPOSED_METHODS,
Expand All @@ -101,9 +114,9 @@ export class MoneyAccountUpgradeController extends BaseController<
* given chain.
*
* @param chainId - The chain to initialize for.
* @param _initConfig - Contract addresses not available from the service details API.
* @param initConfig - Contract addresses not available from the service details API.
*/
async init(chainId: Hex, _initConfig: InitConfig): Promise<void> {
async init(chainId: Hex, initConfig: InitConfig): Promise<void> {
const response = await this.messenger.call(
'ChompApiService:getServiceDetails',
[chainId],
Expand All @@ -127,7 +140,10 @@ export class MoneyAccountUpgradeController extends BaseController<
);
}

this.#initialized = true;
this.#config = {
chainId,
delegatorImplAddress: initConfig.delegatorImplAddress,
};
}

/**
Expand All @@ -139,14 +155,18 @@ export class MoneyAccountUpgradeController extends BaseController<
* @param address - The Money Account address to upgrade.
*/
async upgradeAccount(address: Hex): Promise<void> {
if (!this.#initialized) {
if (!this.#config) {
throw new Error(
'MoneyAccountUpgradeController must be initialized via init() before upgradeAccount() can be called',
);
}

for (const step of this.#steps) {
await step.run({ messenger: this.messenger, address });
await step.run({
messenger: this.messenger,
address,
...this.#config,
});
}
}
}
2 changes: 1 addition & 1 deletion packages/money-account-upgrade-controller/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Step, StepResult } from './step';
export type { Step, StepResult } from './steps/step';
export type { InitConfig, UpgradeConfig } from './types';
export { MoneyAccountUpgradeController } from './MoneyAccountUpgradeController';
export type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import type {
} from '@metamask/messenger';
import type { Hex } from '@metamask/utils';

import type { MoneyAccountUpgradeControllerMessenger } from '../MoneyAccountUpgradeController';
import { associateAddressStep } from './associate-address';
import type { MoneyAccountUpgradeControllerMessenger } from './MoneyAccountUpgradeController';

const MOCK_ADDRESS = '0xabcdef1234567890abcdef1234567890abcdef12' as Hex;
const MOCK_CHAIN_ID = '0x1' as Hex;
const MOCK_DELEGATOR_IMPL = '0x2222222222222222222222222222222222222222' as Hex;
const MOCK_SIGNATURE = '0xdeadbeefcafebabe';
const MOCK_NOW = new Date('2026-04-17T12:00:00.000Z').getTime();

Expand Down Expand Up @@ -79,7 +81,12 @@ describe('associateAddressStep', () => {
it('signs the CHOMP Authentication message with the given address', async () => {
const { messenger, mocks } = setup();

await associateAddressStep.run({ messenger, address: MOCK_ADDRESS });
await associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
});

expect(mocks.signPersonalMessage).toHaveBeenCalledWith({
data: `CHOMP Authentication ${MOCK_NOW}`,
Expand All @@ -90,7 +97,12 @@ describe('associateAddressStep', () => {
it('submits the signature, timestamp, and address to the CHOMP API', async () => {
const { messenger, mocks } = setup();

await associateAddressStep.run({ messenger, address: MOCK_ADDRESS });
await associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
});

expect(mocks.associateAddress).toHaveBeenCalledWith({
signature: MOCK_SIGNATURE,
Expand All @@ -105,6 +117,8 @@ describe('associateAddressStep', () => {
const result = await associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
});

expect(result).toBe('completed');
Expand All @@ -121,6 +135,8 @@ describe('associateAddressStep', () => {
const result = await associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
});

expect(result).toBe('already-done');
Expand All @@ -131,7 +147,12 @@ describe('associateAddressStep', () => {
mocks.signPersonalMessage.mockRejectedValue(new Error('signing failed'));

await expect(
associateAddressStep.run({ messenger, address: MOCK_ADDRESS }),
associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
}),
).rejects.toThrow('signing failed');
expect(mocks.associateAddress).not.toHaveBeenCalled();
});
Expand All @@ -141,7 +162,12 @@ describe('associateAddressStep', () => {
mocks.associateAddress.mockRejectedValue(new Error('api failed'));

await expect(
associateAddressStep.run({ messenger, address: MOCK_ADDRESS }),
associateAddressStep.run({
messenger,
address: MOCK_ADDRESS,
chainId: MOCK_CHAIN_ID,
delegatorImplAddress: MOCK_DELEGATOR_IMPL,
}),
).rejects.toThrow('api failed');
});
});
Loading
Loading