NAME
Bitcoin::Crypto::Key::Private - Bitcoin private keys
SYNOPSIS
use Bitcoin::Crypto::Key::Private;
# get Bitcoin::Crypto::Key::Public instance from private key
my $pub = $priv->get_public_key();
# create signature using private key (sha256 of string byte representation)
my $sig = $priv->sign_message("Hello world");
# signature is returned as byte string
# use unpack to get the representation you need
my $sig_hex = unpack "H*", $sig;
# signature verification
$priv->verify_message("Hello world", $sig);
DESCRIPTION
This class allows you to create a private key instance.
You can use a private key to:
generate public keys
sign and verify messages
Please note that any keys generated are by default compressed.
see Bitcoin::Crypto::Network if you want to work with other networks than Bitcoin Mainnet.
METHODS
from_bytes
sig: from_bytes($class, $data)
Use this method to create a PrivateKey instance from a byte string. Data $data will be used as a private key entropy.
Returns class instance.
new
sig: new($class, $data)
This works exactly the same as from_bytes
to_bytes
sig: to_bytes($self)
Does the opposite of from_bytes on a target object
from_hex
sig: from_hex($class, $hex)
Use this method to create a PrivateKey instance from a hexadecimal number. Number $hex will be used as a private key entropy.
Returns class instance.
to_hex
sig: to_hex($self)
Does the opposite of from_hex on a target object
from_wif
sig: from_wif($class, $str, $network = undef)
Creates a new private key from Wallet Import Format string.
Takes an additional optional argument, which is network name. It may be useful if you use many networks and some have the same WIF byte.
This method will change compression and network states of the created private key, as this data is included in WIF format.
Returns class instance.
to_wif
sig: to_wif($self)
Does the opposite of from_wif on a target object
set_compressed
sig: set_compressed($self, $val)
Change key's compression state to $val (1/0). This will change the WIF generated by toWif() method and also enable creation of uncompressed public keys. If $val is omitted it is set to 1.
Returns current key instance.
set_network
sig: set_network($self, $val)
Change key's network state to $val. It can be either network name present in Bitcoin::Crypto::Network package or an instance of this class.
Returns current key instance.
get_public_key
sig: get_public_key($self)
Returns instance of Bitcoin::Crypto::PublicKey generated from the private key.
sign_message
sig: sign_message($self, $message, $algo = "sha256")
Signs a digest of $message (using $algo digest algorithm) with a private key.
$algo must be available in Digest package.
Returns a byte string containing signature.
Caution: libtomcrypt cryptographic package that is generating signatures does not currently offer a deterministic mechanism. For this reason the sign_message method will always complain with a warning until the RFC6797 procedure is implemented. Non-deterministic signatures can lead to leaking private keys if the random number generator's entropy is insufficient.
verify_message
sig: verify_message($self, $message, $signature, $algo = "sha256")
Verifies $signature against digest of $message (with $algo digest algorithm) using private key. $algo must be available in Digest package. Returns boolean.
EXCEPTIONS
This module throws an instance of Bitcoin::Crypto::Exception if it encounters an error. It can produce the following error types from the Bitcoin::Crypto::Exception namespace:
Sign - couldn't sign the message correctly
Verify - couldn't verify the message correctly
KeyCreate - key couldn't be created correctly
NetworkConfig - incomplete or corrupted network configuration