Skip to content

Instantly share code, notes, and snippets.

@moocowmoo
Forked from nmarley/conv.rb
Created January 27, 2016 02:07
Show Gist options
  • Save moocowmoo/4eb3763efb7d8aef5356 to your computer and use it in GitHub Desktop.
Save moocowmoo/4eb3763efb7d8aef5356 to your computer and use it in GitHub Desktop.
Convert Dash BIP32 extended public key version prefix from 'xpub' to 'drkv'
#! /usr/bin/env ruby
# Example of converting Dash BIP32 extended public key (incorrectly) beginning
# with 'xpub' to correct prefix 'drkv'.
#
require 'bitcoin'
# test extended pub key
xpub = 'xpub661MyMwAqRbcF3jZGiw3QBt7nD13a7k8jVt9me27BL1vSeSpviDj6udJ5cnFyr2q2ofw7kMKGUepVhVsbLVsckxGzDmvnpGPr88CJAz1Ab5'
# Dash extended public key prefix
drkv_prefix = '02fe52f8'
# decode base58 pubkey
bin = Bitcoin.decode_base58(xpub)
# replace 4-byte version prefix
bin[0..7] = drkv_prefix
# add the checksum to the 78-byte xpub key
drkbin = bin[0..155] + Bitcoin.checksum(bin[0..155])
# base58 encode again
drk = Bitcoin.encode_base58(drkbin)
# result has proper 'drkv' prefix and generates same addresses
puts drk
# => drkvjJe5sJgqomjLnD39LKGdme64zzL4d38fDEhkveYsjvqDZzEZSoq6VEE5znetSRvSB6pYAxhTViXJdZ5QygKogD4nsa31hQ8aVuW6psczteC
# original incorrect 'xpub' key:
xpub661MyMwAqRbcF3jZGiw3QBt7nD13a7k8jVt9me27BL1vSeSpviDj6udJ5cnFyr2q2ofw7kMKGUepVhVsbLVsckxGzDmvnpGPr88CJAz1Ab5
# First two receiving addresses generated by Electrum:
XfodG6PGyXksxznJXzKFPgU1q9QdPBNYB2
Xb4AKDUJBkjUSYVzuMXxLAxkUJaWomozGT
# ----------------------------------------------------------------------
# correctly convert 'drkv' key:
drkvjJe5sJgqomjLnD39LKGdme64zzL4d38fDEhkveYsjvqDZzEZSoq6VEE5znetSRvSB6pYAxhTViXJdZ5QygKogD4nsa31hQ8aVuW6psczteC
# First two receiving addresses generated by Electrum:
XfodG6PGyXksxznJXzKFPgU1q9QdPBNYB2
Xb4AKDUJBkjUSYVzuMXxLAxkUJaWomozGT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment