-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkuna-code-validation.js
More file actions
57 lines (43 loc) · 1.41 KB
/
kuna-code-validation.js
File metadata and controls
57 lines (43 loc) · 1.41 KB
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
function validateKunaCode(kunaCode) {
const base58Alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
if(!kunaCode || kunaCode.length < 1) {
throw new Error('Missing Kuna Code');
}
const segments = kunaCode.split('-');
const sufix = segments.slice(-2);
const body = segments.slice(0, -2);
if (sufix[1] !== 'KCode' || body.length !== 9) {
throw new Error('Invalid format');
}
// Check KunaCode checksum
const checksum = base58Alphabet.indexOf(body[0][0]);
const str = body.join('').slice(1);
let i = str.length;
let sum = 0;
while (i--) {
sum += base58Alphabet.indexOf(str.charAt(i));
}
if (sum % 58 !== checksum) {
throw new Error('Invalid checksum');
}
}
try {
validateKunaCode(); // missing
} catch(error) {
console.log('1) Missing Kuna Code -> ', error.message);
}
try {
validateKunaCode('r4wR3-vjUHK-UHYBi-iBkra-hMeqU-L74aD-scBcR-BTC-KCode'); // invalid format
} catch(error) {
console.log('2) Invalid format -> ', error.message);
}
try {
validateKunaCode('r4wR3-vjUHK-s6AhZ-UHYBi-ickra-hMeqU-vp3uh-L74aD-scBcR-BTC-KCode'); // invalid checksum
} catch(error) {
console.log('3) Invalid checksum -> ', error.message);
}
// valid code
try {
validateKunaCode('r4wR3-vjUHK-s6AhZ-UHYBi-iBkra-hMeqU-vj3uh-L74aD-scBcR-BTC-KCode');
console.log('4) Code Valid!');
} catch (error) {}