on 8011c59 help sign in
bridge/src/lib/validators.js
176 lines 4.5 kB history raw
1import * as bip39 from 'bip39';
2import * as ob from 'urbit-ob';
4import { isValidAddress } from './wallet';
6const hexRegExp = /[0-9A-Fa-f]{64}/g;
8// // Note that this validator also transforms data
9// const validateBase64 = ({ data, pass, error }) => {
10// if (pass !== true) return { data, pass, error }
11//
12// try {
13// atob(data);
14// return { data: atob(data), pass: true, error: '' }
15// } catch(e) {
16// return { data: data, pass: false, error: 'File is not valid base64.' }
17// }
18// };
19//
20// // Note that this validator also transforms data
21// const validateJSON = ({ data, pass, error }) => {
22// if (pass !== true) return { data, pass, error }
23//
24// try {
25// JSON.parse(data)
26// return { data: JSON.parse(data), pass: true, error: '' }
27// } catch(e) {
28// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
29// }
30// };
32// Validates a bip39 mnemonic
33const validateMnemonic = m =>
34 simpleValidatorWrapper({
35 prevMessage: m,
36 validator: d => bip39.validateMnemonic(d),
37 errorMessage: 'This is not a valid mnemonic.',
38 });
40// Checks an empty field
41const validateNotEmpty = m =>
42 simpleValidatorWrapper({
43 prevMessage: m,
44 validator: d => d.length > 1,
45 errorMessage: 'This field is required.',
46 });
48// Checks if a patp is a valid galaxy
49const validateGalaxy = m =>
50 simpleValidatorWrapper({
51 prevMessage: m,
52 validator: d => {
53 let point;
54 try {
55 point = parseInt(ob.patp2dec(d), 10);
56 return point >= 0 && point < 256;
57 } catch (e) {
58 return false;
59 }
60 },
61 errorMessage: 'This is not a valid galaxy',
62 });
64const validatePoint = m =>
65 simpleValidatorWrapper({
66 prevMessage: m,
67 validator: d => {
68 try {
69 return ob.isValidPatp(d);
70 } catch (e) {
71 return false;
72 }
73 },
74 errorMessage: 'This is not a valid point',
75 });
77const validateTicket = m =>
78 simpleValidatorWrapper({
79 prevMessage: m,
80 validator: d => {
81 try {
82 return ob.isValidPatq(d);
83 } catch (e) {
84 return false;
85 }
86 },
87 errorMessage: 'This is not a valid ticket',
88 });
90const validateShard = m =>
91 simpleValidatorWrapper({
92 prevMessage: m,
93 validator: d => {
94 try {
95 return ob.isValidPatq(d);
96 } catch (e) {
97 return false;
98 }
99 },
100 errorMessage: 'This is not a valid shard',
101 });
103const validateLength = (m, l) =>
104 simpleValidatorWrapper({
105 prevMessage: m,
106 validator: d => d.length === l,
107 errorMessage: 'This is of an invalid length',
108 });
110const validateNetworkKey = m =>
111 simpleValidatorWrapper({
112 prevMessage: m,
113 validator: d => hexRegExp.test(d),
114 errorMessage: 'This is not a valid network key',
115 });
117const validateNetworkSeed = m =>
118 simpleValidatorWrapper({
119 prevMessage: m,
120 validator: d => hexRegExp.test(d),
121 errorMessage: 'This is not a valid network seed',
122 });
124// Checks if a string is a valid ethereum address
125const validateEthereumAddress = m =>
126 simpleValidatorWrapper({
127 prevMessage: m,
128 validator: d => isValidAddress(d),
129 errorMessage: 'This is not a valid Ethereum address',
130 });
132// Wraps single validation functions in a controlled and predictable way.
133const simpleValidatorWrapper = config => {
134 // If a previous validation has already failed, skip this validation and
135 // return the prev message to the next stage in the validation function chain.
136 // Failed validations should drop all the way down the chain and drop out of
137 // the output.
138 if (config.prevMessage.pass !== true) return config.prevMessage;
140 // Run the validator and return the result.
141 return config.validator(config.prevMessage.data)
142 ? newMessage(config.prevMessage.data, true, '')
143 : newMessage(config.prevMessage.data, false, config.errorMessage);
144};
146// Validation message
147// {
148// data: ...
149// pass: ...
150// error: ...
151// }
152// Creates a new validation message in a uniform way.
153const newMessage = (data, pass, error) => ({
154 // The input data
155 data,
156 // Has the data passed validation?
157 pass,
158 // If data has failed a validator, the error message goes here.
159 error,
160});
162export {
163 // validateBase64,
164 // validateJSON,
165 simpleValidatorWrapper,
166 validateMnemonic,
167 validateNotEmpty,
168 validateGalaxy,
169 validateEthereumAddress,
170 validatePoint,
171 validateTicket,
172 validateShard,
173 validateLength,
174 validateNetworkKey,
175 validateNetworkSeed,
176};