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 }
15// return { data: atob(data), pass: true, error: '' }
17// return { data: data, pass: false, error: 'File is not valid base64.' }
21// // Note that this validator also transforms data
22// const validateJSON = ({ data, pass, error }) => {
23// if (pass !== true) return { data, pass, error }
27// return { data: JSON.parse(data), pass: true, error: '' }
29// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
37// Validates a bip39 mnemonic
38const validateMnemonic = m => simpleValidatorWrapper({
40 validator: d => bip39.validateMnemonic(d),
41 errorMessage: 'This is not a valid mnemonic.'
45// Checks an empty field
46const validateNotEmpty = m => simpleValidatorWrapper({
48 validator: d => d.length > 1,
49 errorMessage: 'This field is required.'
54// Checks if a patp is a valid galaxy
55const validateGalaxy = m => simpleValidatorWrapper({
60 point = parseInt(ob.patp2dec(d), 10)
61 return point >= 0 && point < 256
66 errorMessage: 'This is not a valid galaxy'
71const validatePoint = m => simpleValidatorWrapper({
75 return ob.isValidPatp(d)
80 errorMessage: 'This is not a valid point'
85const validateTicket = m => simpleValidatorWrapper({
89 return ob.isValidPatq(d)
94 errorMessage: 'This is not a valid ticket'
98const validateShard = m => simpleValidatorWrapper({
102 return ob.isValidPatq(d)
107 errorMessage: 'This is not a valid shard'
111const validateLength = (m, l) => simpleValidatorWrapper({
113 validator: d => d.length === l,
114 errorMessage: 'This is of an invalid length'
118const validateNetworkKey = m => simpleValidatorWrapper({
120 validator: d => hexRegExp.test(d),
121 errorMessage: 'This is not a valid network key'
125const validateNetworkSeed = m => simpleValidatorWrapper({
127 validator: d => hexRegExp.test(d),
128 errorMessage: 'This is not a valid network seed'
133// Checks if a string is a valid ethereum address
134const validateEthereumAddress = m => simpleValidatorWrapper({
136 validator: d => isValidAddress(d),
137 errorMessage: 'This is not a valid Ethereum address'
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
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)
165// Creates a new validation message in a uniform way.
166const newMessage = (data, pass, error) => ({
169 // Has the data passed validation?
171 // If data has failed a validator, the error message goes here.
183 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