1import * as bip39 from 'bip39';
2import * as ob from 'urbit-ob';
3import { identity } from 'lodash';
5import { isValidAddress } from './wallet';
6import patp2dec from './patp2dec';
8// via: https://emailregex.com/
9const 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,}))$/;
11// is this 64 hex chars?
12const hexRegExp = /[0-9A-Fa-f]{64}/g;
14// Wraps single validation functions in a controlled and predictable way.
15export const simpleValidatorWrapper = ({
21 // If a previous validation has already failed, skip this validation and
22 // return the prev message to the next stage in the validation function chain.
23 // Failed validations should drop all the way down the chain and drop out of
25 if (!prevMessage.pass) {
29 // Run the validator and return the result.
30 const pass = validator(prevMessage.data);
31 const data = pass ? transform(prevMessage.data) : prevMessage.data;
33 return newMessage(data, pass, !pass && error);
42// Creates a new validation message in a uniform way.
43const newMessage = (data, pass, error) => ({
46 // Has the data passed validation?
48 // If data has failed a validator, the error message goes here.
52// A validator that always passes.
53export const kDefaultValidator = data => newMessage(data, true, null);
55// Validates a bip39 mnemonic
56export const validateMnemonic = m =>
57 simpleValidatorWrapper({
59 validator: d => bip39.validateMnemonic(d),
60 error: 'This is not a valid mnemonic.',
63// Checks an empty field
64export const validateNotEmpty = m =>
65 simpleValidatorWrapper({
74 error: 'This field is required.',
77// Checks if a patp is a valid galaxy
78export const validateGalaxy = m =>
79 simpleValidatorWrapper({
85 return point >= 0 && point < 256;
90 error: 'This is not a valid galaxy',
93export const validatePoint = m =>
94 simpleValidatorWrapper({
98 return ob.isValidPatp(d);
103 error: 'This is not a valid point',
106export const validateTicket = m =>
107 simpleValidatorWrapper({
111 return ob.isValidPatq(d);
116 error: 'This is not a valid ticket',
119export const validateShard = m =>
120 simpleValidatorWrapper({
124 return ob.isValidPatq(d);
129 error: 'This is not a valid shard',
132export const validateLength = (m, l) =>
133 simpleValidatorWrapper({
137 return d.length === l;
142 error: 'This is of an invalid length',
145export const validateNetworkKey = m =>
146 simpleValidatorWrapper({
148 validator: d => hexRegExp.test(d),
149 error: 'This is not a valid network key',
152export const validateNetworkSeed = m =>
153 simpleValidatorWrapper({
155 validator: d => hexRegExp.test(d),
156 error: 'This is not a valid network seed',
159// Checks if a string is a valid ethereum address
160export const validateEthereumAddress = m =>
161 simpleValidatorWrapper({
163 validator: d => isValidAddress(d),
164 error: 'This is not a valid Ethereum address',
167export const validateEmail = m =>
168 simpleValidatorWrapper({
170 validator: d => emailRegExp.test(d),
171 error: 'This is not a valid email address',
174export const validateInt = m =>
175 simpleValidatorWrapper({
177 transform: d => parseInt(d, 10),
186 error: 'This is not a valid number',
189export const validateExactly = (value, error) => m =>
190 simpleValidatorWrapper({
192 validator: d => d === value,
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