Adding wallet sweep script for cleaning up test wallets

This commit is contained in:
Chris Troutner
2022-03-10 10:33:34 -08:00
parent e68514f542
commit bf71810974
3 changed files with 100 additions and 0 deletions
+22
View File
@@ -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",
+1
View File
@@ -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",
+77
View File
@@ -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()