-
Notifications
You must be signed in to change notification settings - Fork 96
/
validators.js
72 lines (68 loc) 路 2.16 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as bitcoin from 'bitcoinjs-lib';
import numeral from 'numeral';
import { tokenMap } from '../constants/tokens';
import getBtcConfig from './api/btc/config';
import i18n from '../i18n';
import reg from './regex';
/**
* Validates the given address with respect to the tokenType
* @param {String} tokenType
* @param {String} address
* @param {Number} [netCode=1]
* @returns {Number} -> 0: valid, 1: invalid, -1: empty
*/
// eslint-disable-next-line import/prefer-default-export
export const validateAddress = (tokenType, address, netCode = 1) => {
if (address === '') {
return -1;
}
switch (tokenType) {
// Reference: https://github.com/bitcoinjs/bitcoinjs-lib/issues/890
case tokenMap.BTC.key:
try {
const config = getBtcConfig(netCode);
bitcoin.address.fromBase58Check(address); // eliminates segwit addresses
bitcoin.address.toOutputScript(address, config.network);
return 0;
} catch (e) {
return 1;
}
case tokenMap.LSK.key:
default:
return reg.address.test(address) ? 0 : 1;
}
};
/**
* Validate the format of the value param
* - Check that only have numbers and commas and points
* - Validate structure of the value, just one . or ,
* - Not ending with . or ,
* - Check that has no more than 8 floating points digits
* @param {Object.<string, srting>} data
* @param {string} data.value
* @param {string} [data.token="LSK"]
* @param {string} [data.locale="en"]
* @returns {Object.<string, string|boolean>}
* data - Object containing the message and if has an error
* data.message - Message of the error or empty string
* data.error - true or false
*/
export const validateAmountFormat = ({
value,
token = 'LSK',
locale = i18n.language,
}) => {
const errors = {
INVALID: i18n.t('Provide a correct amount of {{token}}', { token }),
FLOATING_POINT: i18n.t('Maximum floating point is 8.'),
};
const { format, maxFloating } = reg.amount[locale];
const message = (
(format.test(value) || numeral(value).value() === 0) && errors.INVALID)
|| (maxFloating.test(value) && errors.FLOATING_POINT)
|| '';
return {
error: !!message,
message,
};
};