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 EXCEPTIONS

This module throws an instance of L<Bitcoin::Crypto::Exception::Bech32> if it

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

after / in between words.

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.

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.

=head2 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.

=head2 from_hex_seed

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

);

sub from_wif
{
	my ($class, $wif, $network) = @_;

	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) > Bitcoin::Crypto::Constants::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/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

C<$seed>, which can be fed into 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.

=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
does C<Bitcoin::Crypto::Role::WithDerivationPath>). Returns undef if C<$path>
is not a valid path, otherwise returns the structure as an instance of

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);

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 0.554 second using v1.01-cache-2.11-cpan-26ccb49234f )