-
Notifications
You must be signed in to change notification settings - Fork 542
/
transaction-insight.ts
155 lines (141 loc) · 4.53 KB
/
transaction-insight.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
PermissionSpecificationBuilder,
PermissionType,
EndowmentGetterParams,
ValidPermissionSpecification,
PermissionValidatorConstraint,
PermissionConstraint,
CaveatSpecificationConstraint,
Caveat,
} from '@metamask/permission-controller';
import { SnapCaveatType } from '@metamask/snaps-utils';
import {
assert,
hasProperty,
isObject,
isPlainObject,
Json,
NonEmptyArray,
} from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import { SnapEndowments } from './enum';
const permissionName = SnapEndowments.TransactionInsight;
type TransactionInsightEndowmentSpecification = ValidPermissionSpecification<{
permissionType: PermissionType.Endowment;
targetKey: typeof permissionName;
endowmentGetter: (_options?: EndowmentGetterParams) => undefined;
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
validator: PermissionValidatorConstraint;
}>;
/**
* `endowment:transaction-insight` returns nothing; it is intended to be used as a flag
* by the extension to detect whether the snap has the capability to show information on the transaction confirmation screen.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the transaction-insight endowment.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.Endowment,
any,
TransactionInsightEndowmentSpecification
> = (_builderOptions?: unknown) => {
return {
permissionType: PermissionType.Endowment,
targetKey: permissionName,
allowedCaveats: [SnapCaveatType.TransactionOrigin],
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,
validator: ({ caveats }) => {
if (
(caveats !== null && caveats?.length > 1) ||
(caveats?.length === 1 &&
caveats[0].type !== SnapCaveatType.TransactionOrigin)
) {
throw ethErrors.rpc.invalidParams({
message: `Expected a single "${SnapCaveatType.TransactionOrigin}" caveat.`,
});
}
},
};
};
export const transactionInsightEndowmentBuilder = Object.freeze({
targetKey: permissionName,
specificationBuilder,
} as const);
/**
* Validates the type of the caveat value.
*
* @param caveat - The caveat to validate.
* @throws If the caveat value is invalid.
*/
function validateCaveat(caveat: Caveat<string, any>): void {
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a plain object.',
});
}
const { value } = caveat;
assert(
typeof value === 'boolean',
'Expected caveat value to have type "boolean"',
);
}
/**
* Map a raw value from the `initialPermissions` to a caveat specification.
* Note that this function does not do any validation, that's handled by the
* PermissionsController when the permission is requested.
*
* @param value - The raw value from the `initialPermissions`.
* @returns The caveat specification.
*/
export function getTransactionInsightCaveatMapper(
value: Json,
): Pick<PermissionConstraint, 'caveats'> {
if (
!value ||
!isObject(value) ||
(isObject(value) && Object.keys(value).length === 0)
) {
return { caveats: null };
}
return {
caveats: [
{
type: SnapCaveatType.TransactionOrigin,
value:
hasProperty(value, 'allowTransactionOrigin') &&
value.allowTransactionOrigin,
},
],
};
}
/**
* Getter function to get the transaction origin caveat from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the transaction origin caveat from.
* @returns The transaction origin, or `null` if the permission does not have a
* transaction origin caveat.
*/
export function getTransactionOriginCaveat(
permission?: PermissionConstraint,
): boolean | null {
if (!permission?.caveats) {
return null;
}
assert(permission.caveats.length === 1);
assert(permission.caveats[0].type === SnapCaveatType.TransactionOrigin);
const caveat = permission.caveats[0] as Caveat<string, boolean>;
return caveat.value ?? null;
}
export const transactionInsightCaveatSpecifications: Record<
SnapCaveatType.TransactionOrigin,
CaveatSpecificationConstraint
> = {
[SnapCaveatType.TransactionOrigin]: Object.freeze({
type: SnapCaveatType.TransactionOrigin,
validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),
}),
};