Created
February 3, 2018 23:48
-
-
Save woky/5c00cd2436fbece228116a5dd65313fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const crypto = require("crypto"); | |
const sqlite3 = require('sqlite3').verbose(); | |
const sqljs = require('sql.js'); | |
const zencashjs = require('zencashjs'); | |
function decryptWallet(login, password, path) { | |
let i = Buffer.byteLength(login); | |
let inputBytes = fs.readFileSync(path); | |
let recoveredLogin = inputBytes.slice(0, i).toString("utf8"); | |
console.log('recovered user name: ' + recoveredLogin); | |
let outputBytes = []; | |
let iv = inputBytes.slice(0, i + 64); | |
i += 64; | |
let salt = inputBytes.slice(i, i + 64); | |
i += 64; | |
let tag = inputBytes.slice(i, i + 16); | |
i += 16; | |
let encrypted = inputBytes.slice(i); | |
let key = crypto.pbkdf2Sync(password, salt, 2145, 32, "sha512"); | |
let decipher = crypto.createDecipheriv("aes-256-gcm", key, iv); | |
decipher.setAuthTag(tag); | |
outputBytes = decipher.update(encrypted, "binary", "binary"); | |
try { | |
outputBytes += decipher.final("binary"); | |
} catch (err) { | |
/* | |
* Let's hope node.js crypto won't change error messages. | |
* https://github.com/nodejs/node/blob/ee76f3153b51c60c74e7e4b0882a99f3a3745294/src/node_crypto.cc#L3705 | |
* https://github.com/nodejs/node/blob/ee76f3153b51c60c74e7e4b0882a99f3a3745294/src/node_crypto.cc#L312 | |
*/ | |
if (err.message.match(/Unsupported state/)) { | |
/* | |
* User should be notified that wallet couldn't be decrypted because of an invalid | |
* password or because the wallet file is corrupted. | |
*/ | |
outputBytes = []; | |
} else { | |
// FIXME: handle other errors | |
throw err; | |
} | |
} | |
return outputBytes; | |
} | |
if (process.argv.length < 2+3) { | |
console.error("Usage: decrypt-arizen-db awdpath username password"); | |
process.exit(1); | |
} | |
let encfile_path = process.argv[2]; | |
let username = process.argv[3]; | |
let password = process.argv[4]; | |
let sqlite_bytes = decryptWallet(username, password, encfile_path); | |
let sqlite_path = encfile_path.replace(/\.awd/, '') + '.sqlite3'; | |
if (sqlite_bytes.length) { | |
fs.writeFileSync(sqlite_path, sqlite_bytes); | |
} else { | |
console.error("Incorrect password"); | |
} | |
let db = new sqljs.Database(sqlite_bytes); | |
let sql_ret = db.exec('select pk, addr from wallet'); | |
if (sql_ret.length) { | |
let rows = sql_ret[0].values; | |
for (let row of rows) { | |
let wif = zencashjs.address.privKeyToWIF(row[0]); | |
let addr = row[1]; | |
console.log(wif, addr); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "unpack-arizen-db", | |
"version": "0.1", | |
"dependencies": { | |
"sql.js": "^0.5.0", | |
"sqlite3": "^3.1.13", | |
"zencashjs": "^1.1.9-a" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment