-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathindex.ts
98 lines (80 loc) · 3.97 KB
/
index.ts
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
import { invoke } from '@tauri-apps/api'
import bs58 from 'bs58'
import { minor, valid } from 'semver'
import { getBalance, majorToMinor } from '../requests'
import { Coin } from '../types'
export const validateKey = (key: string): boolean => {
// it must be a valid base58 key
try {
const bytes = bs58.decode(key)
// of length 32
return bytes.length === 32
} catch (e) {
console.log(e)
return false
}
}
export const validateAmount = async (
amount: string,
minimum: string
): Promise<boolean> => {
// tests basic coin value requirements, like no more than 6 decimal places, value lower than total supply, etc
if (!Number(amount)) {
return false
}
try {
const minorValueStr: Coin = await invoke('major_to_minor', {
amount,
})
if (!basicRawCoinValueValidation(minorValueStr.amount)) {
return false
}
const minorValue = parseInt(minorValueStr.amount)
return minorValue >= parseInt(minimum)
} catch (e) {
console.log(e)
return false
}
// this conversion seems really iffy but I'm not sure how to better approach it
}
export const basicRawCoinValueValidation = (rawAmount: string): boolean => {
const amountFloat = parseFloat(rawAmount)
// it cannot have more than 6 decimal places
if (amountFloat !== parseInt(amountFloat.toFixed(6))) {
return false
}
// it cannot be larger than the total supply
if (amountFloat > 1_000_000_000_000_000) {
return false
}
// it can't be lower than one micro coin
return amountFloat >= 0.000001
}
export const isValidHostname = (value: string) => {
// regex for ipv4 and ipv6 and hhostname- source http://jsfiddle.net/DanielD/8S4nq/
const hostnameRegex =
/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*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$)(?=.*[A-Za-z].*)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*)\s*$)/
return hostnameRegex.test(value)
}
export const validateVersion = (version: string): boolean => {
try {
const minorVersion = minor(version)
const validVersion = valid(version)
return validVersion !== null && minorVersion >= 11
} catch (e) {
return false
}
}
export const validateLocation = (location: string): boolean => {
// right now only perform the stupid check of whether the user copy-pasted the tooltip... (with or without brackets)
return !location.trim().includes('physical location of your node')
}
export const validateRawPort = (rawPort: number): boolean =>
!isNaN(rawPort) && rawPort >= 1 && rawPort <= 65535
export const truncate = (text: string, trim: number) =>
text.substring(0, trim) + '...'
export const checkHasEnoughFunds = async (allocationValue: string) => {
const minorValue = await majorToMinor(allocationValue)
const walletValue = await getBalance()
return !(+walletValue.coin.amount - +minorValue.amount < 0)
}