-
Notifications
You must be signed in to change notification settings - Fork 437
/
validations.ts
953 lines (800 loc) · 32.1 KB
/
validations.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
import * as protobufs from "./protobufs";
import { Protocol, UserNameType } from "./protobufs";
import { blake3 } from "@noble/hashes/blake3";
import { err, ok, Result } from "neverthrow";
import { bytesCompare, bytesToBase58, bytesToUtf8String, utf8StringToBytes } from "./bytes";
import { ed25519, eip712 } from "./crypto";
import { HubAsyncResult, HubError, HubResult } from "./errors";
import { getFarcasterTime, toFarcasterTime } from "./time";
import {
makeVerificationAddressClaim,
recreateSolanaClaimMessage,
VerificationAddressClaimSolana,
} from "./verifications";
import { normalize } from "viem/ens";
import { defaultPublicClients, PublicClients } from "./eth/clients";
/** Number of seconds (10 minutes) that is appropriate for clock skew */
export const ALLOWED_CLOCK_SKEW_SECONDS = 10 * 60;
export const FNAME_REGEX = /^[a-z0-9][a-z0-9-]{0,15}$/;
export const HEX_REGEX = /^(0x)?[0-9A-Fa-f]+$/;
export const USERNAME_MAX_LENGTH = 20;
export const EMBEDS_V1_CUTOFF = 73612800; // 5/3/23 00:00 UTC
/**
* CPU intensive validation methods that are used during validations. By default, we use
* pure JS implementations for compatibility, but we can also use native implementations if available.
*/
export type ValidationMethods = {
ed25519_verify: (signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array) => Promise<boolean>;
ed25519_signMessageHash: (hash: Uint8Array, signingKey: Uint8Array) => Promise<Uint8Array>;
blake3_20: (message: Uint8Array) => Uint8Array;
};
/**
* Pure JS implementations. These are used by default, but can be overridden by native implementations.
*/
const pureJSValidationMethods: ValidationMethods = {
ed25519_verify: async (s: Uint8Array, m: Uint8Array, p: Uint8Array) =>
(await ed25519.verifyMessageHashSignature(s, m, p)).unwrapOr(false),
ed25519_signMessageHash: async (h: Uint8Array, s: Uint8Array) =>
(await ed25519.signMessageHash(h, s)).unwrapOr(new Uint8Array([])),
blake3_20: (message: Uint8Array) => blake3(message, { dkLen: 20 }),
};
export const createMessageHash = async (
message?: Uint8Array,
hashScheme?: protobufs.HashScheme,
validationMethods: ValidationMethods = pureJSValidationMethods,
): HubAsyncResult<Uint8Array> => {
if (!message || message.length === 0) {
return err(new HubError("bad_request.validation_failure", "hash is missing"));
}
if (hashScheme !== protobufs.HashScheme.BLAKE3) {
return err(new HubError("bad_request.validation_failure", "unsupported hash scheme"));
}
return ok(validationMethods.blake3_20(message));
};
export const signMessageHash = async (
hash?: Uint8Array,
signingKey?: Uint8Array,
validationMethods: ValidationMethods = pureJSValidationMethods,
): HubAsyncResult<Uint8Array> => {
if (!hash || hash.length === 0) {
return err(new HubError("bad_request.validation_failure", "hash is missing"));
}
if (!signingKey || signingKey.length !== 64) {
return err(new HubError("bad_request.validation_failure", "signingKey is invalid"));
}
return ok(await validationMethods.ed25519_signMessageHash(hash, signingKey));
};
export const verifySignedMessageHash = async (
hash?: Uint8Array,
signature?: Uint8Array,
signer?: Uint8Array,
validationMethods: ValidationMethods = pureJSValidationMethods,
): HubAsyncResult<boolean> => {
if (!hash || hash.length === 0) {
return err(new HubError("bad_request.validation_failure", "hash is missing"));
}
if (!signature || signature.length !== 64) {
return err(new HubError("bad_request.validation_failure", "signature is invalid"));
}
if (!signer || signer.length !== 32) {
return err(new HubError("bad_request.validation_failure", "signer is invalid"));
}
return ok(await validationMethods.ed25519_verify(signature, hash, signer));
};
export const validateMessageHash = (hash?: Uint8Array): HubResult<Uint8Array> => {
if (!hash || hash.length === 0) {
return err(new HubError("bad_request.validation_failure", "hash is missing"));
}
if (hash.length !== 20) {
return err(new HubError("bad_request.validation_failure", "hash must be 20 bytes"));
}
return ok(hash);
};
export const validateCastId = (castId?: protobufs.CastId): HubResult<protobufs.CastId> => {
if (!castId) {
return err(new HubError("bad_request.validation_failure", "castId is missing"));
}
return Result.combineWithAllErrors([validateFid(castId.fid), validateMessageHash(castId.hash)])
.map(() => castId)
.mapErr(
(errs: HubError[]) => new HubError("bad_request.validation_failure", errs.map((e) => e.message).join(", ")),
);
};
export const validateFid = (fid?: number | null): HubResult<number> => {
if (typeof fid !== "number" || fid === 0) {
return err(new HubError("bad_request.validation_failure", "fid is missing"));
}
if (fid < 0) {
return err(new HubError("bad_request.validation_failure", "fid must be positive"));
}
if (!Number.isInteger(fid)) {
return err(new HubError("bad_request.validation_failure", "fid must be an integer"));
}
return ok(fid);
};
export const validateSolAddress = (address?: Uint8Array | null): HubResult<Uint8Array> => {
if (!address || address.length === 0) {
return err(new HubError("bad_request.validation_failure", "solana address is missing"));
}
if (address.length !== 32) {
return err(new HubError("bad_request.validation_failure", "solana address must be 32 bytes"));
}
return ok(address);
};
export const validateSolBlockHash = (blockHash?: Uint8Array | null): HubResult<Uint8Array> => {
if (!blockHash || blockHash.length === 0) {
return err(new HubError("bad_request.validation_failure", "blockHash is missing"));
}
if (blockHash.length !== 32) {
return err(new HubError("bad_request.validation_failure", "blockHash must be 32 bytes"));
}
return ok(blockHash);
};
export const validateEthAddress = (address?: Uint8Array | null): HubResult<Uint8Array> => {
if (!address || address.length === 0) {
return err(new HubError("bad_request.validation_failure", "Ethereum address is missing"));
}
if (address.length !== 20) {
return err(new HubError("bad_request.validation_failure", "Ethereum address must be 20 bytes"));
}
return ok(address);
};
export const validateEthBlockHash = (blockHash?: Uint8Array | null): HubResult<Uint8Array> => {
if (!blockHash || blockHash.length === 0) {
return err(new HubError("bad_request.validation_failure", "blockHash is missing"));
}
if (blockHash.length !== 32) {
return err(new HubError("bad_request.validation_failure", "blockHash must be 32 bytes"));
}
return ok(blockHash);
};
export const validateEd25519PublicKey = (publicKey?: Uint8Array | null): HubResult<Uint8Array> => {
if (!publicKey || publicKey.length === 0) {
return err(new HubError("bad_request.validation_failure", "publicKey is missing"));
}
if (publicKey.length !== 32) {
return err(new HubError("bad_request.validation_failure", "publicKey must be 32 bytes"));
}
return ok(publicKey);
};
export const validateMessage = async (
message: protobufs.Message,
validationMethods: ValidationMethods = pureJSValidationMethods,
publicClients: PublicClients = defaultPublicClients,
): HubAsyncResult<protobufs.Message> => {
// 1. Check the message data
const data = message.data;
if (!data) {
return err(new HubError("bad_request.validation_failure", "data is missing"));
}
const validData = await validateMessageData(data, publicClients);
if (validData.isErr()) {
return err(validData.error);
}
// The hash to verify the signature against. This is either the hash of the data_bytes, or the hash
// of the data field encoded using ts-proto protobuf
const hash = message.hash;
if (!hash) {
return err(new HubError("bad_request.validation_failure", "hash is missing"));
}
// Computed from the data_bytes if set, otherwise from the data
let computedHash;
// 2. If the data_bytes are set, we'll validate signature against that
if (message.dataBytes && message.dataBytes.length > 0) {
if (message.dataBytes.length > 1024) {
return err(new HubError("bad_request.validation_failure", "dataBytes > 1024 bytes"));
}
// 2a. Use the databytes as the hash to check the signature against
computedHash = validationMethods.blake3_20(message.dataBytes);
} else {
// 2b. Use the protobuf encoded data as the hash to check the signature against
computedHash = validationMethods.blake3_20(protobufs.MessageData.encode(data).finish());
}
// 3. Check that the hashScheme and hash are valid
if (message.hashScheme === protobufs.HashScheme.BLAKE3) {
// we have to use bytesCompare, because TypedArrays cannot be compared directly
if (bytesCompare(hash, computedHash) !== 0) {
return err(new HubError("bad_request.validation_failure", "invalid hash"));
}
} else {
return err(new HubError("bad_request.validation_failure", "invalid hashScheme"));
}
// 4. Check that the signatureScheme and signature are valid
const signature = message.signature;
if (!signature) {
return err(new HubError("bad_request.validation_failure", "signature is missing"));
}
// 5. Check that the signer is valid
const signer = message.signer;
if (!signer) {
return err(new HubError("bad_request.validation_failure", "signer is missing"));
}
// 6. Check that the signature is valid
if (message.signatureScheme === protobufs.SignatureScheme.ED25519) {
const signatureIsValid = await validationMethods.ed25519_verify(signature, hash, signer);
if (!signatureIsValid) {
return err(new HubError("bad_request.validation_failure", "invalid signature"));
}
} else {
return err(new HubError("bad_request.validation_failure", "invalid signatureScheme"));
}
return ok(message);
};
export const validateMessageData = async <T extends protobufs.MessageData>(
data: T,
publicClients: PublicClients = defaultPublicClients,
): HubAsyncResult<T> => {
// 1. Validate fid
const validFid = validateFid(data.fid);
if (validFid.isErr()) {
return err(validFid.error);
}
const farcasterTime = getFarcasterTime();
if (farcasterTime.isErr()) {
return err(farcasterTime.error);
}
// 2. Validate timestamp
if (data.timestamp - farcasterTime.value > ALLOWED_CLOCK_SKEW_SECONDS) {
return err(new HubError("bad_request.validation_failure", "timestamp more than 10 mins in the future"));
}
// 3. Validate network
const validNetwork = validateNetwork(data.network);
if (validNetwork.isErr()) {
return err(validNetwork.error);
}
// 4. Validate type
const validType = validateMessageType(data.type);
if (validType.isErr()) {
return err(validType.error);
}
// 5. Validate body
// biome-ignore lint/suspicious/noExplicitAny: legacy from eslint migration
let bodyResult: HubResult<any>;
if (validType.value === protobufs.MessageType.CAST_ADD && !!data.castAddBody) {
// Allow usage of embedsDeprecated if timestamp is before cut-off
const allowEmbedsDeprecated = data.timestamp < EMBEDS_V1_CUTOFF;
bodyResult = validateCastAddBody(data.castAddBody, allowEmbedsDeprecated);
} else if (validType.value === protobufs.MessageType.CAST_REMOVE && !!data.castRemoveBody) {
bodyResult = validateCastRemoveBody(data.castRemoveBody);
} else if (
(validType.value === protobufs.MessageType.REACTION_ADD ||
validType.value === protobufs.MessageType.REACTION_REMOVE) &&
!!data.reactionBody
) {
bodyResult = validateReactionBody(data.reactionBody);
} else if (
(validType.value === protobufs.MessageType.LINK_ADD || validType.value === protobufs.MessageType.LINK_REMOVE) &&
!!data.linkBody
) {
bodyResult = validateLinkBody(data.linkBody);
} else if (validType.value === protobufs.MessageType.USER_DATA_ADD && !!data.userDataBody) {
bodyResult = validateUserDataAddBody(data.userDataBody);
} else if (
validType.value === protobufs.MessageType.VERIFICATION_ADD_ETH_ADDRESS &&
!!data.verificationAddAddressBody
) {
bodyResult = await validateVerificationAddAddressBody(
data.verificationAddAddressBody,
validFid.value,
validNetwork.value,
publicClients,
);
} else if (validType.value === protobufs.MessageType.VERIFICATION_REMOVE && !!data.verificationRemoveBody) {
bodyResult = validateVerificationRemoveBody(data.verificationRemoveBody);
} else if (validType.value === protobufs.MessageType.USERNAME_PROOF && !!data.usernameProofBody) {
bodyResult = validateUsernameProofBody(data.usernameProofBody, data);
} else if (validType.value === protobufs.MessageType.FRAME_ACTION && !!data.frameActionBody) {
bodyResult = validateFrameActionBody(data.frameActionBody);
} else {
return err(new HubError("bad_request.invalid_param", "bodyType is invalid"));
}
if (bodyResult.isErr()) {
return err(bodyResult.error);
}
return ok(data);
};
export const validateVerificationAddSolAddressSignature = async (
body: protobufs.VerificationAddAddressBody,
fid: number,
network: protobufs.FarcasterNetwork,
): HubAsyncResult<Uint8Array> => {
if (body.claimSignature.length !== 64) {
return err(new HubError("bad_request.validation_failure", "claimSignature != 64 bytes"));
}
const reconstructedClaim = makeVerificationAddressClaim(fid, body.address, network, body.blockHash, body.protocol);
if (reconstructedClaim.isErr()) {
return err(reconstructedClaim.error);
}
const fullMessage = recreateSolanaClaimMessage(
reconstructedClaim.value as VerificationAddressClaimSolana,
body.address,
);
const verificationResult = await pureJSValidationMethods.ed25519_verify(
body.claimSignature,
fullMessage,
body.address,
);
if (!verificationResult) {
return err(new HubError("bad_request.validation_failure", "invalid claimSignature"));
}
return ok(body.claimSignature);
};
export const validateVerificationAddEthAddressSignature = async (
body: protobufs.VerificationAddAddressBody,
fid: number,
network: protobufs.FarcasterNetwork,
publicClients: PublicClients = defaultPublicClients,
): HubAsyncResult<Uint8Array> => {
if (body.claimSignature.length > 256) {
return err(new HubError("bad_request.validation_failure", "claimSignature > 256 bytes"));
}
const reconstructedClaim = makeVerificationAddressClaim(
fid,
body.address,
network,
body.blockHash,
Protocol.ETHEREUM,
);
if (reconstructedClaim.isErr()) {
return err(reconstructedClaim.error);
}
const verificationResult = await eip712.verifyVerificationEthAddressClaimSignature(
reconstructedClaim.value,
body.claimSignature,
body.address,
body.verificationType,
body.chainId,
publicClients,
);
if (verificationResult.isErr()) {
return err(verificationResult.error);
}
if (!verificationResult.value) {
return err(new HubError("bad_request.validation_failure", "invalid claimSignature"));
}
return ok(body.claimSignature);
};
export const validateUrl = (url: string): HubResult<string> => {
if (typeof url !== "string") {
return err(new HubError("bad_request.validation_failure", "url must be a string"));
}
const urlBytesResult = utf8StringToBytes(url);
if (urlBytesResult.isErr()) {
return err(new HubError("bad_request.invalid_param", "url must be encodable as utf8"));
}
const urlBytes = urlBytesResult.value;
if (urlBytes.length < 1) {
return err(new HubError("bad_request.invalid_param", "url < 1 byte"));
}
if (urlBytes.length > 256) {
return err(new HubError("bad_request.invalid_param", "url > 256 bytes"));
}
return ok(url);
};
export const validateParent = (parent: protobufs.CastId | string): HubResult<protobufs.CastId | string> => {
if (typeof parent === "string") {
return validateUrl(parent);
} else {
return validateCastId(parent);
}
};
export const validateEmbed = (embed: protobufs.Embed): HubResult<protobufs.Embed> => {
if (embed.url !== undefined) {
return validateUrl(embed.url).map(() => embed);
} else if (embed.castId !== undefined) {
return validateCastId(embed.castId).map(() => embed);
} else {
return err(new HubError("bad_request.validation_failure", "embed must have either url or castId"));
}
};
export const validateCastAddBody = (
body: protobufs.CastAddBody,
allowEmbedsDeprecated = false,
): HubResult<protobufs.CastAddBody> => {
const text = body.text;
if (text === undefined || text === null) {
return err(new HubError("bad_request.validation_failure", "text is missing"));
}
const textUtf8BytesResult = utf8StringToBytes(text);
if (textUtf8BytesResult.isErr()) {
return err(new HubError("bad_request.invalid_param", "text must be encodable as utf8"));
}
const textBytes = textUtf8BytesResult.value;
if (textBytes.length > 320) {
return err(new HubError("bad_request.validation_failure", "text > 320 bytes"));
}
if (body.embeds.length > 2) {
return err(new HubError("bad_request.validation_failure", "embeds > 2"));
}
if (allowEmbedsDeprecated && body.embedsDeprecated.length > 2) {
return err(new HubError("bad_request.validation_failure", "string embeds > 2"));
}
if (!allowEmbedsDeprecated && body.embedsDeprecated.length > 0) {
return err(new HubError("bad_request.validation_failure", "string embeds have been deprecated"));
}
if (body.mentions.length > 10) {
return err(new HubError("bad_request.validation_failure", "mentions > 10"));
}
if (body.mentions.length !== body.mentionsPositions.length) {
return err(new HubError("bad_request.validation_failure", "mentions and mentionsPositions must match"));
}
if (body.embeds.length > 0 && body.embedsDeprecated.length > 0) {
return err(new HubError("bad_request.validation_failure", "cannot use both embeds and string embeds"));
}
if (
body.text.length === 0 &&
body.embeds.length === 0 &&
body.embedsDeprecated.length === 0 &&
body.mentions.length === 0
) {
return err(new HubError("bad_request.validation_failure", "cast is empty"));
}
for (let i = 0; i < body.embeds.length; i++) {
const embed = body.embeds[i];
if (embed === undefined) {
return err(new HubError("bad_request.validation_failure", "embed is missing"));
}
const embedIsValid = validateEmbed(embed);
if (embedIsValid.isErr()) {
return err(embedIsValid.error);
}
}
for (let i = 0; i < body.embedsDeprecated.length; i++) {
const embed = body.embedsDeprecated[i];
if (embed === undefined) {
return err(new HubError("bad_request.validation_failure", "string embed is missing"));
}
const embedIsValid = validateUrl(embed);
if (embedIsValid.isErr()) {
return err(embedIsValid.error);
}
}
for (let i = 0; i < body.mentions.length; i++) {
const mention = validateFid(body.mentions[i]);
if (mention.isErr()) {
return err(mention.error);
}
const position = body.mentionsPositions[i];
if (typeof position !== "number" || !Number.isInteger(position)) {
return err(new HubError("bad_request.validation_failure", "mentionsPositions must be integers"));
}
if (position < 0 || position > textBytes.length) {
return err(new HubError("bad_request.validation_failure", "mentionsPositions must be a position in text"));
}
if (i > 0) {
// biome-ignore lint/style/noNonNullAssertion: not sure why we do this, legacy when migrating from eslint.
const prevPosition = body.mentionsPositions[i - 1]!;
if (position < prevPosition) {
return err(
new HubError("bad_request.validation_failure", "mentionsPositions must be sorted in ascending order"),
);
}
}
}
if (body.parentCastId !== undefined && body.parentUrl !== undefined) {
return err(new HubError("bad_request.validation_failure", "cannot use both parentUrl and parentCastId"));
}
const parent = body.parentCastId ?? body.parentUrl;
if (parent !== undefined) {
const validParent = validateParent(parent);
if (validParent.isErr()) {
return err(validParent.error);
}
}
return ok(body);
};
export const validateCastRemoveBody = (body: protobufs.CastRemoveBody): HubResult<protobufs.CastRemoveBody> => {
return validateMessageHash(body.targetHash).map(() => body);
};
export const validateLinkType = (type: string): HubResult<string> => {
const typeBuffer = Buffer.from(type);
if (type.length === 0 || typeBuffer.length > 8) {
return err(new HubError("bad_request.validation_failure", "type must be between 1-8 bytes"));
}
return ok(type);
};
export const validateReactionType = (type: number): HubResult<protobufs.ReactionType> => {
if (!Object.values(protobufs.ReactionType).includes(type)) {
return err(new HubError("bad_request.validation_failure", "invalid reaction type"));
}
return ok(type);
};
export const validateTarget = (
target: protobufs.CastId | string | number,
): HubResult<protobufs.CastId | string | number> => {
if (typeof target === "string") {
return validateUrl(target);
} else if (typeof target === "number") {
return validateFid(target);
} else {
return validateCastId(target);
}
};
export const validateMessageType = (type: number): HubResult<protobufs.MessageType> => {
if (!Object.values(protobufs.MessageType).includes(type)) {
return err(new HubError("bad_request.validation_failure", "invalid message type"));
}
return ok(type);
};
export const validateNetwork = (network: number): HubResult<protobufs.FarcasterNetwork> => {
if (!Object.values(protobufs.FarcasterNetwork).includes(network)) {
return err(new HubError("bad_request.validation_failure", "invalid network"));
}
return ok(network);
};
export const validateLinkBody = (body: protobufs.LinkBody): HubResult<protobufs.LinkBody> => {
const validatedType = validateLinkType(body.type);
if (validatedType.isErr()) {
return err(validatedType.error);
}
const target = body.targetFid;
if (target === undefined) {
return err(new HubError("bad_request.validation_failure", "target is missing"));
}
return validateTarget(target).map(() => body);
};
export const validateReactionBody = (body: protobufs.ReactionBody): HubResult<protobufs.ReactionBody> => {
const validatedType = validateReactionType(body.type);
if (validatedType.isErr()) {
return err(validatedType.error);
}
if (body.targetCastId !== undefined && body.targetUrl !== undefined) {
return err(new HubError("bad_request.validation_failure", "cannot use both targetUrl and targetCastId"));
}
const target = body.targetCastId ?? body.targetUrl;
if (target === undefined) {
return err(new HubError("bad_request.validation_failure", "target is missing"));
}
return validateTarget(target).map(() => body);
};
export const validateVerificationAddAddressBody = async (
body: protobufs.VerificationAddAddressBody,
fid: number,
network: protobufs.FarcasterNetwork,
publicClients: PublicClients,
): HubAsyncResult<protobufs.VerificationAddAddressBody> => {
switch (body.protocol) {
case protobufs.Protocol.ETHEREUM:
return await validateVerificationAddEthAddressBody(body, fid, network, publicClients);
case protobufs.Protocol.SOLANA: {
return validateVerificationAddSolAddressBody(body, fid, network);
}
default:
return err(new HubError("bad_request.validation_failure", "invalid verification protocol"));
}
};
export const validateVerificationAddEthAddressBody = async (
body: protobufs.VerificationAddAddressBody,
fid: number,
network: protobufs.FarcasterNetwork,
publicClients: PublicClients,
): HubAsyncResult<protobufs.VerificationAddAddressBody> => {
const validAddress = validateEthAddress(body.address);
if (validAddress.isErr()) {
return err(validAddress.error);
}
const validBlockHash = validateEthBlockHash(body.blockHash);
if (validBlockHash.isErr()) {
return err(validBlockHash.error);
}
const validSignature = await validateVerificationAddEthAddressSignature(body, fid, network, publicClients);
if (validSignature.isErr()) {
return err(validSignature.error);
}
return ok(body);
};
export const validateVerificationAddSolAddressBody = async (
body: protobufs.VerificationAddAddressBody,
fid: number,
network: protobufs.FarcasterNetwork,
): HubAsyncResult<protobufs.VerificationAddAddressBody> => {
if (body.protocol !== protobufs.Protocol.SOLANA) {
return err(new HubError("bad_request.validation_failure", "invalid verification protocol"));
}
if (validateSolAddress(body.address).isErr()) {
return err(new HubError("bad_request.validation_failure", "solana address must be 32 bytes"));
}
if (validateSolBlockHash(body.blockHash).isErr()) {
return err(new HubError("bad_request.validation_failure", "blockHash must be 32 bytes"));
}
const isVerified = await validateVerificationAddSolAddressSignature(body, fid, network);
if (isVerified.isErr()) {
return err(isVerified.error);
}
return ok(body);
};
export const validateVerificationRemoveBody = (
body: protobufs.VerificationRemoveBody,
): HubResult<protobufs.VerificationRemoveBody> => {
switch (body.protocol) {
case protobufs.Protocol.ETHEREUM:
return validateEthAddress(body.address).map(() => body);
case protobufs.Protocol.SOLANA:
return validateSolAddress(body.address).map(() => body);
default:
return err(new HubError("bad_request.validation_failure", "invalid verification protocol"));
}
};
export const validateUsernameProofBody = (
body: protobufs.UserNameProof,
data: protobufs.MessageData,
): HubResult<protobufs.UserNameProof> => {
// Gossiped username proofs must only have an ENS type
if (body.type !== UserNameType.USERNAME_TYPE_ENS_L1) {
return err(new HubError("bad_request.validation_failure", `invalid username type: ${body.type}`));
}
const validateName = validateEnsName(body.name);
if (validateName.isErr()) {
return err(validateName.error);
}
if (body.fid !== data.fid) {
return err(
new HubError("bad_request.validation_failure", "fid in username proof does not match fid in message data"),
);
}
// Proof time is in Unix seconds
const proofFarcasterTimestamp = toFarcasterTime(body.timestamp * 1000);
if (proofFarcasterTimestamp.isErr()) {
return err(proofFarcasterTimestamp.error);
}
if (proofFarcasterTimestamp.value !== data.timestamp) {
return err(
new HubError(
"bad_request.validation_failure",
"timestamp in username proof does not match timestamp in message data",
),
);
}
return ok(body);
};
export const validateFrameActionBody = (body: protobufs.FrameActionBody): HubResult<protobufs.FrameActionBody> => {
// url and buttonId are required and must not exceed the length limits. cast id is optional
if (body.buttonIndex > 5) {
return err(new HubError("bad_request.validation_failure", "invalid button index"));
}
if (validateBytesAsString(body.url, 256, true).isErr()) {
return err(new HubError("bad_request.validation_failure", "invalid url"));
}
if (validateBytesAsString(body.inputText, 256).isErr()) {
return err(new HubError("bad_request.validation_failure", "invalid input text"));
}
if (validateBytesAsString(body.state, 4096).isErr()) {
return err(new HubError("bad_request.validation_failure", "invalid state"));
}
if (body.castId !== undefined) {
const result = validateCastId(body.castId);
if (result.isErr()) {
return err(result.error);
}
}
return ok(body);
};
const validateBytesAsString = (byteArray: Uint8Array, maxLength: number, required = false) => {
if (required && byteArray.length === 0) {
return err(new HubError("bad_request.validation_failure", "value is required"));
}
if (byteArray.length > maxLength) {
return err(new HubError("bad_request.validation_failure", "value is too long"));
}
return ok(byteArray);
};
export const validateUserDataType = (type: number): HubResult<protobufs.UserDataType> => {
if (
!Object.values(protobufs.UserDataType).includes(type) ||
type === protobufs.UserDataType.NONE ||
type === protobufs.UserDataType.NONE
) {
return err(new HubError("bad_request.validation_failure", "invalid user data type"));
}
return ok(type);
};
export const validateUserDataAddBody = (body: protobufs.UserDataBody): HubResult<protobufs.UserDataBody> => {
const { type, value } = body;
const textUtf8BytesResult = utf8StringToBytes(value);
if (textUtf8BytesResult.isErr()) {
return err(new HubError("bad_request.invalid_param", "value cannot be encoded as utf8"));
}
const valueBytes = textUtf8BytesResult.value;
switch (type) {
case protobufs.UserDataType.PFP:
if (valueBytes.length > 256) {
return err(new HubError("bad_request.validation_failure", "pfp value > 256"));
}
break;
case protobufs.UserDataType.DISPLAY:
if (valueBytes.length > 32) {
return err(new HubError("bad_request.validation_failure", "display value > 32"));
}
break;
case protobufs.UserDataType.BIO:
if (valueBytes.length > 256) {
return err(new HubError("bad_request.validation_failure", "bio value > 256"));
}
break;
case protobufs.UserDataType.URL:
if (valueBytes.length > 256) {
return err(new HubError("bad_request.validation_failure", "url value > 256"));
}
break;
case protobufs.UserDataType.USERNAME: {
// Users are allowed to set fname = '' to remove their fname, otherwise we need a valid fname to add
if (value !== "") {
const validatedFname = validateFname(value);
const validatedEnsName = validateEnsName(value);
// At least one of fname or ensName must be valid
if (validatedFname.isErr() && validatedEnsName.isErr()) {
return err(validatedFname.error);
}
}
break;
}
default:
return err(new HubError("bad_request.validation_failure", "invalid user data type"));
}
return ok(body);
};
export const validateFname = <T extends string | Uint8Array>(fnameP?: T | null): HubResult<T> => {
if (fnameP === undefined || fnameP === null || fnameP === "") {
return err(new HubError("bad_request.validation_failure", "fname is missing"));
}
let fname;
if (fnameP instanceof Uint8Array) {
const fromBytes = bytesToUtf8String(fnameP);
if (fromBytes.isErr()) {
return err(fromBytes.error);
}
fname = fromBytes.value;
} else {
fname = fnameP;
}
if (fname === undefined || fname === null || fname === "") {
return err(new HubError("bad_request.validation_failure", "fname is missing"));
}
// FNAME_MAX_LENGTH - ".eth".length
if (fname.length > 16) {
return err(new HubError("bad_request.validation_failure", `fname "${fname}" > 16 characters`));
}
const hasValidChars = FNAME_REGEX.test(fname);
if (hasValidChars === false) {
return err(new HubError("bad_request.validation_failure", `fname "${fname}" doesn't match ${FNAME_REGEX}`));
}
return ok(fnameP);
};
export const validateEnsName = <T extends string | Uint8Array>(ensNameP?: T | null): HubResult<T> => {
if (ensNameP === undefined || ensNameP === null || ensNameP === "") {
return err(new HubError("bad_request.validation_failure", "ensName is missing"));
}
let ensName;
if (ensNameP instanceof Uint8Array) {
const fromBytes = bytesToUtf8String(ensNameP);
if (fromBytes.isErr()) {
return err(fromBytes.error);
}
ensName = fromBytes.value;
} else {
ensName = ensNameP;
}
if (ensName === undefined || ensName === null || ensName === "") {
return err(new HubError("bad_request.validation_failure", "ensName is missing"));
}
try {
normalize(ensName);
} catch (e) {
return err(new HubError("bad_request.validation_failure", `ensName "${ensName}" is not a valid ENS name`));
}
if (!ensName.endsWith(".eth")) {
return err(new HubError("bad_request.validation_failure", `ensName "${ensName}" doesn't end with .eth`));
}
const nameParts = ensName.split(".");
if (nameParts[0] === undefined || nameParts.length !== 2) {
return err(new HubError("bad_request.validation_failure", `ensName "${ensName}" unsupported subdomain`));
}
if (ensName.length > USERNAME_MAX_LENGTH) {
return err(new HubError("bad_request.validation_failure", `ensName "${ensName}" > 20 characters`));
}
const hasValidChars = FNAME_REGEX.test(nameParts[0]);
if (!hasValidChars) {
return err(new HubError("bad_request.validation_failure", `ensName "${ensName}" doesn't match ${FNAME_REGEX}`));
}
return ok(ensNameP);
};