-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
110 lines (95 loc) · 2.94 KB
/
main.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
// @ts-ignore
import baseX from 'base-x';
import * as bip from 'bip39';
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const BASE36 = '0123456789abcdefghijklmnopqrstuvwxyz';
function encodeHex(input) {
if(!input) throw new Error('Buffer or uint8 is required.');
var hex = '';
var aux = void 0;
for (var i = 0; i < input.length; i++) {
aux = input[i].toString(16).toUpperCase();
if (aux.length == 1) aux = '0' + aux;
hex += aux;
aux = '';
}
return hex;
};
function decodeHex(hex) {
if(!hex) throw new Error('Hex is required.');
var length = hex.length / 2 | 0;
var uint8 = new Uint8Array(length);
for (var i = 0; i < length; i++) {
uint8[i] = parseInt(hex.substr(i * 2, 2), 16);
} return Buffer.from(uint8);
};
function encodeBase64(input){
if(!input) throw new Error('Buffer or uint8 is required.');
return Buffer.from(input).toString('base64');
}
function decodeBase64(base64){
if(!base64) throw new Error('Base64 is required.');
return Buffer.from(base64, 'base64');
}
function encodeBase58(input){
if(!input) throw new Error('Buffer or uint8 is required.');
return baseX(BASE58).encode(Buffer.from(input));
}
function decodeBase58(base58){
if(!base58) throw new Error('Base58 is required.');
return baseX(BASE58).decode(base58);
}
function encodeBase36(input){
if(!input) throw new Error('Buffer or uint8 is required.');
return baseX(BASE36).encode(Buffer.from(input));
}
function decodeBase36(base36){
if(!base36) throw new Error('Base36 is required.');
return baseX(BASE36).decode(base36);
}
function encodeMnemonic(input, lang = 'english'){
if(!input) throw new Error('Buffer or uint8 is required.');
if(!bip.wordlists[lang]) throw new Error('Language not supported.');
return bip.entropyToMnemonic(encodeHex(input), bip.wordlists[lang]);
}
function decodeMnemonic(mnemonic){
if(!mnemonic) throw new Error('Mnemonic is required.');
return decodeHex(bip.mnemonicToEntropy(mnemonic));
}
function decodeString(input){
if(!input) throw new Error('String argument is required.');
let buffer;
if(input.length === 64){
buffer = decodeHex(input);
} else if(input.length === 49 || input.length === 50){
buffer = decodeBase36(input);
} else {
try{
buffer = decodeBase58(input);
} catch {
buffer = decodeBase64(input);
if(buffer.length !== 32){
try{
buffer = decodeMnemonic(input);
} catch{
buffer = '';
}
}
}
}
if(buffer.length !== 32) throw new Error('Invalid key size.')
return buffer;
}
export default {
encodeBase64,
decodeBase64,
encodeBase58,
decodeBase58,
encodeBase36,
decodeBase36,
encodeHex,
decodeHex,
encodeMnemonic,
decodeMnemonic,
decodeString,
};