Bitcoin-Crypto
view release on metacpan or search on metacpan
ex/tx/taproot_script_redeem.pl view on Meta::CPAN
# used, so the real fee rate will be approx two times smaller
my $wanted_fee_rate = 2;
$tx->outputs->[0]->set_value($tx->fee - int($tx->virtual_size * $wanted_fee_rate));
# semi-manual signing (since the transaction is custom):
# - signing with two private keys to satisfy 2 out of 3 transaction
# - leaving one empty signature for the one signature we don't use (second item)
# - signing with SIGHASH_DEFAULT, so it does not need to be passed explicitly
# - not adding annex, since it has no meaning yet (can lead to funds loss)
my $private_key_1 = btc_prv->from_wif('L2eKy3kX5DYnw7B1sXpEs2gd9xK5PSkiiBS1YSFaHvYyn1M9rsJJ');
my $private_key_3 = btc_prv->from_wif('L2zrD2aQRgGHzJpX7TB7qYhzibFqitH3NyvZUJUzqfHaLmxyBvm5');
# we want to use these keys for schnorr signatures, so we mark them as taproot outputs
$private_key_1->set_taproot_output(!!1);
$private_key_3->set_taproot_output(!!1);
# use sign to build a witness semi-manually
$tx
->sign(
signing_index => 0,
script_tree => $tree,
leaf_id => 0,
public_key => $public_key,
)
->add_signature($private_key_1)
->add_signature('')
->add_signature($private_key_3)
->finalize;
# verify the correctness of the transaction. Throws an exception on failure
$tx->verify;
say $tx->dump;
say to_format [hex => $tx->to_serialized];
__END__
lib/Bitcoin/Crypto/BIP44.pm view on Meta::CPAN
coin_type => Bitcoin::Crypto::Network->get('bitcoin_testnet'), # can also be a number or a key instance
index => 43,
# account => 0,
# change => 0,
);
# stringifies automatically
say "$path";
# can be used in key derivation
$ext_private_key->derive_key($path);
=head1 DESCRIPTION
This class is a helper for constructing BIP44-compilant key derivation paths.
BIP44 describes the mechanism the HD (Hierarchical Deterministic) wallets use
to decide derivation paths for coins. BIP49 and BIP84 are constructed the same
way, but used for compat and segwit addresses respectively.
Each coin has its own C<coin_type> constant, a list of which is maintained
here: L<https://github.com/satoshilabs/slips/blob/master/slip-0044.md>.
lib/Bitcoin/Crypto/BIP85.pm view on Meta::CPAN
__END__
=head1 NAME
Bitcoin::Crypto::BIP85 - BIP85 (deterministic entropy) implementation
=head1 SYNOPSIS
use Bitcoin::Crypto::BIP85;
my $bip85 = Bitcoin::Crypto::BIP85->new(
key => $extended_private_key,
);
# get raw bytestring seed
my $seed = $bip85->derive_entropy("m/0'/0'");
# get a mnemonic
my $mnemonic = $bip85->derive_mnemonic(index => 0);
=head1 DESCRIPTION
lib/Bitcoin/Crypto/Key/Base.pm view on Meta::CPAN
sub get_taproot_output_key
{
my ($self, $tweak_suffix) = @_;
return $self if $self->taproot_output;
my $new_key;
if ($self->_is_private) {
my $internal = $self->raw_key('private');
my $internal_public = ecc->create_public_key($internal);
$internal = ecc->negate_private_key($internal)
unless has_even_y($internal_public);
my $tweak = tagged_hash('TapTweak', ecc->xonly_public_key($internal_public) . ($tweak_suffix // ''));
$new_key = ecc->add_private_key($internal, $tweak);
}
else {
my $internal = $self->raw_key('public_xonly');
my $tweak = tagged_hash('TapTweak', $internal . ($tweak_suffix // ''));
$new_key = ecc->combine_public_keys(ecc->create_public_key($tweak), lift_x $internal);
}
my $pkg = ref $self;
return $pkg->new(
_key_instance => $new_key,
lib/Bitcoin/Crypto/Key/ExtPrivate.pm view on Meta::CPAN
# child number - 4 bytes
$hmac_data .= ensure_length pack('N', $child_num), 4;
my $data = hmac('SHA512', $self->chain_code, $hmac_data);
my $tweak = substr $data, 0, 32;
my $chain_code = substr $data, 32, 32;
Bitcoin::Crypto::Exception::KeyDerive->trap_into(
sub {
$key = ecc->add_private_key($key, $tweak);
die_no_trace 'verification failed' unless ecc->verify_private_key($key);
},
"key $child_num in sequence was found invalid"
);
return $self->new(
_key_instance => $key,
chain_code => $chain_code,
child_number => $child_num,
parent_fingerprint => $self->get_fingerprint,
depth => $self->depth + 1,
lib/Bitcoin/Crypto/Role/Key.pm view on Meta::CPAN
'invalid entropy data passed to key creation method'
) unless defined $is_private;
Bitcoin::Crypto::Exception::KeyCreate->raise(
'trying to create key from unknown key data'
) unless $is_private == $self->_is_private;
if ($is_private) {
Bitcoin::Crypto::Exception::KeyCreate->raise(
'private key is not valid'
) unless ecc->verify_private_key(ensure_length $entropy, KEY_MAX_LENGTH);
}
else {
try {
# keep public keys in compressed form always
$self->_set_key_instance(ecc->compress_public_key($entropy));
}
catch ($e) {
Bitcoin::Crypto::Exception::KeyCreate->raise(
'public key is not valid'
( run in 0.390 second using v1.01-cache-2.11-cpan-62ea2d55848 )