Skip to content

Instantly share code, notes, and snippets.

@marceloneil
Created November 29, 2020 21:50
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 marceloneil/4a5511af978e0a763a6f5f8602faf037 to your computer and use it in GitHub Desktop.
Save marceloneil/4a5511af978e0a763a6f5f8602faf037 to your computer and use it in GitHub Desktop.
Change xpub prefix
const bs58 = require('bs58')
const shajs = require('sha.js')
const start = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
const startBuf = bs58.decode(start)
// overwrite with new base58 key prefix. these prefixes can be found in chainparams.cpp for bitcoin forks:
// https://github.com/bitcoin/bitcoin/blob/7ae86b3c6845873ca96650fc69beb4ae5285c801/src/chainparams.cpp#L134
// https://github.com/dogecoin/dogecoin/blob/0b46a40ed125d7bf4b5a485b91350bc8bdc48fc8/src/chainparams.cpp#L170
const prefix = Buffer.from("02facafd", "hex")
prefix.copy(startBuf, 0, 0, 5)
// regenerate checksum
const dataBuf = startBuf.slice(0, 78)
let checksum = shajs('sha256').update(dataBuf).digest()
checksum = shajs('sha256').update(checksum).digest()
checksum = checksum.slice(0, 4)
// concat checksum
const endBuf = Buffer.concat([dataBuf, checksum], 82)
const end = bs58.encode(endBuf)
console.log(end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment