on 4e849bf help sign in
bridge/src/lib/validators.js
104 lines 3.1 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.';
27// Checks an empty field
28export const validateNotEmpty = v =>
29 (v === undefined || v.length === 0) && 'This field is required.';
31// Checks if a patp is a valid galaxy
32export const validateGalaxy = v => {
33 try {
34 const point = patp2dec(v);
35 const isValidGalaxy = point >= MIN_GALAXY && point <= MAX_GALAXY;
36 if (!isValidGalaxy) {
37 throw new Error();
38 }
39 } catch {
40 return 'This is not a valid galaxy.';
41 }
42};
44export const validatePoint = v => {
45 try {
46 if (!ob.isValidPatp(v)) {
47 throw new Error();
48 }
49 } catch {
50 return 'This is not a valid point.';
51 }
52};
54export const validatePatq = v => {
55 try {
56 if (!ob.isValidPatq(v)) {
57 throw new Error();
58 }
59 } catch {
60 return 'This is not a valid ticket.';
61 }
62};
64export const validateOneOf = (options = []) => v =>
65 !includes(options, v) && 'Is not a valid option.';
67export const validateHexString = v =>
68 !isHexString.test(v) && 'This is not a valid hex string.';
70export const validateHexPrefix = v =>
71 !isHexPrefixed.test(v) && 'Must include 0x prefix.';
73export const validateEthereumAddress = v =>
74 !isValidAddress(v) && 'This is not a valid Ethereum address.';
76export const validateEmail = v =>
77 !emailRegExp.test(v) && 'This is not a valid email address.';
79export const validateExactly = (value, error) => v => v !== value && error;
81export const validateNotAny = (values = []) => v =>
82 values.includes(v) && `Cannot be ${v}.`;
84export const validateLength = l => v =>
85 v.length !== l && `Must be exactly ${l} characters.`;
87export const validateHexLength = l => v =>
88 v.length !== l + 2 && `Must be exactly ${l} hex characters.`;
90export const validateMaximumLength = l => v =>
91 v.length > l && `Must be ${l} characters or fewer.`;
93export const validateGreaterThan = l => v =>
94 !(v > l) && `Must be at least ${l}.`;
96export const validateInSet = (set, error) => v => !set.has(v) && error;
98export const validateMaximumPatpByteLength = byteLength =>
99 validateMaximumLength(patpStringLength(byteLength));
101export const validateNotNullAddress = validateNotAny([
102 ETH_ZERO_ADDR,
103 ETH_ZERO_ADDR_SHORT,
104]);