1import * as bip39 from 'bip39';
2import * as ob from 'urbit-ob';
4import { isValidAddress } from './wallet';
6// via: https://emailregex.com/
7const 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,}))$/;
9// is this 64 hex chars?
10const hexRegExp = /[0-9A-Fa-f]{64}/g;
12// Wraps single validation functions in a controlled and predictable way.
13export const simpleValidatorWrapper = config => {
14 // If a previous validation has already failed, skip this validation and
15 // return the prev message to the next stage in the validation function chain.
16 // Failed validations should drop all the way down the chain and drop out of
18 if (!config.prevMessage.pass) {
19 return config.prevMessage;
22 // Run the validator and return the result.
23 return config.validator(config.prevMessage.data)
24 ? newMessage(config.prevMessage.data, true, null)
25 : newMessage(config.prevMessage.data, false, config.error);
34// Creates a new validation message in a uniform way.
35const newMessage = (data, pass, error) => ({
38 // Has the data passed validation?
40 // If data has failed a validator, the error message goes here.
44// A validator that always passes.
45export const kDefaultValidator = data => newMessage(data, true, null);
47// Validates a bip39 mnemonic
48export const validateMnemonic = m =>
49 simpleValidatorWrapper({
51 validator: d => bip39.validateMnemonic(d),
52 error: 'This is not a valid mnemonic.',
55// Checks an empty field
56export const validateNotEmpty = m =>
57 simpleValidatorWrapper({
66 error: 'This field is required.',
69// Checks if a patp is a valid galaxy
70export const validateGalaxy = m =>
71 simpleValidatorWrapper({
76 point = parseInt(ob.patp2dec(d), 10);
77 return point >= 0 && point < 256;
82 error: 'This is not a valid galaxy',
85export const validatePoint = m =>
86 simpleValidatorWrapper({
90 return ob.isValidPatp(d);
95 error: 'This is not a valid point',
98export const validateTicket = m =>
99 simpleValidatorWrapper({
103 return ob.isValidPatq(d);
108 error: 'This is not a valid ticket',
111export const validateShard = m =>
112 simpleValidatorWrapper({
116 return ob.isValidPatq(d);
121 error: 'This is not a valid shard',
124export const validateLength = (m, l) =>
125 simpleValidatorWrapper({
129 return d.length === l;
134 error: 'This is of an invalid length',
137export const validateNetworkKey = m =>
138 simpleValidatorWrapper({
140 validator: d => hexRegExp.test(d),
141 error: 'This is not a valid network key',
144export const validateNetworkSeed = m =>
145 simpleValidatorWrapper({
147 validator: d => hexRegExp.test(d),
148 error: 'This is not a valid network seed',
151// Checks if a string is a valid ethereum address
152export const validateEthereumAddress = m =>
153 simpleValidatorWrapper({
155 validator: d => isValidAddress(d),
156 error: 'This is not a valid Ethereum address',
159export const validateEmail = m =>
160 simpleValidatorWrapper({
162 validator: d => emailRegExp.test(d),
163 error: 'This is not a valid email address',
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