88import java .io .File ;
99import java .io .IOException ;
1010import java .net .URISyntaxException ;
11- import java .security .GeneralSecurityException ;
1211import java .text .DateFormat ;
1312import java .text .SimpleDateFormat ;
1413import java .util .Date ;
@@ -50,7 +49,7 @@ void writePublicKey_should_equal_source_file() throws URISyntaxException, IOExce
5049 }
5150
5251 @ Test
53- void verify_should_be_ok () throws URISyntaxException , IOException , GeneralSecurityException {
52+ void verify_should_be_ok () throws URISyntaxException , IOException {
5453 String hexSignature = "ea5b38fd0db5fb3d871c47fde1fa4c4db75d1a9e1c0ac54d826e178ee0e63707176b4e63b4f838bd031f007fffd6a4f71d920a10c48ea53dd1573fa2b58a829e" ;
5554
5655 Secp256k1PublicKey pubKey = new Secp256k1PublicKey ();
@@ -60,4 +59,91 @@ void verify_should_be_ok() throws URISyntaxException, IOException, GeneralSecuri
6059
6160 assertTrue (pubKey .verify ("Test message" .getBytes (), Hex .decode (hexSignature )));
6261 }
63- }
62+
63+ @ Test
64+ void signAndRecoverPublicKey_1 () throws URISyntaxException , IOException {
65+
66+ //Get the private key
67+ Secp256k1PrivateKey privKey = new Secp256k1PrivateKey ();
68+ String filePath = getResourcesKeyPath ("secp256k1/secret_key.pem" );
69+ privKey .readPrivateKey (filePath );
70+
71+ //Derive the public key
72+ Secp256k1PublicKey publicKey = (Secp256k1PublicKey ) privKey .derivePublicKey ();
73+
74+ String message = "bc81ca4de9b3a991a6514eddf0e994e0035c7ba58f333c4d7ba5dd18b4c9c547" ;
75+
76+ //Generate the signature
77+ byte [] signature = privKey .sign (message .getBytes ());
78+
79+ //Test
80+ assert publicKey .verify (message .getBytes (), signature );
81+
82+ }
83+
84+ @ Test
85+ void signAndRecoverPublicKey_2 () throws URISyntaxException , IOException {
86+
87+ //Get the private key
88+ Secp256k1PrivateKey privKey = new Secp256k1PrivateKey ();
89+
90+ String filePath = getResourcesKeyPath ("secp256k1/secret_key.pem" );
91+ privKey .readPrivateKey (filePath );
92+
93+ //Derive the public key
94+ Secp256k1PublicKey publicKey = (Secp256k1PublicKey ) privKey .derivePublicKey ();
95+
96+ String message = "1df13c9aaa8217657b7e5ec2442594735eeb4ca7e764877b3d2b593c3909d15f" ;
97+
98+ //Generate the signature
99+ byte [] signature = privKey .sign (message .getBytes ());
100+
101+ //Test
102+ assert publicKey .verify (message .getBytes (), signature );
103+
104+ }
105+
106+ @ Test
107+ void signAndRecoverPublicKey_3 () throws URISyntaxException , IOException {
108+
109+ //Get the private key
110+ Secp256k1PrivateKey privKey = new Secp256k1PrivateKey ();
111+ String filePath = getResourcesKeyPath ("secp256k1/secret_key.pem" );
112+ privKey .readPrivateKey (filePath );
113+
114+ //Derive the public key
115+ Secp256k1PublicKey publicKey = (Secp256k1PublicKey ) privKey .derivePublicKey ();
116+
117+ String message = "Test message" ;
118+
119+ //Generate the signature
120+ byte [] signature = privKey .sign (message .getBytes ());
121+
122+ //Test
123+ assert publicKey .verify (message .getBytes (), signature );
124+
125+ }
126+
127+ @ Test
128+ void signAndRecoverPublicKey_4 () throws URISyntaxException , IOException {
129+
130+ //Get the private key
131+ Secp256k1PrivateKey privKey = new Secp256k1PrivateKey ();
132+ String filePath = getResourcesKeyPath ("secp256k1/secret_key.pem" );
133+ privKey .readPrivateKey (filePath );
134+
135+ //Derive the public key
136+ Secp256k1PublicKey publicKey = (Secp256k1PublicKey ) privKey .derivePublicKey ();
137+
138+ String message = "Test message" ;
139+
140+ //Generate the signature
141+ byte [] signature = privKey .sign (message .getBytes ());
142+
143+ //Test
144+ assert publicKey .verify (message .getBytes (), signature );
145+ assert !publicKey .verify ("Not test message" .getBytes (), signature );
146+
147+ }
148+
149+ }
0 commit comments