Hi guys, I was having a little trouble working out a nice, safe way to get my private keys out of the data.xml for importation back into Mycelium. I think I could have used bitaddress.org but I decided to write something to do it for me. I'm letting it out to the public in case anyone else might find it useful (it also might be interesting to study for other purposes). It must be run in the same directory as the data.xml and it will spit out a list of the public, private and WIF encoded keys to the terminal and will create a webpage with the public and private keys listed on it as QR codes ready for scanning (I would recommend using a browser on the local filesystem to open it and not having it on a live web server (unless you're totally internet disconnected and probably not even then)).
This may be useful for creating a paper backup even. You may want to increase the level of the QR code in that case. Currently set to "L" for low.
#!/usr/bin/perl
#Copyright 2014 Richard Thomas
#Free for noncommercial use
#Commercial license available on request.
#Tips to: 13qnEgPTxJW6mm88dLpnHXZyryN5EXBciq
use XML::Simple qw(:strict);
use Imager::QRCode;
use Math::GMP;
use Digest::SHA qw{sha256};
use strict;
use warnings;
my $qrcode = Imager::QRCode->new(
size => 4,
margin => 2,
version => 1,
level => 'L',
casesensitive => 1,
lightcolor => Imager::Color->new(255, 255, 255),
darkcolor => Imager::Color->new(0, 0, 0),
);
my $p1 = XMLin('data.xml',KeyAttr => { string => 'name' },ForceArray=>['string']);
my $c=$p1->{'string'}->{'records'}->{'content'};
my @v=split('\\|',$c);
my $p=2;
open (my $fp, ">" ,"index.html") or die ("Must be writeable");
print $fp "
My keys\n";
print $fp "
\n";
print $fp "Wallet | Public | Private |
\n";
while($pprint $fp "$v[$p] | | "; print "$p $v[$p] $v[$p+2] " , key2wif($v[$p+2]) . "\n"; my $img=$qrcode->plot($v[$p]); $img->write(file=>"$v[$p].gif"); if($v[$p+2]){ $img=$qrcode->plot(key2wif($v[$p+2])); $img->write(file=>"$v[$p]_private.gif"); print $fp ""; } print $fp " |
\n";
$p+=8
}
print $fp "
\n";
print $fp "\n";
close $fp;
sub key2wif{
my $key=shift;
my $out = hex2bin($key);
my $out2=$out= "\x80" . $out;
$out=sha256($out);
$out=sha256($out);
$out=$out2.substr($out,0,4);
$out=gmpdec58(bin2hex($out));
return $out;
}
sub bin2hex{
my $b=shift;
return unpack("H*",$b);
}
sub hex2bin{
my $h=shift;
return pack("H*" ,unpack("A*" ,$h));
}
sub gmpdec58{
my $h=shift;
my $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
my $t='';
my $remain=Math::GMP->new("$h",16);
my $c=0;
while($remain!=0 && $c++<2000){
my $last=$remain%58;
$remain=$remain/58;
$t=substr($base58chars,$last,1).$t;
}
return $t;
}