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 => {
38 const point = patp2dec(v);
39 const isValidGalaxy = point >= MIN_GALAXY && point <= MAX_GALAXY;
44 return 'This is not a valid galaxy.';
48export const validatePoint = v => {
50 if (!ob.isValidPatp(v)) {
54 return 'This is not a valid point.';
58export const validatePatq = v => {
60 if (!ob.isValidPatq(v)) {
64 return 'This is not a valid ticket.';
68export const validateShard = v => {
70 if (v !== undefined && v !== '' && !ob.isValidPatq(v)) {
74 return 'This is not a valid shard.';
78export const validateOneOf = (options = []) => v =>
79 !includes(options, v) && 'Is not a valid option.';
81export const validateHexString = v =>
82 !isHexString.test(v) && 'This is not a valid hex string.';
84export const validateHexPrefix = v =>
85 !isHexPrefixed.test(v) && 'Must include 0x prefix.';
87export const validateEthereumAddress = v =>
88 !isValidAddress(v) && 'This is not a valid Ethereum address.';
90export const validateEmail = v =>
91 !emailRegExp.test(v) && 'This is not a valid email address.';
93export const validateExactly = (value, error) => v => v !== value && error;
95export const validateNotAny = (values = []) => v =>
96 values.includes(v) && `Cannot be ${v}.`;
98export const validateLength = l => v =>
99 v.length !== l && `Must be exactly ${l} characters.`;
101export const validateHexLength = l => v =>
102 v.length !== l + 2 && `Must be exactly ${l} hex characters.`;
104export const validateMaximumLength = l => v =>
105 v.length > l && `Must be ${l} characters or fewer.`;
107export const validateMinimumLength = l => v =>
108 v.length < l && `Must be ${l} characters or more.`;
110export const validateGreaterThan = l => v =>
111 !(v > l) && `Must be at least ${l}.`;
113export const validateLessThan = l => v => !(v < l) && `Must be less than ${l}`;
115export const validateInSet = (set, error) => v => !set.has(v) && error;
117export const validateMaximumPatpByteLength = byteLength =>
118 validateMaximumLength(patpStringLength(byteLength));
120export const validateMinimumPatpByteLength = byteLength =>
121 validateMinimumLength(patpStringLength(byteLength));
123export const validateNotNullAddress = validateNotAny([
128export const validateUnique = arr => {
129 const res = [...new Set(arr)].length !== arr.length && 'Must be unique';
133export const validateChild = ourShip => ship =>
134 ourShip !== ob.sein(ship) && `This point is not a child of ${ourShip}.`;
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