-
Notifications
You must be signed in to change notification settings - Fork 22
/
encrypt.ts
156 lines (130 loc) · 4.28 KB
/
encrypt.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
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
152
153
154
155
156
const Buffer = require('buffer/').Buffer
const UUID = require('pure-uuid');
import { checkParams, ed25519 } from '~@vite/vitejs-utils';
import { createAddressByPrivateKey } from '~@vite/vitejs-wallet/address';
import { paramsFormat } from '~@vite/vitejs-error';
import isValid from './validated';
import { cipheriv, encryptPwd } from './tools';
import { scryptName, algorithm, currentVersion, defaultScryptParams, additionData } from './vars';
const { random } = ed25519;
const n = defaultScryptParams.n;
const p = defaultScryptParams.p;
const r = defaultScryptParams.r;
const keyLen = defaultScryptParams.keyLen;
export function encrypt(key, pwd, _scryptParams, selfScryptsy) {
const err = checkParams({ key, pwd }, [ 'key', 'pwd' ]);
if (err) {
return Promise.reject(err);
}
const scryptParams = {
n: _scryptParams && _scryptParams.n ? _scryptParams.n : n,
r: _scryptParams && _scryptParams.r ? _scryptParams.r : r,
p: _scryptParams && _scryptParams.p ? _scryptParams.p : p,
keylen: _scryptParams && _scryptParams.keylen ? _scryptParams.keylen : keyLen,
salt: _scryptParams && _scryptParams.salt ? _scryptParams.salt : Buffer.from(random()).toString('hex')
};
const getResult = (encryptPwd, res, rej) => {
try {
const _keystore = getKeystore(key, encryptPwd, scryptParams);
res(_keystore);
} catch (err) {
rej(err);
}
};
return new Promise((res, rej) => {
encryptPwd(pwd, scryptParams, selfScryptsy).then(result => {
getResult(result, res, rej);
}).catch(err => {
rej(err);
});
});
}
// encryptToVersion3
export function encryptV1ToV3(key, keystore) {
const err = checkParams({ key, keystore }, [ 'key', 'keystore' ]);
if (err) {
console.error(new Error(err.message));
return false;
}
const keyJson = isValid(keystore);
if (!keyJson) {
console.error(new Error(`${ paramsFormat.message } Illegal keystore.`));
return false;
}
const scryptParams = keyJson.scryptparams;
const _encryptPwd = Buffer.from(keyJson.encryptp, 'hex');
try {
return getKeystore(key, _encryptPwd, scryptParams);
} catch (err) {
console.error(err);
return false;
}
}
export function encryptOldKeystore(privKey, pwd, selfScryptsy) {
const err = checkParams({ privKey, pwd }, [ 'privKey', 'pwd' ]);
if (err) {
throw new Error(err.message);
}
const AddressObj = createAddressByPrivateKey(privKey);
const scryptParams = {
n,
r,
p,
keylen: keyLen,
salt: Buffer.from(random()).toString('hex')
};
const getResult = (_encryptPwd, res) => {
const nonce = random(12);
const text = cipheriv({
rawText: AddressObj.privateKey,
pwd: _encryptPwd,
nonce,
algorithm
}, additionData);
const cryptoJSON = {
cipherName: algorithm,
KDF: scryptName,
ScryptParams: scryptParams,
CipherText: text,
Nonce: Buffer.from(nonce).toString('hex')
};
const encryptedKeyJSON = {
hexAddress: AddressObj.address,
crypto: cryptoJSON,
id: new UUID(1).format(),
keystoreVersion: 1,
timestamp: new Date().getTime()
};
return res(JSON.stringify(encryptedKeyJSON).toLocaleLowerCase());
};
return new Promise((res, rej) => {
encryptPwd(pwd, scryptParams, selfScryptsy).then(result => {
getResult(result, res);
}).catch(err => {
rej(err);
});
});
}
function getKeystore(rawText, pwd, scryptParams) {
const nonce = random(12);
const ciphertext = cipheriv({
rawText,
pwd,
nonce,
algorithm: algorithm
});
const cryptoJSON = {
cipherName: algorithm,
ciphertext,
Nonce: Buffer.from(nonce).toString('hex'),
KDF: scryptName,
scryptParams
};
const encryptedKeyJSON = {
uuid: new UUID(1).format(),
crypto: cryptoJSON,
version: currentVersion,
timestamp: new Date().getTime()
};
return JSON.stringify(encryptedKeyJSON).toLocaleLowerCase();
}