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

### Changed

- **BREAKING:** `DelegationFrameworkContracts` requires `TimestampEnforcer` ([#11185](https://github.com/MetaMask/core/pull/11185))
- Bump `@metamask/utils` from `^12.0.0` to `^12.0.1` ([#11107](https://github.com/MetaMask/core/pull/11107))

### Fixed

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

## [0.1.0]

### Added
Expand Down
16 changes: 8 additions & 8 deletions packages/money-account-mfa-whitelist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ 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)`) 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 |
| `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)` |

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
13 changes: 9 additions & 4 deletions packages/money-account-mfa-whitelist/src/delegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ function getStandingDelegationRequirement(
}

/**
* Checks a single-use transaction delegation: exactly `LimitedCalls(1)` and
* one exact-execution caveat, in any order, pinning a vault deposit.
* 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.
*
* Withdrawals, payments and Card approvals use the same shape with other
* calls, so they require MFA.
Expand All @@ -283,8 +285,11 @@ function getSingleUseDelegationRequirement(
caveats,
config.contracts.LimitedCallsEnforcer,
);
const exactExecution = caveats.find((caveat) => caveat !== limitedCalls);
if (caveats.length !== 2 || !limitedCalls || !exactExecution) {
const timestamp = findOnlyCaveat(caveats, config.contracts.TimestampEnforcer);
const [exactExecution, ...unexpected] = caveats.filter(
(caveat) => caveat !== limitedCalls && caveat !== timestamp,
);
if (!limitedCalls || !exactExecution || unexpected.length > 0) {
return requireMfa('Single-use delegation caveats are not whitelisted');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createExactExecutionTerms,
createLimitedCallsTerms,
createRedeemerTerms,
createTimestampTerms,
createValueLteTerms,
} from '@metamask/delegation-core';
import { generateEIP7702BatchTransaction } from '@metamask/transaction-controller';
Expand Down Expand Up @@ -1107,6 +1108,35 @@ describe('getMfaRequirement', () => {
},
);

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

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

it('whitelists a deposit into the premium vault', async () => {
expect(
await evaluateDelegation(
Expand Down Expand Up @@ -1403,6 +1433,34 @@ describe('getMfaRequirement', () => {
});
},
],
[
'there are two deadlines',
(caveats: UnsignedDelegation['caveats']): void => {
const deadline = {
enforcer: CONTRACTS.TimestampEnforcer,
terms: createTimestampTerms({
afterThreshold: 0,
beforeThreshold: 1_800_000_000,
}),
args: '0x' as Hex,
};
caveats.push(deadline, deadline);
},
],
[
'there is only LimitedCalls and a deadline',
(caveats: UnsignedDelegation['caveats']): void => {
caveats.pop();
caveats.push({
enforcer: CONTRACTS.TimestampEnforcer,
terms: createTimestampTerms({
afterThreshold: 0,
beforeThreshold: 1_800_000_000,
}),
args: '0x',
});
},
],
[
'there is no caveat but LimitedCalls',
(caveats: UnsignedDelegation['caveats']): void => {
Expand Down
1 change: 1 addition & 0 deletions packages/money-account-mfa-whitelist/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type DelegationFrameworkContracts = {
ExactExecutionEnforcer: Hex;
LimitedCallsEnforcer: Hex;
RedeemerEnforcer: Hex;
TimestampEnforcer: Hex;
ValueLteEnforcer: Hex;
};

Expand Down
29 changes: 25 additions & 4 deletions packages/money-account-mfa-whitelist/tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createExactExecutionTerms,
createLimitedCallsTerms,
createRedeemerTerms,
createTimestampTerms,
createValueLteTerms,
} from '@metamask/delegation-core';
import { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';
Expand Down Expand Up @@ -52,6 +53,7 @@ export const CONTRACTS: DelegationFrameworkContracts = {
ExactExecutionEnforcer: MONAD_CONTRACTS.ExactExecutionEnforcer,
LimitedCallsEnforcer: MONAD_CONTRACTS.LimitedCallsEnforcer,
RedeemerEnforcer: MONAD_CONTRACTS.RedeemerEnforcer,
TimestampEnforcer: MONAD_CONTRACTS.TimestampEnforcer,
ValueLteEnforcer: MONAD_CONTRACTS.ValueLteEnforcer,
};

Expand Down Expand Up @@ -271,11 +273,16 @@ export function wrapInExecute(calls: Execution[]): Execution {
* @param options - Options.
* @param options.mobileOrder - Put the exact-execution caveat first, as
* Mobile does, instead of `LimitedCalls` first, as the extension does.
* @param options.deadline - Adds a `Timestamp` caveat after `LimitedCalls`
* that expires at this Unix time, as the transaction controller does.
* @returns The unsigned delegation.
*/
export function buildSingleUseDelegation(
executions: Execution[],
{ mobileOrder = false }: { mobileOrder?: boolean } = {},
{
mobileOrder = false,
deadline,
}: { mobileOrder?: boolean; deadline?: number } = {},
): UnsignedDelegation {
const limitedCalls = {
enforcer: CONTRACTS.LimitedCallsEnforcer,
Expand All @@ -299,9 +306,23 @@ export function buildSingleUseDelegation(
delegate: ANY_BENEFICIARY,
delegator: MONEY_ACCOUNT,
authority: ROOT_AUTHORITY,
caveats: mobileOrder
? [exactExecution, limitedCalls]
: [limitedCalls, exactExecution],
caveats: [
...(mobileOrder ? [exactExecution] : []),
limitedCalls,
...(deadline === undefined
? []
: [
{
enforcer: CONTRACTS.TimestampEnforcer,
terms: createTimestampTerms({
afterThreshold: 0,
beforeThreshold: deadline,
}),
args: '0x' as Hex,
},
]),
...(mobileOrder ? [] : [exactExecution]),
],
salt: `0x${'ab'.repeat(32)}`,
};
}
Expand Down
Loading