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 }
14// return { data: atob(data), pass: true, error: '' }
16// return { data: data, pass: false, error: 'File is not valid base64.' }
20// // Note that this validator also transforms data
21// const validateJSON = ({ data, pass, error }) => {
22// if (pass !== true) return { data, pass, error }
26// return { data: JSON.parse(data), pass: true, error: '' }
28// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
32// Validates a bip39 mnemonic
33const validateMnemonic = m =>
34 simpleValidatorWrapper({
36 validator: d => bip39.validateMnemonic(d),
37 errorMessage: 'This is not a valid mnemonic.',
40// Checks an empty field
41const validateNotEmpty = m =>
42 simpleValidatorWrapper({
44 validator: d => d.length > 1,
45 errorMessage: 'This field is required.',
48// Checks if a patp is a valid galaxy
49const validateGalaxy = m =>
50 simpleValidatorWrapper({
55 point = parseInt(ob.patp2dec(d), 10);
56 return point >= 0 && point < 256;
61 errorMessage: 'This is not a valid galaxy',
64const validatePoint = m =>
65 simpleValidatorWrapper({
69 return ob.isValidPatp(d);
74 errorMessage: 'This is not a valid point',
77const validateTicket = m =>
78 simpleValidatorWrapper({
82 return ob.isValidPatq(d);
87 errorMessage: 'This is not a valid ticket',
90const validateShard = m =>
91 simpleValidatorWrapper({
95 return ob.isValidPatq(d);
100 errorMessage: 'This is not a valid shard',
103const validateLength = (m, l) =>
104 simpleValidatorWrapper({
106 validator: d => d.length === l,
107 errorMessage: 'This is of an invalid length',
110const validateNetworkKey = m =>
111 simpleValidatorWrapper({
113 validator: d => hexRegExp.test(d),
114 errorMessage: 'This is not a valid network key',
117const validateNetworkSeed = m =>
118 simpleValidatorWrapper({
120 validator: d => hexRegExp.test(d),
121 errorMessage: 'This is not a valid network seed',
124// Checks if a string is a valid ethereum address
125const validateEthereumAddress = m =>
126 simpleValidatorWrapper({
128 validator: d => isValidAddress(d),
129 errorMessage: 'This is not a valid Ethereum address',
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
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);
152// Creates a new validation message in a uniform way.
153const 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,
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