forked from glifio/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomPropTypes.js
94 lines (83 loc) · 2.44 KB
/
customPropTypes.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
import { shape, string, oneOfType, number, oneOf, object } from 'prop-types'
import { validateAddressString } from '@glif/filecoin-address'
import { validatePath } from '@glif/filecoin-wallet-provider'
import { validateMnemonic } from 'bip39'
import {
SINGLE_KEY,
IMPORT_MNEMONIC,
IMPORT_SINGLE_KEY,
LEDGER
} from './constants'
export const ADDRESS_PROPTYPE = (props, propName, componentName) => {
if (!validateAddressString(props[propName]))
return new Error(
`Invalid prop: ${propName} supplied to ${componentName}. Validation failed.`
)
return null
}
export const MNEMONIC_PROPTYPE = (props, propName, componentName) => {
if (!validateMnemonic(props[propName]))
return new Error(
`Invalid prop: ${propName} supplied to ${componentName}. Validation failed.`
)
return null
}
export const FILECOIN_NUMBER_PROP = (props, propName, componentName) => {
// instanceof prop checking is broken in nextjs on server side render cycles
const representsANum = Number.isNaN(Number(props[propName].toString()))
const hasFilecoinNumMethods = !!(
props[propName].toFil &&
props[propName].toAttoFil &&
props[propName].toPicoFil
)
if (!(representsANum || hasFilecoinNumMethods))
return new Error(
`Invalid prop: ${propName} supplied to ${componentName}. Validation failed.`
)
return null
}
export const HD_PATH_PROP = (props, propName, componentName) => {
if (!validatePath(props[propName]) && props[propName] !== SINGLE_KEY)
return new Error(
`Invalid prop: ${propName} supplied to ${componentName}. Validation failed.`
)
return null
}
export const WALLET_PROP_TYPE = shape({
balance: FILECOIN_NUMBER_PROP,
address: ADDRESS_PROPTYPE,
path: HD_PATH_PROP,
type: oneOf([IMPORT_MNEMONIC, IMPORT_SINGLE_KEY, LEDGER]),
index: number
})
export const NO_WALLET_PROP_TYPE = shape({
balance: FILECOIN_NUMBER_PROP,
address: string,
path: string,
index: number
})
export const MESSAGE_PROPS = shape({
/**
* Message sent to this address
*/
to: ADDRESS_PROPTYPE,
/**
* Message sent from this address
*/
from: ADDRESS_PROPTYPE,
/**
* The amount of FIL sent in the message
*/
value: string.isRequired,
/**
* The message's cid
*/
cid: string.isRequired,
/**
* Either pending or confirmed
*/
status: oneOf(['confirmed', 'pending']).isRequired,
timestamp: oneOfType([string, number]).isRequired,
method: string.isRequired,
params: object.isRequired
})