I'm looking to convert a Bitcoin extended public key from one format to another in Ruby.
In JS I've found https://github.com/jlopp/xpub-converter/blob/master/js/xpubConvert.js and Python https://github.com/lian/bitcoin-ruby/blob/master/lib/bitcoin.rb, from which I try to understand how it works.
But in Ruby I can't wrap my head around it. I use the Bitcoin::Util and my code is as follows:
require 'bitcoin'
include Bitcoin::Util
xpub = 'xpub6CUGRUonZSQ4TWtTMmzXdrXDtypWKiKrhko4egpiMZbpiaQL2jkwSB1icqYh2cfDfVxdx4df189oLKnC5fSwqPfgyP3hooxujYzAu3fDVmz'
prefixes = {
xpub: '0488b21e',
ypub: '049d7cb2',
zpub: '04b24746'
}
decoded_key = decode_base58(xpub)
zpub_key = encode_base58(prefixes[:zpub] + decoded_key[8..])
puts zpub_key # => zpub6r8o2p9croV2A7Gh2VZn42iEEv7QCxJrXyqWDUcV7aMapn2nY464gJKzfFTs2Ry4UnCFT1pmvSru6u1KX4GyRs2ti4SYydbtH17TgCSRAgQ
puts base58_checksum?(zpub_key) # => false
The checksum is invalid and I don't understand how to make it work