@@ -5,32 +5,43 @@ import { isValidAddress } from './wallet';
6const hexRegExp = /[0-9A-Fa-f]{64}/g;
6const hexRegExp = /[0-9A-Fa-f]{64}/g;
8−// // Note that this validator also transforms data
8+// Wraps single validation functions in a controlled and predictable way.
9−// const validateBase64 = ({ data, pass, error }) => {
9+export const simpleValidatorWrapper = config => {
10−// if (pass !== true) return { data, pass, error }
10+ // If a previous validation has already failed, skip this validation and
11−//
11+ // return the prev message to the next stage in the validation function chain.
12−// try {
12+ // Failed validations should drop all the way down the chain and drop out of
13−// atob(data);
13+ // the output.
14−// return { data: atob(data), pass: true, error: '' }
14+ if (!config.prevMessage.pass) {
15−// } catch(e) {
15+ return config.prevMessage;
16−// return { data: data, pass: false, error: 'File is not valid base64.' }
16+ }
18+ // Run the validator and return the result.
19+ return config.validator(config.prevMessage.data)
20+ ? newMessage(config.prevMessage.data, true, '')
21+ : newMessage(config.prevMessage.data, false, config.errorMessage);
19−//
30+// Creates a new validation message in a uniform way.
20−// // Note that this validator also transforms data
21−// const validateJSON = ({ data, pass, error }) => {
31+const newMessage = (data, pass, error) => ({
22−// if (pass !== true) return { data, pass, error }
32+ // The input data
24−// try {
34+ // Has the data passed validation?
25−// JSON.parse(data)
35+ pass,
26−// return { data: JSON.parse(data), pass: true, error: '' }
36+ // If data has failed a validator, the error message goes here.
27−// } catch(e) {
37+ error,
28−// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
38+});
30−// };
40+// A validator that always passes.
41+export const kDefaultValidator = data => newMessage(data, true, '');
32// Validates a bip39 mnemonic
43// Validates a bip39 mnemonic
33−const validateMnemonic = m =>
44+export const validateMnemonic = m =>
34 simpleValidatorWrapper({
45 simpleValidatorWrapper({
35 prevMessage: m,
46 prevMessage: m,
36 validator: d => bip39.validateMnemonic(d),
47 validator: d => bip39.validateMnemonic(d),
@@ -38,7 +49,7 @@ const validateMnemonic = m =>
40// Checks an empty field
51// Checks an empty field
41−const validateNotEmpty = m =>
52+export const validateNotEmpty = m =>
42 simpleValidatorWrapper({
53 simpleValidatorWrapper({
43 prevMessage: m,
54 prevMessage: m,
44 validator: d => d.length > 1,
55 validator: d => d.length > 1,
@@ -46,7 +57,7 @@ const validateNotEmpty = m =>
48// Checks if a patp is a valid galaxy
59// Checks if a patp is a valid galaxy
49−const validateGalaxy = m =>
60+export const validateGalaxy = m =>
50 simpleValidatorWrapper({
61 simpleValidatorWrapper({
51 prevMessage: m,
62 prevMessage: m,
52 validator: d => {
63 validator: d => {
@@ -61,7 +72,7 @@ const validateGalaxy = m =>
61 errorMessage: 'This is not a valid galaxy',
72 errorMessage: 'This is not a valid galaxy',
64−const validatePoint = m =>
75+export const validatePoint = m =>
65 simpleValidatorWrapper({
76 simpleValidatorWrapper({
66 prevMessage: m,
77 prevMessage: m,
67 validator: d => {
78 validator: d => {
@@ -74,7 +85,7 @@ const validatePoint = m =>
74 errorMessage: 'This is not a valid point',
85 errorMessage: 'This is not a valid point',
77−const validateTicket = m =>
88+export const validateTicket = m =>
78 simpleValidatorWrapper({
89 simpleValidatorWrapper({
79 prevMessage: m,
90 prevMessage: m,
80 validator: d => {
91 validator: d => {
@@ -87,7 +98,7 @@ const validateTicket = m =>
87 errorMessage: 'This is not a valid ticket',
98 errorMessage: 'This is not a valid ticket',
90−const validateShard = m =>
101+export const validateShard = m =>
91 simpleValidatorWrapper({
102 simpleValidatorWrapper({
92 prevMessage: m,
103 prevMessage: m,
93 validator: d => {
104 validator: d => {
@@ -100,21 +111,21 @@ const validateShard = m =>
100 errorMessage: 'This is not a valid shard',
111 errorMessage: 'This is not a valid shard',
103−const validateLength = (m, l) =>
114+export const validateLength = (m, l) =>
104 simpleValidatorWrapper({
115 simpleValidatorWrapper({
105 prevMessage: m,
116 prevMessage: m,
106 validator: d => d.length === l,
117 validator: d => d.length === l,
107 errorMessage: 'This is of an invalid length',
118 errorMessage: 'This is of an invalid length',
110−const validateNetworkKey = m =>
121+export const validateNetworkKey = m =>
111 simpleValidatorWrapper({
122 simpleValidatorWrapper({
112 prevMessage: m,
123 prevMessage: m,
113 validator: d => hexRegExp.test(d),
124 validator: d => hexRegExp.test(d),
114 errorMessage: 'This is not a valid network key',
125 errorMessage: 'This is not a valid network key',
117−const validateNetworkSeed = m =>
128+export const validateNetworkSeed = m =>
118 simpleValidatorWrapper({
129 simpleValidatorWrapper({
119 prevMessage: m,
130 prevMessage: m,
120 validator: d => hexRegExp.test(d),
131 validator: d => hexRegExp.test(d),
@@ -122,55 +133,9 @@ const validateNetworkSeed = m =>
124// Checks if a string is a valid ethereum address
135// Checks if a string is a valid ethereum address
125−const validateEthereumAddress = m =>
136+export const validateEthereumAddress = m =>
126 simpleValidatorWrapper({
137 simpleValidatorWrapper({
127 prevMessage: m,
138 prevMessage: m,
128 validator: d => isValidAddress(d),
139 validator: d => isValidAddress(d),
129 errorMessage: 'This is not a valid Ethereum address',
140 errorMessage: 'This is not a valid Ethereum address',
132−// Wraps single validation functions in a controlled and predictable way.
133−const 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
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);
146−// Validation message
152−// Creates a new validation message in a uniform way.
153−const newMessage = (data, pass, error) => ({
156− // Has the data passed validation?
158− // If data has failed a validator, the error message goes here.
165− simpleValidatorWrapper,
169− validateEthereumAddress,
175− validateNetworkSeed,