-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextras-joi.js
151 lines (150 loc) · 5.13 KB
/
extras-joi.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const base58check = require('bs58check')
const batPublisher = require('./extras-publisher')
const bitcoin = require('bitcoinjs-lib')
const { getName } = require('country-list');
const currencyCodes = require('currency-codes')
const Joi = require('joi')
const ethereumAddress = require('ethereum-address')
module.exports = Joi.extend((joi) => {
return {
base: joi.string(),
type: 'string',
messages: {
'string.badAltcurrencyAddress': '{{#label}} invalid altcurrency address {{#value}}',
'string.badAltcurrencyCode': '{{#label}} invalid alternate currency code {{#value}}',
'string.badAnycurrencyCode': '{{#label}} invalid alternate/fiat currency code {{#value}}',
'string.badBase58': '{{#label}} bad Base58 encoding {{#value}}',
'string.badCountryCode': '{{#label}} invalid country code {{#value}}',
'string.badCurrencyCode': '{{#label}} invalid currency code {{#value}}',
'string.badEthAddress': '{{#label}} invalid Ethereum address {{#value}}',
'string.badFormat': '{{#label}} invalid format {{#value}}'
},
rules: {
altcurrencyAddress: {
/*
usage of this method requires being inside of an object where the altcurrency code is the key to the value
*/
validate (value, helpers, args, options) {
const { state } = helpers
const parent = state.ancestors[0]
const skipKeys = {
CARD_ID: true,
BAT: true
}
const key = Object.keys(parent).find((key) => {
if (skipKeys[key]) return
return parent[key] === value
})
if (key === 'BTC' || key === 'LTC') {
try {
base58check.decode(value)
} catch (err) {
return helpers.error('string.badBase58', { value }, state, options)
}
} else {
if (!ethereumAddress.isAddress(value)) {
return helpers.error('string.badEthAddress', { value }, state, options)
}
}
return value
}
},
altcurrencyCode: {
validate (value, helpers, args, options) {
const { state } = helpers
const regexp = new RegExp(/^[0-9A-Z]{1,}$/)
if (!regexp.test(value)) {
return helpers.error('string.badAltcurrencyCode', { value }, state, options)
}
return value
}
},
anycurrencyCode: {
validate (value, helpers, args, options) {
const { state } = helpers
const entry = value
const regexp = new RegExp(/^[0-9A-Z]{2,}$/)
if (!entry && !regexp.test(value)) {
return helpers.error('string.badAnycurrencyCode', { value }, state, options)
}
return value
}
},
base58: {
validate (value, helpers, args, options) {
const { state } = helpers
try {
base58check.decode(value)
} catch (err) {
return helpers.error('string.badBase58', { value }, state, options)
}
return value
}
},
countryCode: {
validate (value, helpers, args, options) {
const { state } = helpers
const entry = getName(value)
if (!entry) {
return helpers.error('string.badCountryCode', { value }, state, options)
}
return value
}
},
currencyCode: {
validate (value, helpers, args, options) {
const { state } = helpers
const entry = value
if (!entry) {
return helpers.error('string.badCurrencyCode', { value }, state, options)
}
return value
}
},
numeric: {
validate (value, helpers, args, options) {
const { state } = helpers
const isNumeric = new RegExp(/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i)
if (!isNumeric.test(value)) {
return helpers.error('string.badFormat', { value }, state, options)
}
return value
}
},
owner: {
validate (value, helpers, args, options) {
const { state } = helpers
const props = batPublisher.getPublisherProps(value)
if (!props || !props.publisherType) {
return helpers.error('string.badFormat', { value }, state, options)
}
return value
}
},
publisher: {
validate (value, helpers, args, options) {
const { state } = helpers
if (!batPublisher.isPublisher(value)) {
return helpers.error('string.badFormat', { value }, state, options)
}
return value
}
},
Xpub: {
// courtesy of the good folks at BitGo!
validate (value, helpers, args, options) {
const { state } = helpers
if (value.substr(0, 4) !== 'xpub') {
return helpers.error('string.badFormat', { value }, state, options)
}
try {
bitcoin.HDNode.fromBase58(value)
} catch (err) {
return helpers.error('string.badBase58', { value }, state, options)
}
return value
}
}
}
}
})