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
8 changes: 8 additions & 0 deletions packages/money-account-mfa-whitelist/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Whitelist CHOMP's successor association message for `chompSignInDomain`, so migrating to the MPC Money Account no longer asks for MFA ([#11310](https://github.com/MetaMask/core/pull/11310))

### Changed

- Bump `@metamask/abi-utils` from `^2.0.4` to `^3.0.0` ([#11356](https://github.com/MetaMask/core/pull/11356))
- Bump `@metamask/utils` from `^12.0.1` to `^12.0.2` ([#11474](https://github.com/MetaMask/core/pull/11474))

### Fixed

- Whitelist single-use vault deposit delegations that carry a `Redeemer` caveat ([#11310](https://github.com/MetaMask/core/pull/11310))

## [0.2.0]

### Added
Expand Down
18 changes: 10 additions & 8 deletions packages/money-account-mfa-whitelist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const config = {
},
],
cardSignInDomain: 'link.metamask.io',
chompSignInDomain: 'chomp.api.cx.metamask.io',
};

const requirement = getMfaRequirement(
Expand All @@ -52,14 +53,15 @@ if (requirement.mfaRequired) {

### Whitelist rules

| Rule | Method | Payload |
| --------------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `chomp-authentication` | `signPersonalMessage` | `CHOMP Authentication <timestamp>`, within `maxMessageAge` of now |
| `rewards-binding` | `signPersonalMessage` | `metamask-rewards:money-account-binding:<subject>:<address>:<timestamp>` for the signing account, within `maxMessageAge` of now |
| `card-sign-in` | `signPersonalMessage` | A SIWE message for `cardSignInDomain` and the Money Account chain, signed by the account, that is not expired |
| `eip7702-authorization` | `signEip7702Authorization` | An authorization to `EIP7702StatelessDeleGatorImpl` on the Money Account chain |
| `vault-standing-delegation` | `signTypedData` (V4) | A root delegation to the CHOMP delegate with exactly a `ValueLte(0)`, an `ERC20TransferAmount` of mUSD or vault shares, and a `Redeemer` of that vault's adapter |
| `vault-deposit-delegation` | `signTypedData` (V4) | A single-use root delegation (`LimitedCalls(1)`, optionally with one `Timestamp` deadline) whose exact executions are `mUSD.approve(boringVault, amount)` followed by `teller.deposit(mUSD, amount, minimumMint, 0x0)` |
| Rule | Method | Payload |
| ----------------------------- | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `chomp-authentication` | `signPersonalMessage` | `CHOMP Authentication <timestamp>`, within `maxMessageAge` of now |
| `rewards-binding` | `signPersonalMessage` | `metamask-rewards:money-account-binding:<subject>:<address>:<timestamp>` for the signing account, within `maxMessageAge` of now |
| `card-sign-in` | `signPersonalMessage` | A SIWE message for `cardSignInDomain` and the Money Account chain, signed by the account, that is not expired |
| `chomp-successor-association` | `signPersonalMessage` | CHOMP's SIWE message for `chompSignInDomain` that links the signing account as successor of one predecessor, on the Money Account chain, that is not expired |
| `eip7702-authorization` | `signEip7702Authorization` | An authorization to `EIP7702StatelessDeleGatorImpl` on the Money Account chain |
| `vault-standing-delegation` | `signTypedData` (V4) | A root delegation to the CHOMP delegate with exactly a `ValueLte(0)`, an `ERC20TransferAmount` of mUSD or vault shares, and a `Redeemer` of that vault's adapter |
| `vault-deposit-delegation` | `signTypedData` (V4) | A single-use root delegation (`LimitedCalls(1)`, optionally with one `Timestamp` deadline and one `Redeemer` allowlist) whose exact executions are `mUSD.approve(boringVault, amount)` followed by `teller.deposit(mUSD, amount, minimumMint, 0x0)` |

Every delegation must be signed by the delegator, use the root authority, and be verified by the pinned `DelegationManager` on the Money Account chain.

Expand Down
9 changes: 6 additions & 3 deletions packages/money-account-mfa-whitelist/src/delegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ function getStandingDelegationRequirement(
/**
* Checks a single-use transaction delegation: exactly `LimitedCalls(1)`, one
* exact-execution caveat pinning a vault deposit and, optionally, one
* `Timestamp` deadline, in any order. A deadline only narrows when the
* delegation can be redeemed.
* `Timestamp` deadline and one `Redeemer` allowlist, in any order. A deadline
* and a redeemer allowlist only narrow when and by whom the delegation can be
* redeemed.
*
* Withdrawals, payments and Card approvals use the same shape with other
* calls, so they require MFA.
Expand All @@ -286,8 +287,10 @@ function getSingleUseDelegationRequirement(
config.contracts.LimitedCallsEnforcer,
);
const timestamp = findOnlyCaveat(caveats, config.contracts.TimestampEnforcer);
const redeemer = findOnlyCaveat(caveats, config.contracts.RedeemerEnforcer);
const [exactExecution, ...unexpected] = caveats.filter(
(caveat) => caveat !== limitedCalls && caveat !== timestamp,
(caveat) =>
caveat !== limitedCalls && caveat !== timestamp && caveat !== redeemer,
);
if (!limitedCalls || !exactExecution || unexpected.length > 0) {
return requireMfa('Single-use delegation caveats are not whitelisted');
Expand Down
168 changes: 166 additions & 2 deletions packages/money-account-mfa-whitelist/src/get-mfa-requirement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { describe, it, expect } from 'vitest';

import {
CARD_SIGN_IN_DOMAIN,
CHOMP_SIGN_IN_DOMAIN,
CHAIN_ID,
CONFIG,
CONTRACTS,
Expand Down Expand Up @@ -45,6 +46,7 @@ import type {
MfaSignerContext,
MfaRequirement,
MfaWhitelistConfig,
MfaWhitelistRule,
MoneyAccountSignatureRequest,
} from './types.js';

Expand Down Expand Up @@ -188,8 +190,39 @@ function buildCardSignInMessage(
return `${domain} wants you to sign in with your Ethereum account:\n${address}\n\nProve address ownership\n\nURI: ${uri}\nVersion: 1\nChain ID: ${chainId}\nNonce: 5f2c1e0a-nonce\nIssued At: ${issuedAt}\nExpiration Time: ${expirationTime}`;
}

const whitelistedAs = (rule: string): MfaRequirement =>
({ mfaRequired: false, rule }) as MfaRequirement;
const PREDECESSOR = '0xc1c732b47c6604f94ae69db0a34ced0b03f1fbe8';

function buildChompSuccessorMessage(
overrides: Partial<{
domain: string;
address: string;
successor: string;
predecessor: string;
resourcePredecessor: string;
uri: string;
chainId: string;
issuedAt: string;
expirationTime: string;
}> = {},
): string {
const {
domain = CHOMP_SIGN_IN_DOMAIN,
address = MONEY_ACCOUNT,
successor = MONEY_ACCOUNT.toLowerCase(),
predecessor = PREDECESSOR,
resourcePredecessor = predecessor,
uri = `https://${domain}/`,
chainId = '143',
issuedAt = new Date(NOW).toISOString(),
expirationTime = new Date(NOW + 5 * MINUTE).toISOString(),
} = overrides;
return `${domain} wants you to sign in with your Ethereum account:\n${address}\n\nAssociate ${successor} as the successor of ${predecessor} for CHOMP Money Account migration.\n\nURI: ${uri}\nVersion: 1\nChain ID: ${chainId}\nNonce: f60ff0d2a6c02b70ce5106097dabd163\nIssued At: ${issuedAt}\nExpiration Time: ${expirationTime}\nRequest ID: bcde79a3-12a8-419a-b404-579f7b5f69c3\nResources:\n- urn:chomp:profile:79678b20-061e-4246-a1de-5dc1a8074564\n- urn:chomp:purpose:ASSOCIATE_SUCCESSOR\n- urn:chomp:predecessor:${resourcePredecessor}`;
}

const whitelistedAs = (rule: MfaWhitelistRule): MfaRequirement => ({
mfaRequired: false,
rule,
});

const mfaRequired = (reason: string): MfaRequirement => ({
mfaRequired: true,
Expand Down Expand Up @@ -545,6 +578,96 @@ describe('getMfaRequirement', () => {
});
});

describe('CHOMP successor association', () => {
it('whitelists a fresh association of the Money Account as successor', () => {
expect(evaluateMessage(buildChompSuccessorMessage())).toStrictEqual(
whitelistedAs('chomp-successor-association'),
);
});

it('requires MFA when no CHOMP domain is configured', () => {
expect(
evaluateMessage(buildChompSuccessorMessage(), {
...CONFIG,
chompSignInDomain: undefined,
}),
).toStrictEqual(
mfaRequired('Sign-in message is not for the CHOMP domain'),
);
});

it('requires MFA for another domain', () => {
expect(
evaluateMessage(
buildChompSuccessorMessage({ domain: 'evil.example' }),
),
).toStrictEqual(
mfaRequired('Sign-in message is not for the CHOMP domain'),
);
});

it('requires MFA when the URI does not match the domain', () => {
expect(
evaluateMessage(
buildChompSuccessorMessage({ uri: 'https://evil.example/' }),
),
).toStrictEqual(
mfaRequired('Sign-in message URI does not match its domain'),
);
});

it.each([
['signer', { address: RECIPIENT }],
['successor', { successor: RECIPIENT }],
])('requires MFA when the %s is another address', (_name, overrides) => {
expect(
evaluateMessage(buildChompSuccessorMessage(overrides)),
).toStrictEqual(
mfaRequired('Sign-in message is not for the Money Account'),
);
});

it('requires MFA when the statement and resources name different predecessors', () => {
expect(
evaluateMessage(
buildChompSuccessorMessage({ resourcePredecessor: RECIPIENT }),
),
).toStrictEqual(mfaRequired('Sign-in message names two predecessors'));
});

it('requires MFA for another chain', () => {
expect(
evaluateMessage(buildChompSuccessorMessage({ chainId: '59144' })),
).toStrictEqual(
mfaRequired('Sign-in message is not for the Money Account chain'),
);
});

it('requires MFA when the message has expired', () => {
expect(
evaluateMessage(
buildChompSuccessorMessage({
issuedAt: new Date(NOW - 3 * MINUTE).toISOString(),
expirationTime: new Date(NOW - MINUTE).toISOString(),
}),
),
).toStrictEqual(mfaRequired('Sign-in message is expired or not fresh'));
});

it('requires MFA for another purpose', () => {
expect(
evaluateMessage(
buildChompSuccessorMessage().replace(
'urn:chomp:purpose:ASSOCIATE_SUCCESSOR',
'urn:chomp:purpose:ASSOCIATE',
),
),
).toStrictEqual(
mfaRequired('Message does not match a whitelisted format'),
);
});
});

describe('card provider sign-in', () => {
it('whitelists a fresh sign-in message for the Money Account', () => {
expect(evaluateMessage(buildCardSignInMessage())).toStrictEqual(
Expand Down Expand Up @@ -1125,6 +1248,36 @@ describe('getMfaRequirement', () => {
},
);

it.each([
['the extension', false],
['Mobile', true],
])(
'whitelists a deposit with a deadline and relay redeemers, with caveats ordered as %s orders them',
async (_client, mobileOrder) => {
expect(
await evaluateDelegation(
buildSingleUseDelegation(buildDepositCalls(), {
mobileOrder,
deadline: 1_800_000_000,
redeemers: [RECIPIENT, MUSD],
}),
),
).toStrictEqual(whitelistedAs('vault-deposit-delegation'));
},
);

it('requires MFA for a withdrawal with relay redeemers', async () => {
expect(
await evaluateDelegation(
buildSingleUseDelegation(buildWithdrawCalls(), {
redeemers: [RECIPIENT],
}),
),
).toStrictEqual(
mfaRequired('Delegation executions are not a vault deposit'),
);
});

it('requires MFA for a withdrawal with a deadline', async () => {
expect(
await evaluateDelegation(
Expand Down Expand Up @@ -1447,6 +1600,17 @@ describe('getMfaRequirement', () => {
caveats.push(deadline, deadline);
},
],
[
'there are two redeemer allowlists',
(caveats: UnsignedDelegation['caveats']): void => {
const redeemer = {
enforcer: CONTRACTS.RedeemerEnforcer,
terms: createRedeemerTerms({ redeemers: [RECIPIENT] }),
args: '0x' as Hex,
};
caveats.push(redeemer, redeemer);
},
],
[
'there is only LimitedCalls and a deadline',
(caveats: UnsignedDelegation['caveats']): void => {
Expand Down
Loading
Loading