-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do i get compressed public keys? #2
Comments
I've just looked at the code (which I haven't done for years btw) and apparently I wrote a "compress" method. So you should be able to do something like:
and get the compressed version of the public key. |
But don't I need it on the EC::DSA::PrivateKey? The Bitcoin::Key doesnt have a public_key method, nor a compress .. |
oh, EC::DSA::PrivateKey -does- have public_key method which gets me the following result; my $privkey = new EC::DSA::PrivateKey 1; print $privkey->public_key->compress; Output : point is not on elliptic curve at EC.pm line 46. |
I can't reproduce your error. The following works on my machine:
As you can see I had to bless the public key manually in order to access the compress method, though. That should be automatic. |
Ehr, maybe i am missing some step then O_o use Bitcoin; print $pubkey; my $pubkey = bless $privkey->public_key, "EC::DSA::PublicKey::UnCompressed"; print $pubkey -> compress; This outputs: x: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 x: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 |
The only difference between a compressed public key and an uncompressed one is the "serialize" method, which is not used for pretty print (what you get when you print the object). This is maybe not the best, but apparently that's how I ended up implementing it. If you want the compressed output to show up on the screen you have to call the serialize method and unpack it (because it's binary).
|
Then i still don't seem to be able to feed the compressed key into Bitcoin::Key :( I think somehow i need to tell Bitcoin::Key to use the other serializer? |
Bitcoin::Key inherits from EC::DSA::PrivateKey. A private key is a secret exponent, and as such it is just a natural integer. There is no compression format for this. Only the public key can be compressed. So you feed Bitcoin::Key with an integer, you get the public key with the public_key method, then you bless it with EC::DSA::PublicKey::Compressed, and then you call the serialize method. You can also explicitly call the serializer you want indeed:
|
But then how do i turn that back into a Bitcoin::Key so i can create the WIF and address to feed into a wallet? |
Or actually, feed to a QR printer ;) |
Trying making the address manually:
I can't test this myself as I don't have all required modules anymore. |
So, my $privkey = new EC::DSA::PrivateKey 1; dies in a point is not on elliptic curve at EC.pm line 46. And this creates -a- address, but the wrong one; my $privkey = new EC::DSA::PrivateKey 1; my $compkey = new Bitcoin::Key $privkey; my $address = new Bitcoin::Address bless $compkey, "EC::DSA::PublicKey::Compressed"; This outputs the address 12aJDuq56zPqWB31AHL38MUhNmr9xFNo3K but according to bitaddress , for secret exponent 0x1 it should be 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH |
I am making a 'input 64 hexadecimal dice rolls and get two priv/pub keys' program, and was looking for some speedincrease.
Your library seems faster than my current code, but, i fail to be able to get the compressed keys from it.
Care to write a small example for that?
This gives me the uncompressed one ;
my $privkey = new EC::DSA::PrivateKey 1;
my $key = new Bitcoin::Key $privkey;
print $key;
print "\n";
print $key->address;
print "\n";
Was messing with EC::DSA::PublicKey::UnCompressed but doesnt seem to even be used to make an address?
The text was updated successfully, but these errors were encountered: