1import * as bip39 from 'bip39';
2import * as ob from 'urbit-ob';
4import { isValidAddress } from './wallet';
6const hexRegExp = /[0-9A-Fa-f]{64}/g;
8// Wraps single validation functions in a controlled and predictable way.
9export const simpleValidatorWrapper = config => {
10 // If a previous validation has already failed, skip this validation and
11 // return the prev message to the next stage in the validation function chain.
12 // Failed validations should drop all the way down the chain and drop out of
14 if (!config.prevMessage.pass) {
15 return config.prevMessage;
18 // Run the validator and return the result.
19 return config.validator(config.prevMessage.data)
20 ? newMessage(config.prevMessage.data, true, null)
21 : newMessage(config.prevMessage.data, false, config.errorMessage);
30// Creates a new validation message in a uniform way.
31const newMessage = (data, pass, error) => ({
34 // Has the data passed validation?
36 // If data has failed a validator, the error message goes here.
40// A validator that always passes.
41export const kDefaultValidator = data => newMessage(data, true, null);
43// Validates a bip39 mnemonic
44export const validateMnemonic = m =>
45 simpleValidatorWrapper({
47 validator: d => bip39.validateMnemonic(d),
48 errorMessage: 'This is not a valid mnemonic.',
51// Checks an empty field
52export const validateNotEmpty = m =>
53 simpleValidatorWrapper({
55 validator: d => d.length > 1,
56 errorMessage: 'This field is required.',
59// Checks if a patp is a valid galaxy
60export const validateGalaxy = m =>
61 simpleValidatorWrapper({
66 point = parseInt(ob.patp2dec(d), 10);
67 return point >= 0 && point < 256;
72 errorMessage: 'This is not a valid galaxy',
75export const validatePoint = m =>
76 simpleValidatorWrapper({
80 return ob.isValidPatp(d);
85 errorMessage: 'This is not a valid point',
88export const validateTicket = m =>
89 simpleValidatorWrapper({
93 return ob.isValidPatq(d);
98 errorMessage: 'This is not a valid ticket',
101export const validateShard = m =>
102 simpleValidatorWrapper({
106 return ob.isValidPatq(d);
111 errorMessage: 'This is not a valid shard',
114export const validateLength = (m, l) =>
115 simpleValidatorWrapper({
117 validator: d => d.length === l,
118 errorMessage: 'This is of an invalid length',
121export const validateNetworkKey = m =>
122 simpleValidatorWrapper({
124 validator: d => hexRegExp.test(d),
125 errorMessage: 'This is not a valid network key',
128export const validateNetworkSeed = m =>
129 simpleValidatorWrapper({
131 validator: d => hexRegExp.test(d),
132 errorMessage: 'This is not a valid network seed',
135// Checks if a string is a valid ethereum address
136export const validateEthereumAddress = m =>
137 simpleValidatorWrapper({
139 validator: d => isValidAddress(d),
140 errorMessage: 'This is not a valid Ethereum 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