-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-encrypted-wallet.js
43 lines (37 loc) · 1.47 KB
/
generate-encrypted-wallet.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
const CryptoJS = require("crypto-js");
const Arweave = require('arweave');
const fs = require('fs');
var BUILD_DIRECTORY = './build';
if (!fs.existsSync(BUILD_DIRECTORY)){
fs.mkdirSync(BUILD_DIRECTORY);
}
const arweave = Arweave.init({});
arweave.wallets.generate().then((key) => {
arweave.wallets.jwkToAddress(key).then((address) => {
encodeKeyAndGeneratePassword(address, key);
});
});
function prettyPrintPassword(password) {
const insertNewLinesAt = 16;
for (var i = 0; i < password.length; i += insertNewLinesAt) {
const maxIndex = Math.min(i + 8, password.length);
console.log(password.substring(i, maxIndex));
}
}
function encodeKeyAndGeneratePassword(address, key) {
const secretPassword = CryptoJS.lib.WordArray.random(64).toString();
console.log(`Secret Password is: ${secretPassword}. Write this down and don't lose it.`);
prettyPrintPassword(secretPassword);
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(key), secretPassword).toString();
buildIndexHtml(address, ciphertext);
}
function buildIndexHtml(publicAddress, cipherText) {
try {
var data = fs.readFileSync('./index-template.html', 'utf8');
data = data.replace(new RegExp('{{ wallet_address }}', 'g'), publicAddress);
data = data.replace(new RegExp('{{ encrypted_private_key }}', 'g'), cipherText);
fs.writeFileSync(`${BUILD_DIRECTORY}/index.html`, data);
} catch(e) {
console.log('Error:', e.stack);
}
}