From 15e6cf599dcf9da45f6ec1ca4ca21acc4ccbe42d Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 14 Nov 2017 23:06:17 +0300 Subject: [PATCH] unecrypted wallet.dat keys reader --- package.json | 3 +- routes/appConfig.js | 1 - routes/shepherd.js | 1 + routes/shepherd/coindWalletKeys.js | 99 ++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 routes/shepherd/coindWalletKeys.js diff --git a/package.json b/package.json index 4213f1c..82a4a03 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "remote-file-size": "^3.0.3", "request": "^2.80.0", "sha256": "^0.2.0", - "socket.io": "^1.7.3" + "socket.io": "^1.7.3", + "wif": "^2.0.6" } } diff --git a/routes/appConfig.js b/routes/appConfig.js index e9cb64d..0da5311 100644 --- a/routes/appConfig.js +++ b/routes/appConfig.js @@ -15,7 +15,6 @@ const appConfig = { walletUnlockTimeout: 3600, }, cliStopTimeout: 1000, - disableKomododDownModal: false, failedRPCAttemptsThreshold: 10, stopNativeDaemonsOnQuit: true, }, diff --git a/routes/shepherd.js b/routes/shepherd.js index 4d6de77..222b6cf 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -107,6 +107,7 @@ shepherd = require('./shepherd/appInfo.js')(shepherd); shepherd = require('./shepherd/daemonControl.js')(shepherd); shepherd = require('./shepherd/auth.js')(shepherd); shepherd = require('./shepherd/coins.js')(shepherd); +shepherd = require('./shepherd/coindWalletKeys.js')(shepherd); shepherd.printDirs(); diff --git a/routes/shepherd/coindWalletKeys.js b/routes/shepherd/coindWalletKeys.js new file mode 100644 index 0000000..8ddbaf7 --- /dev/null +++ b/routes/shepherd/coindWalletKeys.js @@ -0,0 +1,99 @@ +module.exports = (shepherd) => { + /* + * type: GET + * + */ + shepherd.get('/coindwalletkeys', (req, res, next) => { + const wif = require('wif'); + const fs = require('fs'); + const chain = req.query.chain; + + // ref: https://gist.github.com/kendricktan/1e62495150ad236b38616d733aac4eb9 + let _walletDatLocation = chain === 'komodo' ? `${shepherd.komodoDir}/wallet.dat` : `${shepherd.komodoDir}/${chain}/wallet.dat`; + _walletDatLocation = chain === 'CHIPS' ? `${shepherd.chipsDir}/wallet.dat` : _walletDatLocation; + + try { + shepherd._fs.access(_walletDatLocation, shepherd.fs.constants.R_OK, (err) => { + if (err) { + shepherd.log(`error reading ${_walletDatLocation}`); + successObj = { + msg: 'error', + result: `error reading ${_walletDatLocation}`, + }; + + res.end(JSON.stringify(successObj)); + } else { + shepherd.log(`reading ${_walletDatLocation}`); + fs.readFile(_walletDatLocation, (err, data) => { + if (err) { + shepherd.log(`read wallet.dat err: ${err}`); + successObj = { + msg: 'error', + result: `error reading ${_walletDatLocation}`, + }; + + res.end(JSON.stringify(successObj)); + } else { + const re = /\x30\x81\xD3\x02\x01\x01\x04\x20(.{32})/gm; + const dataHexStr = data.toString('latin1'); + privateKeys = dataHexStr.match(re); + + if (!privateKeys) { + shepherd.log('wallet is encrypted?'); + + successObj = { + msg: 'error', + result: 'wallet is encrypted?', + }; + + res.end(JSON.stringify(successObj)); + } else { + let _keys = []; + privateKeys = privateKeys.map(x => x.replace('\x30\x81\xD3\x02\x01\x01\x04\x20', '')); + privateKeys = privateKeys.filter((v, i, a) => a.indexOf(v) === i); + shepherd.log(`found ${privateKeys.length} keys`); + + for (let i = 0; i < privateKeys.length; i++) { + const privateKey = new Buffer(Buffer.from(privateKeys[i], 'latin1').toString('hex'), 'hex'); + const key = wif.encode(0xbc, privateKey, true); + const keyObj = wif.decode(key); + const wifKey = wif.encode(keyObj); + + const keyPair = shepherd.bitcoinJS.ECPair.fromWIF(wifKey, shepherd.electrumJSNetworks.komodo); + const _keyPair = { + priv: keyPair.toWIF(), + pub: keyPair.getAddress(), + }; + + if (req.query.search) { + if (_keyPair.pub.indexOf(req.query.search) > -1) { + _keys.push(_keyPair); + } + } else { + _keys.push(_keyPair); + } + } + + successObj = { + msg: 'success', + result: _keys, + }; + + res.end(JSON.stringify(successObj)); + } + } + }); + } + }); + } catch (e) { + successObj = { + msg: 'error', + result: `error reading ${_walletDatLocation}`, + }; + + res.end(JSON.stringify(successObj)); + } + }); + + return shepherd; +}; \ No newline at end of file