Bitcoin Forum
November 08, 2024, 07:05:39 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bitcoin Perl library  (Read 4581 times)
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
December 19, 2011, 08:10:28 AM
Last edit: June 13, 2012, 10:02:28 AM by grondilu
Merited by ABCbits (2)
 #1

I know you guys you all like Python and do plenty of bitcoin-related stuff in this language and that's cool.

But for people like me who are much more script lovers, a complete Perl bitcoin library would be great.

So, while I was translating ThomasV's Electrum client, I happened to had written such a library. I think it is already awesome, so I think it worth publishing it.

You can see the main POD in html here.

The tgz is there


In a nutshell, you can do stuffs such as:

Code:
use Bitcoin qw(BTC);

printf "you owe me %d%s.\n", 10, BTC;

use Bitcoin::Address;

my $addr = new Bitcoin::Address '1DxH3bjYeCKbSKvVEsXQUBjsTcxagmWjHy'; # this does checksum verif

use Bitcoin::PrivateKey;

my $key = new Bitcoin::PrivateKey; # generates a random private key
print $key->address; # prints the corresponding bitcoin address

mndrix
Michael Hendricks
VIP
Sr. Member
*
Offline Offline

Activity: 447
Merit: 258


View Profile
December 19, 2011, 06:08:58 PM
 #2

Publish to CPAN?
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
December 20, 2011, 12:18:57 PM
 #3

Publish to CPAN?

Yeah I will eventually but I don't know CPAN much and I don't have a regular internet access so it might take some time.

grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
January 04, 2012, 10:25:11 AM
Last edit: January 04, 2012, 10:49:35 AM by grondilu
 #4

Until I publish to CPAN, I set up a github repo:
https://github.com/grondilu/libbitcoin-perl

It now ships a bitcoin block dumper !!

To my amazement, it does actually fly pretty well:

Code:
# I want to check out the 100,000th block:

use Bitcoin::Block;
use Data::Dumper;

print +Dumper new Bitcoin::Block 100_000;


Boussac
Legendary
*
Offline Offline

Activity: 1221
Merit: 1025


e-ducat.fr


View Profile WWW
January 04, 2012, 12:12:54 PM
 #5

Thanks Grondilu this is awesome!

grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
January 05, 2012, 04:04:11 PM
 #6


This gave me a crazy idea recently.  I was planning on writing a CGI script so I can browse the block chain with my local thttpd web server (kind of a local bitcoinexplorer), and then I thought that it would be cool if I could do it all inside Vim, as I spend so much time inside this text editor anyway.  Vim has a help syntax with hyperlink-like capabilities.  I'm pretty sure it can be done.

Also, Vim has support for both Perl and Python scripting, so if the thing works with one language, it should work with the other.

PS.  By the way, the regex search should work now.


grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
January 23, 2012, 07:21:01 AM
 #7


The scripting mecanism is now implemented.  It hasn't been tested much though.

Also, a "master key" class has been created.  So it is possible to derive a key from an other one, using a 32 bytes string.

Code:
my $master_key = new Bitcoin::Key::Master;
my $slave_key = $master_key << "some string identifying an account";


Information is completely included in the secret exponent, in a reversible way (I used the master key as a symmetric encryption key to do so):

Code:
my $account_name = $master_key >> $slave_key;


I must say I am a bit disappointed by the lack of interest in this project.  I know much people here only love python, but still...

mndrix
Michael Hendricks
VIP
Sr. Member
*
Offline Offline

Activity: 447
Merit: 258


View Profile
January 23, 2012, 03:06:13 PM
 #8

I must say I am a bit disappointed by the lack of interest in this project.  I know much people here only love python, but still...

I too hear little from users of Finance::MtGox module or my Haskell interface to Bitcoin RPC.  However, I occasionally get an email or bug report, so I know there are some users.  They're just mostly quiet.  I bet your project is similar.  Many Perl developers I've worked with only search CPAN for modules.  Perhaps uploading it there could draw more attention.

Anyway, keep up the good work.
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
June 12, 2012, 01:03:54 PM
 #9



Developping a bitcoin library in Perl6 gave me a lot of ideas about how to improve this library in Perl5.

I can't publish just yet but it's gonna come out soon.  Stay tuned.

grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
June 13, 2012, 09:25:40 AM
Last edit: June 13, 2012, 09:42:22 AM by grondilu
 #10

Ok, maybe it's a bit early to push to github but I did it anyway because I really like what I've done and I want to show it to people.  I should have created an 'experimental' branch but I totally forgot and I don't master git enough to revert the process.  So it's still in the main branch.

I went back to basics, got rid of unnecessary stuff (such as encryption and master keys) and focus on dealing efficiently with keys and addresses.

