Skip to content

Instantly share code, notes, and snippets.

@gabmontes
Created February 17, 2019 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabmontes/567f73186777c581318aa67b973a0976 to your computer and use it in GitHub Desktop.
Save gabmontes/567f73186777c581318aa67b973a0976 to your computer and use it in GitHub Desktop.
Walk through all addresses derived from a mnemonic and sum up balances
const got = require('got')
const Mnemonic = require('bitcore-mnemonic')
const mnemonic = '? ? ? ? ? ? ? ? ? ? ? ?' // 12-word mnemonic
const words = Mnemonic.Words.ENGLISH // or SPANISH, etc
const derivationPath = "m/44'/0'/0'" // BIP-44
const insightApiUrl = 'https://insight.bitpay.com/api'
const toBtc = sat => sat / 100000000
const hdPrivKey = new Mnemonic(mnemonic, words).toHDPrivateKey()
let balance = 0
function printBalance (addr) {
return got(`${insightApiUrl}/addr/${addr}/balance`, { json: true})
.then(function (res) {
console.log(`${addr}: ${toBtc(res.body)}`)
balance += res.body
})
}
function getBalance (accountKey, index) {
const privateKey = accountKey.derive(0).derive(index).privateKey
const addr = privateKey.toAddress().toString()
return addr && got(`${insightApiUrl}/txs?address=${addr}`, { json: true})
.then(res => res.body.txs.length && printBalance(addr).then(() => true))
}
function promiseLoop (fn) {
const loop = i => Promise.resolve(fn(i)).then(cont => cont ? loop(i + 1) : i)
return loop(0)
}
promiseLoop(i => getBalance(hdPrivKey.derive(derivationPath), i))
.then(function () {
console.log(`Total: ${toBtc(balance)}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment