Bitcoin-Crypto
view release on metacpan or search on metacpan
lib/Bitcoin/Crypto/Util.pm view on Meta::CPAN
use Bitcoin::Crypto::Util qw(
validate_wif
validate_segwit
get_address_type
get_key_type
get_public_key_compressed
generate_mnemonic
mnemonic_from_entropy
mnemonic_to_seed
get_path_info
from_format
to_format
pack_compactsize
unpack_compactsize
hash160
hash256
merkle_root
tagged_hash
lift_x
has_even_y
get_taproot_ext
);
=head1 DESCRIPTION
These are basic utilities for working with Bitcoin. They do not fit well as a
part of other, more specialized packages.
=head1 FUNCTIONS
=head2 validate_wif
$is_wif = validate_wif($wif)
Ensures Base58 encoded string looks like encoded private key in WIF format.
Throws an exception if C<$wif> is not valid base58.
This method accepts a secret argument. See L<Bitcoin::Crypto::Secret> for details.
=head2 validate_segwit
$segwit_version = validate_segwit($program)
Performs a segwit program validation on C<$program>, which is expected to be a
byte string in which the first byte is a segwit version.
The function returns the detected segwit program version. Note that it does not
perform any more checks than ensuring the byte string is in correct format.
The current implementation is in line with validations for segwit versions C<0>
and C<1>. Future segwit version addresses will work just fine, but no special
validation will be performed until implemented.
Raises an exception (C<Bitcoin::Crypto::Exception::SegwitProgram>) on error.
Returns the detected segwit program version.
=head2 get_address_type
$type = get_address_type($address, $network = Bitcoin::Crypto::Network->get)
Tries to guess the type of C<$address>. Returns C<P2PKH>, C<P2SH>, C<P2WPKH>,
C<P2WSH> or C<P2TR>. May throw Base58, Bech32, SegwitProgram, Address or other
exceptions if the string is not a valid address.
=head2 get_key_type
$is_private = get_key_type($bytestr);
Checks if the C<$bytestr> looks like a valid ASN X9.62 format (compressed /
uncompressed / hybrid public key or private key entropy up to curve size bits).
Returns boolean which states whether the key is private. Returns
undef if C<$bytestr> does not look like a valid key entropy.
=head2 get_public_key_compressed
$is_compressed = get_public_key_compressed($bytestr);
Checks if the C<$bytestr> looks like a valid ASN X9.62 format (compressed /
uncompressed / hybrid public key).
Returns boolean which states whether the key is compressed. Returns
undef if C<$bytestr> does not look like a valid public key.
=head2 generate_mnemonic
$mnemonic = generate_mnemonic($len = 128, $lang = 'en')
Generates a new mnemonic code using L<Bytes::Random::Secure>. Default entropy
is C<128> bits. This can be increased up to C<256> bits (increasing by C<32>
bits each step) with C<$len> argument.
Other languages than english require installation of additional modules
language-specific for L<Bitcoin::BIP39>.
Returns newly generated BIP39 mnemonic string. Dies when C<$len> is invalid
(less than C<128>, more than C<256> or not divisible by C<32>).
In some environments a problem may be encountered that causes the secure random
bytes generator to block the program execution (See
L<Bytes::Random::Secure/"BLOCKING ENTROPY SOURCE">). In this case you can use
L</mnemonic_from_entropy> and pass in entropy generated by
L<Bytes::Random::Secure> in non-blocking mode (via the OO interface).
=head2 mnemonic_from_entropy
$mnemonic = mnemonic_from_entropy($entropy, $lang = 'en')
Generates a new mnemonic code from custom entropy given in C<$entropy> (a
bytestring). This entropy should be of the same bit size as in
L</"generate_mnemonic">. Returns newly generated BIP39 mnemonic string.
This can be useful to avoid relying on the underlying PRNG implementation used
by L<Bitcoin::BIP39>.
Another use would be implementing one's own entropy source that can be truly
random, not just cryptographically-secure. A popular example would be capturing
user's mouse movements.
Be aware that the method you use to generate a mnemonic will be a very
important factor in your key's security. If possible, use real sources of
( run in 0.436 second using v1.01-cache-2.11-cpan-39bf76dae61 )