The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bitcoin::Crypto::PrivateKey - class for Bitcoin private keys

SYNOPSIS

  use Bitcoin::Crypto::PrivateKey;

  # get Bitcoin::Crypto::PublicKey instance from private key

  my $pub = $priv->getPublicKey();

  # create signature using private key (sha256 of string byte representation)

  my $sig = $priv->signMessage("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->verifyMessage("Hello world", $sig);
  $priv->verifyBytes($packed_data, $sig);

DESCRIPTION

This class allows you to create a private key instance.

You can use a private key to:

  • read from and export to popular formats

  • generate public keys

  • sign and verify messages

This class doesn't:

  • generate entropy for a private key

  • derive private keys from a master key

After creating an instance private key entropy will be prepended by some NULL bytes if needed, for example if your entropy is 19 bytes long one extra NULL byte will be added so that it is 20 bytes long. Minimum byte size is 16, maximum is 32 and the step is 4 bytes. This allows creation of mnemonics of standard word counts: 12, 15, 18, 21, 24.

see Bitcoin::Crypto::Network if you want to work with other networks than Bitcoin Mainnet.

METHODS

fromHex($str) / fromBytes($str) / fromBip39Mnemonic($str)

Use these methods to create a PrivateKey instance. All take single string argument with private key data. Returns class instance.

fromWif($str, $network = undef)

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. Will fail with 0 / undef if passed WIF string is invalid. Will croak if it encounters a problem with network configuration. Returns class instance.

new($instance)

Takes a single argument which must be instance of Crypt::PK::ECC. This allows you to use raw Crypt::PK::ECC methods to create key on your own.

setCompressed($val)

Change key's compression state to $val (1/0). This will change the WIF generated by toWif() method and also enable creation of compressed public keys.

setNetwork($val)

Change key's network state to $val. It can be either network name present in Bitcoin::Crypto::Network package or a valid network hashref. This will change the WIF generated by toWif() method and also enable creation of public keys generating this network's addresses.

getPublicKey()

Returns instance of Bitcoin::Crypto::PublicKey generated from the private key.

signMessage($message, $algo = "sha256")

Signs a digest of $message (usinig $algo digest algorithm) with a private key. $algo must be available in Digest package. Returns a byte string containing signature.

verifyMessage($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.

toHex() / toBytes() / toWif() / toBip39Mnemonic()

Returns private key representation in specified format.

SEE ALSO

Bitcoin::Crypto::PublicKey
Bitcoin::Crypto::Network