From 969d58e7652b0604cc6eb5e243958ecfc02da92d Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sun, 7 Jan 2018 16:45:36 +0300 Subject: [PATCH] faster spv login --- main.js | 20 +--------- package.json | 4 +- routes/shepherd/electrum/auth.js | 55 +++++++++++++------------- routes/shepherd/electrum/coins.js | 10 +++++ routes/shepherd/electrum/keys.js | 65 +++++-------------------------- 5 files changed, 52 insertions(+), 102 deletions(-) diff --git a/main.js b/main.js index 9cb3651..44ffc41 100644 --- a/main.js +++ b/main.js @@ -17,7 +17,6 @@ const express = require('express'); const bodyParser = require('body-parser'); const fsnode = require('fs'); const fs = require('fs-extra'); -const numCPUs = require('os').cpus().length; const Promise = require('bluebird'); const arch = require('arch'); @@ -364,7 +363,6 @@ function createWindow(status, hideLoadingWindow) { if (closeAppAfterLoading) { mainWindow = null; loadingWindow = null; - pm2Exit(); } const staticMenu = Menu.buildFromTemplate([ // if static @@ -510,7 +508,7 @@ function createWindow(status, hideLoadingWindow) { } } - // if window closed we kill iguana proc + // close app mainWindow.on('closed', () => { appExit(); }); @@ -534,18 +532,6 @@ app.on('before-quit', (event) => { if (process.argv.indexOf('dexonly') > -1) { shepherd.killRogueProcess('marketmaker'); } - /*if (!forceQuitApp && - mainWindow === null && - loadingWindow != null) { // mainWindow not intitialised and loadingWindow not dereferenced - // loading window is still open - shepherd.log('before-quit prevented'); - shepherd.writeLog('quit app after loading is done'); - closeAppAfterLoading = true; - // obsolete(?) - let code = `$('#loading_status_text').html('Preparing to shutdown the wallet.
Please wait while all daemons are closed...')`; - loadingWindow.webContents.executeJavaScript(code); - event.preventDefault(); - }*/ }); // Emitted when all windows have been closed and the application will quit. @@ -571,10 +557,6 @@ app.on('quit', (event) => { } }) -app.on('activate', () => { - if (mainWindow === null) {} -}); - app.commandLine.appendSwitch('ignore-certificate-errors'); // dirty hack function formatBytes(bytes, decimals) { diff --git a/package.json b/package.json index 9dc2b4e..c9a73a2 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "author": "SuperNET Team", "license": "MIT", "devDependencies": { - "electron": "1.6.5", + "electron": "1.7.10", "electron-installer-debian": "^0.6.0", "electron-installer-redhat": "^0.5.0" }, @@ -40,7 +40,7 @@ "body-parser": "^1.15.2", "buffer-reverse": "^1.0.1", "coinselect": "github:bitcoinjs/coinselect", - "electron": "1.6.5", + "electron": "1.7.10", "express": "^4.14.0", "fix-path": "^2.1.0", "fs-extra": "^4.0.2", diff --git a/routes/shepherd/electrum/auth.js b/routes/shepherd/electrum/auth.js index 60554fc..f017c30 100644 --- a/routes/shepherd/electrum/auth.js +++ b/routes/shepherd/electrum/auth.js @@ -1,40 +1,43 @@ const bs58check = require('bs58check'); const bitcoinZcash = require('bitcoinjs-lib-zcash'); +const bitcoin = require('bitcoinjs-lib'); module.exports = (shepherd) => { shepherd.post('/electrum/login', (req, res, next) => { let _wifError = false; - for (let key in shepherd.electrumServers) { - const _abbr = shepherd.electrumServers[key].abbr; - const _seed = req.body.seed; - let keys; - let isWif = false; + for (let key in shepherd.electrumCoins) { + if (key !== 'auth') { + const _abbr = key; + const _seed = req.body.seed; + let keys; + let isWif = false; - try { - bs58check.decode(_seed); - isWif = true; - } catch (e) {} - - if (isWif) { try { - let key = shepherd.isZcash(_abbr.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true) : shepherd.bitcoinJS.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true); - keys = { - priv: key.toWIF(), - pub: key.getAddress(), - }; - } catch (e) { - _wifError = true; - break; + bs58check.decode(_seed); + isWif = true; + } catch (e) {} + + if (isWif) { + try { + let key = shepherd.isZcash(_abbr.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true) : bitcoin.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true); + keys = { + priv: key.toWIF(), + pub: key.getAddress(), + }; + } catch (e) { + _wifError = true; + break; + } + } else { + keys = shepherd.seedToWif(_seed, shepherd.findNetworkObj(_abbr), req.body.iguana); } - } else { - keys = shepherd.seedToWif(_seed, shepherd.findNetworkObj(_abbr), req.body.iguana); - } - shepherd.electrumKeys[_abbr] = { - priv: keys.priv, - pub: keys.pub, - }; + shepherd.electrumKeys[_abbr] = { + priv: keys.priv, + pub: keys.pub, + }; + } } shepherd.electrumCoins.auth = true; diff --git a/routes/shepherd/electrum/coins.js b/routes/shepherd/electrum/coins.js index 1130163..bc73562 100644 --- a/routes/shepherd/electrum/coins.js +++ b/routes/shepherd/electrum/coins.js @@ -54,6 +54,16 @@ module.exports = (shepherd) => { shepherd.log(`${coin} doesnt have any backup electrum servers`, true); } + if (Object.keys(shepherd.electrumKeys).length > 0) { + const _keys = shepherd.wifToWif(shepherd.electrumKeys[Object.keys(shepherd.electrumKeys)[0]].priv, coin); + + shepherd.electrumKeys[coin] = { + priv: _keys.priv, + pub: _keys.pub, + }; + console.log(shepherd.electrumKeys[coin]); + } + return true; } } diff --git a/routes/shepherd/electrum/keys.js b/routes/shepherd/electrum/keys.js index ff3fe2b..de01cc8 100644 --- a/routes/shepherd/electrum/keys.js +++ b/routes/shepherd/electrum/keys.js @@ -1,4 +1,5 @@ const sha256 = require('js-sha256'); +const buggySha256 = require('sha256'); const bip39 = require('bip39'); const crypto = require('crypto'); const bigi = require('bigi'); @@ -8,8 +9,10 @@ const bs58check = require('bs58check'); module.exports = (shepherd) => { shepherd.wifToWif = (wif, network) => { - const key = shepherd.isZcash(network) ? new bitcoinZcash.ECPair(d, null, { network: shepherd.getNetworkData(network) }) : new bitcoinZcash.ECPair(d, null, { network: shepherd.getNetworkData(network) }); - + network = network === 'KMD' ? 'komodo' : network.toLowerCase(); + console.log(shepherd.getNetworkData(network)); + const key = shepherd.isZcash(network) ? new bitcoinZcash.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true) : new bitcoin.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true); + return { pub: key.getAddress(), priv: key.toWIF(), @@ -20,7 +23,7 @@ module.exports = (shepherd) => { let bytes; if (process.argv.indexOf('spvold=true') > -1) { - bytes = shepherd.sha256(seed, { asBytes: true }); + bytes = buggySha256(seed, { asBytes: true }); } else { const hash = sha256.create().update(seed); bytes = hash.array(); @@ -33,7 +36,7 @@ module.exports = (shepherd) => { } const d = bigi.fromBuffer(bytes); - const keyPair = shepherd.isZcash(network) ? new bitcoinZcash.ECPair(d, null, { network: shepherd.getNetworkData(network) }) : new bitcoinZcash.ECPair(d, null, { network: shepherd.getNetworkData(network) }); + const keyPair = shepherd.isZcash(network) ? new bitcoinZcash.ECPair(d, null, { network: shepherd.getNetworkData(network) }) : new bitcoin.ECPair(d, null, { network: shepherd.getNetworkData(network) }); const keys = { pub: keyPair.getAddress(), priv: keyPair.toWIF(), @@ -48,7 +51,7 @@ module.exports = (shepherd) => { } shepherd.get('/electrum/wiftopub', (req, res, next) => { - let key = shepherd.isZcash(req.query.coin.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(req.query.wif, shepherd.electrumJSNetworks[req.query.coin], true) : shepherd.bitcoinJS.ECPair.fromWIF(req.query.wif, shepherd.electrumJSNetworks[req.query.coin], true); + let key = shepherd.isZcash(req.query.coin.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(req.query.wif, shepherd.electrumJSNetworks[req.query.coin], true) : bitcoin.ECPair.fromWIF(req.query.wif, shepherd.electrumJSNetworks[req.query.coin], true); keys = { priv: key.toWIF(), pub: key.getAddress(), @@ -97,7 +100,7 @@ module.exports = (shepherd) => { if (isWif) { try { - let key = shepherd.isZcash(_abbr.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true) : shepherd.bitcoinJS.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true); + let key = shepherd.isZcash(_abbr.toLowerCase()) ? bitcoinZcash.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true) : bitcoin.ECPair.fromWIF(_seed, shepherd.getNetworkData(_abbr.toLowerCase()), true); priv = key.toWIF(); pub = key.getAddress(); } catch (e) { @@ -151,7 +154,7 @@ module.exports = (shepherd) => { shepherd.post('/electrum/seed/bip39/match', (req, res, next) => { const seed = bip39.mnemonicToSeed(req.body.seed); - const hdMaster = shepherd.bitcoinJS.HDNode.fromSeedBuffer(seed, shepherd.electrumJSNetworks.komodo); // seed from above + const hdMaster = bitcoin.HDNode.fromSeedBuffer(seed, shepherd.electrumJSNetworks.komodo); // seed from above const matchPattern = req.body.match; const _defaultAddressDepth = req.body.addressdepth; const _defaultAccountCount = req.body.accounts; @@ -169,10 +172,6 @@ module.exports = (shepherd) => { priv: _key.keyPair.toWIF(), }; } - /*_addresses.push({ - pub: _key.keyPair.getAddress(), - priv: _key.keyPair.toWIF(), - });*/ } } } @@ -185,49 +184,5 @@ module.exports = (shepherd) => { res.end(JSON.stringify(successObj)); }); - // spv v2 - /*shepherd.get('/electrum/bip39/seed', (req, res, next) => { - const _seed = ''; - // TODO - const bip39 = require('bip39'); // npm i -S bip39 - const crypto = require('crypto'); - - // what you describe as 'seed' - const randomBytes = crypto.randomBytes(16); // 128 bits is enough - - // your 12 word phrase - const mnemonic = bip39.entropyToMnemonic(randomBytes.toString('hex')); - - // what is accurately described as the wallet seed - // var seed = bip39.mnemonicToSeed(mnemonic) // you'll use this in #3 below - const seed = bip39.mnemonicToSeed(_seed); - - console.log(seed); - - const successObj = { - msg: 'success', - result: { - servers: shepherd.electrumServers, - }, - }; - - res.end(JSON.stringify(successObj)); - - console.log(shepherd.bitcoinJS.networks.komodo); - const hdMaster = shepherd.bitcoinJS.HDNode.fromSeedBuffer(seed, shepherd.electrumJSNetworks.komodo); // seed from above - - const key1 = hdMaster.derivePath("m/44'/141'/0'/0/0"); - const key2 = hdMaster.derivePath('m/1'); - console.log(hdMaster); - - console.log(key1.keyPair.toWIF()); - console.log(key1.keyPair.getAddress()); - console.log(key2.keyPair.toWIF()); - - const hdnode = shepherd.bitcoinJS.HDNode.fromSeedBuffer(seed, shepherd.electrumJSNetworks.komodo).deriveHardened(0).derive(0).derive(1); - console.log(`address: ${hdnode.getAddress()}`); - console.log(`priv (WIF): ${hdnode.keyPair.toWIF()}`); - });*/ - return shepherd; }; \ No newline at end of file