@@ -77,6 +77,13 @@ mod test {
77
77
use super :: * ;
78
78
use sp_core:: { crypto:: Pair as _, sr25519:: Pair } ;
79
79
80
+ fn bad_sig ( ) -> Signature {
81
+ Signature :: from_slice (
82
+ b"bogus_signature_bogus_signature_bogus_signature_bogus_signature!" . as_slice ( ) ,
83
+ )
84
+ . expect ( "Should be able to create a bogus signature." )
85
+ }
86
+
80
87
#[ test]
81
88
fn sr25519_signature_with_good_sig ( ) {
82
89
let pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
@@ -93,16 +100,11 @@ mod test {
93
100
#[ test]
94
101
fn sr25519_signature_with_bad_sig ( ) {
95
102
let simplified_tx = b"hello world" . as_slice ( ) ;
96
- let bad_sig = Signature :: from_slice (
97
- b"bogus_signature_bogus_signature_bogus_signature_bogus_signature!" . as_slice ( ) ,
98
- )
99
- . expect ( "Should be able to create a bogus signature." ) ;
100
-
101
103
let sr25519_signature = Sr25519Signature {
102
104
owner_pubkey : H256 :: zero ( ) ,
103
105
} ;
104
106
105
- assert ! ( !sr25519_signature. verify( simplified_tx, 0 , & bad_sig) ) ;
107
+ assert ! ( !sr25519_signature. verify( simplified_tx, 0 , & bad_sig( ) ) ) ;
106
108
}
107
109
108
110
#[ test]
@@ -117,7 +119,41 @@ mod test {
117
119
assert ! ( p2pkh. verify( simplified_tx, 0 , & ( pair. public( ) , sig) ) ) ;
118
120
}
119
121
120
- // Correct pubkey but bad sig
121
- // Incorrect pubkey with valid sig (from provided pubkey)
122
- // Incorrect pubkey and bogus sig
122
+ #[ test]
123
+ fn p2pkh_correct_pubkey_bad_sig ( ) {
124
+ let pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
125
+ let owner_pubkey_hash = BlakeTwo256 :: hash ( & pair. public ( ) ) ;
126
+ let simplified_tx = b"hello world" . as_slice ( ) ;
127
+
128
+ let p2pkh = P2PKH { owner_pubkey_hash } ;
129
+
130
+ assert ! ( !p2pkh. verify( simplified_tx, 0 , & ( pair. public( ) , bad_sig( ) ) ) ) ;
131
+ }
132
+
133
+ #[ test]
134
+ fn p2pkh_incorrect_pubkey_but_valid_sig_from_provided_pubkey ( ) {
135
+ let owner_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
136
+ let owner_pubkey_hash = BlakeTwo256 :: hash ( & owner_pair. public ( ) ) ;
137
+ let simplified_tx = b"hello world" . as_slice ( ) ;
138
+
139
+ let p2pkh = P2PKH { owner_pubkey_hash } ;
140
+
141
+ let attacker_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
142
+ let attacker_sig = attacker_pair. sign ( simplified_tx) ;
143
+
144
+ assert ! ( !p2pkh. verify( simplified_tx, 0 , & ( attacker_pair. public( ) , attacker_sig) ) ) ;
145
+ }
146
+
147
+ #[ test]
148
+ fn p2pkh_incorrect_pubkey_and_bogus_sig ( ) {
149
+ let owner_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
150
+ let owner_pubkey_hash = BlakeTwo256 :: hash ( & owner_pair. public ( ) ) ;
151
+ let simplified_tx = b"hello world" . as_slice ( ) ;
152
+
153
+ let p2pkh = P2PKH { owner_pubkey_hash } ;
154
+
155
+ let attacker_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
156
+
157
+ assert ! ( !p2pkh. verify( simplified_tx, 0 , & ( attacker_pair. public( ) , bad_sig( ) ) ) ) ;
158
+ }
123
159
}
0 commit comments