on 5b3f2fd help sign in
bridge/src/lib/validators.js
121 lines 3.6 kB history raw
1import * as bip39 from 'bip39';
2import ob from 'urbit-ob';
3import { includes } from 'lodash';
5import { isValidAddress, ETH_ZERO_ADDR, ETH_ZERO_ADDR_SHORT } from './wallet';
6import patp2dec from './patp2dec';
7import { patpStringLength } from './lib';
8import { MIN_GALAXY, MAX_GALAXY } from './constants';
10// NOTE: do not use the /g modifier for these regexes
11// https://stackoverflow.com/a/21373261
12// https://stackoverflow.com/a/1520853
14// via: https://emailregex.com/
15const emailRegExp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
17// validate only strings that use hex characters
18const isHexString = /[0-9A-Fa-f]/;
20// validate only strings that are prefixed with 0x
21const isHexPrefixed = /^0x/;
23// Validates a bip39 mnemonic
24export const validateMnemonic = v =>
25 !bip39.validateMnemonic(v) && 'This is not a valid mnemonic.';
27const isHdPath = /^m(\/[0-9]+'?)*$/;
28export const validateHdPath = v =>
29 !isHdPath.test(v) && 'Invalid HD derivation path.';
31// Checks an empty field
32export const validateNotEmpty = v =>
33 (v === undefined || v.length === 0) && 'This field is required.';
35// Checks if a patp is a valid galaxy
36export const validateGalaxy = v => {
37 try {
38 const point = patp2dec(v);
39 const isValidGalaxy = point >= MIN_GALAXY && point <= MAX_GALAXY;
40 if (!isValidGalaxy) {
41 throw new Error();
42 }
43 } catch {
44 return 'This is not a valid galaxy.';
45 }
46};
48export const validatePoint = v => {
49 try {
50 if (!ob.isValidPatp(v)) {
51 throw new Error();
52 }
53 } catch {
54 return 'This is not a valid point.';
55 }
56};
58export const validatePatq = v => {
59 try {
60 if (!ob.isValidPatq(v)) {
61 throw new Error();
62 }
63 } catch {
64 return 'This is not a valid ticket.';
65 }
66};
68export const validateOneOf = (options = []) => v =>
69 !includes(options, v) && 'Is not a valid option.';
71export const validateHexString = v =>
72 !isHexString.test(v) && 'This is not a valid hex string.';
74export const validateHexPrefix = v =>
75 !isHexPrefixed.test(v) && 'Must include 0x prefix.';
77export const validateEthereumAddress = v =>
78 !isValidAddress(v) && 'This is not a valid Ethereum address.';
80export const validateEmail = v =>
81 !emailRegExp.test(v) && 'This is not a valid email address.';
83export const validateExactly = (value, error) => v => v !== value && error;
85export const validateNotAny = (values = []) => v =>
86 values.includes(v) && `Cannot be ${v}.`;
88export const validateLength = l => v =>
89 v.length !== l && `Must be exactly ${l} characters.`;
91export const validateHexLength = l => v =>
92 v.length !== l + 2 && `Must be exactly ${l} hex characters.`;
94export const validateMaximumLength = l => v =>
95 v.length > l && `Must be ${l} characters or fewer.`;
97export const validateMinimumLength = l => v =>
98 v.length < l && `Must be ${l} characters or more.`;
100export const validateGreaterThan = l => v =>
101 !(v > l) && `Must be at least ${l}.`;
103export const validateLessThan = l => v => !(v < l) && `Must be less than ${l}`;
105export const validateInSet = (set, error) => v => !set.has(v) && error;
107export const validateMaximumPatpByteLength = byteLength =>
108 validateMaximumLength(patpStringLength(byteLength));
110export const validateMinimumPatpByteLength = byteLength =>
111 validateMinimumLength(patpStringLength(byteLength));
113export const validateNotNullAddress = validateNotAny([
114 ETH_ZERO_ADDR,
115 ETH_ZERO_ADDR_SHORT,
116]);
118export const validateUnique = arr => {
119 const res = [...new Set(arr)].length !== arr.length && 'Must be unique';
120 return res;
121};