Skip to content

Instantly share code, notes, and snippets.

@justinmoon
Created April 3, 2020 03:08
Show Gist options
  • Save justinmoon/9016285deacadf32138289758113b29f to your computer and use it in GitHub Desktop.
Save justinmoon/9016285deacadf32138289758113b29f to your computer and use it in GitHub Desktop.
bitcoin.conf javascript utils
// Just update for a single network. Maybe they don't want to enable their mainnet RPC, for instance ...
function updateBitcoinConfig(config, network) {
// Would be better if this required confirmation ...
delete config['global']['testnet']
delete config['global']['regtest']
config[network]['server'] = 1
config[network]['disablewallet'] = 0
return config
}
function writeBitcoinConfig(config) {
const bitcoinConfigPath = getBitcoinConfigPath()
const writer = fs.createWriteStream(bitcoinConfigPath)
// FIXME: wrap in a promise
writer.once('open', function (fd) {
// global
for (const [key, value] of Object.entries(config['global'])) {
writer.write(`${key}=${value}\n`)
}
// mainnet
if (Object.keys(config['mainnet']).length) {
writer.write('[main]\n')
for (const [key, value] of Object.entries(config['mainnet'])) {
writer.write(`${key}=${value}\n`)
}
}
// testnet
if (Object.keys(config['testnet']).length) {
writer.write('[test]\n')
for (const [key, value] of Object.entries(config['testnet'])) {
writer.write(`${key}=${value}\n`)
}
}
// regtest
if (Object.keys(config['regtest']).length) {
writer.write('[regtest]\n')
for (const [key, value] of Object.entries(config['regtest'])) {
writer.write(`${key}=${value}\n`)
}
}
// Close file descriptor
writer.end()
})
}
async function rewriteBitcoinConfig(network) {
const config = await readBitcoinConfig(network)
const updatedConfig = updateBitcoinConfig(config, network)
writeBitcoinConfig(updatedConfig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment