11package org .tron .common .crypto ;
22
3+ import static org .junit .Assert .assertArrayEquals ;
34import static org .junit .Assert .assertEquals ;
5+ import static org .junit .Assert .assertFalse ;
6+ import static org .junit .Assert .assertNotNull ;
7+ import static org .junit .Assert .assertTrue ;
8+ import static org .junit .Assert .fail ;
9+
10+ import java .math .BigInteger ;
11+ import java .security .KeyPairGenerator ;
12+ import java .security .Security ;
13+ import java .security .SignatureException ;
14+ import java .util .Arrays ;
415
516import lombok .extern .slf4j .Slf4j ;
617import org .junit .Test ;
718import org .spongycastle .util .encoders .Hex ;
8- import org .tron .common .utils .ByteArray ;
919import org .tron .core .Wallet ;
20+ import org .tron .common .crypto .ECKey .ECDSASignature ;
1021
1122@ Slf4j
1223public class ECKeyTest {
1324
25+ private String privString = "c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4" ;
26+ private BigInteger privateKey = new BigInteger (privString , 16 );
27+
28+ private String pubString = "040947751e3022ecf3016be03ec77ab0ce3c2662b4843898cb068d74f698ccc8ad75aa17564ae80a20bb044ee7a6d903e8e8df624b089c95d66a0570f051e5a05b" ;
29+ private String compressedPubString = "030947751e3022ecf3016be03ec77ab0ce3c2662b4843898cb068d74f698ccc8ad" ;
30+ private byte [] pubKey = Hex .decode (pubString );
31+ private byte [] compressedPubKey = Hex .decode (compressedPubString );
32+ private String address = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826" ;
33+
34+ @ Test
35+ public void testHashCode () {
36+ assertEquals (-351262686 , ECKey .fromPrivate (privateKey ).hashCode ());
37+ }
38+
39+ @ Test
40+ public void testECKey () {
41+ ECKey key = new ECKey ();
42+ assertTrue (key .isPubKeyCanonical ());
43+ assertNotNull (key .getPubKey ());
44+ assertNotNull (key .getPrivKeyBytes ());
45+ logger .info (Hex .toHexString (key .getPrivKeyBytes ()) + " :Generated privkey" );
46+ logger .info (Hex .toHexString (key .getPubKey ()) + " :Generated pubkey" );
47+ }
48+
49+ @ Test
50+ public void testFromPrivateKey () {
51+ ECKey key = ECKey .fromPrivate (privateKey );
52+ assertTrue (key .isPubKeyCanonical ());
53+ assertTrue (key .hasPrivKey ());
54+ assertArrayEquals (pubKey , key .getPubKey ());
55+ }
56+
57+ @ Test (expected = IllegalArgumentException .class )
58+ public void testPrivatePublicKeyBytesNoArg () {
59+ new ECKey ((BigInteger ) null , null );
60+ fail ("Expecting an IllegalArgumentException for using only null-parameters" );
61+ }
62+
63+ @ Test (expected = IllegalArgumentException .class )
64+ public void testInvalidPrivateKey () throws Exception {
65+ new ECKey (
66+ Security .getProvider ("SunEC" ),
67+ KeyPairGenerator .getInstance ("RSA" ).generateKeyPair ().getPrivate (),
68+ ECKey .fromPublicOnly (pubKey ).getPubKeyPoint ());
69+ fail ("Expecting an IllegalArgumentException for using an non EC private key" );
70+ }
71+
72+ @ Test
73+ public void testIsPubKeyOnly () {
74+ ECKey key = ECKey .fromPublicOnly (pubKey );
75+ assertTrue (key .isPubKeyCanonical ());
76+ assertTrue (key .isPubKeyOnly ());
77+ assertArrayEquals (key .getPubKey (), pubKey );
78+ }
79+
80+ @ Test (expected = IllegalArgumentException .class )
81+ public void testSignIncorrectInputSize () {
82+ ECKey key = new ECKey ();
83+ String message = "The quick brown fox jumps over the lazy dog." ;
84+ ECDSASignature sig = key .doSign (message .getBytes ());
85+ fail ("Expecting an IllegalArgumentException for a non 32-byte input" );
86+ }
87+
88+ @ Test (expected = SignatureException .class )
89+ public void testBadBase64Sig () throws SignatureException {
90+ byte [] messageHash = new byte [32 ];
91+ ECKey .signatureToKey (messageHash , "This is not valid Base64!" );
92+ fail ("Expecting a SignatureException for invalid Base64" );
93+ }
94+
95+ @ Test (expected = SignatureException .class )
96+ public void testInvalidSignatureLength () throws SignatureException {
97+ byte [] messageHash = new byte [32 ];
98+ ECKey .signatureToKey (messageHash , "abcdefg" );
99+ fail ("Expecting a SignatureException for invalid signature length" );
100+ }
101+
102+ @ Test
103+ public void testPublicKeyFromPrivate () {
104+ byte [] pubFromPriv = ECKey .publicKeyFromPrivate (privateKey , false );
105+ assertArrayEquals (pubKey , pubFromPriv );
106+ }
107+
108+ @ Test
109+ public void testPublicKeyFromPrivateCompressed () {
110+ byte [] pubFromPriv = ECKey .publicKeyFromPrivate (privateKey , true );
111+ assertArrayEquals (compressedPubKey , pubFromPriv );
112+ }
113+
114+ @ Test
115+ public void testGetAddress () {
116+ ECKey key = ECKey .fromPublicOnly (pubKey );
117+ // Addresses are prefixed with a constant.
118+ byte [] prefixedAddress = key .getAddress ();
119+ byte [] unprefixedAddress = Arrays .copyOfRange (key .getAddress (), 1 , prefixedAddress .length );
120+ assertArrayEquals (Hex .decode (address ), unprefixedAddress );
121+ assertEquals (Wallet .getAddressPreFixByte (), prefixedAddress [0 ]);
122+ }
123+
124+ @ Test
125+ public void testGetAddressFromPrivateKey () {
126+ ECKey key = ECKey .fromPrivate (privateKey );
127+ // Addresses are prefixed with a constant.
128+ byte [] prefixedAddress = key .getAddress ();
129+ byte [] unprefixedAddress = Arrays .copyOfRange (key .getAddress (), 1 , prefixedAddress .length );
130+ assertArrayEquals (Hex .decode (address ), unprefixedAddress );
131+ assertEquals (Wallet .getAddressPreFixByte (), prefixedAddress [0 ]);
132+ }
133+
14134 @ Test
15- public void testGeClientTestEcKey () {
16- final ECKey key = ECKey .fromPrivate (
17- Hex .decode ("1cd5a70741c6e583d2dd3c5f17231e608eb1e52437210d948c5085e141c2d830" ));
135+ public void testToString () {
136+ ECKey key = ECKey .fromPrivate (BigInteger .TEN ); // An example private key.
137+ assertEquals ("pub:04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7" , key .toString ());
138+ }
139+
140+ @ Test
141+ public void testIsPubKeyCanonicalCorect () {
142+ // Test correct prefix 4, right length 65
143+ byte [] canonicalPubkey1 = new byte [65 ];
144+ canonicalPubkey1 [0 ] = 0x04 ;
145+ assertTrue (ECKey .isPubKeyCanonical (canonicalPubkey1 ));
146+ // Test correct prefix 2, right length 33
147+ byte [] canonicalPubkey2 = new byte [33 ];
148+ canonicalPubkey2 [0 ] = 0x02 ;
149+ assertTrue (ECKey .isPubKeyCanonical (canonicalPubkey2 ));
150+ // Test correct prefix 3, right length 33
151+ byte [] canonicalPubkey3 = new byte [33 ];
152+ canonicalPubkey3 [0 ] = 0x03 ;
153+ assertTrue (ECKey .isPubKeyCanonical (canonicalPubkey3 ));
154+ }
155+
156+ @ Test
157+ public void testIsPubKeyCanonicalWrongLength () {
158+ // Test correct prefix 4, but wrong length !65
159+ byte [] nonCanonicalPubkey1 = new byte [64 ];
160+ nonCanonicalPubkey1 [0 ] = 0x04 ;
161+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey1 ));
162+ // Test correct prefix 2, but wrong length !33
163+ byte [] nonCanonicalPubkey2 = new byte [32 ];
164+ nonCanonicalPubkey2 [0 ] = 0x02 ;
165+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey2 ));
166+ // Test correct prefix 3, but wrong length !33
167+ byte [] nonCanonicalPubkey3 = new byte [32 ];
168+ nonCanonicalPubkey3 [0 ] = 0x03 ;
169+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey3 ));
170+ }
18171
19- logger .info ("address = {}" , ByteArray .toHexString (key .getAddress ()));
172+ @ Test
173+ public void testIsPubKeyCanonicalWrongPrefix () {
174+ // Test wrong prefix 4, right length 65
175+ byte [] nonCanonicalPubkey4 = new byte [65 ];
176+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey4 ));
177+ // Test wrong prefix 2, right length 33
178+ byte [] nonCanonicalPubkey5 = new byte [33 ];
179+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey5 ));
180+ // Test wrong prefix 3, right length 33
181+ byte [] nonCanonicalPubkey6 = new byte [33 ];
182+ assertFalse (ECKey .isPubKeyCanonical (nonCanonicalPubkey6 ));
183+ }
184+
185+ @ Test
186+ public void testGetPrivKeyBytes () {
187+ ECKey key = new ECKey ();
188+ assertNotNull (key .getPrivKeyBytes ());
189+ assertEquals (32 , key .getPrivKeyBytes ().length );
190+ }
191+
192+ @ Test
193+ public void testEqualsObject () {
194+ ECKey key0 = new ECKey ();
195+ ECKey key1 = ECKey .fromPrivate (privateKey );
196+ ECKey key2 = ECKey .fromPrivate (privateKey );
197+
198+ assertFalse (key0 .equals (key1 ));
199+ assertTrue (key1 .equals (key1 ));
200+ assertTrue (key1 .equals (key2 ));
201+ }
202+
203+ @ Test
204+ public void decryptAECSIC (){
205+ ECKey key = ECKey .fromPrivate (Hex .decode ("abb51256c1324a1350598653f46aa3ad693ac3cf5d05f36eba3f495a1f51590f" ));
206+ byte [] payload = key .decryptAES (Hex .decode ("84a727bc81fa4b13947dc9728b88fd08" ));
207+ System .out .println (Hex .toHexString (payload ));
208+ }
209+
210+ @ Test
211+ public void testNodeId () {
212+ ECKey key = ECKey .fromPublicOnly (pubKey );
20213
21- assertEquals (Wallet .getAddressPreFixString () + "125b6c87b3d67114b3873977888c34582f27bbb0" ,
22- ByteArray .toHexString (key .getAddress ()));
214+ assertEquals (key , ECKey .fromNodeId (key .getNodeId ()));
23215 }
24216}
0 commit comments