-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltc-privkeytoaddr.pl
262 lines (220 loc) · 10.2 KB
/
ltc-privkeytoaddr.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/perl
###########################################################################################################################
# NOTE: this script was created for Litecoin by adapting the corresponding Bitcoin script privkeytobitcoinaddress.pl
# See Litecoin Note.txt file for description of the changes.
###########################################################################################################################
# privkeytoltcaddress.pl
# ======================
# Convert Litecoin WIF-compressed or Bitcoin WIF-uncompressed private key into corresponding
# compressed/uncompressed Litecoin address.
# Usage :
# see `perl privkeytoltcaddress.pl -h'
#
# Testing
# =======
# Tested on Perl v5.12.3 on Windows 7
# Beware using on any older Perl version in case a feature is being used not available in that earlier version.
# Newer versions should be OK, so long as they maintain backward compatibility with Perl v5.12.3
#
use strict;
use warnings;
use Crypt::Digest::SHA256 qw(sha256);
use Crypt::Digest::RIPEMD160 qw(ripemd160);
use Crypt::PK::ECC;
use Crypt::Misc qw(decode_b58b encode_b58b);
# sub get_priv_key_bin
# ====================
# Usage : get_priv_key_bin($priv_key)
# Accepts as input a private key in WIF-compressed format (starting with a 'T') or
# WIF-uncompressed format (starting with a '6'), and returns the corresponding 32 byte long
# BINARY private key as a binary string.
sub get_priv_key_bin {
my $priv_key = $_[0];
my $first_char = substr($priv_key, 0, 1);
if ( $first_char ne 'T' && $first_char ne '6' ) {
printf(STDERR "get_priv_key_bin: Private key first character should be 'T' (compressed key),\nor '6' (uncompressed key), found '%s'\n", $first_char);
exit 1;
}
# function decode_bitcoin_base58 returns binary string
my $priv_key_decode = decode_bitcoin_base58($priv_key);
if ( $priv_key_decode eq "" ) {
printf(STDERR "get_priv_key_bin: Unable to Base58 decode private key %s\n", $priv_key);
exit 1;
}
&check_priv_key_decode($priv_key_decode, ($first_char ne '6') );
return substr($priv_key_decode, 1, 32);
}
# sub check_priv_key_decode
# =========================
# usage: check_priv_key_decode($priv_key_decode, $is_compressed_priv_key)
# Checks whether $priv_key_decode (binary string obtained by Base58 decoding a WIF-compressed or WIF-uncompressed
# format private key) has the correct format below :-
# <0xB0> <32 byte private key> 0x01 <4 checksum bytes> (compressed)
# <0xB0> <32 byte private key> <4 checksum bytes> (uncompressed)
# The boolean $is_compressed_priv_key determines whether to check for compressed or uncompressed key.
# Exits the program with error code 1 if an error is detected, otherwise returns to calling procedure taking no action.
sub check_priv_key_decode() {
my $priv_key_decode = $_[0];
my $is_compressed_priv_key = $_[1];
my $error_message;
my $compression_type = $is_compressed_priv_key ? "compressed" : "uncompressed";
my $required_length = $is_compressed_priv_key ? 38 : 37;
my $len = length($priv_key_decode);
if ( $len ne $required_length ) {
printf(STDERR "check_priv_key_decode: Invalid length of Base58 decoded WIF-%s private key: %u\nLength must be %u\n", $compression_type, $len, $required_length);
exit 1;
}
my $version_prefix = substr($priv_key_decode, 0, 1);
if ( $version_prefix ne "\xB0" ) {
printf(STDERR "check_priv_key_decode: Invalid version prefix in Base58 decoded WIF-%s private key: 0x%s\nVersion prefix 0xB0 required\n", $compression_type, &bin_to_hex($version_prefix) );
exit 1;
}
if ($is_compressed_priv_key) {
my $compression_suffix = substr($priv_key_decode, 33, 1);
if ( $compression_suffix ne "\x01" ) {
printf(STDERR "check_priv_key_decode: Invalid compression suffix in Base58 decoded WIF-compressed private key: 0x%s\nSuffix must be 0x01\n", &bin_to_hex($compression_suffix));
exit 1;
}
}
# note Crypt::Digest::SHA256 function sha256 always returns a 32 byte long binary string
# even if there are leading zero bytes
my $checksum = substr($priv_key_decode, -4);
my $checksum_calc = substr( sha256(sha256(substr($priv_key_decode, 0, -4))), 0, 4 );
if ( $checksum_calc ne $checksum ) {
printf(STDERR "check_priv_key_decode: Invalid checksum in Base58 decoded WIF-%s private key\n", $compression_type);
exit 1;
}
}
# sub get_litecoin_address
# ========================
# usage: get_litecoin_address($priv_key_bin, $is_compressed_priv_key)
# Accepts a private key as 32 byte BINARY string as input and returns the Base58Check encoded compressed or
# uncompressed Litecoin address for it (according as $is_compressed_priv_key is true or false). The returned
# Litecoin address always starts with an 'L'.
sub get_litecoin_address {
my $priv_key_bin = $_[0];
my $is_compressed_priv_key = $_[1];
my $pub_key;
# obtain 33 (or 65) byte long binary string containing compressed (or uncompressed) public key
my $ecc = Crypt::PK::ECC->new();
my $curve = "secp256k1";
$ecc->import_key_raw($priv_key_bin, $curve);
if ($is_compressed_priv_key) {
$pub_key = $ecc->export_key_raw('public_compressed');
} else {
$pub_key = $ecc->export_key_raw('public');
}
# Base58Check encode compressed/uncompressed public key into Litecoin address.
# Note Crypt::Digest::SHA256 function sha256 always returns a 32 byte long binary string
# even if there are leading zero bytes and
# Crypt::Digest::RIPEMD160 function ripemd160 always returns a 20 byte long binary string
# even if there are leading zero bytes
my $raw_litecoin_address = ripemd160(sha256($pub_key));
my $raw_litecoin_address_encode = "\x30$raw_litecoin_address";
my $checksum = sha256( sha256($raw_litecoin_address_encode) );
$raw_litecoin_address_encode .= substr($checksum, 0, 4);
return encode_b58b($raw_litecoin_address_encode);
}
###########################################################################################################
# NOTE: function decode_bitcoin_base58 identical to Bitcoin version in privkeytobitcoinaddress.pl
###########################################################################################################
# decode_bitcoin_base58
# =====================
# usage: decode_bitcoin_base58($base58_string)
# Takes a string as input parameter and returns :-
# (1) if string is a valid non-null Bitcoin Base58 string, the Bitcoin Base58 decoding of it as a binary string,
# otherwise,
# (2) the empty string
# NOTE: decode_b58b has a bug causing it to accept invalid Bitcoin Base58 characters of 0, O, I, l, assigning
# them same values as valid Bitcoin Base58 characters 1, R, K, p respectively. Otherwise this function appears
# to work. (See bug report).
sub decode_bitcoin_base58 {
my $base58_string = $_[0];
my $bitcoin_base58_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
if ($base58_string !~ /^[$bitcoin_base58_alphabet]+$/) {
return "";
}
# function decode_b58b returns binary string
my $decode = decode_b58b($base58_string);
if ( !defined($decode) ) {
return "";
} else {
return $decode;
}
}
# sub bin_to_hex
# ==============
# usage: bin_to_hex($bin)
# Takes a binary string as input parameter and returns equivalent hex string (using upper case A-F)
# of twice the length, with no preceding '0x'. Every byte is converted to a 2 hex digit representation,
# eg byte value 8 -> 08, byte value 254 -> FE
sub bin_to_hex {
my $bin = $_[0];
my $hex = "";
for my $i ( 0..length($bin) - 1 ) {
my $char = substr($bin, $i, 1);
$hex .= sprintf("%02X", ord($char));
}
return $hex;
}
######################################################################################################################
# Main code
######################################################################################################################
use constant BLANKLINE => "\n";
my $priv_key;
my $priv_key_bin;
my $first_char;
my $litecoin_address;
# two separate blocks use this (statefully)
my $param1;
if (@ARGV == 0 || $ARGV[0] eq "-h") {
print( "----------------------------------------------------------------\n" .
"privkeytoltcaddress.pl private key to Litecoin address converter\n" .
"----------------------------------------------------------------\n" .
BLANKLINE .
"Usage:\n" .
BLANKLINE .
"To convert Litecoin WIF-compressed or WIF-uncompressed private key to\n" .
"compressed/uncompressed Litecoin address :-\n" .
"perl privkeytoltcaddress.pl <key>\n" .
BLANKLINE .
"To take input from a file(s) :-\n" .
"perl privkeytoltcaddress.pl -f <filename1> <filename2> ... \n" .
"where each <filename> contains WIF format private keys (compressed or uncompressed),\n" .
"1 per line. Any amount of white space can appear at the start of a line.\n" .
BLANKLINE .
"A <filename> of '-' means the standard input, and an empty list of filenames means\n" .
"ALL the input is from standard input. Blank lines and lines beginning with '#' in a\n" .
"file are ignored. 1 line of output is produced for every line of input.\n" .
BLANKLINE .
"To process piped output from another program :-\n" .
"<other program> | perl privkeytoltcaddress.pl -f\n"
);
} elsif ( ($param1 = shift @ARGV) eq "-f" ) {
# read in all the lines from the input files
while (<>) {
chomp;
/\s*(\S*)/;
# skip any blank lines in input
next if ($1 eq "");
# skip any line beginning with '#'
next if ( substr($1, 0, 1) eq '#' );
$priv_key = $1;
$priv_key_bin = &get_priv_key_bin($priv_key);
# $first_char must be 'T' or '6' due to checks in &get_priv_key_bin
$first_char = substr($priv_key, 0, 1);
$litecoin_address = &get_litecoin_address($priv_key_bin, ($first_char ne '6') );
printf("%s\n", $litecoin_address);
}
} else {
if ( @ARGV != 0 ) {
print(STDERR "Invalid parameters : run 'perl privkeytoltcaddress.pl -h' for usage information.\n");
exit 1;
}
$priv_key = $param1;
$priv_key_bin = &get_priv_key_bin($priv_key);
# $first_char must be 'T' or '6' due to checks in &get_priv_key_bin
$first_char = substr($priv_key, 0, 1);
$litecoin_address = &get_litecoin_address($priv_key_bin, ($first_char ne '6') );
printf("%s\n", $litecoin_address);
}