Created
June 5, 2018 22:00
-
-
Save miguelmota/0b812d709c390d174fcb623b218aeb69 to your computer and use it in GitHub Desktop.
Electrum client get balance in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ElectrumClient = require('electrum-client') | |
const peers = require('electrum-host-parse').getDefaultPeers('BitcoinSegwit').filter(v => v.ssl) | |
const getRandomPeer = () => peers[peers.length * Math.random() | 0] | |
let mainClient = false | |
const main = async () => { | |
const peer = getRandomPeer() | |
console.log('begin connection:', JSON.stringify(peer)) | |
mainClient = new ElectrumClient(peer.ssl, peer.host, 'ssl') | |
try { | |
await mainClient.connect() | |
const ver = await mainClient.server_version('2.7.11', '1.0') | |
console.log('connected to ', ver) | |
} catch (e) { | |
console.log('bad connection:', JSON.stringify(peer)) | |
console.log('trying again') | |
return main() | |
} | |
} | |
async function getBalance(address) { | |
await main() | |
let balance = await mainClient.blockchainAddress_getBalance(address) | |
return balance.confirmed | |
} | |
module.exports = { | |
getBalance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment