4

I am trying to run base58perl.pl in my terminal using the following command:

perl base58perl.pl

but I get the following error:

Cannot decode! Invalid Base58 Character(s)!

Here's the code:

my $fileSrc = 'base58.txt';
open my $fhSrc, $fileSrc or die "Could not open $fileSrc: $!";

my $fileDest = 'hex.txt';
open( my $fhDest, '>>', $fileDest) or die "Could not open file $fileDest: $!";

while ( my $base58_encoded_address = <$fhSrc >)  {   
    my $binary_address = decodebase58tohex($base58_encoded_address);
    say $fhDest $binary_address;
}

close $fhSrc;
close $fhDest;

The content of base58.txt is a list of BTC address in base58 form.

I also have tried

chmod a+x base58perl.pl
perl base58perl.pl

base58.txt contents:

1E5PBfSaFawBy1RjBHkS6FDtCwXkYSsVTo
1DCgptTS2uY2occbVdW1qcVT72T75RXbyg
1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb 

I still get the same error.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Quin Noaj
  • 65
  • 1
  • 10
  • 1
    Where is the `decodebase58tohex`function from? Some module from CPAN? Or did you write that yourself? The error message is pretty clear. Your input is flawed. – simbabque Nov 22 '17 at 12:56
  • A bit of googling found me http://lenschulwitz.com/b58/base58perl.txt, which implements a function with that name. – simbabque Nov 22 '17 at 12:58
  • Please [edit] and include the content of _base58.txt_ in your question. – simbabque Nov 22 '17 at 12:59
  • @simbabque the code originally came from [here](http://lenschulwitz.com/b58/base58perl.txt) – Quin Noaj Nov 22 '17 at 13:00
  • Works for me. I was going to say you have to `chomp`, but I don't get the error message with a direct copy of your code, only adapted to read from `DATA`. Are you sure you provided the real data? The error message is from the 3rd line of `unbase58`. Just try adding `chomp $base58_encoded_address;` as the first line in the loop. – simbabque Nov 22 '17 at 13:06
  • yes I just replace the bottom part to accept the base58.txt It's weird it's not working in my terminal. – Quin Noaj Nov 22 '17 at 13:19
  • Did you try to add `chomp`? – simbabque Nov 22 '17 at 13:21
  • yeah but it but it returned compilation errors. – Quin Noaj Nov 22 '17 at 13:24
  • Looks like you are not user to writing Perl code. Update the question please. – simbabque Nov 22 '17 at 13:25
  • yeah it's my first time in Perl. I just needed to convert a bunch of base58 address that's why I bumped into this Perl code. – Quin Noaj Nov 22 '17 at 13:27
  • Well you did a good job of reading and writing files. Googled the right stuff I guess. ;-) – simbabque Nov 22 '17 at 13:28
  • 1
    Nope. That's not what we do here. Sorry. – simbabque Nov 22 '17 at 13:50
  • Your Perl code is *not* what you are running: there is no `decodebase58tohex` defined and the program will simply die, without producing the output that you say it does, saying `Undefined subroutine &main::decodebase58tohex called`. Furthermore, *Stack Overflow* is a site for professionals and accomplished hobbyists to ask for peer help with a specific programming problem. It isn't the best place for someone who has no idea what they're doing to get their work done for free, or to find a tutorial. – Borodin Nov 22 '17 at 14:24

4 Answers4

0

That error message comes from the unbase58 function in the code you have linked.

die "Cannot Decode! Invalid Base58 Character(s)!\n" unless $bitcoin_address =~ /^[1-9A-HJ-NP-Za-km-z]*$/;

That line checks if the input contains only characters of the character group [1-9A-HJ-NP-Za-km-z]. Since your input does, it must dislike something else.

My guess is that it disliked the newline characters at the end of your lines. You need to chomp them off before passing the value to decodebase58tohex.

while( my $base58_encoded_address = <$fhSrc>)  {   
    chomp $base58_encoded_address;
    my $binary_address = decodebase58tohex($base58_encoded_address);
    say $fhDest $binary_address;
}
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • Yes I included `chomp` yet it return the same error. – Quin Noaj Nov 22 '17 at 14:20
  • @Quin Then your data contains something else. Is the file from a different operating system? Maybe it has Windows line endings, and you're on Linux? Copy/paste it manually into a new file file with a graphical editor. That often helps to fix line endings. – simbabque Nov 22 '17 at 14:25
  • Will try to install Perl on Windows and check if it will return some good results. – Quin Noaj Nov 22 '17 at 14:27
  • @QuinNoaj that's an aweful lot of work just for that. Instead, just see if you can covert the line endings. – simbabque Nov 22 '17 at 14:27
  • sorry but what is line endings? I tried to run the script in cmd and I got a different error `Can't locate object method "say"` – Quin Noaj Nov 22 '17 at 14:51
  • @QuinNoaj you are clearly not familiar with how programming works. The first step after you get an error message that you do not understand is always to put that error message into Google. In your case, you will be pointed towards the Perl documentation for `say`, http://perldoc.perl.org/functions/say.html. It will tell you what `say` does, and why you see that error message. – simbabque Nov 22 '17 at 15:01
0

You probably need to remove whitespace. You appear to be passing only chunks of the string to the decode function at a time, which could also be a problem. Read the whole file into a var, remove any whitespace, then decode.

my $base58_encoded_address = do { local $/; <$fhSrc> };
$base58_encoded_address =~ s/\s+//g;
my $binary_address = decodebase58tohex($base58_encoded_address);
say $fhDest $binary_address;
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Thanks this kinda worked (no error) but throws a single same hex output though. – Quin Noaj Nov 22 '17 at 15:36
  • Same output as what? Are you implying you think that output is wrong? Is that a question? I see you still haven't fixed your question to provide a minimal, runnable demonstration of the problem (See [mcve]). – ikegami Nov 22 '17 at 15:38
  • Each line in that file represents one record, and they want to convert each record separately I believe. – simbabque Nov 23 '17 at 16:20
0

The script is now working properly, the problem was the base58.txt the file was created using notepad. I created a new file using a different text editor.

Quin Noaj
  • 65
  • 1
  • 10
0
my $fileSrc = 'base58.txt';
open my $fhSrc, $fileSrc or die "Could not open $fileSrc: $!";

my $fileDest = 'hex.txt';
open( my $fhDest, '>>', $fileDest) or die "Could not open file $fileDest: $!";

my @tmp = <$fhSrc>;
chomp @tmp;
for my $line (@tmp)  {   
    print "decoding '$line'\n";
    my $binary_address = decodebase58tohex($line);
    say $fhDest $binary_address;
}

close $fhSrc;
close $fhDest;

As someone else mentioned I think your dealing with whitespaces. chomp will take care of that for you.

The next thing to do is print the string you are trying to decode in quotes which will confirm your only decoding what you want to.

Jacob Jarick
  • 63
  • 1
  • 7