Skip to content

Commit 17b50f6

Browse files
committed
Update all of the e2e tests to match the new interface (in reality we're mostly just creating delegation object literals).
1 parent b8fa1d2 commit 17b50f6

32 files changed

Lines changed: 1031 additions & 783 deletions

packages/delegation-toolkit/src/caveatBuilder/blockNumberBuilder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,3 @@ export const blockNumberBuilder = (
6161
args: '0x',
6262
};
6363
};
64-
65-
type A = { type: 'a' } | { type: 'b' };

packages/delegation-toolkit/src/caveatBuilder/coreCaveatBuilder.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import {
33
allowedCalldata,
44
allowedCalldataBuilder,
55
} from './allowedCalldataBuilder';
6-
import {
7-
allowedMethods,
8-
allowedMethodsBuilder,
9-
AllowedMethodsBuilderConfig,
10-
} from './allowedMethodsBuilder';
6+
import { allowedMethods, allowedMethodsBuilder } from './allowedMethodsBuilder';
117
import { allowedTargets, allowedTargetsBuilder } from './allowedTargetsBuilder';
128
import {
139
argsEqualityCheck,
@@ -133,24 +129,25 @@ type CoreCaveatMap = {
133129
*/
134130
export type CoreCaveatBuilder = CaveatBuilder<CoreCaveatMap>;
135131

136-
type ExtractCaveatMapType<T> = T extends CaveatBuilder<infer U> ? U : never;
132+
type ExtractCaveatMapType<TCaveatBuilder extends CaveatBuilder<any>> =
133+
TCaveatBuilder extends CaveatBuilder<infer TCaveatMap> ? TCaveatMap : never;
137134
type ExtractedCoreMap = ExtractCaveatMapType<CoreCaveatBuilder>;
138135

139136
export type CaveatConfigurations = {
140-
[K in keyof ExtractedCoreMap]: {
141-
type: K;
142-
} & Parameters<ExtractedCoreMap[K]>[1];
137+
[TType in keyof ExtractedCoreMap]: {
138+
type: TType;
139+
} & Parameters<ExtractedCoreMap[TType]>[1];
143140
}[keyof ExtractedCoreMap];
144141

145142
export type CaveatConfiguration<
146-
T extends CaveatBuilder<any>,
147-
CaveatMap = ExtractCaveatMapType<T>,
143+
TCaveatBuilder extends CaveatBuilder<any>,
144+
CaveatMap = ExtractCaveatMapType<TCaveatBuilder>,
148145
> =
149146
CaveatMap extends Record<string, (...args: any[]) => any>
150147
? {
151-
[K in keyof CaveatMap]: {
152-
type: K;
153-
} & Parameters<CaveatMap[K]>[1];
148+
[TType in keyof CaveatMap]: {
149+
type: TType;
150+
} & Parameters<CaveatMap[TType]>[1];
154151
}[keyof CaveatMap]
155152
: never;
156153

@@ -159,9 +156,9 @@ export type CoreCaveatConfiguration = CaveatConfiguration<CoreCaveatBuilder>;
159156
/**
160157
* Creates a caveat builder with all core caveat types pre-configured.
161158
*
162-
* @param environment - The DeleGator environment configuration
163-
* @param config - Optional configuration for the caveat builder
164-
* @returns A fully configured CoreCaveatBuilder instance with all core caveat types
159+
* @param environment - The DeleGator environment configuration.
160+
* @param config - Optional configuration for the caveat builder.
161+
* @returns A fully configured CoreCaveatBuilder instance with all core caveat types.
165162
*/
166163
export const createCaveatBuilder = (
167164
environment: DeleGatorEnvironment,

packages/delegation-toolkit/src/caveatBuilder/resolveCaveats.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { createCaveatBuilderFromScope, type ScopeConfig } from './scope';
2-
3-
import type { CoreCaveatConfiguration } from './coreCaveatBuilder';
41
import type { CaveatBuilder } from './caveatBuilder';
5-
import { Caveat, DeleGatorEnvironment } from '../types';
2+
import type { CoreCaveatConfiguration } from './coreCaveatBuilder';
3+
import { createCaveatBuilderFromScope, type ScopeConfig } from './scope';
4+
import type { Caveat, DeleGatorEnvironment } from '../types';
65

76
export type Caveats = CaveatBuilder | (Caveat | CoreCaveatConfiguration)[];
87

@@ -33,13 +32,13 @@ export const resolveCaveats = ({
3332
caveats.forEach((caveat) => {
3433
try {
3534
if ('type' in caveat) {
36-
const { type, ...config } = caveat as CoreCaveatConfiguration;
35+
const { type, ...config } = caveat;
3736
scopeCaveatBuilder.addCaveat(type, config);
3837
} else {
39-
scopeCaveatBuilder.addCaveat(caveat as Caveat);
38+
scopeCaveatBuilder.addCaveat(caveat);
4039
}
4140
} catch (error) {
42-
throw new Error(`Invalid caveat: ${error}`);
41+
throw new Error(`Invalid caveat: ${(error as Error).message}`);
4342
}
4443
});
4544
}

packages/delegation-toolkit/test/caveatBuilder/resolveCaveats.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { expect } from 'chai';
22

3-
import { resolveCaveats } from '../../src/caveatBuilder/resolveCaveats';
43
import { CaveatBuilder } from '../../src/caveatBuilder/caveatBuilder';
5-
import {
6-
CoreCaveatConfiguration,
7-
createCaveatBuilder,
8-
} from '../../src/caveatBuilder/coreCaveatBuilder';
9-
import type { Caveat, DeleGatorEnvironment } from '../../src/types';
4+
import type { CoreCaveatConfiguration } from '../../src/caveatBuilder/coreCaveatBuilder';
5+
import { createCaveatBuilder } from '../../src/caveatBuilder/coreCaveatBuilder';
6+
import { resolveCaveats } from '../../src/caveatBuilder/resolveCaveats';
107
import type { ScopeConfig } from '../../src/caveatBuilder/scope';
8+
import type { Caveat, DeleGatorEnvironment } from '../../src/types';
119
import { randomAddress } from '../utils';
1210

1311
describe('resolveCaveats', () => {

packages/delegator-e2e/test/caveats/allowedCalldata.test.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { beforeEach, test, expect } from 'vitest';
22
import {
3-
createCaveatBuilder,
4-
createDelegation,
53
createExecution,
4+
Delegation,
65
Implementation,
6+
ROOT_AUTHORITY,
77
toMetaMaskSmartAccount,
88
type MetaMaskSmartAccount,
99
} from '@metamask/delegation-toolkit';
10+
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils';
1011
import {
1112
encodeExecutionCalldatas,
1213
encodePermissionContexts,
@@ -139,14 +140,22 @@ const runTest_expectSuccess = async (
139140
) => {
140141
const { environment } = aliceSmartAccount;
141142

142-
const delegation = createDelegation({
143-
to: bobSmartAccount.address,
144-
from: aliceSmartAccount.address,
145-
caveats: caveats.reduce((builder, caveat) => {
146-
builder.addCaveat('allowedCalldata', caveat.from, caveat.calldata);
147-
return builder;
148-
}, createCaveatBuilder(environment)),
149-
});
143+
const delegation: Delegation = {
144+
delegate: bobSmartAccount.address,
145+
delegator: aliceSmartAccount.address,
146+
authority: ROOT_AUTHORITY,
147+
caveats: caveats
148+
.reduce((builder, caveat) => {
149+
builder.addCaveat('allowedCalldata', {
150+
startIndex: caveat.from,
151+
value: caveat.calldata,
152+
});
153+
return builder;
154+
}, createCaveatBuilder(environment))
155+
.build(),
156+
salt: '0x',
157+
signature: '0x',
158+
};
150159

151160
const signedDelegation = {
152161
...delegation,
@@ -207,14 +216,22 @@ const runTest_expectFailure = async (
207216
caveats: { from: number; calldata: Hex }[],
208217
expectedError: string,
209218
) => {
210-
const delegation = createDelegation({
211-
to: bobSmartAccount.address,
212-
from: aliceSmartAccount.address,
213-
caveats: caveats.reduce((builder, caveat) => {
214-
builder.addCaveat('allowedCalldata', caveat.from, caveat.calldata);
215-
return builder;
216-
}, createCaveatBuilder(aliceSmartAccount.environment)),
217-
});
219+
const delegation: Delegation = {
220+
delegate: bobSmartAccount.address,
221+
delegator: aliceSmartAccount.address,
222+
authority: ROOT_AUTHORITY,
223+
caveats: caveats
224+
.reduce((builder, caveat) => {
225+
builder.addCaveat('allowedCalldata', {
226+
startIndex: caveat.from,
227+
value: caveat.calldata,
228+
});
229+
return builder;
230+
}, createCaveatBuilder(aliceSmartAccount.environment))
231+
.build(),
232+
salt: '0x',
233+
signature: '0x',
234+
};
218235

219236
const signedDelegation = {
220237
...delegation,

packages/delegator-e2e/test/caveats/allowedMethods.test.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {
55
SINGLE_DEFAULT_MODE,
66
} from '@metamask/delegation-toolkit/utils';
77
import {
8-
createCaveatBuilder,
98
createExecution,
10-
createDelegation,
9+
Delegation,
1110
Implementation,
11+
ROOT_AUTHORITY,
1212
toMetaMaskSmartAccount,
1313
type MetaMaskSmartAccount,
1414
} from '@metamask/delegation-toolkit';
15+
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils';
1516
import {
1617
transport,
1718
gasPrice,
@@ -135,14 +136,16 @@ const runTest_expectSuccess = async (
135136
allowedMethods: (string | AbiFunction | Hex)[],
136137
calledMethod: string,
137138
) => {
138-
const delegation = createDelegation({
139-
to: bobSmartAccount.address,
140-
from: aliceSmartAccount.address,
141-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
142-
'allowedMethods',
143-
allowedMethods,
144-
),
145-
});
139+
const delegation: Delegation = {
140+
delegate: bobSmartAccount.address,
141+
delegator: aliceSmartAccount.address,
142+
authority: ROOT_AUTHORITY,
143+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
144+
.addCaveat('allowedMethods', { selectors: allowedMethods })
145+
.build(),
146+
salt: '0x',
147+
signature: '0x',
148+
};
146149

147150
const signedDelegation = {
148151
...delegation,
@@ -201,14 +204,16 @@ const runTest_expectFailure = async (
201204
calledMethod: string,
202205
expectedError: string,
203206
) => {
204-
const delegation = createDelegation({
205-
to: bobSmartAccount.address,
206-
from: aliceSmartAccount.address,
207-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
208-
'allowedMethods',
209-
allowedMethods,
210-
),
211-
});
207+
const delegation: Delegation = {
208+
delegate: bobSmartAccount.address,
209+
delegator: aliceSmartAccount.address,
210+
authority: ROOT_AUTHORITY,
211+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
212+
.addCaveat('allowedMethods', { selectors: allowedMethods })
213+
.build(),
214+
salt: '0x',
215+
signature: '0x',
216+
};
212217

213218
const signedDelegation = {
214219
...delegation,

packages/delegator-e2e/test/caveats/allowedTargets.test.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {
55
SINGLE_DEFAULT_MODE,
66
} from '@metamask/delegation-toolkit/utils';
77
import {
8-
createCaveatBuilder,
98
createExecution,
10-
createDelegation,
9+
Delegation,
1110
Implementation,
11+
ROOT_AUTHORITY,
1212
toMetaMaskSmartAccount,
1313
type MetaMaskSmartAccount,
1414
} from '@metamask/delegation-toolkit';
15+
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils';
1516
import {
1617
transport,
1718
gasPrice,
@@ -105,14 +106,16 @@ const runTest_expectSuccess = async (
105106
allowedTargets: Hex[],
106107
calledTarget: Hex,
107108
) => {
108-
const delegation = createDelegation({
109-
to: bobSmartAccount.address,
110-
from: aliceSmartAccount.address,
111-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
112-
'allowedTargets',
113-
allowedTargets,
114-
),
115-
});
109+
const delegation: Delegation = {
110+
delegate: bobSmartAccount.address,
111+
delegator: aliceSmartAccount.address,
112+
authority: ROOT_AUTHORITY,
113+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
114+
.addCaveat('allowedTargets', { targets: allowedTargets })
115+
.build(),
116+
salt: '0x',
117+
signature: '0x',
118+
};
116119

117120
const signedDelegation = {
118121
...delegation,
@@ -171,14 +174,16 @@ const runTest_expectFailure = async (
171174
calledTarget: Hex,
172175
expectedError: string,
173176
) => {
174-
const delegation = createDelegation({
175-
to: bobSmartAccount.address,
176-
from: aliceSmartAccount.address,
177-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
178-
'allowedTargets',
179-
allowedTargets,
180-
),
181-
});
177+
const delegation: Delegation = {
178+
delegate: bobSmartAccount.address,
179+
delegator: aliceSmartAccount.address,
180+
authority: ROOT_AUTHORITY,
181+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
182+
.addCaveat('allowedTargets', { targets: allowedTargets })
183+
.build(),
184+
salt: '0x',
185+
signature: '0x',
186+
};
182187

183188
const signedDelegation = {
184189
...delegation,

packages/delegator-e2e/test/caveats/argsEqualityCheck.test.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {
55
SINGLE_DEFAULT_MODE,
66
} from '@metamask/delegation-toolkit/utils';
77
import {
8-
createCaveatBuilder,
98
createExecution,
10-
createDelegation,
9+
Delegation,
1110
Implementation,
11+
ROOT_AUTHORITY,
1212
toMetaMaskSmartAccount,
1313
type MetaMaskSmartAccount,
1414
} from '@metamask/delegation-toolkit';
15+
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils';
1516
import {
1617
transport,
1718
gasPrice,
@@ -129,14 +130,16 @@ test('Bob attempts to redeem the delegation with args when none are expected', a
129130
});
130131

131132
const runTest_expectSuccess = async (expectedArgs: Hex, actualArgs: Hex) => {
132-
const delegation = createDelegation({
133-
to: bobSmartAccount.address,
134-
from: aliceSmartAccount.address,
135-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
136-
'argsEqualityCheck',
137-
expectedArgs,
138-
),
139-
});
133+
const delegation: Delegation = {
134+
delegate: bobSmartAccount.address,
135+
delegator: aliceSmartAccount.address,
136+
authority: ROOT_AUTHORITY,
137+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
138+
.addCaveat('argsEqualityCheck', { expectedArgs })
139+
.build(),
140+
salt: '0x',
141+
signature: '0x',
142+
};
140143

141144
const signedDelegation = {
142145
...delegation,
@@ -197,14 +200,16 @@ const runTest_expectFailure = async (
197200
actualArgs: Hex,
198201
expectedError: string,
199202
) => {
200-
const delegation = createDelegation({
201-
to: bobSmartAccount.address,
202-
from: aliceSmartAccount.address,
203-
caveats: createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
204-
'argsEqualityCheck',
205-
expectedArgs,
206-
),
207-
});
203+
const delegation: Delegation = {
204+
delegate: bobSmartAccount.address,
205+
delegator: aliceSmartAccount.address,
206+
authority: ROOT_AUTHORITY,
207+
caveats: createCaveatBuilder(aliceSmartAccount.environment)
208+
.addCaveat('argsEqualityCheck', { expectedArgs })
209+
.build(),
210+
salt: '0x',
211+
signature: '0x',
212+
};
208213

209214
const signedDelegation = {
210215
...delegation,

0 commit comments

Comments
 (0)