-
Notifications
You must be signed in to change notification settings - Fork 20
/
bip38_decryption_helper.js
56 lines (36 loc) · 1.36 KB
/
bip38_decryption_helper.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
'use strict';
var CoinKey = require('coinkey'),
bip38 = require('bip38'),
wif = require('wif');
module.exports = class Bip38DecryptionHelper {
constructor (publicAddress, encryptedPrivateKey, secret, log) {
this.publicAddress = publicAddress;
this.encryptedPrivateKey = encryptedPrivateKey;
this.secret = secret;
this.log = log || console.log;
return this;
}
decrypt () {
this.log('Decrypting "' + this.encryptedPrivateKey + '" with "' + this.secret + '"...');
this.decryptedKey = bip38.decrypt(this.encryptedPrivateKey, this.secret, this.logProgessInPercent);
this.privateKeyWif = wif.encode(128, this.decryptedKey.privateKey, this.decryptedKey.compressed);
this.isValidSecret = this.publicAddressesMatch();
return this;
}
getResult () {
return {
decryptedKey: this.decryptedKey,
privateKeyWif: this.privateKeyWif,
isValidSecret: this.isValidSecret
}
}
logProgessInPercent (status) {
return;
var ansiEscapeCode = '\x1b[0G';
process.stdout.write("Decrypting " + Math.round(status.percent) + '%' + ansiEscapeCode);
}
publicAddressesMatch () {
var coinKey = CoinKey.fromWif(this.privateKeyWif)
return coinKey.publicAddress === this.publicAddress;
}
};