on da9b631 help sign in
bridge/src/lib/validators.js
164 lines 4.1 kB history raw
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
17 // the output.
18 if (!config.prevMessage.pass) {
19 return config.prevMessage;
20 }
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);
26};
28// Validation message
29// {
30// data: ...
31// pass: ...
32// error: ...
33// }
34// Creates a new validation message in a uniform way.
35const newMessage = (data, pass, error) => ({
36 // The input data
37 data,
38 // Has the data passed validation?
39 pass,
40 // If data has failed a validator, the error message goes here.
41 error,
42});
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({
50 prevMessage: m,
51 validator: d => bip39.validateMnemonic(d),
52 error: 'This is not a valid mnemonic.',
53 });
55// Checks an empty field
56export const validateNotEmpty = m =>
57 simpleValidatorWrapper({
58 prevMessage: m,
59 validator: d => {
60 try {
61 return d.length > 1;
62 } catch {
63 return false;
64 }
65 },
66 error: 'This field is required.',
67 });
69// Checks if a patp is a valid galaxy
70export const validateGalaxy = m =>
71 simpleValidatorWrapper({
72 prevMessage: m,
73 validator: d => {
74 let point;
75 try {
76 point = parseInt(ob.patp2dec(d), 10);
77 return point >= 0 && point < 256;
78 } catch (e) {
79 return false;
80 }
81 },
82 error: 'This is not a valid galaxy',
83 });
85export const validatePoint = m =>
86 simpleValidatorWrapper({
87 prevMessage: m,
88 validator: d => {
89 try {
90 return ob.isValidPatp(d);
91 } catch (e) {
92 return false;
93 }
94 },
95 error: 'This is not a valid point',
96 });
98export const validateTicket = m =>
99 simpleValidatorWrapper({
100 prevMessage: m,
101 validator: d => {
102 try {
103 return ob.isValidPatq(d);
104 } catch (e) {
105 return false;
106 }
107 },
108 error: 'This is not a valid ticket',
109 });
111export const validateShard = m =>
112 simpleValidatorWrapper({
113 prevMessage: m,
114 validator: d => {
115 try {
116 return ob.isValidPatq(d);
117 } catch (e) {
118 return false;
119 }
120 },
121 error: 'This is not a valid shard',
122 });
124export const validateLength = (m, l) =>
125 simpleValidatorWrapper({
126 prevMessage: m,
127 validator: d => {
128 try {
129 return d.length === l;
130 } catch {
131 return false;
132 }
133 },
134 error: 'This is of an invalid length',
135 });
137export const validateNetworkKey = m =>
138 simpleValidatorWrapper({
139 prevMessage: m,
140 validator: d => hexRegExp.test(d),
141 error: 'This is not a valid network key',
142 });
144export const validateNetworkSeed = m =>
145 simpleValidatorWrapper({
146 prevMessage: m,
147 validator: d => hexRegExp.test(d),
148 error: 'This is not a valid network seed',
149 });
151// Checks if a string is a valid ethereum address
152export const validateEthereumAddress = m =>
153 simpleValidatorWrapper({
154 prevMessage: m,
155 validator: d => isValidAddress(d),
156 error: 'This is not a valid Ethereum address',
157 });
159export const validateEmail = m =>
160 simpleValidatorWrapper({
161 prevMessage: m,
162 validator: d => emailRegExp.test(d),
163 error: 'This is not a valid email address',
164 });