Bitcoin-Crypto
view release on metacpan or search on metacpan
lib/Bitcoin/Crypto/Bech32.pm view on Meta::CPAN
L<Bitcoin::Crypto::Util/validate_segwit>.
Encoding takes two arguments which are a human readable part and a bytestring
(segwit program).
Decoding takes bech32-encoded string. Returns the entire encoded data
(bytestring) along with the segwit program version byte.
Encoding scheme (C<bech32> or C<bech32m>) is chosen based on version included
in the segwit program.
=head2 encode_bech32
=head2 decode_bech32
my $encoded_bech32 = encode_bech32($hrp, \@data, $type = 'bech32m');
my ($hrp, $data_aref, $type) = decode_bech32($encoded_bech32);
Basic bech32 encoding / decoding.
Encoding takes up to three arguments which are:
=over
=item * a human readable part
=item * an array reference of integer values to be encoded in bech32 (each must
be between 0 and 31)
=item * optional type, which may be C<'bech32'> or C<'bech32m'> (also available
in constant values L<Bitcoin::Crypto::Constants/BECH32> and
L<Bitcoin::Crypto::Constants/BECH32M>)
=back
If omitted, the type will be equal to C<'bech32m'>, which has more robust checksum.
Decoding takes a single parameter: a bech32-encoded string and returns a list
which has the same values as arguments to C<encode_bech32> function, including
C<$type>. This means you can feed both bech32 and bech32m encodings to
C<decode_bech32> and the function will identify and return the type for you.
B<< These methods are not meant to work with Bitcoin SegWit addresses, use
C<encode_segwit> and C<decode_segwit> for that instead. >>
=head2 translate_5to8
=head2 translate_8to5
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>
( run in 0.644 second using v1.01-cache-2.11-cpan-39bf76dae61 )