Bitcoin-Crypto

 view release on metacpan or  search on metacpan

lib/Bitcoin/Crypto/Base58.pm  view on Meta::CPAN

	decode_base58
	decode_base58check
);

our %EXPORT_TAGS = (all => [@EXPORT_OK]);

my $CHECKSUM_SIZE = 4;

sub verify_checksum
{
	my ($decoded) = @_;
	my $encoded_val = substr $decoded, 0, -$CHECKSUM_SIZE;
	my $checksum = substr $decoded, -$CHECKSUM_SIZE;
	return unpack('a' . $CHECKSUM_SIZE, hash256($encoded_val)) eq $checksum;
}

signature_for encode_base58 => (
	positional => [ByteStr],
);

sub encode_base58
{
	my ($bytes) = @_;

lib/Bitcoin/Crypto/Base58.pm  view on Meta::CPAN

}

signature_for decode_base58 => (
	positional => [Str],
);

sub decode_base58
{
	my ($base58encoded) = @_;

	my $decoded = decode_b58b($base58encoded);
	Bitcoin::Crypto::Exception::Base58InputFormat->raise(
		'illegal characters in base58 string'
	) unless defined $decoded;

	return $decoded;
}

signature_for decode_base58check => (
	positional => [Str],
);

sub decode_base58check
{
	my ($base58encoded) = @_;

	my $decoded = decode_base58($base58encoded);
	Bitcoin::Crypto::Exception::Base58InputChecksum->raise(
		'incorrect base58check checksum'
	) unless verify_checksum($decoded);

	return substr $decoded, 0, -$CHECKSUM_SIZE;
}

1;

__END__

=head1 NAME

Bitcoin::Crypto::Base58 - Base58 helpers

lib/Bitcoin/Crypto/Bech32.pm  view on Meta::CPAN

	my $bytestr = translate_5to8(\@int_array);

	my $int_aref = translate_8to5($bytestr);

These are helper functions that implement 5-bit to 8-bit encoding used in
bech32 segwit addresses. C<translate_8to5> is used during encoding, and
C<translate_5to8> during decoding. They are used as means to store full byte
data in bech32 strings, like so:

	my $data = encode_bech32('hrp', translate_8to5($bytes));
	my @decoded_parts = decode_bech32($data);
	my $decoded = translate_5to8($decoded_parts[1]);

=head2 get_hrp

	$hrp = get_hrp($address)

Returns the human readable part encoded in the bech32 address.

=head1 SEE ALSO

L<Bitcoin::Crypto::Base58>

lib/Bitcoin/Crypto/Key/ExtPrivate.pm  view on Meta::CPAN

If no C<$lang> is given then any string passed as C<$mnemonic> will produce a
valid key. B<This means even adding whitespace (eg. trailing newline) will
produce a different key>. Be careful when using this method without C<$lang>
argument as you can easily create keys incompatible with other software due to
these whitespace problems.

This method accepts a secret argument. See L<Bitcoin::Crypto::Secret> for details.

Returns a new instance of this class.

B<Important note about unicode:> this function only accepts UTF8-decoded
strings (both C<$mnemonic> and C<$password>), but can't detect whether it got
it or not. This will only become a problem if you use non-ascii mnemonic and/or
password. If there's a possibility of non-ascii, always use utf8 and set
binmodes to get decoded (wide) characters to avoid problems recovering your
wallet.

=head3 from_seed

	$key_object = $class->from_seed($seed)

Creates and returns a new key from seed, which can be any data of any length.
C<$seed> is expected to be a byte string.

This method accepts a secret argument. See L<Bitcoin::Crypto::Secret> for details.

