@metamask/money-account-mfa-whitelist 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -1
- package/README.md +11 -9
- package/dist/constants.d.ts +13 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +13 -0
- package/dist/constants.js.map +1 -1
- package/dist/delegation.d.ts +2 -2
- package/dist/delegation.d.ts.map +1 -1
- package/dist/delegation.js +13 -10
- package/dist/delegation.js.map +1 -1
- package/dist/executions.d.ts +16 -5
- package/dist/executions.d.ts.map +1 -1
- package/dist/executions.js +54 -12
- package/dist/executions.js.map +1 -1
- package/dist/personal-message.d.ts +10 -3
- package/dist/personal-message.d.ts.map +1 -1
- package/dist/personal-message.js +52 -3
- package/dist/personal-message.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Whitelist MetaMask Rewards mUSD claims from the Money Account ([#11731](https://github.com/MetaMask/core/pull/11731))
|
|
15
|
+
- Add the `rewards-claim-delegation` rule: a single-use delegation whose exact executions are `mUSD.receiveWithAuthorization` to the Money Account, followed by a vault deposit of the same amount
|
|
16
|
+
- Add the `rewards-claim-proof` rule: the Rewards claim proof message when its earning address and payout address are the Money Account and it expires within `maxMessageAge` plus one minute
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- Bump `@metamask/utils` from `^12.0.2` to `^12.1.0` ([#11796](https://github.com/MetaMask/core/pull/11796))
|
|
21
|
+
|
|
10
22
|
## [0.3.0]
|
|
11
23
|
|
|
12
24
|
### Added
|
|
@@ -46,7 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
46
58
|
- A whitelisted request must be for the signing account and hash to exactly the given hash (EIP-191, EIP-712 or EIP-7702), so the signer can sign the hash after inspecting the request
|
|
47
59
|
- Whitelist the CHOMP authentication, Rewards binding and Card sign-in messages, the EIP-7702 authorization to `EIP7702StatelessDeleGatorImpl`, standing vault delegations to the CHOMP delegate, and single-use vault deposit delegations
|
|
48
60
|
|
|
49
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/money-account-mfa-whitelist@0.
|
|
61
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/money-account-mfa-whitelist@0.4.0...HEAD
|
|
62
|
+
[0.4.0]: https://github.com/MetaMask/core/compare/@metamask/money-account-mfa-whitelist@0.3.0...@metamask/money-account-mfa-whitelist@0.4.0
|
|
50
63
|
[0.3.0]: https://github.com/MetaMask/core/compare/@metamask/money-account-mfa-whitelist@0.2.0...@metamask/money-account-mfa-whitelist@0.3.0
|
|
51
64
|
[0.2.0]: https://github.com/MetaMask/core/compare/@metamask/money-account-mfa-whitelist@0.1.0...@metamask/money-account-mfa-whitelist@0.2.0
|
|
52
65
|
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/money-account-mfa-whitelist@0.1.0
|
package/README.md
CHANGED
|
@@ -53,15 +53,17 @@ if (requirement.mfaRequired) {
|
|
|
53
53
|
|
|
54
54
|
### Whitelist rules
|
|
55
55
|
|
|
56
|
-
| Rule | Method | Payload
|
|
57
|
-
| ----------------------------- | -------------------------- |
|
|
58
|
-
| `chomp-authentication` | `signPersonalMessage` | `CHOMP Authentication <timestamp>`, within `maxMessageAge` of now
|
|
59
|
-
| `rewards-binding` | `signPersonalMessage` | `metamask-rewards:money-account-binding:<subject>:<address>:<timestamp>` for the signing account, within `maxMessageAge` of now
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
64
|
-
| `vault-
|
|
56
|
+
| Rule | Method | Payload |
|
|
57
|
+
| ----------------------------- | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
58
|
+
| `chomp-authentication` | `signPersonalMessage` | `CHOMP Authentication <timestamp>`, within `maxMessageAge` of now |
|
|
59
|
+
| `rewards-binding` | `signPersonalMessage` | `metamask-rewards:money-account-binding:<subject>:<address>:<timestamp>` for the signing account, within `maxMessageAge` of now |
|
|
60
|
+
| `rewards-claim-proof` | `signPersonalMessage` | The MetaMask Rewards claim proof whose `Earning address` (`eip155:<address>`) and `Pay to` are the signing account, expiring no later than `maxMessageAge` plus one minute of clock skew from now and not yet expired. The Rewards backend's challenge TTL (`CLAIM_PROOF_CHALLENGE_TTL_SECONDS`, default 300) must stay at or below that, or every proof requires MFA |
|
|
61
|
+
| `card-sign-in` | `signPersonalMessage` | A SIWE message for `cardSignInDomain` and the Money Account chain, signed by the account, that is not expired |
|
|
62
|
+
| `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 |
|
|
63
|
+
| `eip7702-authorization` | `signEip7702Authorization` | An authorization to `EIP7702StatelessDeleGatorImpl` on the Money Account chain |
|
|
64
|
+
| `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 |
|
|
65
|
+
| `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)` |
|
|
66
|
+
| `rewards-claim-delegation` | `signTypedData` (V4) | The same single-use delegation as `vault-deposit-delegation`, with `mUSD.receiveWithAuthorization(from, account, amount, validAfter, validBefore, nonce, signature)` (EIP-3009) before the approve and deposit of the same `amount` |
|
|
65
67
|
|
|
66
68
|
Every delegation must be signed by the delegator, use the root authority, and be verified by the pinned `DelegationManager` on the Money Account chain.
|
|
67
69
|
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { Hex } from '@metamask/utils';
|
|
2
2
|
/** The default `maxMessageAge` of the whitelist config: five minutes. */
|
|
3
3
|
export declare const DEFAULT_MAX_MESSAGE_AGE: number;
|
|
4
|
+
/**
|
|
5
|
+
* How far past `maxMessageAge` a Rewards claim proof's expiry may be, for
|
|
6
|
+
* clock skew between the Rewards backend, which sets the expiry, and the
|
|
7
|
+
* signer: one minute. The expiry is set by the Rewards money service's
|
|
8
|
+
* `CLAIM_PROOF_CHALLENGE_TTL_SECONDS` (default 300), which must stay at or
|
|
9
|
+
* below `maxMessageAge` plus this skew, or every claim proof requires MFA.
|
|
10
|
+
*/
|
|
11
|
+
export declare const CLAIM_PROOF_CLOCK_SKEW: number;
|
|
4
12
|
export declare const ZERO_ADDRESS: Hex;
|
|
5
13
|
/** `approve(address,uint256)` */
|
|
6
14
|
export declare const ERC20_APPROVE_SELECTOR: Hex;
|
|
15
|
+
/**
|
|
16
|
+
* `receiveWithAuthorization(address,address,uint256,uint256,uint256,bytes32,bytes)`
|
|
17
|
+
* from EIP-3009, the overload Mobile's Rewards claim calls on mUSD.
|
|
18
|
+
*/
|
|
19
|
+
export declare const RECEIVE_WITH_AUTHORIZATION_SELECTOR: Hex;
|
|
7
20
|
/** `deposit(address,uint256,uint256,address)` on the vault teller. */
|
|
8
21
|
export declare const TELLER_DEPOSIT_SELECTOR: Hex;
|
|
9
22
|
/** `execute(bytes32,bytes)` from ERC-7821. */
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,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
3
|
+
/**
|
|
4
|
+
* How far past `maxMessageAge` a Rewards claim proof's expiry may be, for
|
|
5
|
+
* clock skew between the Rewards backend, which sets the expiry, and the
|
|
6
|
+
* signer: one minute. The expiry is set by the Rewards money service's
|
|
7
|
+
* `CLAIM_PROOF_CHALLENGE_TTL_SECONDS` (default 300), which must stay at or
|
|
8
|
+
* below `maxMessageAge` plus this skew, or every claim proof requires MFA.
|
|
9
|
+
*/
|
|
10
|
+
export const CLAIM_PROOF_CLOCK_SKEW = 60 * 1000;
|
|
3
11
|
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
4
12
|
/** `approve(address,uint256)` */
|
|
5
13
|
export const ERC20_APPROVE_SELECTOR = '0x095ea7b3';
|
|
14
|
+
/**
|
|
15
|
+
* `receiveWithAuthorization(address,address,uint256,uint256,uint256,bytes32,bytes)`
|
|
16
|
+
* from EIP-3009, the overload Mobile's Rewards claim calls on mUSD.
|
|
17
|
+
*/
|
|
18
|
+
export const RECEIVE_WITH_AUTHORIZATION_SELECTOR = '0x88b7ab63';
|
|
6
19
|
/** `deposit(address,uint256,uint256,address)` on the vault teller. */
|
|
7
20
|
export const TELLER_DEPOSIT_SELECTOR = '0x8b6099db';
|
|
8
21
|
/** `execute(bytes32,bytes)` from ERC-7821. */
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,yEAAyE;AACzE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAErD,MAAM,CAAC,MAAM,YAAY,GAAQ,4CAA4C,CAAC;AAE9E,iCAAiC;AACjC,MAAM,CAAC,MAAM,sBAAsB,GAAQ,YAAY,CAAC;AAExD,sEAAsE;AACtE,MAAM,CAAC,MAAM,uBAAuB,GAAQ,YAAY,CAAC;AAEzD,8CAA8C;AAC9C,MAAM,CAAC,MAAM,wBAAwB,GAAQ,YAAY,CAAC;AAE1D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAQ,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;AAExE,yDAAyD;AACzD,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,YAAY,EAAE;QACZ,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;QAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;QACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;KAC/C;IACD,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE;QACrC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;KAClC;IACD,MAAM,EAAE;QACN,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;KACjC;CACO,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\n/** The default `maxMessageAge` of the whitelist config: five minutes. */\nexport const DEFAULT_MAX_MESSAGE_AGE = 5 * 60 * 1000;\n\nexport const ZERO_ADDRESS: Hex = '0x0000000000000000000000000000000000000000';\n\n/** `approve(address,uint256)` */\nexport const ERC20_APPROVE_SELECTOR: Hex = '0x095ea7b3';\n\n/** `deposit(address,uint256,uint256,address)` on the vault teller. */\nexport const TELLER_DEPOSIT_SELECTOR: Hex = '0x8b6099db';\n\n/** `execute(bytes32,bytes)` from ERC-7821. */\nexport const ERC7821_EXECUTE_SELECTOR: Hex = '0xe9ae5c53';\n\n/**\n * The ERC-7579 mode of an atomic batch (call type batch, exec type default),\n * which reverts every call if one fails.\n */\nexport const ERC7579_BATCH_DEFAULT_MODE: Hex = `0x01${'00'.repeat(31)}`;\n\n/** The EIP-712 types every delegation is signed with. */\nexport const DELEGATION_TYPES = {\n EIP712Domain: [\n { name: 'name', type: 'string' },\n { name: 'version', type: 'string' },\n { name: 'chainId', type: 'uint256' },\n { name: 'verifyingContract', type: 'address' },\n ],\n Delegation: [\n { name: 'delegate', type: 'address' },\n { name: 'delegator', type: 'address' },\n { name: 'authority', type: 'bytes32' },\n { name: 'caveats', type: 'Caveat[]' },\n { name: 'salt', type: 'uint256' },\n ],\n Caveat: [\n { name: 'enforcer', type: 'address' },\n { name: 'terms', type: 'bytes' },\n ],\n} as const;\n"]}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,yEAAyE;AACzE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,GAAG,IAAI,CAAC;AAEhD,MAAM,CAAC,MAAM,YAAY,GAAQ,4CAA4C,CAAC;AAE9E,iCAAiC;AACjC,MAAM,CAAC,MAAM,sBAAsB,GAAQ,YAAY,CAAC;AAExD;;;GAGG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAQ,YAAY,CAAC;AAErE,sEAAsE;AACtE,MAAM,CAAC,MAAM,uBAAuB,GAAQ,YAAY,CAAC;AAEzD,8CAA8C;AAC9C,MAAM,CAAC,MAAM,wBAAwB,GAAQ,YAAY,CAAC;AAE1D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAQ,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;AAExE,yDAAyD;AACzD,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,YAAY,EAAE;QACZ,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;QAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;QACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;KAC/C;IACD,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE;QACrC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;KAClC;IACD,MAAM,EAAE;QACN,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;KACjC;CACO,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\n/** The default `maxMessageAge` of the whitelist config: five minutes. */\nexport const DEFAULT_MAX_MESSAGE_AGE = 5 * 60 * 1000;\n\n/**\n * How far past `maxMessageAge` a Rewards claim proof's expiry may be, for\n * clock skew between the Rewards backend, which sets the expiry, and the\n * signer: one minute. The expiry is set by the Rewards money service's\n * `CLAIM_PROOF_CHALLENGE_TTL_SECONDS` (default 300), which must stay at or\n * below `maxMessageAge` plus this skew, or every claim proof requires MFA.\n */\nexport const CLAIM_PROOF_CLOCK_SKEW = 60 * 1000;\n\nexport const ZERO_ADDRESS: Hex = '0x0000000000000000000000000000000000000000';\n\n/** `approve(address,uint256)` */\nexport const ERC20_APPROVE_SELECTOR: Hex = '0x095ea7b3';\n\n/**\n * `receiveWithAuthorization(address,address,uint256,uint256,uint256,bytes32,bytes)`\n * from EIP-3009, the overload Mobile's Rewards claim calls on mUSD.\n */\nexport const RECEIVE_WITH_AUTHORIZATION_SELECTOR: Hex = '0x88b7ab63';\n\n/** `deposit(address,uint256,uint256,address)` on the vault teller. */\nexport const TELLER_DEPOSIT_SELECTOR: Hex = '0x8b6099db';\n\n/** `execute(bytes32,bytes)` from ERC-7821. */\nexport const ERC7821_EXECUTE_SELECTOR: Hex = '0xe9ae5c53';\n\n/**\n * The ERC-7579 mode of an atomic batch (call type batch, exec type default),\n * which reverts every call if one fails.\n */\nexport const ERC7579_BATCH_DEFAULT_MODE: Hex = `0x01${'00'.repeat(31)}`;\n\n/** The EIP-712 types every delegation is signed with. */\nexport const DELEGATION_TYPES = {\n EIP712Domain: [\n { name: 'name', type: 'string' },\n { name: 'version', type: 'string' },\n { name: 'chainId', type: 'uint256' },\n { name: 'verifyingContract', type: 'address' },\n ],\n Delegation: [\n { name: 'delegate', type: 'address' },\n { name: 'delegator', type: 'address' },\n { name: 'authority', type: 'bytes32' },\n { name: 'caveats', type: 'Caveat[]' },\n { name: 'salt', type: 'uint256' },\n ],\n Caveat: [\n { name: 'enforcer', type: 'address' },\n { name: 'terms', type: 'bytes' },\n ],\n} as const;\n"]}
|
package/dist/delegation.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare function parseDelegationTypedData(data: unknown, config: MfaWhite
|
|
|
32
32
|
*
|
|
33
33
|
* The delegation must be a root delegation from the Money Account, parsed
|
|
34
34
|
* with {@link parseDelegationTypedData}. Whitelisted are the standing vault
|
|
35
|
-
* delegations to CHOMP's delegate, and single-use vault deposit
|
|
36
|
-
* redeemable by anyone.
|
|
35
|
+
* delegations to CHOMP's delegate, and single-use vault deposit and Rewards
|
|
36
|
+
* claim delegations redeemable by anyone.
|
|
37
37
|
*
|
|
38
38
|
* @param delegation - The delegation message.
|
|
39
39
|
* @param context - The request context.
|
package/dist/delegation.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delegation.d.ts","sourceRoot":"","sources":["../src/delegation.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"delegation.d.ts","sourceRoot":"","sources":["../src/delegation.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAQ3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAQrE;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,GAAG,CAAC;IACd,SAAS,EAAE,GAAG,CAAC;IACf,SAAS,EAAE,GAAG,CAAC;IACf,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,kBAAkB,CAAC;CAC5B,CAAC;AAuFF;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,GAAG,MAAM,CAyD5B;AA2HD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CACzC,UAAU,EAAE,iBAAiB,EAC7B,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,iBAAiB,GACrC,cAAc,CAoBhB"}
|
package/dist/delegation.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ANY_BENEFICIARY, ROOT_AUTHORITY, createLimitedCallsTerms, createValueLteTerms, decodeERC20TransferAmountTerms, decodeRedeemerTerms, } from '@metamask/delegation-core';
|
|
2
2
|
import { isObject } from '@metamask/utils';
|
|
3
3
|
import { DELEGATION_TYPES } from './constants.js';
|
|
4
|
-
import { getExactExecutions, isVaultDeposit } from './executions.js';
|
|
4
|
+
import { getExactExecutions, isRewardsClaim, isVaultDeposit, } from './executions.js';
|
|
5
5
|
import { isAddress, isSameHex, requireMfa, whitelisted } from './utils.js';
|
|
6
6
|
const BYTES_PATTERN = /^0x(?:[0-9a-fA-F]{2})*$/u;
|
|
7
7
|
const BYTES32_PATTERN = /^0x[0-9a-fA-F]{64}$/u;
|
|
@@ -186,10 +186,10 @@ function getStandingDelegationRequirement(caveats, config) {
|
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
188
188
|
* Checks a single-use transaction delegation: exactly `LimitedCalls(1)`, one
|
|
189
|
-
* exact-execution caveat pinning a vault deposit and,
|
|
190
|
-
* `Timestamp` deadline and one `Redeemer` allowlist, in any
|
|
191
|
-
* and a redeemer allowlist only narrow when and by whom the
|
|
192
|
-
* redeemed.
|
|
189
|
+
* exact-execution caveat pinning a vault deposit or a Rewards claim and,
|
|
190
|
+
* optionally, one `Timestamp` deadline and one `Redeemer` allowlist, in any
|
|
191
|
+
* order. A deadline and a redeemer allowlist only narrow when and by whom the
|
|
192
|
+
* delegation can be redeemed.
|
|
193
193
|
*
|
|
194
194
|
* Withdrawals, payments and Card approvals use the same shape with other
|
|
195
195
|
* calls, so they require MFA.
|
|
@@ -214,17 +214,20 @@ function getSingleUseDelegationRequirement(caveats, delegator, config) {
|
|
|
214
214
|
if (!executions) {
|
|
215
215
|
return requireMfa('Delegation does not pin its executions');
|
|
216
216
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
217
|
+
if (isVaultDeposit(executions, config)) {
|
|
218
|
+
return whitelisted('vault-deposit-delegation');
|
|
219
|
+
}
|
|
220
|
+
return isRewardsClaim(executions, delegator, config)
|
|
221
|
+
? whitelisted('rewards-claim-delegation')
|
|
222
|
+
: requireMfa('Delegation executions are not a vault deposit or Rewards claim');
|
|
220
223
|
}
|
|
221
224
|
/**
|
|
222
225
|
* Determines whether a delegation requires MFA.
|
|
223
226
|
*
|
|
224
227
|
* The delegation must be a root delegation from the Money Account, parsed
|
|
225
228
|
* with {@link parseDelegationTypedData}. Whitelisted are the standing vault
|
|
226
|
-
* delegations to CHOMP's delegate, and single-use vault deposit
|
|
227
|
-
* redeemable by anyone.
|
|
229
|
+
* delegations to CHOMP's delegate, and single-use vault deposit and Rewards
|
|
230
|
+
* claim delegations redeemable by anyone.
|
|
228
231
|
*
|
|
229
232
|
* @param delegation - The delegation message.
|
|
230
233
|
* @param context - The request context.
|
package/dist/delegation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delegation.js","sourceRoot":"","sources":["../src/delegation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,cAAc,EACd,uBAAuB,EACvB,mBAAmB,EACnB,8BAA8B,EAC9B,mBAAmB,GACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGrE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE3E,MAAM,aAAa,GAAG,0BAA0B,CAAC;AACjD,MAAM,eAAe,GAAG,sBAAsB,CAAC;AAC/C,MAAM,YAAY,GAAG,2BAA2B,CAAC;AACjD,MAAM,WAAW,GAAG,EAAE,IAAI,IAAI,GAAG,EAAE,CAAC;AAmBpC;;;;;;GAMG;AACH,SAAS,YAAY,CAAC,KAA8B,EAAE,IAAc;IAClE,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClC,OAAO,CACL,MAAM,CAAC,MA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AS,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAChE,OAAO,gCAAgC,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,SAAS,CAAC,UAAU,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC;QACpD,OAAO,iCAAiC,CACtC,UAAU,CAAC,OAAO,EAClB,UAAU,CAAC,SAAS,EACpB,MAAM,CACP,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC,wCAAwC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import {\n ANY_BENEFICIARY,\n ROOT_AUTHORITY,\n createLimitedCallsTerms,\n createValueLteTerms,\n decodeERC20TransferAmountTerms,\n decodeRedeemerTerms,\n} from '@metamask/delegation-core';\nimport { isObject } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { DELEGATION_TYPES } from './constants.js';\nimport { getExactExecutions, isVaultDeposit } from './executions.js';\nimport type { Caveat } from './executions.js';\nimport type { MfaRequirement, MfaWhitelistConfig } from './types.js';\nimport { isAddress, isSameHex, requireMfa, whitelisted } from './utils.js';\n\nconst BYTES_PATTERN = /^0x(?:[0-9a-fA-F]{2})*$/u;\nconst BYTES32_PATTERN = /^0x[0-9a-fA-F]{64}$/u;\nconst UINT_PATTERN = /^(?:0x[0-9a-fA-F]+|\\d+)$/u;\nconst MAX_UINT256 = 2n ** 256n - 1n;\n\n/**\n * The fields of a delegation that are signed. The domain is not included,\n * because it must equal the pinned one.\n */\nexport type DelegationMessage = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: bigint;\n};\n\ntype DelegationContext = {\n address: Hex;\n config: MfaWhitelistConfig;\n};\n\n/**\n * Checks that an object has exactly the given keys.\n *\n * @param value - The object.\n * @param keys - The expected keys.\n * @returns Whether the object has exactly these keys.\n */\nfunction hasExactKeys(value: Record<string, unknown>, keys: string[]): boolean {\n const actual = Object.keys(value);\n return (\n actual.length === keys.length && keys.every((key) => actual.includes(key))\n );\n}\n\n/**\n * Checks that an EIP-712 type definition matches the expected one exactly,\n * including field order.\n *\n * @param value - The type definition.\n * @param expected - The expected fields.\n * @returns Whether the definitions match.\n */\nfunction isFieldList(\n value: unknown,\n expected: readonly { name: string; type: string }[],\n): boolean {\n return (\n Array.isArray(value) &&\n value.length === expected.length &&\n value.every(\n (field: unknown, index) =>\n isObject(field) &&\n hasExactKeys(field, ['name', 'type']) &&\n field.name === expected[index].name &&\n field.type === expected[index].type,\n )\n );\n}\n\n/**\n * Parses an unsigned integer the way EIP-712 encoders accept it: a number,\n * a bigint, or a decimal or hex string.\n *\n * @param value - The value.\n * @returns The integer, or `undefined` if the value isn't a uint256.\n */\nfunction parseUint256(value: unknown): bigint | undefined {\n let parsed: bigint | undefined;\n if (typeof value === 'bigint') {\n parsed = value;\n } else if (typeof value === 'number' && Number.isSafeInteger(value)) {\n parsed = BigInt(value);\n } else if (typeof value === 'string' && UINT_PATTERN.test(value)) {\n parsed = BigInt(value);\n }\n return parsed !== undefined && parsed >= 0n && parsed <= MAX_UINT256\n ? parsed\n : undefined;\n}\n\n/**\n * Parses the caveats of a delegation message.\n *\n * @param value - The caveats.\n * @returns The caveats, or `undefined` if any is malformed.\n */\nfunction parseCaveats(value: unknown): Caveat[] | undefined {\n if (!Array.isArray(value)) {\n return undefined;\n }\n const caveats: Caveat[] = [];\n for (const caveat of value as unknown[]) {\n if (\n !isObject(caveat) ||\n !isAddress(caveat.enforcer) ||\n typeof caveat.terms !== 'string' ||\n !BYTES_PATTERN.test(caveat.terms)\n ) {\n return undefined;\n }\n caveats.push({ enforcer: caveat.enforcer, terms: caveat.terms as Hex });\n }\n return caveats;\n}\n\n/**\n * Parses and validates EIP-712 typed data as a delegation for the pinned\n * `DelegationManager` on the Money Account chain. Fields the EIP-712 types\n * don't declare (e.g. caveat `args` or an empty `signature`) don't affect\n * the signed digest and are ignored.\n *\n * @param data - The typed data, as an object or a JSON string.\n * @param config - The whitelist config.\n * @returns The delegation message, or the reason it isn't a valid delegation.\n */\nexport function parseDelegationTypedData(\n data: unknown,\n config: MfaWhitelistConfig,\n): DelegationMessage | string {\n const typedData: unknown = typeof data === 'string' ? JSON.parse(data) : data;\n\n if (!isObject(typedData) || typedData.primaryType !== 'Delegation') {\n return 'Typed data is not a delegation';\n }\n\n const { types, domain, message } = typedData;\n if (\n !isObject(types) ||\n !hasExactKeys(types, Object.keys(DELEGATION_TYPES)) ||\n !isFieldList(types.EIP712Domain, DELEGATION_TYPES.EIP712Domain) ||\n !isFieldList(types.Delegation, DELEGATION_TYPES.Delegation) ||\n !isFieldList(types.Caveat, DELEGATION_TYPES.Caveat)\n ) {\n return 'Typed data types are not the delegation types';\n }\n\n if (\n !isObject(domain) ||\n !hasExactKeys(domain, ['name', 'version', 'chainId', 'verifyingContract'])\n ) {\n return 'Typed data domain is malformed';\n }\n if (\n domain.name !== 'DelegationManager' ||\n domain.version !== '1' ||\n parseUint256(domain.chainId) !== BigInt(config.chainId) ||\n !isAddress(domain.verifyingContract) ||\n !isSameHex(domain.verifyingContract, config.contracts.DelegationManager)\n ) {\n return 'Delegation is not for the pinned DelegationManager and chain';\n }\n\n if (!isObject(message)) {\n return 'Delegation message is malformed';\n }\n const caveats = parseCaveats(message.caveats);\n const salt = parseUint256(message.salt);\n if (\n !isAddress(message.delegate) ||\n !isAddress(message.delegator) ||\n typeof message.authority !== 'string' ||\n !BYTES32_PATTERN.test(message.authority) ||\n salt === undefined ||\n !caveats\n ) {\n return 'Delegation message is malformed';\n }\n\n return {\n delegate: message.delegate,\n delegator: message.delegator,\n authority: message.authority as Hex,\n caveats,\n salt,\n };\n}\n\n/**\n * Finds the only caveat with the given enforcer.\n *\n * @param caveats - The caveats.\n * @param enforcer - The enforcer.\n * @returns The caveat, or `undefined` if there is none or more than one.\n */\nfunction findOnlyCaveat(caveats: Caveat[], enforcer: Hex): Caveat | undefined {\n const matches = caveats.filter((caveat) =>\n isSameHex(caveat.enforcer, enforcer),\n );\n return matches.length === 1 ? matches[0] : undefined;\n}\n\n/**\n * Checks a standing vault delegation, signed during the upgrade for CHOMP's\n * auto-deposit: exactly `ValueLte(0)`, `ERC20TransferAmount(token, amount)`\n * and `Redeemer([adapter])`, in any order, where the adapter belongs to a\n * pinned vault and the token is mUSD or that vault's share token.\n *\n * These are standing allowances, so their safety rests entirely on the\n * pinned delegate and adapter.\n *\n * @param caveats - The delegation's caveats.\n * @param config - The whitelist config.\n * @returns Whether MFA is required.\n */\nfunction getStandingDelegationRequirement(\n caveats: Caveat[],\n config: MfaWhitelistConfig,\n): MfaRequirement {\n const { ERC20TransferAmountEnforcer, RedeemerEnforcer, ValueLteEnforcer } =\n config.contracts;\n\n const valueLte = findOnlyCaveat(caveats, ValueLteEnforcer);\n const transferAmount = findOnlyCaveat(caveats, ERC20TransferAmountEnforcer);\n const redeemer = findOnlyCaveat(caveats, RedeemerEnforcer);\n if (caveats.length !== 3 || !valueLte || !transferAmount || !redeemer) {\n return requireMfa('Standing delegation caveats are not whitelisted');\n }\n\n if (!isSameHex(valueLte.terms, createValueLteTerms({ maxValue: 0n }))) {\n return requireMfa('Standing delegation allows native value');\n }\n\n // The packed-terms decoders throw unless the terms have the exact length.\n const { tokenAddress } = decodeERC20TransferAmountTerms(transferAmount.terms);\n const { redeemers } = decodeRedeemerTerms(redeemer.terms);\n if (redeemers.length !== 1) {\n return requireMfa('Standing delegation has more than one redeemer');\n }\n\n const vault = config.vaults.find(({ vedaVaultAdapterAddress }) =>\n isSameHex(vedaVaultAdapterAddress, redeemers[0]),\n );\n if (!vault) {\n return requireMfa('Standing delegation redeemer is not a vault adapter');\n }\n if (\n !isSameHex(tokenAddress, config.musdTokenAddress) &&\n !isSameHex(tokenAddress, vault.boringVault)\n ) {\n return requireMfa('Standing delegation token is not mUSD or vault shares');\n }\n\n return whitelisted('vault-standing-delegation');\n}\n\n/**\n * Checks a single-use transaction delegation: exactly `LimitedCalls(1)`, one\n * exact-execution caveat pinning a vault deposit and, optionally, one\n * `Timestamp` deadline and one `Redeemer` allowlist, in any order. A deadline\n * and a redeemer allowlist only narrow when and by whom the delegation can be\n * redeemed.\n *\n * Withdrawals, payments and Card approvals use the same shape with other\n * calls, so they require MFA.\n *\n * @param caveats - The delegation's caveats.\n * @param delegator - The Money Account.\n * @param config - The whitelist config.\n * @returns Whether MFA is required.\n */\nfunction getSingleUseDelegationRequirement(\n caveats: Caveat[],\n delegator: Hex,\n config: MfaWhitelistConfig,\n): MfaRequirement {\n const limitedCalls = findOnlyCaveat(\n caveats,\n config.contracts.LimitedCallsEnforcer,\n );\n const timestamp = findOnlyCaveat(caveats, config.contracts.TimestampEnforcer);\n const redeemer = findOnlyCaveat(caveats, config.contracts.RedeemerEnforcer);\n const [exactExecution, ...unexpected] = caveats.filter(\n (caveat) =>\n caveat !== limitedCalls && caveat !== timestamp && caveat !== redeemer,\n );\n if (!limitedCalls || !exactExecution || unexpected.length > 0) {\n return requireMfa('Single-use delegation caveats are not whitelisted');\n }\n\n if (!isSameHex(limitedCalls.terms, createLimitedCallsTerms({ limit: 1 }))) {\n return requireMfa('Delegation is not limited to a single call');\n }\n\n const executions = getExactExecutions(exactExecution, delegator, config);\n if (!executions) {\n return requireMfa('Delegation does not pin its executions');\n }\n\n return isVaultDeposit(executions, config)\n ? whitelisted('vault-deposit-delegation')\n : requireMfa('Delegation executions are not a vault deposit');\n}\n\n/**\n * Determines whether a delegation requires MFA.\n *\n * The delegation must be a root delegation from the Money Account, parsed\n * with {@link parseDelegationTypedData}. Whitelisted are the standing vault\n * delegations to CHOMP's delegate, and single-use vault deposit delegations\n * redeemable by anyone.\n *\n * @param delegation - The delegation message.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @returns Whether MFA is required.\n */\nexport function getDelegationMfaRequirement(\n delegation: DelegationMessage,\n { address, config }: DelegationContext,\n): MfaRequirement {\n if (!isSameHex(delegation.delegator, address)) {\n return requireMfa('Delegator is not the Money Account');\n }\n if (!isSameHex(delegation.authority, ROOT_AUTHORITY)) {\n return requireMfa('Delegation is not a root delegation');\n }\n\n if (isSameHex(delegation.delegate, config.chompDelegateAddress)) {\n return getStandingDelegationRequirement(delegation.caveats, config);\n }\n if (isSameHex(delegation.delegate, ANY_BENEFICIARY)) {\n return getSingleUseDelegationRequirement(\n delegation.caveats,\n delegation.delegator,\n config,\n );\n }\n\n return requireMfa('Delegation delegate is not whitelisted');\n}\n"]}
|
|
1
|
+
{"version":3,"file":"delegation.js","sourceRoot":"","sources":["../src/delegation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,cAAc,EACd,uBAAuB,EACvB,mBAAmB,EACnB,8BAA8B,EAC9B,mBAAmB,GACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,cAAc,GACf,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE3E,MAAM,aAAa,GAAG,0BAA0B,CAAC;AACjD,MAAM,eAAe,GAAG,sBAAsB,CAAC;AAC/C,MAAM,YAAY,GAAG,2BAA2B,CAAC;AACjD,MAAM,WAAW,GAAG,EAAE,IAAI,IAAI,GAAG,EAAE,CAAC;AAmBpC;;;;;;GAMG;AACH,SAAS,YAAY,CAAC,KAA8B,EAAE,IAAc;IAClE,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClC,OAAO,CACL,MAAM,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CA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,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAChE,OAAO,gCAAgC,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,SAAS,CAAC,UAAU,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC;QACpD,OAAO,iCAAiC,CACtC,UAAU,CAAC,OAAO,EAClB,UAAU,CAAC,SAAS,EACpB,MAAM,CACP,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC,wCAAwC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import {\n ANY_BENEFICIARY,\n ROOT_AUTHORITY,\n createLimitedCallsTerms,\n createValueLteTerms,\n decodeERC20TransferAmountTerms,\n decodeRedeemerTerms,\n} from '@metamask/delegation-core';\nimport { isObject } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { DELEGATION_TYPES } from './constants.js';\nimport {\n getExactExecutions,\n isRewardsClaim,\n isVaultDeposit,\n} from './executions.js';\nimport type { Caveat } from './executions.js';\nimport type { MfaRequirement, MfaWhitelistConfig } from './types.js';\nimport { isAddress, isSameHex, requireMfa, whitelisted } from './utils.js';\n\nconst BYTES_PATTERN = /^0x(?:[0-9a-fA-F]{2})*$/u;\nconst BYTES32_PATTERN = /^0x[0-9a-fA-F]{64}$/u;\nconst UINT_PATTERN = /^(?:0x[0-9a-fA-F]+|\\d+)$/u;\nconst MAX_UINT256 = 2n ** 256n - 1n;\n\n/**\n * The fields of a delegation that are signed. The domain is not included,\n * because it must equal the pinned one.\n */\nexport type DelegationMessage = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: bigint;\n};\n\ntype DelegationContext = {\n address: Hex;\n config: MfaWhitelistConfig;\n};\n\n/**\n * Checks that an object has exactly the given keys.\n *\n * @param value - The object.\n * @param keys - The expected keys.\n * @returns Whether the object has exactly these keys.\n */\nfunction hasExactKeys(value: Record<string, unknown>, keys: string[]): boolean {\n const actual = Object.keys(value);\n return (\n actual.length === keys.length && keys.every((key) => actual.includes(key))\n );\n}\n\n/**\n * Checks that an EIP-712 type definition matches the expected one exactly,\n * including field order.\n *\n * @param value - The type definition.\n * @param expected - The expected fields.\n * @returns Whether the definitions match.\n */\nfunction isFieldList(\n value: unknown,\n expected: readonly { name: string; type: string }[],\n): boolean {\n return (\n Array.isArray(value) &&\n value.length === expected.length &&\n value.every(\n (field: unknown, index) =>\n isObject(field) &&\n hasExactKeys(field, ['name', 'type']) &&\n field.name === expected[index].name &&\n field.type === expected[index].type,\n )\n );\n}\n\n/**\n * Parses an unsigned integer the way EIP-712 encoders accept it: a number,\n * a bigint, or a decimal or hex string.\n *\n * @param value - The value.\n * @returns The integer, or `undefined` if the value isn't a uint256.\n */\nfunction parseUint256(value: unknown): bigint | undefined {\n let parsed: bigint | undefined;\n if (typeof value === 'bigint') {\n parsed = value;\n } else if (typeof value === 'number' && Number.isSafeInteger(value)) {\n parsed = BigInt(value);\n } else if (typeof value === 'string' && UINT_PATTERN.test(value)) {\n parsed = BigInt(value);\n }\n return parsed !== undefined && parsed >= 0n && parsed <= MAX_UINT256\n ? parsed\n : undefined;\n}\n\n/**\n * Parses the caveats of a delegation message.\n *\n * @param value - The caveats.\n * @returns The caveats, or `undefined` if any is malformed.\n */\nfunction parseCaveats(value: unknown): Caveat[] | undefined {\n if (!Array.isArray(value)) {\n return undefined;\n }\n const caveats: Caveat[] = [];\n for (const caveat of value as unknown[]) {\n if (\n !isObject(caveat) ||\n !isAddress(caveat.enforcer) ||\n typeof caveat.terms !== 'string' ||\n !BYTES_PATTERN.test(caveat.terms)\n ) {\n return undefined;\n }\n caveats.push({ enforcer: caveat.enforcer, terms: caveat.terms as Hex });\n }\n return caveats;\n}\n\n/**\n * Parses and validates EIP-712 typed data as a delegation for the pinned\n * `DelegationManager` on the Money Account chain. Fields the EIP-712 types\n * don't declare (e.g. caveat `args` or an empty `signature`) don't affect\n * the signed digest and are ignored.\n *\n * @param data - The typed data, as an object or a JSON string.\n * @param config - The whitelist config.\n * @returns The delegation message, or the reason it isn't a valid delegation.\n */\nexport function parseDelegationTypedData(\n data: unknown,\n config: MfaWhitelistConfig,\n): DelegationMessage | string {\n const typedData: unknown = typeof data === 'string' ? JSON.parse(data) : data;\n\n if (!isObject(typedData) || typedData.primaryType !== 'Delegation') {\n return 'Typed data is not a delegation';\n }\n\n const { types, domain, message } = typedData;\n if (\n !isObject(types) ||\n !hasExactKeys(types, Object.keys(DELEGATION_TYPES)) ||\n !isFieldList(types.EIP712Domain, DELEGATION_TYPES.EIP712Domain) ||\n !isFieldList(types.Delegation, DELEGATION_TYPES.Delegation) ||\n !isFieldList(types.Caveat, DELEGATION_TYPES.Caveat)\n ) {\n return 'Typed data types are not the delegation types';\n }\n\n if (\n !isObject(domain) ||\n !hasExactKeys(domain, ['name', 'version', 'chainId', 'verifyingContract'])\n ) {\n return 'Typed data domain is malformed';\n }\n if (\n domain.name !== 'DelegationManager' ||\n domain.version !== '1' ||\n parseUint256(domain.chainId) !== BigInt(config.chainId) ||\n !isAddress(domain.verifyingContract) ||\n !isSameHex(domain.verifyingContract, config.contracts.DelegationManager)\n ) {\n return 'Delegation is not for the pinned DelegationManager and chain';\n }\n\n if (!isObject(message)) {\n return 'Delegation message is malformed';\n }\n const caveats = parseCaveats(message.caveats);\n const salt = parseUint256(message.salt);\n if (\n !isAddress(message.delegate) ||\n !isAddress(message.delegator) ||\n typeof message.authority !== 'string' ||\n !BYTES32_PATTERN.test(message.authority) ||\n salt === undefined ||\n !caveats\n ) {\n return 'Delegation message is malformed';\n }\n\n return {\n delegate: message.delegate,\n delegator: message.delegator,\n authority: message.authority as Hex,\n caveats,\n salt,\n };\n}\n\n/**\n * Finds the only caveat with the given enforcer.\n *\n * @param caveats - The caveats.\n * @param enforcer - The enforcer.\n * @returns The caveat, or `undefined` if there is none or more than one.\n */\nfunction findOnlyCaveat(caveats: Caveat[], enforcer: Hex): Caveat | undefined {\n const matches = caveats.filter((caveat) =>\n isSameHex(caveat.enforcer, enforcer),\n );\n return matches.length === 1 ? matches[0] : undefined;\n}\n\n/**\n * Checks a standing vault delegation, signed during the upgrade for CHOMP's\n * auto-deposit: exactly `ValueLte(0)`, `ERC20TransferAmount(token, amount)`\n * and `Redeemer([adapter])`, in any order, where the adapter belongs to a\n * pinned vault and the token is mUSD or that vault's share token.\n *\n * These are standing allowances, so their safety rests entirely on the\n * pinned delegate and adapter.\n *\n * @param caveats - The delegation's caveats.\n * @param config - The whitelist config.\n * @returns Whether MFA is required.\n */\nfunction getStandingDelegationRequirement(\n caveats: Caveat[],\n config: MfaWhitelistConfig,\n): MfaRequirement {\n const { ERC20TransferAmountEnforcer, RedeemerEnforcer, ValueLteEnforcer } =\n config.contracts;\n\n const valueLte = findOnlyCaveat(caveats, ValueLteEnforcer);\n const transferAmount = findOnlyCaveat(caveats, ERC20TransferAmountEnforcer);\n const redeemer = findOnlyCaveat(caveats, RedeemerEnforcer);\n if (caveats.length !== 3 || !valueLte || !transferAmount || !redeemer) {\n return requireMfa('Standing delegation caveats are not whitelisted');\n }\n\n if (!isSameHex(valueLte.terms, createValueLteTerms({ maxValue: 0n }))) {\n return requireMfa('Standing delegation allows native value');\n }\n\n // The packed-terms decoders throw unless the terms have the exact length.\n const { tokenAddress } = decodeERC20TransferAmountTerms(transferAmount.terms);\n const { redeemers } = decodeRedeemerTerms(redeemer.terms);\n if (redeemers.length !== 1) {\n return requireMfa('Standing delegation has more than one redeemer');\n }\n\n const vault = config.vaults.find(({ vedaVaultAdapterAddress }) =>\n isSameHex(vedaVaultAdapterAddress, redeemers[0]),\n );\n if (!vault) {\n return requireMfa('Standing delegation redeemer is not a vault adapter');\n }\n if (\n !isSameHex(tokenAddress, config.musdTokenAddress) &&\n !isSameHex(tokenAddress, vault.boringVault)\n ) {\n return requireMfa('Standing delegation token is not mUSD or vault shares');\n }\n\n return whitelisted('vault-standing-delegation');\n}\n\n/**\n * Checks a single-use transaction delegation: exactly `LimitedCalls(1)`, one\n * exact-execution caveat pinning a vault deposit or a Rewards claim and,\n * optionally, one `Timestamp` deadline and one `Redeemer` allowlist, in any\n * order. A deadline and a redeemer allowlist only narrow when and by whom the\n * delegation can be redeemed.\n *\n * Withdrawals, payments and Card approvals use the same shape with other\n * calls, so they require MFA.\n *\n * @param caveats - The delegation's caveats.\n * @param delegator - The Money Account.\n * @param config - The whitelist config.\n * @returns Whether MFA is required.\n */\nfunction getSingleUseDelegationRequirement(\n caveats: Caveat[],\n delegator: Hex,\n config: MfaWhitelistConfig,\n): MfaRequirement {\n const limitedCalls = findOnlyCaveat(\n caveats,\n config.contracts.LimitedCallsEnforcer,\n );\n const timestamp = findOnlyCaveat(caveats, config.contracts.TimestampEnforcer);\n const redeemer = findOnlyCaveat(caveats, config.contracts.RedeemerEnforcer);\n const [exactExecution, ...unexpected] = caveats.filter(\n (caveat) =>\n caveat !== limitedCalls && caveat !== timestamp && caveat !== redeemer,\n );\n if (!limitedCalls || !exactExecution || unexpected.length > 0) {\n return requireMfa('Single-use delegation caveats are not whitelisted');\n }\n\n if (!isSameHex(limitedCalls.terms, createLimitedCallsTerms({ limit: 1 }))) {\n return requireMfa('Delegation is not limited to a single call');\n }\n\n const executions = getExactExecutions(exactExecution, delegator, config);\n if (!executions) {\n return requireMfa('Delegation does not pin its executions');\n }\n\n if (isVaultDeposit(executions, config)) {\n return whitelisted('vault-deposit-delegation');\n }\n return isRewardsClaim(executions, delegator, config)\n ? whitelisted('rewards-claim-delegation')\n : requireMfa(\n 'Delegation executions are not a vault deposit or Rewards claim',\n );\n}\n\n/**\n * Determines whether a delegation requires MFA.\n *\n * The delegation must be a root delegation from the Money Account, parsed\n * with {@link parseDelegationTypedData}. Whitelisted are the standing vault\n * delegations to CHOMP's delegate, and single-use vault deposit and Rewards\n * claim delegations redeemable by anyone.\n *\n * @param delegation - The delegation message.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @returns Whether MFA is required.\n */\nexport function getDelegationMfaRequirement(\n delegation: DelegationMessage,\n { address, config }: DelegationContext,\n): MfaRequirement {\n if (!isSameHex(delegation.delegator, address)) {\n return requireMfa('Delegator is not the Money Account');\n }\n if (!isSameHex(delegation.authority, ROOT_AUTHORITY)) {\n return requireMfa('Delegation is not a root delegation');\n }\n\n if (isSameHex(delegation.delegate, config.chompDelegateAddress)) {\n return getStandingDelegationRequirement(delegation.caveats, config);\n }\n if (isSameHex(delegation.delegate, ANY_BENEFICIARY)) {\n return getSingleUseDelegationRequirement(\n delegation.caveats,\n delegation.delegator,\n config,\n );\n }\n\n return requireMfa('Delegation delegate is not whitelisted');\n}\n"]}
|
package/dist/executions.d.ts
CHANGED
|
@@ -22,15 +22,26 @@ export type Caveat = {
|
|
|
22
22
|
*/
|
|
23
23
|
export declare function getExactExecutions(caveat: Caveat, delegator: Hex, config: MfaWhitelistConfig): Execution[] | undefined;
|
|
24
24
|
/**
|
|
25
|
-
* Checks whether calls are a Money Account vault deposit
|
|
26
|
-
*
|
|
27
|
-
* `teller.deposit(mUSD, amount, minimumMint, 0x0)` of the same vault, with
|
|
28
|
-
* no native value. Funds never leave the user: mUSD in the Money Account
|
|
29
|
-
* becomes vault shares owned by the Money Account.
|
|
25
|
+
* Checks whether calls are a Money Account vault deposit, as described in
|
|
26
|
+
* {@link getVaultDepositAmount}.
|
|
30
27
|
*
|
|
31
28
|
* @param executions - The calls.
|
|
32
29
|
* @param config - The whitelist config.
|
|
33
30
|
* @returns Whether the calls are a vault deposit.
|
|
34
31
|
*/
|
|
35
32
|
export declare function isVaultDeposit(executions: Execution[], config: MfaWhitelistConfig): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether calls are a MetaMask Rewards claim:
|
|
35
|
+
* `mUSD.receiveWithAuthorization(from, delegator, amount, ...)` (EIP-3009)
|
|
36
|
+
* followed by a vault deposit of the same `amount`, with no native value.
|
|
37
|
+
* The voucher can only pay the Money Account itself, and the deposit only
|
|
38
|
+
* turns what it received into vault shares owned by the Money Account, so
|
|
39
|
+
* funds only come in.
|
|
40
|
+
*
|
|
41
|
+
* @param executions - The calls.
|
|
42
|
+
* @param delegator - The Money Account.
|
|
43
|
+
* @param config - The whitelist config.
|
|
44
|
+
* @returns Whether the calls are a Rewards claim.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isRewardsClaim(executions: Execution[], delegator: Hex, config: MfaWhitelistConfig): boolean;
|
|
36
47
|
//# sourceMappingURL=executions.d.ts.map
|
package/dist/executions.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executions.d.ts","sourceRoot":"","sources":["../src/executions.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"executions.d.ts","sourceRoot":"","sources":["../src/executions.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAU3C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGrD,MAAM,MAAM,SAAS,GAAG;IACtB,MAAM,EAAE,GAAG,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,GAAG,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,GAAG,CAAC;CACZ,CAAC;AAqDF;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,GAAG,EACd,MAAM,EAAE,kBAAkB,GACzB,SAAS,EAAE,GAAG,SAAS,CAiBzB;AAsED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,SAAS,EAAE,EACvB,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAET;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,SAAS,EAAE,EACvB,SAAS,EAAE,GAAG,EACd,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAwBT"}
|
package/dist/executions.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createExactExecutionBatchTerms, decodeExactExecutionBatchTerms, decodeExactExecutionTerms, } from '@metamask/delegation-core';
|
|
2
2
|
import { bytesToHex } from '@metamask/utils';
|
|
3
|
-
import { ERC20_APPROVE_SELECTOR, ERC7579_BATCH_DEFAULT_MODE, ERC7821_EXECUTE_SELECTOR, TELLER_DEPOSIT_SELECTOR, ZERO_ADDRESS, } from './constants.js';
|
|
3
|
+
import { ERC20_APPROVE_SELECTOR, ERC7579_BATCH_DEFAULT_MODE, ERC7821_EXECUTE_SELECTOR, RECEIVE_WITH_AUTHORIZATION_SELECTOR, TELLER_DEPOSIT_SELECTOR, ZERO_ADDRESS, } from './constants.js';
|
|
4
4
|
import { decodeCall, decodeCanonical, isSameHex } from './utils.js';
|
|
5
5
|
/**
|
|
6
6
|
* Unwraps an execution of the account's own ERC-7821 `execute()`, which
|
|
@@ -66,7 +66,7 @@ export function getExactExecutions(caveat, delegator, config) {
|
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
69
|
+
* Gets the amount of a Money Account vault deposit:
|
|
70
70
|
* `mUSD.approve(boringVault, amount)` followed by
|
|
71
71
|
* `teller.deposit(mUSD, amount, minimumMint, 0x0)` of the same vault, with
|
|
72
72
|
* no native value. Funds never leave the user: mUSD in the Money Account
|
|
@@ -74,30 +74,31 @@ export function getExactExecutions(caveat, delegator, config) {
|
|
|
74
74
|
*
|
|
75
75
|
* @param executions - The calls.
|
|
76
76
|
* @param config - The whitelist config.
|
|
77
|
-
* @returns
|
|
77
|
+
* @returns The deposit amount, or `undefined` if the calls are not a vault
|
|
78
|
+
* deposit.
|
|
78
79
|
*/
|
|
79
|
-
|
|
80
|
+
function getVaultDepositAmount(executions, config) {
|
|
80
81
|
if (executions.length !== 2) {
|
|
81
|
-
return
|
|
82
|
+
return undefined;
|
|
82
83
|
}
|
|
83
84
|
const [approve, deposit] = executions;
|
|
84
85
|
if (approve.value !== 0n || deposit.value !== 0n) {
|
|
85
|
-
return
|
|
86
|
+
return undefined;
|
|
86
87
|
}
|
|
87
88
|
if (!isSameHex(approve.target, config.musdTokenAddress)) {
|
|
88
|
-
return
|
|
89
|
+
return undefined;
|
|
89
90
|
}
|
|
90
91
|
const approveArgs = decodeCall(approve.callData, ERC20_APPROVE_SELECTOR, [
|
|
91
92
|
'address',
|
|
92
93
|
'uint256',
|
|
93
94
|
]);
|
|
94
95
|
if (!approveArgs) {
|
|
95
|
-
return
|
|
96
|
+
return undefined;
|
|
96
97
|
}
|
|
97
98
|
const [spender, approveAmount] = approveArgs;
|
|
98
99
|
const vault = config.vaults.find(({ boringVault }) => isSameHex(boringVault, spender));
|
|
99
100
|
if (!vault || !isSameHex(deposit.target, vault.tellerAddress)) {
|
|
100
|
-
return
|
|
101
|
+
return undefined;
|
|
101
102
|
}
|
|
102
103
|
const depositArgs = decodeCall(deposit.callData, TELLER_DEPOSIT_SELECTOR, [
|
|
103
104
|
'address',
|
|
@@ -106,12 +107,53 @@ export function isVaultDeposit(executions, config) {
|
|
|
106
107
|
'address',
|
|
107
108
|
]);
|
|
108
109
|
if (!depositArgs) {
|
|
109
|
-
return
|
|
110
|
+
return undefined;
|
|
110
111
|
}
|
|
111
112
|
const [asset, depositAmount, , referral] = depositArgs;
|
|
112
|
-
return
|
|
113
|
+
return isSameHex(asset, config.musdTokenAddress) &&
|
|
113
114
|
depositAmount === approveAmount &&
|
|
114
115
|
depositAmount > 0n &&
|
|
115
|
-
isSameHex(referral, ZERO_ADDRESS)
|
|
116
|
+
isSameHex(referral, ZERO_ADDRESS)
|
|
117
|
+
? depositAmount
|
|
118
|
+
: undefined;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Checks whether calls are a Money Account vault deposit, as described in
|
|
122
|
+
* {@link getVaultDepositAmount}.
|
|
123
|
+
*
|
|
124
|
+
* @param executions - The calls.
|
|
125
|
+
* @param config - The whitelist config.
|
|
126
|
+
* @returns Whether the calls are a vault deposit.
|
|
127
|
+
*/
|
|
128
|
+
export function isVaultDeposit(executions, config) {
|
|
129
|
+
return getVaultDepositAmount(executions, config) !== undefined;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Checks whether calls are a MetaMask Rewards claim:
|
|
133
|
+
* `mUSD.receiveWithAuthorization(from, delegator, amount, ...)` (EIP-3009)
|
|
134
|
+
* followed by a vault deposit of the same `amount`, with no native value.
|
|
135
|
+
* The voucher can only pay the Money Account itself, and the deposit only
|
|
136
|
+
* turns what it received into vault shares owned by the Money Account, so
|
|
137
|
+
* funds only come in.
|
|
138
|
+
*
|
|
139
|
+
* @param executions - The calls.
|
|
140
|
+
* @param delegator - The Money Account.
|
|
141
|
+
* @param config - The whitelist config.
|
|
142
|
+
* @returns Whether the calls are a Rewards claim.
|
|
143
|
+
*/
|
|
144
|
+
export function isRewardsClaim(executions, delegator, config) {
|
|
145
|
+
const [receive, ...deposit] = executions;
|
|
146
|
+
if (!receive ||
|
|
147
|
+
receive.value !== 0n ||
|
|
148
|
+
!isSameHex(receive.target, config.musdTokenAddress)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const receiveArgs = decodeCall(receive.callData, RECEIVE_WITH_AUTHORIZATION_SELECTOR, ['address', 'address', 'uint256', 'uint256', 'uint256', 'bytes32', 'bytes']);
|
|
152
|
+
if (!receiveArgs) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const [, to, amount] = receiveArgs;
|
|
156
|
+
return (isSameHex(to, delegator) &&
|
|
157
|
+
getVaultDepositAmount(deposit, config) === amount);
|
|
116
158
|
}
|
|
117
159
|
//# sourceMappingURL=executions.js.map
|
package/dist/executions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"execut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ent":["import {\n createExactExecutionBatchTerms,\n decodeExactExecutionBatchTerms,\n decodeExactExecutionTerms,\n} from '@metamask/delegation-core';\nimport { bytesToHex } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n ERC20_APPROVE_SELECTOR,\n ERC7579_BATCH_DEFAULT_MODE,\n ERC7821_EXECUTE_SELECTOR,\n TELLER_DEPOSIT_SELECTOR,\n ZERO_ADDRESS,\n} from './constants.js';\nimport type { MfaWhitelistConfig } from './types.js';\nimport { decodeCall, decodeCanonical, isSameHex } from './utils.js';\n\nexport type Execution = {\n target: Hex;\n value: bigint;\n callData: Hex;\n};\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n};\n\n/**\n * Unwraps an execution of the account's own ERC-7821 `execute()`, which\n * `Delegation7702PublishHook` signs instead of the individual calls. Any\n * other execution is returned as is.\n *\n * @param execution - The execution.\n * @param delegator - The Money Account.\n * @returns The calls the execution makes, or `undefined` if it calls the\n * account with anything other than an atomic batch `execute()`.\n */\nfunction unwrapExecute(\n execution: Execution,\n delegator: Hex,\n): Execution[] | undefined {\n if (!isSameHex(execution.target, delegator)) {\n return [execution];\n }\n if (execution.value !== 0n) {\n return undefined;\n }\n\n const args = decodeCall(execution.callData, ERC7821_EXECUTE_SELECTOR, [\n 'bytes32',\n 'bytes',\n ]);\n if (!args) {\n return undefined;\n }\n\n const [mode, executionData] = args as [Uint8Array, Uint8Array];\n if (!isSameHex(bytesToHex(mode), ERC7579_BATCH_DEFAULT_MODE)) {\n return undefined;\n }\n\n const calls = decodeCanonical(\n ['(address,uint256,bytes)[]'],\n bytesToHex(executionData),\n );\n if (!calls) {\n return undefined;\n }\n\n return (calls[0] as [Hex, bigint, Uint8Array][]).map(\n ([target, value, callData]) => ({\n target,\n value,\n callData: bytesToHex(callData),\n }),\n );\n}\n\n/**\n * Resolves the calls an exact-execution caveat pins. The same calls arrive\n * as an `ExactExecutionBatch` of the individual calls, or as one\n * `ExactExecution` of the account's own `execute()`.\n *\n * @param caveat - The exact-execution caveat.\n * @param delegator - The Money Account.\n * @param config - The whitelist config.\n * @returns The pinned calls, or `undefined` if the caveat is not an\n * exact-execution caveat or its terms are not canonical.\n */\nexport function getExactExecutions(\n caveat: Caveat,\n delegator: Hex,\n config: MfaWhitelistConfig,\n): Execution[] | undefined {\n const { ExactExecutionBatchEnforcer, ExactExecutionEnforcer } =\n config.contracts;\n\n if (isSameHex(caveat.enforcer, ExactExecutionBatchEnforcer)) {\n const { executions } = decodeExactExecutionBatchTerms(caveat.terms);\n const reencoded = createExactExecutionBatchTerms({ executions });\n return isSameHex(reencoded, caveat.terms) ? executions : undefined;\n }\n\n if (isSameHex(caveat.enforcer, ExactExecutionEnforcer)) {\n // Packed terms: everything after the target and value is the calldata.\n const { execution } = decodeExactExecutionTerms(caveat.terms);\n return unwrapExecute(execution, delegator);\n }\n\n return undefined;\n}\n\n/**\n * Checks whether calls are a Money Account vault deposit:\n * `mUSD.approve(boringVault, amount)` followed by\n * `teller.deposit(mUSD, amount, minimumMint, 0x0)` of the same vault, with\n * no native value. Funds never leave the user: mUSD in the Money Account\n * becomes vault shares owned by the Money Account.\n *\n * @param executions - The calls.\n * @param config - The whitelist config.\n * @returns Whether the calls are a vault deposit.\n */\nexport function isVaultDeposit(\n executions: Execution[],\n config: MfaWhitelistConfig,\n): boolean {\n if (executions.length !== 2) {\n return false;\n }\n\n const [approve, deposit] = executions;\n if (approve.value !== 0n || deposit.value !== 0n) {\n return false;\n }\n if (!isSameHex(approve.target, config.musdTokenAddress)) {\n return false;\n }\n\n const approveArgs = decodeCall(approve.callData, ERC20_APPROVE_SELECTOR, [\n 'address',\n 'uint256',\n ]);\n if (!approveArgs) {\n return false;\n }\n const [spender, approveAmount] = approveArgs as [Hex, bigint];\n\n const vault = config.vaults.find(({ boringVault }) =>\n isSameHex(boringVault, spender),\n );\n if (!vault || !isSameHex(deposit.target, vault.tellerAddress)) {\n return false;\n }\n\n const depositArgs = decodeCall(deposit.callData, TELLER_DEPOSIT_SELECTOR, [\n 'address',\n 'uint256',\n 'uint256',\n 'address',\n ]);\n if (!depositArgs) {\n return false;\n }\n const [asset, depositAmount, , referral] = depositArgs as [\n Hex,\n bigint,\n bigint,\n Hex,\n ];\n\n return (\n isSameHex(asset, config.musdTokenAddress) &&\n depositAmount === approveAmount &&\n depositAmount > 0n &&\n isSameHex(referral, ZERO_ADDRESS)\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"executions.js","sourceRoot":"","sources":["../src/executions.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,GAC1B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAG7C,OAAO,EACL,sBAAsB,EACtB,0BAA0B,EAC1B,wBAAwB,EACxB,mCAAmC,EACnC,uBAAuB,EACvB,YAAY,GACb,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAapE;;;;;;;;;GASG;AACH,SAAS,aAAa,CACpB,SAAoB,EACpB,SAAc;IAEd,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC;QAC5C,OAAO,CAAC,SAAS,CAAC,CAAC;IACrB,CAAC;IACD,IAAI,SAAS,CAAC,KAAK,KAAK,EAAE,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,wBAAwB,EAAE;QACpE,SAAS;QACT,OAAO;KACR,CAAC,CAAC;IACH,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,GAAG,IAAgC,CAAC;IAC/D,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,C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,MAAM,CAClD,CAAC;AACJ,CAAC","sourcesContent":["import {\n createExactExecutionBatchTerms,\n decodeExactExecutionBatchTerms,\n decodeExactExecutionTerms,\n} from '@metamask/delegation-core';\nimport { bytesToHex } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n ERC20_APPROVE_SELECTOR,\n ERC7579_BATCH_DEFAULT_MODE,\n ERC7821_EXECUTE_SELECTOR,\n RECEIVE_WITH_AUTHORIZATION_SELECTOR,\n TELLER_DEPOSIT_SELECTOR,\n ZERO_ADDRESS,\n} from './constants.js';\nimport type { MfaWhitelistConfig } from './types.js';\nimport { decodeCall, decodeCanonical, isSameHex } from './utils.js';\n\nexport type Execution = {\n target: Hex;\n value: bigint;\n callData: Hex;\n};\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n};\n\n/**\n * Unwraps an execution of the account's own ERC-7821 `execute()`, which\n * `Delegation7702PublishHook` signs instead of the individual calls. Any\n * other execution is returned as is.\n *\n * @param execution - The execution.\n * @param delegator - The Money Account.\n * @returns The calls the execution makes, or `undefined` if it calls the\n * account with anything other than an atomic batch `execute()`.\n */\nfunction unwrapExecute(\n execution: Execution,\n delegator: Hex,\n): Execution[] | undefined {\n if (!isSameHex(execution.target, delegator)) {\n return [execution];\n }\n if (execution.value !== 0n) {\n return undefined;\n }\n\n const args = decodeCall(execution.callData, ERC7821_EXECUTE_SELECTOR, [\n 'bytes32',\n 'bytes',\n ]);\n if (!args) {\n return undefined;\n }\n\n const [mode, executionData] = args as [Uint8Array, Uint8Array];\n if (!isSameHex(bytesToHex(mode), ERC7579_BATCH_DEFAULT_MODE)) {\n return undefined;\n }\n\n const calls = decodeCanonical(\n ['(address,uint256,bytes)[]'],\n bytesToHex(executionData),\n );\n if (!calls) {\n return undefined;\n }\n\n return (calls[0] as [Hex, bigint, Uint8Array][]).map(\n ([target, value, callData]) => ({\n target,\n value,\n callData: bytesToHex(callData),\n }),\n );\n}\n\n/**\n * Resolves the calls an exact-execution caveat pins. The same calls arrive\n * as an `ExactExecutionBatch` of the individual calls, or as one\n * `ExactExecution` of the account's own `execute()`.\n *\n * @param caveat - The exact-execution caveat.\n * @param delegator - The Money Account.\n * @param config - The whitelist config.\n * @returns The pinned calls, or `undefined` if the caveat is not an\n * exact-execution caveat or its terms are not canonical.\n */\nexport function getExactExecutions(\n caveat: Caveat,\n delegator: Hex,\n config: MfaWhitelistConfig,\n): Execution[] | undefined {\n const { ExactExecutionBatchEnforcer, ExactExecutionEnforcer } =\n config.contracts;\n\n if (isSameHex(caveat.enforcer, ExactExecutionBatchEnforcer)) {\n const { executions } = decodeExactExecutionBatchTerms(caveat.terms);\n const reencoded = createExactExecutionBatchTerms({ executions });\n return isSameHex(reencoded, caveat.terms) ? executions : undefined;\n }\n\n if (isSameHex(caveat.enforcer, ExactExecutionEnforcer)) {\n // Packed terms: everything after the target and value is the calldata.\n const { execution } = decodeExactExecutionTerms(caveat.terms);\n return unwrapExecute(execution, delegator);\n }\n\n return undefined;\n}\n\n/**\n * Gets the amount of a Money Account vault deposit:\n * `mUSD.approve(boringVault, amount)` followed by\n * `teller.deposit(mUSD, amount, minimumMint, 0x0)` of the same vault, with\n * no native value. Funds never leave the user: mUSD in the Money Account\n * becomes vault shares owned by the Money Account.\n *\n * @param executions - The calls.\n * @param config - The whitelist config.\n * @returns The deposit amount, or `undefined` if the calls are not a vault\n * deposit.\n */\nfunction getVaultDepositAmount(\n executions: Execution[],\n config: MfaWhitelistConfig,\n): bigint | undefined {\n if (executions.length !== 2) {\n return undefined;\n }\n\n const [approve, deposit] = executions;\n if (approve.value !== 0n || deposit.value !== 0n) {\n return undefined;\n }\n if (!isSameHex(approve.target, config.musdTokenAddress)) {\n return undefined;\n }\n\n const approveArgs = decodeCall(approve.callData, ERC20_APPROVE_SELECTOR, [\n 'address',\n 'uint256',\n ]);\n if (!approveArgs) {\n return undefined;\n }\n const [spender, approveAmount] = approveArgs as [Hex, bigint];\n\n const vault = config.vaults.find(({ boringVault }) =>\n isSameHex(boringVault, spender),\n );\n if (!vault || !isSameHex(deposit.target, vault.tellerAddress)) {\n return undefined;\n }\n\n const depositArgs = decodeCall(deposit.callData, TELLER_DEPOSIT_SELECTOR, [\n 'address',\n 'uint256',\n 'uint256',\n 'address',\n ]);\n if (!depositArgs) {\n return undefined;\n }\n const [asset, depositAmount, , referral] = depositArgs as [\n Hex,\n bigint,\n bigint,\n Hex,\n ];\n\n return isSameHex(asset, config.musdTokenAddress) &&\n depositAmount === approveAmount &&\n depositAmount > 0n &&\n isSameHex(referral, ZERO_ADDRESS)\n ? depositAmount\n : undefined;\n}\n\n/**\n * Checks whether calls are a Money Account vault deposit, as described in\n * {@link getVaultDepositAmount}.\n *\n * @param executions - The calls.\n * @param config - The whitelist config.\n * @returns Whether the calls are a vault deposit.\n */\nexport function isVaultDeposit(\n executions: Execution[],\n config: MfaWhitelistConfig,\n): boolean {\n return getVaultDepositAmount(executions, config) !== undefined;\n}\n\n/**\n * Checks whether calls are a MetaMask Rewards claim:\n * `mUSD.receiveWithAuthorization(from, delegator, amount, ...)` (EIP-3009)\n * followed by a vault deposit of the same `amount`, with no native value.\n * The voucher can only pay the Money Account itself, and the deposit only\n * turns what it received into vault shares owned by the Money Account, so\n * funds only come in.\n *\n * @param executions - The calls.\n * @param delegator - The Money Account.\n * @param config - The whitelist config.\n * @returns Whether the calls are a Rewards claim.\n */\nexport function isRewardsClaim(\n executions: Execution[],\n delegator: Hex,\n config: MfaWhitelistConfig,\n): boolean {\n const [receive, ...deposit] = executions;\n if (\n !receive ||\n receive.value !== 0n ||\n !isSameHex(receive.target, config.musdTokenAddress)\n ) {\n return false;\n }\n\n const receiveArgs = decodeCall(\n receive.callData,\n RECEIVE_WITH_AUTHORIZATION_SELECTOR,\n ['address', 'address', 'uint256', 'uint256', 'uint256', 'bytes32', 'bytes'],\n );\n if (!receiveArgs) {\n return false;\n }\n const [, to, amount] = receiveArgs as [Hex, Hex, bigint];\n\n return (\n isSameHex(to, delegator) &&\n getVaultDepositAmount(deposit, config) === amount\n );\n}\n"]}
|
|
@@ -11,9 +11,16 @@ type PersonalMessageContext = {
|
|
|
11
11
|
*
|
|
12
12
|
* Whitelisted are the CHOMP authentication message, the CHOMP successor
|
|
13
13
|
* association message, the card provider sign-in message and the Rewards
|
|
14
|
-
* binding message, each with a fresh timestamp. At worst, signing them
|
|
15
|
-
* Account to another CHOMP profile or
|
|
16
|
-
* service but not move funds.
|
|
14
|
+
* binding message, each with a fresh timestamp. At worst, signing them
|
|
15
|
+
* without MFA lets someone tie the Money Account to another CHOMP profile or
|
|
16
|
+
* Rewards subscription, which can deny service but not move funds.
|
|
17
|
+
*
|
|
18
|
+
* Also whitelisted is the Rewards claim proof, which has an expiry instead of
|
|
19
|
+
* an issue time: it must not have expired and must expire within
|
|
20
|
+
* `maxMessageAge` plus one minute. Signing it leads to a payout, so the proof
|
|
21
|
+
* must name the Money Account as both the earning address and `Pay to`. That
|
|
22
|
+
* keeps the payout in the Money Account as long as the Rewards backend pays
|
|
23
|
+
* to the signed `Pay to`, which it does but this check can't enforce.
|
|
17
24
|
*
|
|
18
25
|
* @param message - The message as hex-encoded UTF-8.
|
|
19
26
|
* @param context - The request context.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"personal-message.d.ts","sourceRoot":"","sources":["../src/personal-message.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"personal-message.d.ts","sourceRoot":"","sources":["../src/personal-message.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AA0ErE,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,kBAAkB,CAAC;IAC3B,aAAa,EAAE,MAAM,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AA8JF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,sBAAsB,GAC9B,cAAc,CAyChB"}
|
package/dist/personal-message.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { hexToNumber } from '@metamask/utils';
|
|
2
|
+
import { CLAIM_PROOF_CLOCK_SKEW } from './constants.js';
|
|
2
3
|
import { decodeUtf8, isFresh, isSameHex, requireMfa, whitelisted, } from './utils.js';
|
|
3
4
|
/** Signed by `MoneyAccountUpgradeController` to associate the address with CHOMP. */
|
|
4
5
|
const CHOMP_AUTHENTICATION_PATTERN = /^CHOMP Authentication (?<timestamp>\d{13})$/u;
|
|
@@ -37,6 +38,18 @@ const CHOMP_SUCCESSOR_PATTERN = new RegExp([
|
|
|
37
38
|
'- urn:chomp:purpose:ASSOCIATE_SUCCESSOR',
|
|
38
39
|
'- urn:chomp:predecessor:(?<resourcePredecessor>0x[0-9a-fA-F]{40})$',
|
|
39
40
|
].join('\n'), 'u');
|
|
41
|
+
/**
|
|
42
|
+
* The Rewards backend's claim proof challenge, signed by the address that
|
|
43
|
+
* earned the rewards. The earning address is its `namespace:address` key.
|
|
44
|
+
*/
|
|
45
|
+
const REWARDS_CLAIM_PROOF_PATTERN = new RegExp([
|
|
46
|
+
'^MetaMask Rewards - authorize a claim of mUSD earnings',
|
|
47
|
+
'Earning address: eip155:(?<earningAddress>0x[0-9a-fA-F]{40})',
|
|
48
|
+
'Amount: \\d+ mUSD base units',
|
|
49
|
+
'Pay to: (?<payTo>0x[0-9a-fA-F]{40})',
|
|
50
|
+
'Claim intent: \\S+',
|
|
51
|
+
'Expires: (?<expires>\\S+)$',
|
|
52
|
+
].join('\n'), 'u');
|
|
40
53
|
/**
|
|
41
54
|
* Parses an ISO 8601 timestamp in the exact form `Date.prototype.toISOString`
|
|
42
55
|
* produces.
|
|
@@ -137,14 +150,46 @@ function getChompSuccessorRequirement(groups, { address, config, maxMessageAge,
|
|
|
137
150
|
}
|
|
138
151
|
return whitelisted('chomp-successor-association');
|
|
139
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Checks the Rewards claim proof. It only proves the Money Account earned the
|
|
155
|
+
* rewards and asks for them to be paid to itself. The payout stays in the
|
|
156
|
+
* Money Account as long as the Rewards backend pays to the signed `Pay to`.
|
|
157
|
+
*
|
|
158
|
+
* @param groups - The named groups of {@link REWARDS_CLAIM_PROOF_PATTERN}.
|
|
159
|
+
* @param context - The request context.
|
|
160
|
+
* @param context.address - The Money Account address.
|
|
161
|
+
* @param context.maxMessageAge - The maximum message age in milliseconds.
|
|
162
|
+
* @param context.now - The current time in milliseconds.
|
|
163
|
+
* @returns Whether MFA is required.
|
|
164
|
+
*/
|
|
165
|
+
function getRewardsClaimProofRequirement(groups, { address, maxMessageAge, now }) {
|
|
166
|
+
if (!isSameHex(groups.earningAddress, address) ||
|
|
167
|
+
!isSameHex(groups.payTo, address)) {
|
|
168
|
+
return requireMfa('Rewards claim proof is not for the Money Account');
|
|
169
|
+
}
|
|
170
|
+
const expires = parseIsoTimestamp(groups.expires);
|
|
171
|
+
if (expires === undefined ||
|
|
172
|
+
now > expires ||
|
|
173
|
+
expires - now > maxMessageAge + CLAIM_PROOF_CLOCK_SKEW) {
|
|
174
|
+
return requireMfa('Rewards claim proof is expired or expires too late');
|
|
175
|
+
}
|
|
176
|
+
return whitelisted('rewards-claim-proof');
|
|
177
|
+
}
|
|
140
178
|
/**
|
|
141
179
|
* Determines whether a personal message requires MFA.
|
|
142
180
|
*
|
|
143
181
|
* Whitelisted are the CHOMP authentication message, the CHOMP successor
|
|
144
182
|
* association message, the card provider sign-in message and the Rewards
|
|
145
|
-
* binding message, each with a fresh timestamp. At worst, signing them
|
|
146
|
-
* Account to another CHOMP profile or
|
|
147
|
-
* service but not move funds.
|
|
183
|
+
* binding message, each with a fresh timestamp. At worst, signing them
|
|
184
|
+
* without MFA lets someone tie the Money Account to another CHOMP profile or
|
|
185
|
+
* Rewards subscription, which can deny service but not move funds.
|
|
186
|
+
*
|
|
187
|
+
* Also whitelisted is the Rewards claim proof, which has an expiry instead of
|
|
188
|
+
* an issue time: it must not have expired and must expire within
|
|
189
|
+
* `maxMessageAge` plus one minute. Signing it leads to a payout, so the proof
|
|
190
|
+
* must name the Money Account as both the earning address and `Pay to`. That
|
|
191
|
+
* keeps the payout in the Money Account as long as the Rewards backend pays
|
|
192
|
+
* to the signed `Pay to`, which it does but this check can't enforce.
|
|
148
193
|
*
|
|
149
194
|
* @param message - The message as hex-encoded UTF-8.
|
|
150
195
|
* @param context - The request context.
|
|
@@ -171,6 +216,10 @@ export function getPersonalMessageMfaRequirement(message, context) {
|
|
|
171
216
|
? whitelisted('rewards-binding')
|
|
172
217
|
: requireMfa('Rewards binding timestamp is not fresh');
|
|
173
218
|
}
|
|
219
|
+
const claimProof = REWARDS_CLAIM_PROOF_PATTERN.exec(text)?.groups;
|
|
220
|
+
if (claimProof) {
|
|
221
|
+
return getRewardsClaimProofRequirement(claimProof, context);
|
|
222
|
+
}
|
|
174
223
|
const chompSuccessor = CHOMP_SUCCESSOR_PATTERN.exec(text)?.groups;
|
|
175
224
|
if (chompSuccessor) {
|
|
176
225
|
return getChompSuccessorRequirement(chompSuccessor, context);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"personal-message.js","sourceRoot":"","sources":["../src/personal-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAI9C,OAAO,EACL,UAAU,EACV,OAAO,EACP,SAAS,EACT,UAAU,EACV,WAAW,GACZ,MAAM,YAAY,CAAC;AAEpB,qFAAqF;AACrF,MAAM,4BAA4B,GAChC,8CAA8C,CAAC;AAEjD,0EAA0E;AAC1E,MAAM,uBAAuB,GAC3B,mGAAmG,CAAC;AAEtG,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,IAAI,MAAM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AAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAC9D,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC;YACzD,CAAC,CAAC,WAAW,CAAC,sBAAsB,CAAC;YACrC,CAAC,CAAC,UAAU,CAAC,6CAA6C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,OAAO,GAAG,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAC3D,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,CAAC;YACzC,OAAO,UAAU,CAAC,8CAA8C,CAAC,CAAC;QACpE,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC;YAC3D,CAAC,CAAC,WAAW,CAAC,iBAAiB,CAAC;YAChC,CAAC,CAAC,UAAU,CAAC,wCAAwC,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,cAAc,GAAG,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAClE,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,4BAA4B,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAC/D,CAAC;IAED,MAAM,UAAU,GAAG,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAC3D,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,wBAAwB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;IAED,OAAO,UAAU,CAAC,6CAA6C,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { MfaRequirement, MfaWhitelistConfig } from './types.js';\nimport {\n decodeUtf8,\n isFresh,\n isSameHex,\n requireMfa,\n whitelisted,\n} from './utils.js';\n\n/** Signed by `MoneyAccountUpgradeController` to associate the address with CHOMP. */\nconst CHOMP_AUTHENTICATION_PATTERN =\n /^CHOMP Authentication (?<timestamp>\\d{13})$/u;\n\n/** Signed by Mobile's `RewardsController.registerMoneyAccountBinding`. */\nconst REWARDS_BINDING_PATTERN =\n /^metamask-rewards:money-account-binding:[^:\\s]+:(?<address>0x[0-9a-f]{40}):(?<timestamp>\\d{13})$/u;\n\n/** Signed by Mobile's `CardController.linkMoneyAccountCard`. */\nconst CARD_SIGN_IN_PATTERN = new RegExp(\n [\n '^(?<domain>\\\\S+) wants you to sign in with your Ethereum account:',\n '(?<address>0x[0-9a-fA-F]{40})',\n '',\n 'Prove address ownership',\n '',\n 'URI: (?<uri>\\\\S+)',\n 'Version: 1',\n 'Chain ID: (?<chainId>\\\\d+)',\n 'Nonce: \\\\S+',\n 'Issued At: (?<issuedAt>\\\\S+)',\n 'Expiration Time: (?<expirationTime>\\\\S+)$',\n ].join('\\n'),\n 'u',\n);\n\n/** Signed by `MoneyAccountUpgradeController` to link the MPC Money Account to the account it replaces. */\nconst CHOMP_SUCCESSOR_PATTERN = new RegExp(\n [\n '^(?<domain>\\\\S+) wants you to sign in with your Ethereum account:',\n '(?<address>0x[0-9a-fA-F]{40})',\n '',\n 'Associate (?<successor>0x[0-9a-fA-F]{40}) as the successor of (?<predecessor>0x[0-9a-fA-F]{40}) for CHOMP Money Account migration\\\\.',\n '',\n 'URI: (?<uri>\\\\S+)',\n 'Version: 1',\n 'Chain ID: (?<chainId>\\\\d+)',\n 'Nonce: \\\\S+',\n 'Issued At: (?<issuedAt>\\\\S+)',\n 'Expiration Time: (?<expirationTime>\\\\S+)',\n 'Request ID: \\\\S+',\n 'Resources:',\n '- urn:chomp:profile:\\\\S+',\n '- urn:chomp:purpose:ASSOCIATE_SUCCESSOR',\n '- urn:chomp:predecessor:(?<resourcePredecessor>0x[0-9a-fA-F]{40})$',\n ].join('\\n'),\n 'u',\n);\n\ntype PersonalMessageContext = {\n address: Hex;\n config: MfaWhitelistConfig;\n maxMessageAge: number;\n now: number;\n};\n\n/**\n * Parses an ISO 8601 timestamp in the exact form `Date.prototype.toISOString`\n * produces.\n *\n * @param value - The timestamp.\n * @returns The time in milliseconds, or `undefined` if the value isn't in\n * that form.\n */\nfunction parseIsoTimestamp(value: string): number | undefined {\n const time = Date.parse(value);\n if (Number.isNaN(time) || new Date(time).toISOString() !== value) {\n return undefined;\n }\n return time;\n}\n\n/**\n * Whether a sign-in message was issued recently and has not expired.\n *\n * @param groups - The named groups, with `issuedAt` and `expirationTime`.\n * @param now - The current time in milliseconds.\n * @param maxMessageAge - The maximum message age in milliseconds.\n * @returns Whether the message is fresh.\n */\nfunction isFreshSignIn(\n groups: Record<string, string>,\n now: number,\n maxMessageAge: number,\n): boolean {\n const issuedAt = parseIsoTimestamp(groups.issuedAt);\n const expirationTime = parseIsoTimestamp(groups.expirationTime);\n return (\n issuedAt !== undefined &&\n expirationTime !== undefined &&\n isFresh(issuedAt, now, maxMessageAge) &&\n expirationTime > issuedAt &&\n expirationTime - issuedAt <= maxMessageAge &&\n now <= expirationTime\n );\n}\n\n/**\n * Checks the card provider sign-in message. It only proves ownership of the\n * address; funding the card needs a separate approval, which requires MFA.\n *\n * @param groups - The named groups of {@link CARD_SIGN_IN_PATTERN}.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @param context.maxMessageAge - The maximum message age in milliseconds.\n * @param context.now - The current time in milliseconds.\n * @returns Whether MFA is required.\n */\nfunction getCardSignInRequirement(\n groups: Record<string, string>,\n { address, config, maxMessageAge, now }: PersonalMessageContext,\n): MfaRequirement {\n const { cardSignInDomain } = config;\n if (!cardSignInDomain || groups.domain !== cardSignInDomain) {\n return requireMfa('Sign-in message is not for the card sign-in domain');\n }\n if (groups.uri !== `https://${cardSignInDomain}`) {\n return requireMfa('Sign-in message URI does not match its domain');\n }\n if (!isSameHex(groups.address, address)) {\n return requireMfa('Sign-in message is not for the Money Account');\n }\n if (groups.chainId !== String(hexToNumber(config.chainId))) {\n return requireMfa('Sign-in message is not for the Money Account chain');\n }\n\n if (!isFreshSignIn(groups, now, maxMessageAge)) {\n return requireMfa('Sign-in message is expired or not fresh');\n }\n\n return whitelisted('card-sign-in');\n}\n\n/**\n * Checks the CHOMP message that links the MPC Money Account, as successor, to\n * the Money Account it replaces. Moving the funds needs the predecessor's own\n * signature, so this only links the two addresses.\n *\n * @param groups - The named groups of {@link CHOMP_SUCCESSOR_PATTERN}.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @param context.maxMessageAge - The maximum message age in milliseconds.\n * @param context.now - The current time in milliseconds.\n * @returns Whether MFA is required.\n */\nfunction getChompSuccessorRequirement(\n groups: Record<string, string>,\n { address, config, maxMessageAge, now }: PersonalMessageContext,\n): MfaRequirement {\n const { chompSignInDomain } = config;\n if (!chompSignInDomain || groups.domain !== chompSignInDomain) {\n return requireMfa('Sign-in message is not for the CHOMP domain');\n }\n if (groups.uri !== `https://${chompSignInDomain}/`) {\n return requireMfa('Sign-in message URI does not match its domain');\n }\n if (\n !isSameHex(groups.address, address) ||\n !isSameHex(groups.successor, address)\n ) {\n return requireMfa('Sign-in message is not for the Money Account');\n }\n if (!isSameHex(groups.predecessor, groups.resourcePredecessor)) {\n return requireMfa('Sign-in message names two predecessors');\n }\n if (groups.chainId !== String(hexToNumber(config.chainId))) {\n return requireMfa('Sign-in message is not for the Money Account chain');\n }\n if (!isFreshSignIn(groups, now, maxMessageAge)) {\n return requireMfa('Sign-in message is expired or not fresh');\n }\n\n return whitelisted('chomp-successor-association');\n}\n\n/**\n * Determines whether a personal message requires MFA.\n *\n * Whitelisted are the CHOMP authentication message, the CHOMP successor\n * association message, the card provider sign-in message and the Rewards\n * binding message, each with a fresh timestamp. At worst, signing them without MFA lets someone tie the Money\n * Account to another CHOMP profile or Rewards subscription, which can deny\n * service but not move funds.\n *\n * @param message - The message as hex-encoded UTF-8.\n * @param context - The request context.\n * @returns Whether MFA is required.\n */\nexport function getPersonalMessageMfaRequirement(\n message: unknown,\n context: PersonalMessageContext,\n): MfaRequirement {\n const text = decodeUtf8(message);\n if (text === undefined) {\n return requireMfa('Message is not hex-encoded UTF-8');\n }\n\n const { address, maxMessageAge, now } = context;\n\n const chomp = CHOMP_AUTHENTICATION_PATTERN.exec(text)?.groups;\n if (chomp) {\n return isFresh(Number(chomp.timestamp), now, maxMessageAge)\n ? whitelisted('chomp-authentication')\n : requireMfa('CHOMP authentication timestamp is not fresh');\n }\n\n const rewards = REWARDS_BINDING_PATTERN.exec(text)?.groups;\n if (rewards) {\n if (!isSameHex(rewards.address, address)) {\n return requireMfa('Rewards binding is not for the Money Account');\n }\n return isFresh(Number(rewards.timestamp), now, maxMessageAge)\n ? whitelisted('rewards-binding')\n : requireMfa('Rewards binding timestamp is not fresh');\n }\n\n const chompSuccessor = CHOMP_SUCCESSOR_PATTERN.exec(text)?.groups;\n if (chompSuccessor) {\n return getChompSuccessorRequirement(chompSuccessor, context);\n }\n\n const cardSignIn = CARD_SIGN_IN_PATTERN.exec(text)?.groups;\n if (cardSignIn) {\n return getCardSignInRequirement(cardSignIn, context);\n }\n\n return requireMfa('Message does not match a whitelisted format');\n}\n"]}
|
|
1
|
+
{"version":3,"file":"personal-message.js","sourceRoot":"","sources":["../src/personal-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,EACL,UAAU,EACV,OAAO,EACP,SAAS,EACT,UAAU,EACV,WAAW,GACZ,MAAM,YAAY,CAAC;AAEpB,qFAAqF;AACrF,MAAM,4BAA4B,GAChC,8CAA8C,CAAC;AAEjD,0EAA0E;AAC1E,MAAM,uBAAuB,GAC3B,mGAAmG,CAAC;AAEtG,gEAAgE;AAChE,MAAM,o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S,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,CAAC;YACzC,OAAO,UAAU,CAAC,8CAA8C,CAAC,CAAC;QACpE,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC;YAC3D,CAAC,CAAC,WAAW,CAAC,iBAAiB,CAAC;YAChC,CAAC,CAAC,UAAU,CAAC,wCAAwC,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,UAAU,GAAG,2BAA2B,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAClE,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,+BAA+B,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,cAAc,GAAG,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAClE,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,4BAA4B,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAC/D,CAAC;IAED,MAAM,UAAU,GAAG,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAC3D,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,wBAAwB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;IAED,OAAO,UAAU,CAAC,6CAA6C,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { CLAIM_PROOF_CLOCK_SKEW } from './constants.js';\nimport type { MfaRequirement, MfaWhitelistConfig } from './types.js';\nimport {\n decodeUtf8,\n isFresh,\n isSameHex,\n requireMfa,\n whitelisted,\n} from './utils.js';\n\n/** Signed by `MoneyAccountUpgradeController` to associate the address with CHOMP. */\nconst CHOMP_AUTHENTICATION_PATTERN =\n /^CHOMP Authentication (?<timestamp>\\d{13})$/u;\n\n/** Signed by Mobile's `RewardsController.registerMoneyAccountBinding`. */\nconst REWARDS_BINDING_PATTERN =\n /^metamask-rewards:money-account-binding:[^:\\s]+:(?<address>0x[0-9a-f]{40}):(?<timestamp>\\d{13})$/u;\n\n/** Signed by Mobile's `CardController.linkMoneyAccountCard`. */\nconst CARD_SIGN_IN_PATTERN = new RegExp(\n [\n '^(?<domain>\\\\S+) wants you to sign in with your Ethereum account:',\n '(?<address>0x[0-9a-fA-F]{40})',\n '',\n 'Prove address ownership',\n '',\n 'URI: (?<uri>\\\\S+)',\n 'Version: 1',\n 'Chain ID: (?<chainId>\\\\d+)',\n 'Nonce: \\\\S+',\n 'Issued At: (?<issuedAt>\\\\S+)',\n 'Expiration Time: (?<expirationTime>\\\\S+)$',\n ].join('\\n'),\n 'u',\n);\n\n/** Signed by `MoneyAccountUpgradeController` to link the MPC Money Account to the account it replaces. */\nconst CHOMP_SUCCESSOR_PATTERN = new RegExp(\n [\n '^(?<domain>\\\\S+) wants you to sign in with your Ethereum account:',\n '(?<address>0x[0-9a-fA-F]{40})',\n '',\n 'Associate (?<successor>0x[0-9a-fA-F]{40}) as the successor of (?<predecessor>0x[0-9a-fA-F]{40}) for CHOMP Money Account migration\\\\.',\n '',\n 'URI: (?<uri>\\\\S+)',\n 'Version: 1',\n 'Chain ID: (?<chainId>\\\\d+)',\n 'Nonce: \\\\S+',\n 'Issued At: (?<issuedAt>\\\\S+)',\n 'Expiration Time: (?<expirationTime>\\\\S+)',\n 'Request ID: \\\\S+',\n 'Resources:',\n '- urn:chomp:profile:\\\\S+',\n '- urn:chomp:purpose:ASSOCIATE_SUCCESSOR',\n '- urn:chomp:predecessor:(?<resourcePredecessor>0x[0-9a-fA-F]{40})$',\n ].join('\\n'),\n 'u',\n);\n\n/**\n * The Rewards backend's claim proof challenge, signed by the address that\n * earned the rewards. The earning address is its `namespace:address` key.\n */\nconst REWARDS_CLAIM_PROOF_PATTERN = new RegExp(\n [\n '^MetaMask Rewards - authorize a claim of mUSD earnings',\n 'Earning address: eip155:(?<earningAddress>0x[0-9a-fA-F]{40})',\n 'Amount: \\\\d+ mUSD base units',\n 'Pay to: (?<payTo>0x[0-9a-fA-F]{40})',\n 'Claim intent: \\\\S+',\n 'Expires: (?<expires>\\\\S+)$',\n ].join('\\n'),\n 'u',\n);\n\ntype PersonalMessageContext = {\n address: Hex;\n config: MfaWhitelistConfig;\n maxMessageAge: number;\n now: number;\n};\n\n/**\n * Parses an ISO 8601 timestamp in the exact form `Date.prototype.toISOString`\n * produces.\n *\n * @param value - The timestamp.\n * @returns The time in milliseconds, or `undefined` if the value isn't in\n * that form.\n */\nfunction parseIsoTimestamp(value: string): number | undefined {\n const time = Date.parse(value);\n if (Number.isNaN(time) || new Date(time).toISOString() !== value) {\n return undefined;\n }\n return time;\n}\n\n/**\n * Whether a sign-in message was issued recently and has not expired.\n *\n * @param groups - The named groups, with `issuedAt` and `expirationTime`.\n * @param now - The current time in milliseconds.\n * @param maxMessageAge - The maximum message age in milliseconds.\n * @returns Whether the message is fresh.\n */\nfunction isFreshSignIn(\n groups: Record<string, string>,\n now: number,\n maxMessageAge: number,\n): boolean {\n const issuedAt = parseIsoTimestamp(groups.issuedAt);\n const expirationTime = parseIsoTimestamp(groups.expirationTime);\n return (\n issuedAt !== undefined &&\n expirationTime !== undefined &&\n isFresh(issuedAt, now, maxMessageAge) &&\n expirationTime > issuedAt &&\n expirationTime - issuedAt <= maxMessageAge &&\n now <= expirationTime\n );\n}\n\n/**\n * Checks the card provider sign-in message. It only proves ownership of the\n * address; funding the card needs a separate approval, which requires MFA.\n *\n * @param groups - The named groups of {@link CARD_SIGN_IN_PATTERN}.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @param context.maxMessageAge - The maximum message age in milliseconds.\n * @param context.now - The current time in milliseconds.\n * @returns Whether MFA is required.\n */\nfunction getCardSignInRequirement(\n groups: Record<string, string>,\n { address, config, maxMessageAge, now }: PersonalMessageContext,\n): MfaRequirement {\n const { cardSignInDomain } = config;\n if (!cardSignInDomain || groups.domain !== cardSignInDomain) {\n return requireMfa('Sign-in message is not for the card sign-in domain');\n }\n if (groups.uri !== `https://${cardSignInDomain}`) {\n return requireMfa('Sign-in message URI does not match its domain');\n }\n if (!isSameHex(groups.address, address)) {\n return requireMfa('Sign-in message is not for the Money Account');\n }\n if (groups.chainId !== String(hexToNumber(config.chainId))) {\n return requireMfa('Sign-in message is not for the Money Account chain');\n }\n\n if (!isFreshSignIn(groups, now, maxMessageAge)) {\n return requireMfa('Sign-in message is expired or not fresh');\n }\n\n return whitelisted('card-sign-in');\n}\n\n/**\n * Checks the CHOMP message that links the MPC Money Account, as successor, to\n * the Money Account it replaces. Moving the funds needs the predecessor's own\n * signature, so this only links the two addresses.\n *\n * @param groups - The named groups of {@link CHOMP_SUCCESSOR_PATTERN}.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.config - The whitelist config.\n * @param context.maxMessageAge - The maximum message age in milliseconds.\n * @param context.now - The current time in milliseconds.\n * @returns Whether MFA is required.\n */\nfunction getChompSuccessorRequirement(\n groups: Record<string, string>,\n { address, config, maxMessageAge, now }: PersonalMessageContext,\n): MfaRequirement {\n const { chompSignInDomain } = config;\n if (!chompSignInDomain || groups.domain !== chompSignInDomain) {\n return requireMfa('Sign-in message is not for the CHOMP domain');\n }\n if (groups.uri !== `https://${chompSignInDomain}/`) {\n return requireMfa('Sign-in message URI does not match its domain');\n }\n if (\n !isSameHex(groups.address, address) ||\n !isSameHex(groups.successor, address)\n ) {\n return requireMfa('Sign-in message is not for the Money Account');\n }\n if (!isSameHex(groups.predecessor, groups.resourcePredecessor)) {\n return requireMfa('Sign-in message names two predecessors');\n }\n if (groups.chainId !== String(hexToNumber(config.chainId))) {\n return requireMfa('Sign-in message is not for the Money Account chain');\n }\n if (!isFreshSignIn(groups, now, maxMessageAge)) {\n return requireMfa('Sign-in message is expired or not fresh');\n }\n\n return whitelisted('chomp-successor-association');\n}\n\n/**\n * Checks the Rewards claim proof. It only proves the Money Account earned the\n * rewards and asks for them to be paid to itself. The payout stays in the\n * Money Account as long as the Rewards backend pays to the signed `Pay to`.\n *\n * @param groups - The named groups of {@link REWARDS_CLAIM_PROOF_PATTERN}.\n * @param context - The request context.\n * @param context.address - The Money Account address.\n * @param context.maxMessageAge - The maximum message age in milliseconds.\n * @param context.now - The current time in milliseconds.\n * @returns Whether MFA is required.\n */\nfunction getRewardsClaimProofRequirement(\n groups: Record<string, string>,\n { address, maxMessageAge, now }: PersonalMessageContext,\n): MfaRequirement {\n if (\n !isSameHex(groups.earningAddress, address) ||\n !isSameHex(groups.payTo, address)\n ) {\n return requireMfa('Rewards claim proof is not for the Money Account');\n }\n\n const expires = parseIsoTimestamp(groups.expires);\n if (\n expires === undefined ||\n now > expires ||\n expires - now > maxMessageAge + CLAIM_PROOF_CLOCK_SKEW\n ) {\n return requireMfa('Rewards claim proof is expired or expires too late');\n }\n\n return whitelisted('rewards-claim-proof');\n}\n\n/**\n * Determines whether a personal message requires MFA.\n *\n * Whitelisted are the CHOMP authentication message, the CHOMP successor\n * association message, the card provider sign-in message and the Rewards\n * binding message, each with a fresh timestamp. At worst, signing them\n * without MFA lets someone tie the Money Account to another CHOMP profile or\n * Rewards subscription, which can deny service but not move funds.\n *\n * Also whitelisted is the Rewards claim proof, which has an expiry instead of\n * an issue time: it must not have expired and must expire within\n * `maxMessageAge` plus one minute. Signing it leads to a payout, so the proof\n * must name the Money Account as both the earning address and `Pay to`. That\n * keeps the payout in the Money Account as long as the Rewards backend pays\n * to the signed `Pay to`, which it does but this check can't enforce.\n *\n * @param message - The message as hex-encoded UTF-8.\n * @param context - The request context.\n * @returns Whether MFA is required.\n */\nexport function getPersonalMessageMfaRequirement(\n message: unknown,\n context: PersonalMessageContext,\n): MfaRequirement {\n const text = decodeUtf8(message);\n if (text === undefined) {\n return requireMfa('Message is not hex-encoded UTF-8');\n }\n\n const { address, maxMessageAge, now } = context;\n\n const chomp = CHOMP_AUTHENTICATION_PATTERN.exec(text)?.groups;\n if (chomp) {\n return isFresh(Number(chomp.timestamp), now, maxMessageAge)\n ? whitelisted('chomp-authentication')\n : requireMfa('CHOMP authentication timestamp is not fresh');\n }\n\n const rewards = REWARDS_BINDING_PATTERN.exec(text)?.groups;\n if (rewards) {\n if (!isSameHex(rewards.address, address)) {\n return requireMfa('Rewards binding is not for the Money Account');\n }\n return isFresh(Number(rewards.timestamp), now, maxMessageAge)\n ? whitelisted('rewards-binding')\n : requireMfa('Rewards binding timestamp is not fresh');\n }\n\n const claimProof = REWARDS_CLAIM_PROOF_PATTERN.exec(text)?.groups;\n if (claimProof) {\n return getRewardsClaimProofRequirement(claimProof, context);\n }\n\n const chompSuccessor = CHOMP_SUCCESSOR_PATTERN.exec(text)?.groups;\n if (chompSuccessor) {\n return getChompSuccessorRequirement(chompSuccessor, context);\n }\n\n const cardSignIn = CARD_SIGN_IN_PATTERN.exec(text)?.groups;\n if (cardSignIn) {\n return getCardSignInRequirement(cardSignIn, context);\n }\n\n return requireMfa('Message does not match a whitelisted format');\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -97,7 +97,7 @@ export type MoneyAccountSignatureRequest = {
|
|
|
97
97
|
/**
|
|
98
98
|
* The whitelist rule a payload matched.
|
|
99
99
|
*/
|
|
100
|
-
export type MfaWhitelistRule = 'chomp-authentication' | 'card-sign-in' | 'chomp-successor-association' | 'rewards-binding' | 'eip7702-authorization' | 'vault-standing-delegation' | 'vault-deposit-delegation';
|
|
100
|
+
export type MfaWhitelistRule = 'chomp-authentication' | 'card-sign-in' | 'chomp-successor-association' | 'rewards-binding' | 'rewards-claim-proof' | 'eip7702-authorization' | 'vault-standing-delegation' | 'vault-deposit-delegation' | 'rewards-claim-delegation';
|
|
101
101
|
/**
|
|
102
102
|
* Whether a signature request requires MFA. A whitelisted request names the
|
|
103
103
|
* rule it matched; any other request gives the reason it isn't whitelisted.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{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EAAE,qBAAqB,CAAC;IAC9B,iCAAiC;IACjC,OAAO,EAAE,GAAG,CAAC;IACb,kEAAkE;IAClE,OAAO,EAAE,GAAG,CAAC;CACd,GACD;IACE,MAAM,EAAE,eAAe,CAAC;IACxB,iCAAiC;IACjC,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAC5B,wDAAwD;IACxD,IAAI,EAAE,OAAO,CAAC;CACf,GACD;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,iCAAiC;IACjC,OAAO,EAAE,GAAG,CAAC;IACb,aAAa,EAAE,oBAAoB,CAAC;CACrC,CAAC;AAEN;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,sBAAsB,GACtB,cAAc,GACd,6BAA6B,GAC7B,iBAAiB,GACjB,qBAAqB,GACrB,uBAAuB,GACvB,2BAA2B,GAC3B,0BAA0B,GAC1B,0BAA0B,CAAC;AAE/B;;;GAGG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,WAAW,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,gBAAgB,CAAA;CAAE,GAC9C;IAAE,WAAW,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1C;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,kDAAkD;IAClD,OAAO,EAAE,GAAG,CAAC;IACb,gCAAgC;IAChC,IAAI,EAAE,GAAG,CAAC;IACV,mCAAmC;IACnC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,kEAAkE;IAClE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\n/**\n * The Delegation Framework contracts the whitelist pins. The keys match the\n * contract names in `@metamask/delegation-deployments`, so a deployment entry\n * (e.g. `DELEGATOR_CONTRACTS['1.3.0'][143]`) can be passed as is.\n */\nexport type DelegationFrameworkContracts = {\n /** The EIP-712 `verifyingContract` of every delegation. */\n DelegationManager: Hex;\n /** The only implementation an EIP-7702 authorization may delegate to. */\n EIP7702StatelessDeleGatorImpl: Hex;\n ERC20TransferAmountEnforcer: Hex;\n ExactExecutionBatchEnforcer: Hex;\n ExactExecutionEnforcer: Hex;\n LimitedCallsEnforcer: Hex;\n RedeemerEnforcer: Hex;\n TimestampEnforcer: Hex;\n ValueLteEnforcer: Hex;\n};\n\n/**\n * A Money Account vault. The base vault comes from the\n * `moneyAccountVaultConfig` remote feature flag, the premium vault from\n * `moneyAccountPremiumVaultConfig`, and each vault's adapter from the CHOMP\n * service details.\n */\nexport type MoneyAccountVault = {\n /** The boring vault, which is also the vault share token (e.g. vmUSD). */\n boringVault: Hex;\n /** The teller that mints vault shares on deposit. */\n tellerAddress: Hex;\n /** The Veda vault adapter, the only redeemer of standing delegations. */\n vedaVaultAdapterAddress: Hex;\n};\n\n/**\n * The pinned configuration a payload must match to be whitelisted. Anything\n * that doesn't match requires MFA, so a configuration change fails closed.\n */\nexport type MfaWhitelistConfig = {\n /** The Money Account chain, e.g. `0x8f` for Monad. */\n chainId: Hex;\n /** The Delegation Framework contracts on that chain. */\n contracts: DelegationFrameworkContracts;\n /** The mUSD token. */\n musdTokenAddress: Hex;\n /** CHOMP's auto-deposit delegate, the delegate of standing delegations. */\n chompDelegateAddress: Hex;\n /** The vaults deposits may go into. */\n vaults: MoneyAccountVault[];\n /**\n * The domain of the card provider sign-in message (the MetaMask universal\n * link host). The sign-in message requires MFA when this is not set.\n */\n cardSignInDomain?: string;\n /**\n * The domain of the CHOMP sign-in message that links the MPC Money Account\n * to the account it replaces. The message requires MFA when this is not set.\n */\n chompSignInDomain?: string;\n /**\n * How far, in milliseconds, a signed message's timestamp may be from the\n * current time. Defaults to five minutes.\n */\n maxMessageAge?: number;\n};\n\n/**\n * An EIP-7702 authorization tuple, as passed to the keyring's\n * `signEip7702Authorization`.\n */\nexport type Eip7702Authorization = [\n chainId: number,\n contractAddress: Hex,\n nonce: number,\n];\n\n/**\n * A signature request for the Money Account, mirroring the three signing\n * methods of the Money keyring.\n */\nexport type MoneyAccountSignatureRequest =\n | {\n method: 'signPersonalMessage';\n /** The Money Account address. */\n address: Hex;\n /** The message as hex-encoded UTF-8, as passed to the keyring. */\n message: Hex;\n }\n | {\n method: 'signTypedData';\n /** The Money Account address. */\n address: Hex;\n version: 'V1' | 'V3' | 'V4';\n /** The typed data, as an object or as a JSON string. */\n data: unknown;\n }\n | {\n method: 'signEip7702Authorization';\n /** The Money Account address. */\n address: Hex;\n authorization: Eip7702Authorization;\n };\n\n/**\n * The whitelist rule a payload matched.\n */\nexport type MfaWhitelistRule =\n | 'chomp-authentication'\n | 'card-sign-in'\n | 'chomp-successor-association'\n | 'rewards-binding'\n | 'eip7702-authorization'\n | 'vault-standing-delegation'\n | 'vault-deposit-delegation';\n\n/**\n * Whether a signature request requires MFA. A whitelisted request names the\n * rule it matched; any other request gives the reason it isn't whitelisted.\n */\nexport type MfaRequirement =\n | { mfaRequired: false; rule: MfaWhitelistRule }\n | { mfaRequired: true; reason: string };\n\n/**\n * What the signer itself knows about the signature: its key, the hash it is\n * asked to sign, its pinned config and its clock. None of this may be taken\n * from the signature request, which is what is being checked.\n */\nexport type MfaSignerContext = {\n /** The Money Account whose key signs the hash. */\n address: Hex;\n /** The 32-byte hash to sign. */\n hash: Hex;\n /** The pinned whitelist config. */\n config: MfaWhitelistConfig;\n /** The current time in milliseconds. Defaults to `Date.now()`. */\n now?: number;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from '@metamask/utils';\n\n/**\n * The Delegation Framework contracts the whitelist pins. The keys match the\n * contract names in `@metamask/delegation-deployments`, so a deployment entry\n * (e.g. `DELEGATOR_CONTRACTS['1.3.0'][143]`) can be passed as is.\n */\nexport type DelegationFrameworkContracts = {\n /** The EIP-712 `verifyingContract` of every delegation. */\n DelegationManager: Hex;\n /** The only implementation an EIP-7702 authorization may delegate to. */\n EIP7702StatelessDeleGatorImpl: Hex;\n ERC20TransferAmountEnforcer: Hex;\n ExactExecutionBatchEnforcer: Hex;\n ExactExecutionEnforcer: Hex;\n LimitedCallsEnforcer: Hex;\n RedeemerEnforcer: Hex;\n TimestampEnforcer: Hex;\n ValueLteEnforcer: Hex;\n};\n\n/**\n * A Money Account vault. The base vault comes from the\n * `moneyAccountVaultConfig` remote feature flag, the premium vault from\n * `moneyAccountPremiumVaultConfig`, and each vault's adapter from the CHOMP\n * service details.\n */\nexport type MoneyAccountVault = {\n /** The boring vault, which is also the vault share token (e.g. vmUSD). */\n boringVault: Hex;\n /** The teller that mints vault shares on deposit. */\n tellerAddress: Hex;\n /** The Veda vault adapter, the only redeemer of standing delegations. */\n vedaVaultAdapterAddress: Hex;\n};\n\n/**\n * The pinned configuration a payload must match to be whitelisted. Anything\n * that doesn't match requires MFA, so a configuration change fails closed.\n */\nexport type MfaWhitelistConfig = {\n /** The Money Account chain, e.g. `0x8f` for Monad. */\n chainId: Hex;\n /** The Delegation Framework contracts on that chain. */\n contracts: DelegationFrameworkContracts;\n /** The mUSD token. */\n musdTokenAddress: Hex;\n /** CHOMP's auto-deposit delegate, the delegate of standing delegations. */\n chompDelegateAddress: Hex;\n /** The vaults deposits may go into. */\n vaults: MoneyAccountVault[];\n /**\n * The domain of the card provider sign-in message (the MetaMask universal\n * link host). The sign-in message requires MFA when this is not set.\n */\n cardSignInDomain?: string;\n /**\n * The domain of the CHOMP sign-in message that links the MPC Money Account\n * to the account it replaces. The message requires MFA when this is not set.\n */\n chompSignInDomain?: string;\n /**\n * How far, in milliseconds, a signed message's timestamp may be from the\n * current time. Defaults to five minutes.\n */\n maxMessageAge?: number;\n};\n\n/**\n * An EIP-7702 authorization tuple, as passed to the keyring's\n * `signEip7702Authorization`.\n */\nexport type Eip7702Authorization = [\n chainId: number,\n contractAddress: Hex,\n nonce: number,\n];\n\n/**\n * A signature request for the Money Account, mirroring the three signing\n * methods of the Money keyring.\n */\nexport type MoneyAccountSignatureRequest =\n | {\n method: 'signPersonalMessage';\n /** The Money Account address. */\n address: Hex;\n /** The message as hex-encoded UTF-8, as passed to the keyring. */\n message: Hex;\n }\n | {\n method: 'signTypedData';\n /** The Money Account address. */\n address: Hex;\n version: 'V1' | 'V3' | 'V4';\n /** The typed data, as an object or as a JSON string. */\n data: unknown;\n }\n | {\n method: 'signEip7702Authorization';\n /** The Money Account address. */\n address: Hex;\n authorization: Eip7702Authorization;\n };\n\n/**\n * The whitelist rule a payload matched.\n */\nexport type MfaWhitelistRule =\n | 'chomp-authentication'\n | 'card-sign-in'\n | 'chomp-successor-association'\n | 'rewards-binding'\n | 'rewards-claim-proof'\n | 'eip7702-authorization'\n | 'vault-standing-delegation'\n | 'vault-deposit-delegation'\n | 'rewards-claim-delegation';\n\n/**\n * Whether a signature request requires MFA. A whitelisted request names the\n * rule it matched; any other request gives the reason it isn't whitelisted.\n */\nexport type MfaRequirement =\n | { mfaRequired: false; rule: MfaWhitelistRule }\n | { mfaRequired: true; reason: string };\n\n/**\n * What the signer itself knows about the signature: its key, the hash it is\n * asked to sign, its pinned config and its clock. None of this may be taken\n * from the signature request, which is what is being checked.\n */\nexport type MfaSignerContext = {\n /** The Money Account whose key signs the hash. */\n address: Hex;\n /** The 32-byte hash to sign. */\n hash: Hex;\n /** The pinned whitelist config. */\n config: MfaWhitelistConfig;\n /** The current time in milliseconds. Defaults to `Date.now()`. */\n now?: number;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/money-account-mfa-whitelist",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Determines which Money Account signature requests can be signed without MFA approval",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@metamask/abi-utils": "^3.0.0",
|
|
56
56
|
"@metamask/delegation-core": "^2.2.1",
|
|
57
|
-
"@metamask/utils": "^12.0
|
|
57
|
+
"@metamask/utils": "^12.1.0",
|
|
58
58
|
"@noble/hashes": "^1.8.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
@@ -63,14 +63,15 @@
|
|
|
63
63
|
"@metamask/delegation-deployments": "^1.4.0",
|
|
64
64
|
"@metamask/messenger": "^3.0.0",
|
|
65
65
|
"@metamask/money-account-utils": "^2.2.1",
|
|
66
|
-
"@metamask/transaction-controller": "^74.
|
|
66
|
+
"@metamask/transaction-controller": "^74.6.0",
|
|
67
67
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
68
68
|
"ethers": "^6.17.0",
|
|
69
|
-
"rimraf": "^6.1.
|
|
69
|
+
"rimraf": "^6.1.4",
|
|
70
70
|
"typedoc": "^0.28.20",
|
|
71
71
|
"typedoc-plugin-missing-exports": "^4.1.4",
|
|
72
72
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
73
|
-
"
|
|
73
|
+
"vite": "^8.4.0",
|
|
74
|
+
"vitest": "5.1.2"
|
|
74
75
|
},
|
|
75
76
|
"engines": {
|
|
76
77
|
"node": "^22.14.0 || ^24 || ^26"
|