@@ -16,52 +16,60 @@ use Moo::Role;
16
16
17
17
requires qw(
18
18
raw_key
19
+ taproot
19
20
_is_private
20
21
) ;
21
22
23
+ my %algorithms = (
24
+ default => {
25
+ digest => \&hash256,
26
+ signing_method => sub {
27
+ my ($key , $digest ) = @_ ;
28
+
29
+ return ecc-> sign_digest($key -> raw_key, $digest );
30
+ },
31
+ verification_method => sub {
32
+ my ($key , $signature , $digest ) = @_ ;
33
+
34
+ my $normalized = ecc-> normalize_signature($signature );
35
+ return !!0 if $normalized ne $signature ;
36
+ return ecc-> verify_digest($key -> raw_key(' public' ), $signature , $digest );
37
+ },
38
+ },
39
+ schnorr => {
40
+ digest => sub { tagged_hash(' TapSighash' , shift ) },
41
+ signing_method => sub {
42
+ my ($key , $digest ) = @_ ;
43
+
44
+ return ecc-> sign_digest_schnorr($key -> raw_key, $digest );
45
+ },
46
+ verification_method => sub {
47
+ my ($key , $signature , $digest ) = @_ ;
48
+
49
+ return ecc-> verify_digest_schnorr($key -> raw_key(' public_xonly' ), $signature , $digest );
50
+ },
51
+ },
52
+ );
53
+
22
54
signature_for sign_message => (
23
55
method => Object,
24
- head => [ByteStr],
25
- named => [
26
- algorithm => SignatureAlgorithm,
27
- {default => Bitcoin::Crypto::Constants::signing_algorithm_ecdsa},
28
- taproot_tweak_suffix => Maybe [ByteStr],
29
- {default => undef },
30
- ],
31
- bless => !!0,
56
+ positional => [ByteStr],
32
57
);
33
58
34
59
sub sign_message
35
60
{
36
- my ($self , $preimage , $args ) = @_ ;
61
+ my ($self , $preimage ) = @_ ;
62
+ my $algorithm = $self -> taproot ? ' schnorr' : ' default' ;
37
63
38
64
Bitcoin::Crypto::Exception::Sign-> raise(
39
65
' cannot sign a message with a public key'
40
66
) unless $self -> _is_private;
41
67
42
- my %algorithms = (
43
- (Bitcoin::Crypto::Constants::signing_algorithm_ecdsa) => {
44
- digest => \&hash256,
45
- signing_method => sub { ecc-> sign_digest(@_ ) },
46
- raw_key => sub { $self -> raw_key },
47
- },
48
- (Bitcoin::Crypto::Constants::signing_algorithm_schnorr) => {
49
- digest => sub { tagged_hash(' TapSighash' , shift ) },
50
- signing_method => sub { ecc-> sign_digest_schnorr(@_ ) },
51
- raw_key => sub {
52
- $self -> taproot_tweaked_key(
53
- tweak_suffix => $args -> {taproot_tweak_suffix }
54
- );
55
- },
56
- },
57
- );
58
-
59
- my $key = $algorithms {$args -> {algorithm }}{raw_key }-> ();
60
- my $digest = $algorithms {$args -> {algorithm }}{digest }-> ($preimage );
68
+ my $digest = $algorithms {$algorithm }{digest }-> ($preimage );
61
69
62
70
return Bitcoin::Crypto::Exception::Sign-> trap_into(
63
71
sub {
64
- return $algorithms {$args -> { algorithm }} {signing_method }-> ($key , $digest );
72
+ return $algorithms {$algorithm }{signing_method }-> ($self , $digest );
65
73
}
66
74
);
67
75
}
@@ -94,13 +102,13 @@ signature_for verify_message => (
94
102
sub verify_message
95
103
{
96
104
my ($self , $preimage , $signature ) = @_ ;
97
- my $digest = hash256($preimage );
105
+ my $algorithm = $self -> taproot ? ' schnorr' : ' default' ;
106
+
107
+ my $digest = $algorithms {$algorithm }{digest }-> ($preimage );
98
108
99
109
return Bitcoin::Crypto::Exception::Verify-> trap_into(
100
110
sub {
101
- my $normalized = ecc-> normalize_signature($signature );
102
- return !!0 if $normalized ne $signature ;
103
- return ecc-> verify_digest($self -> raw_key(' public' ), $signature , $digest );
111
+ return $algorithms {$algorithm }{verification_method }-> ($self , $signature , $digest );
104
112
}
105
113
);
106
114
}
0 commit comments