1import * as bip39 from 'bip39';
2import * as 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// is this only hex values with a 0x prefix?
18const isHexString = /0x[0-9A-Fa-f]/;
20// Validates a bip39 mnemonic
21export const validateMnemonic = v =>
22 !bip39.validateMnemonic(v) && 'This is not a valid mnemonic.';
24// Checks an empty field
25export const validateNotEmpty = v =>
26 v.length === 0 && 'This field is required.';
28// Checks if a patp is a valid galaxy
29export const validateGalaxy = v => {
31 const point = patp2dec(v);
32 const isValidGalaxy = point >= MIN_GALAXY && point <= MAX_GALAXY;
37 return 'This is not a valid galaxy.';
41export const validatePoint = v => {
43 if (!ob.isValidPatp(v)) {
47 return 'This is not a valid point.';
51export const validatePatq = v => {
53 if (!ob.isValidPatq(v)) {
57 return 'This is not a valid ticket.';
61export const validateOneOf = (options = []) => v =>
62 !includes(options, v) && 'Is not a valid option.';
64export const validateHexString = v =>
65 !isHexString.test(v) && 'This is not a valid hex string.';
67export const validateEthereumAddress = v =>
68 !isValidAddress(v) && 'This is not a valid Ethereum address.';
70export const validateEmail = v =>
71 !emailRegExp.test(v) && 'This is not a valid email address.';
73export const validateExactly = (value, error) => v => v !== value && error;
75export const validateNotAny = (values = []) => v =>
76 values.includes(v) && `Cannot be ${v}.`;
78export const validateLength = l => v =>
79 v.length !== l && `Must be exactly ${l} characters.`;
81export const validateHexLength = l => v =>
82 v.length !== l + 2 && `Must be exactly ${l} hex characters.`;
84export const validateMaximumLength = l => v =>
85 v.length > l && `Must be ${l} characters or fewer.`;
87export const validateGreaterThan = l => v =>
88 !(v > l) && `Must be at least ${l}.`;
90export const validateInSet = (set, error) => v => !set.has(v) && error;
92export const validateMaximumPatpByteLength = byteLength =>
93 validateMaximumLength(patpStringLength(byteLength));
95export const validateNotNullAddress = validateNotAny([
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list