-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbip38-decrypt.js
32 lines (27 loc) · 1.11 KB
/
bip38-decrypt.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
'use strict'
const c = console
const fs = require('fs')
const exists = fs.existsSync
const readFile = fs.readFileSync
const bip38 = require('bip38')
const wif = require('wif')
const decrypt = bip38.decrypt
const keyToWif = (privateKey) => {
return wif.encode(0x80, privateKey.privateKey, privateKey.compressed)
}
const bip38PrivateKey = process.env.BIP38_PRIVATE_KEY || process.argv[2]
const passwordFile = process.env.PASSWORD_FILE || process.argv[3] || ".password"
if (!bip38PrivateKey) throw new Error("Empty BIP38 private Key, aborting!")
if (!exists(passwordFile)) throw new Error(`Password file '${passwordFile}' not found, aborting!`)
let password = readFile(passwordFile)
const emptyPasswordError = new Error("Empty BIP38 password, aborting!")
if (!password || password == "") throw emptyPasswordError
password = password.toString().trim()
if (password == "") throw new emptyPasswordError
if (process.env.NODE_ENV != "test") c.log("decrypting private key...")
module.exports = {
bip38PrivateKey: bip38PrivateKey,
password: password,
decrypt: decrypt,
keyToWif: keyToWif,
}