Now, you don't have to 'use Bitcoin::Key' or 'use Bitcoin::Address', just:

Code:
use Bitcoin;

You don't have to explicitely call a constructor, either.  I overloaded the constants interpretation with the overload pragma and its overload::constants function.  So now when you type a string litteral that looks like a bitcoin address or a bitcoin key in WIF, perl checks it and see what it is exacly (while verifying the checksum, of course).

This allows you to write something like:

Code:
say "5KFtidLNEgFicv1QWGjMUECprKEQdLEqDGA7V5F8PJDZxaazRG7"->address;

Perl will magically recognize this is a bitcoin key so it will create the corresponding object before calling its address method.  This should normally print "1NZunrMgUVoe7H46GZW3x1NUxGPSyJUCSL".


Also, now Bitcoin::Key inherits from Math::BigInt, so you can transparently do arithmetics on them and you'll get a derived key.  This can be usefull for defining a master key or for Diffie-Hellman like schemes:

Code:
say "5KFtidLNEgFicv1QWGjMUECprKEQdLEqDGA7V5F8PJDZxaazRG7" * 2;

One thing that still needs to be done is having this arithmetics being modular, but it should not be too difficult.

Random generation of a new key is done with a special constructor called 'random':

Code:
say random Bitcoin::Key;

It is actually a constructor for EC::DSA::PrivateKey, from whom Bitcoin::Key inherits too (yes, I've done multiple inheritance).   I think it makes sense since a bitcoin key is both a Base58 encoded data and a ECDSA privatekey.

Also, I've decided to use environment variables to tune the behavior of the library.  For instance, if you want to work with the test network, just define the BITCOIN_TEST env var (you can put anything in it, it doesn't matter):

Code:
$ export BITCOIN_TEST=yes
$ perl -wE 'use Bitcoin; [your code here...]'
or
Code:
$ BITCOIN_TEST= perl -wE 'use Bitcoin;'


More will come.

AndyRossy
Sr. Member
****
Offline Offline

Activity: 448
Merit: 250


View Profile
June 18, 2012, 03:00:13 AM
 #11

Why have you chosen Perl6 over Perl5? Has Perl6 been even finalize yet?

I am assuming that perl 6 would be backwards compatible.
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1080


View Profile
June 18, 2012, 11:58:43 AM
 #12

Why have you chosen Perl6 over Perl5?

I haven't.  I kind of felt stuck in Perl5 as I was reaching the point where I should implement networking.  Yet I don't know much about networking programming, I have difficulties understanding Satoshi's code about it, and I don't have a regular internet access so I could not make as many tests as I wanted.   So I felt like starting a Perl6 implementation until I get a regular internet access.

Quote
Has Perl6 been even finalize yet?

Specifications are pretty much done, yes.  Implementations have done very impressive progress lately.  Rakudo is pretty much usable now, for instance.

Quote
I am assuming that perl 6 would be backwards compatible.

No.  Perl6 will NOT be backwards compatible.   At all.

Still, it will be (and is already) an awesome programming language.

rico666
Legendary
*
Offline Offline

Activity: 1120
Merit: 1037


฿ → ∞


View Profile WWW
July 24, 2016, 11:16:45 AM
 #13

I fetched the library, squashed some bugs and silenced some others.

At least it "runs" now with Perl 5.24 without immediately breaking. Unfortunately it runs, but doesn't "work" yet. (Wrong computations).
I'll dig into that deeper.

The original author did a great job, but one can see that the code has been mostly lying around for 4+ years and that is never a good thing.
I'm confident I'll be able to shape it up again and will publish then. Stay tuned.


Rico

all non self-referential signatures except mine are lame ... oh wait ...   ·  LBC Thread (News)  ·  Past BURST Activities
rico666
Legendary
*
Offline Offline

Activity: 1120
Merit: 1037


฿ → ∞


View Profile WWW
July 28, 2016, 03:35:25 PM
Last edit: July 29, 2016, 08:01:57 AM by rico666
 #14

It seems "grondilu" also contributed code that is part of the library to the Rosetta Code site, so I took the liberty of rewriting it.

Result is code which is about 40 times faster (orig vs pcun)

Code:
# packunpack.pl 
Benchmark: timing 10000 iterations of orig, pcun...
      orig: 42 wallclock secs (42.15 usr +  0.00 sys = 42.15 CPU) @   NaN/s (n=10000)
      pcun:  1 wallclock secs ( 1.26 usr +  0.00 sys =  1.26 CPU) @   NaN/s (n=10000)

So I may finally ditch that Go implementation in my Collision Attack Study.

Rico

all non self-referential signatures except mine are lame ... oh wait ...   ·  LBC Thread (News)  ·  Past BURST Activities
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!