on a54b5ac help sign in
bridge/src/lib/validators.js
190 lines 4.3 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
9// // Note that this validator also transforms data
10// const validateBase64 = ({ data, pass, error }) => {
11// if (pass !== true) return { data, pass, error }
12//
13// try {
14// atob(data);
15// return { data: atob(data), pass: true, error: '' }
16// } catch(e) {
17// return { data: data, pass: false, error: 'File is not valid base64.' }
18// }
19// };
20//
21// // Note that this validator also transforms data
22// const validateJSON = ({ data, pass, error }) => {
23// if (pass !== true) return { data, pass, error }
24//
25// try {
26// JSON.parse(data)
27// return { data: JSON.parse(data), pass: true, error: '' }
28// } catch(e) {
29// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
30// }
31// };
37// Validates a bip39 mnemonic
38const validateMnemonic = m => simpleValidatorWrapper({
39 prevMessage: m,
40 validator: d => bip39.validateMnemonic(d),
41 errorMessage: 'This is not a valid mnemonic.'
42});
45// Checks an empty field
46const validateNotEmpty = m => simpleValidatorWrapper({
47 prevMessage: m,
48 validator: d => d.length > 1,
49 errorMessage: 'This field is required.'
50});
54// Checks if a patp is a valid galaxy
55const validateGalaxy = m => simpleValidatorWrapper({
56 prevMessage: m,
57 validator: d => {
58 let point
59 try {
60 point = parseInt(ob.patp2dec(d), 10)
61 return point >= 0 && point < 256
62 } catch(e) {
63 return false
64 }
65 },
66 errorMessage: 'This is not a valid galaxy'
67});
71const validatePoint = m => simpleValidatorWrapper({
72 prevMessage: m,
73 validator: d => {
74 try {
75 return ob.isValidPatp(d)
76 } catch(e) {
77 return false
78 }
79 },
80 errorMessage: 'This is not a valid point'
81});
85const validateTicket = m => simpleValidatorWrapper({
86 prevMessage: m,
87 validator: d => {
88 try {
89 return ob.isValidPatq(d)
90 } catch(e) {
91 return false
92 }
93 },
94 errorMessage: 'This is not a valid ticket'
95});
98const validateShard = m => simpleValidatorWrapper({
99 prevMessage: m,
100 validator: d => {
101 try {
102 return ob.isValidPatq(d)
103 } catch(e) {
104 return false
105 }
106 },
107 errorMessage: 'This is not a valid shard'
108});
111const validateLength = (m, l) => simpleValidatorWrapper({
112 prevMessage: m,
113 validator: d => d.length === l,
114 errorMessage: 'This is of an invalid length'
115});
118const validateNetworkKey = m => simpleValidatorWrapper({
119 prevMessage: m,
120 validator: d => hexRegExp.test(d),
121 errorMessage: 'This is not a valid network key'
122});
125const validateNetworkSeed = m => simpleValidatorWrapper({
126 prevMessage: m,
127 validator: d => hexRegExp.test(d),
128 errorMessage: 'This is not a valid network seed'
129});
133// Checks if a string is a valid ethereum address
134const validateEthereumAddress = m => simpleValidatorWrapper({
135 prevMessage: m,
136 validator: d => isValidAddress(d),
137 errorMessage: 'This is not a valid Ethereum address'
138});
144// Wraps single validation functions in a controlled and predictable way.
145const simpleValidatorWrapper = config => {
147 // If a previous validation has already failed, skip this validation and
148 // return the prev message to the next stage in the validation function chain.
149 // Failed validations should drop all the way down the chain and drop out of
150 // the output.
151 if (config.prevMessage.pass !== true) return config.prevMessage
153 // Run the validator and return the result.
154 return config.validator(config.prevMessage.data)
155 ? newMessage(config.prevMessage.data, true, '')
156 : newMessage(config.prevMessage.data, false, config.errorMessage)
159// Validation message
160// {
161// data: ...
162// pass: ...
163// error: ...
164// }
165// Creates a new validation message in a uniform way.
166const newMessage = (data, pass, error) => ({
167 // The input data
168 data,
169 // Has the data passed validation?
170 pass,
171 // If data has failed a validator, the error message goes here.
172 error,
173})
177export {
178 // validateBase64,
179 // validateJSON,
180 validateMnemonic,
181 validateNotEmpty,
182 validateGalaxy,
183 validateEthereumAddress,
184 validatePoint,
185 validateTicket,
186 validateShard,
187 validateLength,
188 validateNetworkKey,
189 validateNetworkSeed