Skip to content

Commit 3165c85

Browse files
committed
Function call scope no longer accepts native token value, unless explicitly configured.
1 parent 4e6277b commit 3165c85

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

packages/smart-accounts-kit/src/caveatBuilder/scope/functionCallScope.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import type { AllowedTargetsBuilderConfig } from '../allowedTargetsBuilder';
66
import { createCaveatBuilder } from '../coreCaveatBuilder';
77
import type { CoreCaveatBuilder } from '../coreCaveatBuilder';
88
import type { ExactCalldataBuilderConfig } from '../exactCalldataBuilder';
9+
import { ValueLteBuilderConfig } from '../valueLteBuilder';
910

1011
type FunctionCallScopeBaseConfig = {
1112
type: 'functionCall';
13+
allowedCalldata?: AllowedCalldataBuilderConfig[];
14+
exactCalldata?: ExactCalldataBuilderConfig;
15+
valueLte?: ValueLteBuilderConfig;
1216
};
1317

1418
export type FunctionCallScopeConfig = FunctionCallScopeBaseConfig &
1519
AllowedTargetsBuilderConfig &
16-
AllowedMethodsBuilderConfig & {
17-
allowedCalldata?: AllowedCalldataBuilderConfig[];
18-
exactCalldata?: ExactCalldataBuilderConfig;
19-
};
20+
AllowedMethodsBuilderConfig;
2021

2122
const isFunctionCallConfig = (
2223
config: FunctionCallScopeConfig,
@@ -49,9 +50,12 @@ export function createFunctionCallCaveatBuilder(
4950
);
5051
}
5152

53+
const valueLteConfig = config.valueLte || { maxValue: 0n };
54+
5255
const caveatBuilder = createCaveatBuilder(environment)
5356
.addCaveat('allowedTargets', { targets })
54-
.addCaveat('allowedMethods', { selectors });
57+
.addCaveat('allowedMethods', { selectors })
58+
.addCaveat('valueLte', valueLteConfig);
5559

5660
if (allowedCalldata && allowedCalldata.length > 0) {
5761
allowedCalldata.forEach((calldataConfig) => {

packages/smart-accounts-kit/test/caveatBuilder/scope/functionCallScope.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('createFunctionCallCaveatBuilder', () => {
1414
AllowedMethodsEnforcer: randomAddress(),
1515
AllowedCalldataEnforcer: randomAddress(),
1616
ExactCalldataEnforcer: randomAddress(),
17+
ValueLteEnforcer: randomAddress(),
1718
},
1819
} as unknown as SmartAccountsEnvironment;
1920

@@ -39,6 +40,11 @@ describe('createFunctionCallCaveatBuilder', () => {
3940
args: '0x',
4041
terms: concat(config.selectors as Hex[]),
4142
},
43+
{
44+
enforcer: environment.caveatEnforcers.ValueLteEnforcer,
45+
args: '0x',
46+
terms: toHex(0n, { size: 32 }),
47+
},
4248
]);
4349
});
4450

@@ -66,6 +72,11 @@ describe('createFunctionCallCaveatBuilder', () => {
6672
args: '0x',
6773
terms: concat(config.selectors as Hex[]),
6874
},
75+
{
76+
enforcer: environment.caveatEnforcers.ValueLteEnforcer,
77+
args: '0x',
78+
terms: toHex(0n, { size: 32 }),
79+
},
6980
{
7081
enforcer: environment.caveatEnforcers.AllowedCalldataEnforcer,
7182
args: '0x',
@@ -101,6 +112,11 @@ describe('createFunctionCallCaveatBuilder', () => {
101112
args: '0x',
102113
terms: concat(config.selectors as Hex[]),
103114
},
115+
{
116+
enforcer: environment.caveatEnforcers.ValueLteEnforcer,
117+
args: '0x',
118+
terms: toHex(0n, { size: 32 }),
119+
},
104120
{
105121
enforcer: environment.caveatEnforcers.ExactCalldataEnforcer,
106122
args: '0x',
@@ -109,6 +125,37 @@ describe('createFunctionCallCaveatBuilder', () => {
109125
]);
110126
});
111127

128+
it('creates a Function Call CaveatBuilder with configured valueLte', () => {
129+
const config: FunctionCallScopeConfig = {
130+
type: 'functionCall',
131+
targets: [randomAddress()],
132+
selectors: ['0x12345678'],
133+
valueLte: { maxValue: 123n },
134+
};
135+
136+
const caveatBuilder = createFunctionCallCaveatBuilder(environment, config);
137+
138+
const caveats = caveatBuilder.build();
139+
140+
expect(caveats).to.deep.equal([
141+
{
142+
enforcer: environment.caveatEnforcers.AllowedTargetsEnforcer,
143+
args: '0x',
144+
terms: concat(config.targets),
145+
},
146+
{
147+
enforcer: environment.caveatEnforcers.AllowedMethodsEnforcer,
148+
args: '0x',
149+
terms: concat(config.selectors as Hex[]),
150+
},
151+
{
152+
enforcer: environment.caveatEnforcers.ValueLteEnforcer,
153+
args: '0x',
154+
terms: toHex(123n, { size: 32 }),
155+
},
156+
]);
157+
});
158+
112159
it('throws an error for invalid configuration', () => {
113160
const config = {
114161
type: 'functionCall',

shared/config/base.tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
"$schema": "https://json.schemastore.org/tsconfig",
33
"display": "Default",
44
"compilerOptions": {
5+
"files": ["src/**/*.ts"],
56
"composite": false,
67
"declaration": true,
78
"declarationMap": true,
89
"esModuleInterop": true,
910
"incremental": false,
1011
"isolatedModules": true,
11-
"lib": [
12-
"dom",
13-
"esnext"
14-
],
12+
"lib": ["dom", "esnext"],
1513
"module": "ESNext",
1614
"moduleResolution": "Bundler",
1715
"moduleDetection": "force",
@@ -20,6 +18,6 @@
2018
"skipLibCheck": true,
2119
"strict": true,
2220
"target": "ES2022",
23-
"removeComments": true,
21+
"removeComments": true
2422
}
25-
}
23+
}

0 commit comments

Comments
 (0)