-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexactCalldata.ts
More file actions
56 lines (51 loc) · 1.76 KB
/
Copy pathexactCalldata.ts
File metadata and controls
56 lines (51 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { BytesLike } from '@metamask/utils';
import {
defaultOptions,
prepareResult,
type EncodingOptions,
type ResultValue,
} from '../returns';
import type { Hex } from '../types';
/**
* Terms for configuring an ExactCalldata caveat.
*/
export type ExactCalldataTerms = {
/** The expected calldata to match against. */
calldata: BytesLike;
};
/**
* Creates terms for an ExactCalldata caveat that ensures the provided execution calldata
* matches exactly the expected calldata.
*
* @param terms - The terms for the ExactCalldata caveat.
* @param encodingOptions - The encoding options for the result.
* @returns The terms as the calldata itself.
* @throws Error if the `calldata` is invalid.
*/
export function createExactCalldataTerms(
terms: ExactCalldataTerms,
encodingOptions?: EncodingOptions<'hex'>,
): Hex;
export function createExactCalldataTerms(
terms: ExactCalldataTerms,
encodingOptions: EncodingOptions<'bytes'>,
): Uint8Array;
/**
* Creates terms for an ExactCalldata caveat that ensures the provided execution calldata
* matches exactly the expected calldata.
* @param terms - The terms for the ExactCalldata caveat.
* @param encodingOptions - The encoding options for the result.
* @returns The terms as the calldata itself.
* @throws Error if the `calldata` is invalid.
*/
export function createExactCalldataTerms(
terms: ExactCalldataTerms,
encodingOptions: EncodingOptions<ResultValue> = defaultOptions,
): Hex | Uint8Array {
const { calldata } = terms;
if (typeof calldata === 'string' && !calldata.startsWith('0x')) {
throw new Error('Invalid calldata: must be a hex string starting with 0x');
}
// For exact calldata, the terms are simply the expected calldata
return prepareResult(calldata, encodingOptions);
}