-
Notifications
You must be signed in to change notification settings - Fork 4
/
encryption.ts
398 lines (366 loc) · 10.3 KB
/
encryption.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* eslint-disable no-case-declarations */
import { z } from 'zod';
import {
Amount,
AmountSchema,
EvmAddress,
EvmAddressSchema,
NetworkAddress,
NetworkAddressSchema,
ProfileId,
ProfileIdSchema,
PublicationId,
PublicationIdSchema,
TokenId,
TokenIdSchema,
nonEmptyStringSchema,
} from '../../primitives.js';
import { hasTwoOrMore, Brand, TwoAtLeastArray } from '../../utils.js';
export enum EncryptionProvider {
LIT_PROTOCOL = 'LIT_PROTOCOL',
}
export enum NftContractType {
ERC721 = 'ERC721',
ERC1155 = 'ERC1155',
}
export enum ConditionType {
NFT_OWNERSHIP = 'NFT_OWNERSHIP',
ERC20_OWNERSHIP = 'ERC20_OWNERSHIP',
EOA_OWNERSHIP = 'EOA_OWNERSHIP',
PROFILE_OWNERSHIP = 'PROFILE_OWNERSHIP',
FOLLOW = 'FOLLOW',
COLLECT = 'COLLECT',
ADVANCED_CONTRACT = 'ADVANCED_CONTRACT',
AND = 'AND',
OR = 'OR',
}
export type NftOwnershipCondition = {
type: ConditionType.NFT_OWNERSHIP;
contractType: NftContractType;
contract: NetworkAddress;
tokenIds?: TokenId[];
};
/**
* @private
*/
export function refineNftOwnershipCondition(
condition: NftOwnershipCondition,
ctx: z.RefinementCtx,
) {
if (condition.contractType === NftContractType.ERC1155) {
if (condition.tokenIds === undefined || condition.tokenIds.length === 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'ERC1155 requires at least one token id.',
path: [...ctx.path, 'tokenIds'],
});
}
}
}
/**
* @internal
*/
export const NftOwnershipConditionSchema = z.object({
type: z.literal(ConditionType.NFT_OWNERSHIP),
contract: NetworkAddressSchema,
contractType: z.nativeEnum(NftContractType),
tokenIds: TokenIdSchema.array()
.min(1)
.optional()
.describe(
'A list of token IDs you want to check ownership of. The list is optional for ERC721, ' +
'you MUST provide a list of token IDs for ERC1155.',
),
});
export enum ConditionComparisonOperator {
EQUAL = 'EQUAL',
NOT_EQUAL = 'NOT_EQUAL',
GREATER_THAN = 'GREATER_THAN',
GREATER_THAN_OR_EQUAL = 'GREATER_THAN_OR_EQUAL',
LESS_THAN = 'LESS_THAN',
LESS_THAN_OR_EQUAL = 'LESS_THAN_OR_EQUAL',
}
export type Erc20OwnershipCondition = {
type: ConditionType.ERC20_OWNERSHIP;
amount: Amount;
condition: ConditionComparisonOperator;
};
/**
* @internal
*/
export const Erc20OwnershipConditionSchema = z.object({
type: z.literal(ConditionType.ERC20_OWNERSHIP),
amount: AmountSchema,
condition: z.nativeEnum(ConditionComparisonOperator),
});
export type EoaOwnershipCondition = {
type: ConditionType.EOA_OWNERSHIP;
address: EvmAddress;
};
/**
* @internal
*/
export const EoaOwnershipConditionSchema = z.object({
type: z.literal(ConditionType.EOA_OWNERSHIP),
address: EvmAddressSchema,
});
export type ProfileOwnershipCondition = {
type: ConditionType.PROFILE_OWNERSHIP;
profileId: ProfileId;
};
/**
* @internal
*/
export const ProfileOwnershipConditionSchema = z.object({
type: z.literal(ConditionType.PROFILE_OWNERSHIP),
profileId: ProfileIdSchema,
});
export type FollowCondition = {
type: ConditionType.FOLLOW;
follow: ProfileId;
};
/**
* @internal
*/
export const FollowConditionSchema = z.object({
type: z.literal(ConditionType.FOLLOW),
follow: ProfileIdSchema,
});
export type CollectCondition = {
type: ConditionType.COLLECT;
publicationId: PublicationId;
thisPublication: boolean;
};
/**
* @internal
*/
export const CollectConditionSchema = z.object({
type: z.literal(ConditionType.COLLECT),
publicationId: PublicationIdSchema, // TODO check on-chain publication ID
thisPublication: z.boolean().optional().default(false),
});
export type AdvancedContractCondition = {
type: ConditionType.ADVANCED_CONTRACT;
contract: NetworkAddress;
functionName: string;
abi: string;
params: string[];
comparison: ConditionComparisonOperator;
value: string;
};
/**
* @internal
*/
export const AdvancedContractConditionSchema = z.object({
type: z.literal(ConditionType.ADVANCED_CONTRACT),
contract: NetworkAddressSchema.describe('The contract address and chain id'),
functionName: z.string().min(1).describe('The name of the function you want to call'),
abi: z
.string()
.min(1)
.describe(
'The contract ABI. Has to be in human readable ' +
'single string format containing the signature of the function you want to call. See ' +
'https://docs.ethers.org/v5/api/utils/abi/formats/#abi-formats--human-readable-abi for more info',
),
params: z
.string()
.array()
.describe(
'The parameters to pass to the function. Must be ' +
'exactly matching the function arguments. You *must* pass in the `:userAddress` parameter to ' +
'represent the decrypter address. Any array or tuple arguments, must be stringified JSON arrays.',
),
comparison: z
.nativeEnum(ConditionComparisonOperator)
.describe(
'The comparison operator to use. In case of boolean functions you can only use EQUAL or NOT_EQUAL',
),
value: z
.string()
.regex(/^(true|false|\d{1,70})$/)
.describe(`The comparison value. Accepts 'true', 'false' or a number`),
});
export type SimpleCondition =
| CollectCondition
| AdvancedContractCondition
| EoaOwnershipCondition
| Erc20OwnershipCondition
| FollowCondition
| NftOwnershipCondition
| ProfileOwnershipCondition;
type BaseCondition = {
type: ConditionType;
};
type ComposableConditionSchema<T extends BaseCondition = BaseCondition> = z.ZodObject<
{
type: z.ZodTypeAny;
} & z.ZodRawShape,
z.UnknownKeysParam,
z.ZodTypeAny,
T
>;
export type AndCondition<T extends BaseCondition = SimpleCondition> = {
type: ConditionType.AND;
criteria: TwoAtLeastArray<T>;
};
function andConditionSchema<
Criteria extends [
ComposableConditionSchema,
ComposableConditionSchema,
...ComposableConditionSchema[],
],
>(options: Criteria): ComposableConditionSchema<AndCondition<z.infer<Criteria[number]>>> {
return z.object({
type: z.literal(ConditionType.AND),
criteria: z
.discriminatedUnion('type', options)
.array()
.max(5, 'Should have at most 5 conditions')
.refine(hasTwoOrMore, 'Should have at least 2 conditions'),
});
}
/**
* @internal
*/
export const AndConditionSchema = andConditionSchema([
NftOwnershipConditionSchema,
Erc20OwnershipConditionSchema,
EoaOwnershipConditionSchema,
ProfileOwnershipConditionSchema,
FollowConditionSchema,
CollectConditionSchema,
AdvancedContractConditionSchema,
]);
export type OrCondition<T extends BaseCondition = SimpleCondition> = {
type: ConditionType.OR;
criteria: TwoAtLeastArray<T>;
};
function orConditionSchema<
Criteria extends [
ComposableConditionSchema,
ComposableConditionSchema,
...ComposableConditionSchema[],
],
>(options: Criteria): ComposableConditionSchema<OrCondition<z.infer<Criteria[number]>>> {
return z.object({
type: z.literal(ConditionType.OR),
criteria: z
.discriminatedUnion('type', options)
.array()
.max(5, 'Should have at most 5 conditions')
.refine(hasTwoOrMore, 'Should have at least 2 conditions'),
});
}
/**
* @internal
*/
export const OrConditionSchema = orConditionSchema([
NftOwnershipConditionSchema,
Erc20OwnershipConditionSchema,
EoaOwnershipConditionSchema,
ProfileOwnershipConditionSchema,
FollowConditionSchema,
CollectConditionSchema,
AdvancedContractConditionSchema,
]);
export type AnyCondition =
| SimpleCondition
| AndCondition<SimpleCondition>
| OrCondition<SimpleCondition>;
function refineAnyCondition(condition: AnyCondition, ctx: z.RefinementCtx) {
if (condition.type === ConditionType.AND || condition.type === ConditionType.OR) {
condition.criteria.forEach((c, idx) =>
refineAnyCondition(c, { ...ctx, path: [...ctx.path, 'criteria', idx] }),
);
}
if (condition.type === ConditionType.NFT_OWNERSHIP) {
refineNftOwnershipCondition(condition, ctx);
}
}
export type AccessCondition = OrCondition<AnyCondition>;
/**
* @internal
*/
export const AccessConditionSchema: z.ZodType<AccessCondition, z.ZodTypeDef, object> =
orConditionSchema([
AndConditionSchema,
CollectConditionSchema,
AdvancedContractConditionSchema,
EoaOwnershipConditionSchema,
Erc20OwnershipConditionSchema,
FollowConditionSchema,
NftOwnershipConditionSchema,
OrConditionSchema,
ProfileOwnershipConditionSchema,
]).superRefine((root, ctx): root is AccessCondition => {
root.criteria.forEach((condition, idx) => {
refineAnyCondition(condition, {
...ctx,
path: [...ctx.path, 'criteria', idx],
});
});
return z.NEVER;
});
/**
* A symmetric encryption key.
*/
export type LitEncryptionKey = Brand<string, 'LitEncryptionKey'>;
/**
* @internal
*/
export function toLitEncryptionKey(value: string): LitEncryptionKey {
return value as LitEncryptionKey;
}
/**
* @internal
*/
export const LitEncryptionKeySchema: z.Schema<LitEncryptionKey, z.ZodTypeDef, string> = z
.string()
.describe('A symmetric encryption key.')
.length(368, 'Encryption key should be 368 characters long.')
.transform(toLitEncryptionKey);
/**
* @internal
*/
export const EncryptedPaths = nonEmptyStringSchema(
'An encrypted path is a string of keys separated by . that describe ' +
'a path to a value in a JSON object (e.g. lens.attachments.0.item.url, lens.content).',
)
.array()
.min(1);
export type EncryptedPaths = z.infer<typeof EncryptedPaths>;
export type LitEncryptionStrategy = {
provider: EncryptionProvider;
encryptionKey: LitEncryptionKey;
accessCondition: AccessCondition;
encryptedPaths: string[];
};
/**
* @internal
*/
export const LitEncryptionStrategySchema = z.object(
{
provider: z.literal(EncryptionProvider.LIT_PROTOCOL),
encryptionKey: LitEncryptionKeySchema,
accessCondition: AccessConditionSchema,
encryptedPaths: EncryptedPaths,
},
{
description: 'Publication encryption strategy powered by the LIT Protocol.',
},
);
/**
* The publication encryption strategy.
*
* This is normally populated by the Lens SDK so the vast majority of developers will not need to use this directly.
*/
export type PublicationEncryptionStrategy = LitEncryptionStrategy;
/**
* @internal
*/
export const PublicationEncryptionStrategySchema: z.ZodType<
PublicationEncryptionStrategy,
z.ZodTypeDef,
object
> = z.discriminatedUnion('provider', [LitEncryptionStrategySchema]);