-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathwallet.js
More file actions
144 lines (122 loc) · 3.78 KB
/
wallet.js
File metadata and controls
144 lines (122 loc) · 3.78 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
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
let walletApi = require('ethereumjs-wallet');
let ethUtil = require('ethereumjs-util');
let hdKey = require('ethereumjs-wallet/hdkey');
let fs = require('fs');
let Web3 = require('web3');
const web3 = new Web3();
const path = require('path');
const readline = require('readline-sync');
class WalletUtil {
static getKeystoreJSON(keyPath) {
keyPath = keyPath || path.resolve() + '/keystore.json';
let file = null;
try {
file = fs.readFileSync(keyPath, 'utf8');
} catch (e) {
throw e;
}
let json = {};
try {
json = JSON.parse(file);
} catch (e) {
throw e;
}
if (!json)
throw 'No JSON type keystore.';
if (json.Crypto)
json.crypto = json.Crypto;
return json;
}
static getWallet(keystore, password) {
let wallet = walletApi.fromV3(keystore, password);
return {
address: wallet.getAddressString(),
addressBuffer: wallet.getAddress(),
pk: wallet.getPrivateKey().toString('hex'),
pkBuffer: wallet.getPrivateKey()
}
}
static getWalletFromPK(pk) {
let wallet = walletApi.fromPrivateKey(pk);
return {
address: wallet.getAddressString(),
addressBuffer: wallet.getAddress(),
pk: wallet.getPrivateKey().toString('hex'),
pkBuffer: wallet.getPrivateKey(),
publicKey: wallet.getPublicKeyString()
}
}
static makeWallet(seed, password, encryptCount) {
let wallet = hdKey.fromMasterSeed(seed).getWallet();
return wallet.toV3(password, {
n: encryptCount || 8192
});
}
static migrateWallet(pk, password, encryptCount) {
pk = pk.replace('0x', '');
if (pk === '') {
let newaccount = web3.eth.accounts.create(web3.utils.randomHex(32));
pk = newaccount.privateKey;
if (password === ''){
console.log(newaccount.address, pk);
process.exit(0);
}
}
let pkBuffer = ethUtil.toBuffer('0x' + pk.replace('0x', ''));
let wallet = walletApi.fromPrivateKey(pkBuffer);
return wallet.toV3(password, {
n: encryptCount || 8192
});
}
}
if (process.argv[1].indexOf('wallet') !== -1) {
let argvAccount = getAccountFromParams();
if (argvAccount) {
let keystore = WalletUtil.migrateWallet(argvAccount.pk, argvAccount.pw);
let address = keystore.address;
let filename = `keystore_${address}.json`;
fs.writeFileSync('./' + filename, JSON.stringify(keystore), 'utf8');
console.log('Keystore file saved: ' + filename);
}
}
function getAccountFromParams() {
if (process.argv.length < 2)
return null;
let pk = process.argv[2];
let pw = process.argv[3];
if (pk.length === 66) {
pk = pk.replace('0x', '');
} else if (pk === 'load') {
let kpw = readline.question('Password: ', {
hideEchoBack: true
});
try {
console.log(WalletUtil.getWallet(WalletUtil.getKeystoreJSON(pw), kpw));
process.exit(0);
} catch (e) {
console.log(e);
}
} else if (pk === '0x') {
return {
pk:'',
pw:''
}
} else if (pk === 'new') {
pk = ''
} else if (pk.length !== 64) {
throw 'Account private key must be 32Byte Hex.'
}
if (!pw || pw.length == 0) {
pw = readline.question('Password: ', {
hideEchoBack: true
});
if (pw.length == 0) {
throw 'Password length muse be bigger than 1';
}
}
return {
pk: pk,
pw: pw
}
}
module.exports = WalletUtil;