From bf7181097415b570d4e1dd92611aff7fe5908b45 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 10 Mar 2022 10:33:34 -0800 Subject: [PATCH] Adding wallet sweep script for cleaning up test wallets --- package-lock.json | 22 +++++++++++ package.json | 1 + util/wallet/sweep-funds.js | 77 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 util/wallet/sweep-funds.js diff --git a/package-lock.json b/package-lock.json index 253d420..33aa0c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,6 +45,7 @@ }, "devDependencies": { "apidoc": "0.26.0", + "bch-token-sweep": "1.5.16", "chai": "4.3.0", "coveralls": "2.11.4", "eslint": "7.19.0", @@ -2972,6 +2973,17 @@ "minimal-slp-wallet": "4.4.1" } }, + "node_modules/bch-token-sweep": { + "version": "1.5.16", + "resolved": "http://94.130.170.209:4873/bch-token-sweep/-/bch-token-sweep-1.5.16.tgz", + "integrity": "sha512-9Fsb0qXUZ62OUDdH/tIF8QZDs12vWduoGDtU9txpSEF6GEyK0NOHfu8YYx5henoHz7SGS/fMUWDCibmedSpsmA==", + "dev": true, + "license": "MIT", + "dependencies": { + "bch-donation": "1.1.1", + "bignumber.js": "9.0.0" + } + }, "node_modules/bchaddrjs-slp": { "version": "0.2.5", "resolved": "http://94.130.170.209:4873/bchaddrjs-slp/-/bchaddrjs-slp-0.2.5.tgz", @@ -24320,6 +24332,16 @@ "version": "2.1.4", "requires": {} }, + "bch-token-sweep": { + "version": "1.5.16", + "resolved": "http://94.130.170.209:4873/bch-token-sweep/-/bch-token-sweep-1.5.16.tgz", + "integrity": "sha512-9Fsb0qXUZ62OUDdH/tIF8QZDs12vWduoGDtU9txpSEF6GEyK0NOHfu8YYx5henoHz7SGS/fMUWDCibmedSpsmA==", + "dev": true, + "requires": { + "bch-donation": "1.1.1", + "bignumber.js": "9.0.0" + } + }, "bchaddrjs-slp": { "version": "0.2.5", "resolved": "http://94.130.170.209:4873/bchaddrjs-slp/-/bchaddrjs-slp-0.2.5.tgz", diff --git a/package.json b/package.json index bacdcd5..6dfb5fd 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ }, "devDependencies": { "apidoc": "0.26.0", + "bch-token-sweep": "1.5.16", "chai": "4.3.0", "coveralls": "2.11.4", "eslint": "7.19.0", diff --git a/util/wallet/sweep-funds.js b/util/wallet/sweep-funds.js new file mode 100644 index 0000000..ea27250 --- /dev/null +++ b/util/wallet/sweep-funds.js @@ -0,0 +1,77 @@ +/* + This script will travers the HD wallet and sweep funds and tokens back + into the root address (index 0). That root address needs to have funds + to pay for the transactions. + + The root address will make a final transaction to consolidate all tokens. +*/ + +// Public npm libraries +const BCHJS = require('@psf/bch-js') +const BchTokenSweep = require('bch-token-sweep/index') + +// Local libraries +const WalletAdapter = require('../../src/adapters/wallet') + +async function sweepFunds () { + try { + // Open the wallet files. + const wallet = new WalletAdapter() + const walletInfo = await wallet.openWallet() + console.log('walletInfo: ', walletInfo) + + const rootAddr = walletInfo.cashAddress + const rootWif = walletInfo.privateKey + console.log(`Sweeping all funds into root address ${rootAddr}...`) + + // Generate an HD tree + const bchjs = new BCHJS() + const rootSeed = await bchjs.Mnemonic.toSeed(walletInfo.mnemonic) + const masterHDNode = bchjs.HDNode.fromSeed(rootSeed) + + let emptyAddrCnt = 0 + let hdIndex = 1 + + do { + // Generate a keypair from the HD wallet. + const childNode = masterHDNode.derivePath(`m/44'/245'/0'/0/${hdIndex}`) + // const cashAddress = bchjs.HDNode.toCashAddress(childNode) + const wifToSweep = bchjs.HDNode.toWIF(childNode) + + try { + // Sweep tokens from address + const sweeper = new BchTokenSweep( + wifToSweep, + rootWif, + bchjs, + 550, + rootAddr + ) + await sweeper.populateObjectFromNetwork() + + const hex = await sweeper.sweepTo(rootAddr) + // console.log(`hex: ${hex}`) + + const txid = await sweeper.blockchain.broadcast(hex) + + // console.log('Transaction ID', txid) + console.log(`Swept HD index ${hdIndex}. TXID: ${txid}`) + + emptyAddrCnt = 0 + + // Wait between loop iterations. + await bchjs.Util.sleep(3000) + } catch (err) { + console.log(`error message: ${err.message}`) + emptyAddrCnt++ + } + + hdIndex++ + } while (emptyAddrCnt < 5) + + console.log('5 empty addresses detected. Exiting.') + } catch (err) { + console.error('Error in sweepFunds(): ', err) + } +} +sweepFunds()