lib/Bitcoin/Crypto/Key/Private.pm  view on Meta::CPAN

	my ($class, $secret, $network) = @_;

	return $secret->unmask_to(
		sub {
			my ($wif) = @_;

			Bitcoin::Crypto::Exception::KeyCreate->raise(
				'base58 string is not valid WIF'
			) unless validate_wif($wif);

			my $decoded = decode_base58check($wif);
			my $private = substr $decoded, 1;

			my $compressed = 0;
			if (length($private) > KEY_MAX_LENGTH) {
				chop $private;
				$compressed = 1;
			}

			my $wif_network_byte = substr $decoded, 0, 1;
			my @found_networks =
				Bitcoin::Crypto::Network->find(sub { shift->wif_byte eq $wif_network_byte });
			@found_networks = grep { $_ eq $network } @found_networks
				if defined $network;

			if (@found_networks > 1) {
				my $default_network = Bitcoin::Crypto::Network->get->id;

				Bitcoin::Crypto::Exception::KeyCreate->raise(
					'found multiple networks possible for given WIF: ' . join ', ', @found_networks

lib/Bitcoin/Crypto/Manual.pod  view on Meta::CPAN


See L<Bitcoin::Crypto::PSBT> for more details and a complete reference of field
types and what objects their serializers / deserializers use.

=head2 Performance

A lot of work has been done to ensure Bitcoin::Crypto transaction decoding and
verification performance is somewhat acceptable. Benchmarks on author's (not
very powerful) machine have shown that up to 600 legacy transactions (each
having 2 P2MS inputs) or 800 taproot transactions (each having 4 key spend
inputs) can be decoded and verified per second, per process.

For best performance, extra modules from CPAN should be installed, listed
below. This module will use them if they can be loaded.

=over

=item * Modules from L<Mooish::Base/Moo>

=item * L<Type::Tiny::XS> (used automatically by L<Type::Tiny>)

lib/Bitcoin/Crypto/PSBT.pm  view on Meta::CPAN


	# import PSBT from a serialized form
	my $psbt = btc_psbt->from_serialized([base64 => $psbt_string]);

	# dump in readable format
	print $psbt->dump;

	# get a single PSBT field
	my $field = $psbt->get_field('PSBT_GLOBAL_TX_VERSION');

	# get decoded field key and value
	my $key = $field->key;
	my $value = $field->value;

	# get all PSBT fields of a given type
	my @fields = $psbt->get_all_fields('PSBT_GLOBAL_PROPRIETARY');

=head1 DESCRIPTION

This is a class implementing the PSBT format as described in BIP174 and
BIP370. It currently supports versions 0 and 2 of the spec. It allows

lib/Bitcoin/Crypto/Script.pm  view on Meta::CPAN

}

sub _build
{
	my ($self, $type, $address) = @_;

	state $types = do {
		my $legacy = sub {
			my ($self, $address, $type) = @_;

			my $decoded = decode_base58check($address);
			my $network_byte = substr $decoded, 0, 1, '';

			Bitcoin::Crypto::Exception::Address->raise(
				"legacy scripts should contain 20 bytes"
			) unless length $decoded == 20;

			my $byte_method = lc "p2${type}_byte";
			Bitcoin::Crypto::Exception::NetworkCheck->raise(
				"provided address $address is not P2$type on network " . $self->network->name
			) if $network_byte ne $self->network->$byte_method;

			Bitcoin::Crypto::Script::Common->fill($type => $self, $decoded);
		};

		my $witness = sub {
			my ($self, $address, $name, $version, $length) = @_;

			my $data = decode_segwit $address;
			my $this_version = substr $data, 0, 1, '';

			Bitcoin::Crypto::Exception::SegwitProgram->raise(
				"$name script only handles witness version $version"

lib/Bitcoin/Crypto/Util.pm  view on Meta::CPAN

L<Bitcoin::Crypto::Key::ExtPrivate/from_seed>.

C<$seed> is a C<512> bit bytestring (64 characters). C<$mnemonic> should be a
BIP39 mnemonic, but will not be checked against a dictionary.

This function is only useful if you need a seed instead of mnemonic (for
example, you use a wallet implementation which does not implement BIP39). If
you only want to create a private key from mnemonic, you should consider using
L<Bitcoin::Crypto::Key::ExtPrivate/from_mnemonic> instead.

B<Important note about unicode:> this function only accepts UTF8-decoded
strings (both C<$mnemonic> and C<$password>), but can't detect whether it got
it or not. This will only become a problem if you use non-ascii mnemonic and/or
password. If there's a possibility of non-ascii, always use utf8 and set
binmodes to get decoded (wide) characters to avoid problems recovering your
wallet.

This method accepts a secret argument. See L<Bitcoin::Crypto::Secret> for details.

=head2 get_path_info

	$path_data = get_path_info($path);

Tries to get derivation path data from C<$path>, which can be a string like
C<"m/1/3'"> or an object which implements C<get_derivation_path> method (and

lib/Bitcoin/Crypto/Util.pm  view on Meta::CPAN

=item * C<hex>, encodes as a hexadecimal string (no C<0x> prefix)

=item * C<base58>, uses base58 and includes the checksum (base58check)

=item * C<base64>, uses base64

=back

=head2 from_format

	$decoded = from_format [$format => $string]

Reverse of L</to_format> - decodes C<$string> into bytestring, treating it as
C<$format>.

I<Note: this is not usually needed to be called explicitly, as every bytestring
parameter of the module will do this conversion implicitly.>

=head2 pack_compactsize

	$bytestr = pack_compactsize($integer)

lib/Bitcoin/Crypto/Util.pm  view on Meta::CPAN

Calculates a merkle root of input array reference. Leaves will be run through a
double SHA256 before calculating the root.

=head2 tagged_hash

	$hash = tagged_hash($tag, $message)

Calculates a tagged hash of C<$message> using C<$tag> as a tag. These hashes
are described in BIP340.

B<Important note about unicode:> this function only accepts UTF8-decoded
strings for C<$tag>, but can't detect whether it got it or not. This will only
become a problem if you use non-ascii tag. If there's a possibility of
non-ascii, always use utf8 and set binmodes to get decoded (wide) characters.

=head2 lift_x

	$public_key = lift_x($xonly_public_key)

This implements C<lift_x> function defined in BIP340. Returns a compressed ECC
public key with even Y coordinate as a bytestring for a given 32-byte bytestring
C<$xonly_public_key>. Throws an exception if the result is not a valid public
key.

t/Base58.t  view on Meta::CPAN

	],
);

my $case_num = 0;
foreach my $case (@cases) {
	subtest "testing valid base58, case $case_num" => sub {
		my $case_packed = pack('H*', $case->[0]);
		is($case_packed, decode_base58check($case->[1]), 'valid decoding');
		is($case->[1], encode_base58check($case_packed), 'valid encoding');

		my $decoded_with_check = decode_base58($case->[1]);
		is(substr($decoded_with_check, 0, -4), $case_packed, 'base58check value unchanged');
		is(
			pack('a4', sha256(sha256(substr $decoded_with_check, 0, -4))),
			substr($decoded_with_check, -4),
			'checksum is valid'
		);
	};

	++$case_num;
}

$case_num = 0;
foreach my $case (@cases_error) {
	subtest "testing invalid base58, case $case_num" => sub {



( run in 1.477 second using v1.01-cache-2.11-cpan-39bf76dae61 )