Skip to content

Instantly share code, notes, and snippets.

@junderw
Created September 6, 2017 06:41
Show Gist options
  • Save junderw/cfc288985d6b814af48880b05fd920a7 to your computer and use it in GitHub Desktop.
Save junderw/cfc288985d6b814af48880b05fd920a7 to your computer and use it in GitHub Desktop.
Convert all your existing addresses from bitcoind to segwit P2SH addresses (multisig will not work)
'use strict';
// first, run sudo apt-get install jq nodejs npm
// second, run npm install bluebird co to install dependencies, and make sure bitcoin-cli can run these commands.
// third, run node thisScript.js and it will change all the addresses on your bitcoind into segwit addresses.
var Promise = require('bluebird');
var co = require('co');
var exec = Promise.promisify(require('child_process').exec);
var main = function() {
return co(function*() {
var accountsString = yield exec("bitcoin-cli listaccounts | jq -r 'keys'");
var accountsArray = JSON.parse(accountsString);
var finalData = {};
for (var i = 0; i < accountsArray.length; i++) {
var addressesString = yield exec("bitcoin-cli getaddressesbyaccount \"" + accountsArray[i] + "\" | jq -r '.'");
var addressesArray = JSON.parse(addressesString);
finalData[accountsArray[i]] = {};
for (var j = 0; j < addressesArray.length; j++) {
if (addressesArray[j].slice(0,1) == "1") {
var segWitAddress = yield exec("bitcoin-cli addwitnessaddress " + addressesArray[j]);
finalData[accountsArray[i]][addressesArray[j]] = segWitAddress.trim();
}
}
}
return JSON.stringify(finalData, null, 2);
});
};
main().then(function (result) { console.log(result) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment