22
33import com .google .gson .GsonBuilder ;
44import java .math .BigInteger ;
5- import java .util .Arrays ;
65import java .util .HashMap ;
76import java .util .List ;
87import java .util .Map ;
98import org .arkecosystem .crypto .encoding .Hex ;
109import org .arkecosystem .crypto .identities .Address ;
1110import org .arkecosystem .crypto .identities .PrivateKey ;
11+ import org .arkecosystem .crypto .identities .PublicKey ;
1212import org .arkecosystem .crypto .transactions .Serializer ;
1313import org .arkecosystem .crypto .utils .AbiDecoder ;
1414import org .arkecosystem .crypto .utils .TransactionUtils ;
1515import org .bitcoinj .core .ECKey ;
16- import org .bitcoinj .core .Sha256Hash ;
1716
1817public abstract class AbstractTransaction {
1918 public int network ;
@@ -123,69 +122,15 @@ public AbstractTransaction legacySecondSign(String secondPassphrase) {
123122 }
124123
125124 private static String signHash (byte [] hash , ECKey privateKey ) {
126- ECKey .ECDSASignature signature = privateKey .sign (Sha256Hash .wrap (hash ));
127-
128- int recId = -1 ;
129- for (int i = 0 ; i < 4 ; i ++) {
130- ECKey k = ECKey .recoverFromSignature (i , signature , Sha256Hash .wrap (hash ), true );
131- if (k != null && k .getPubKeyPoint ().equals (privateKey .getPubKeyPoint ())) {
132- recId = i ;
133- break ;
134- }
135- }
136- if (recId == -1 ) {
137- throw new RuntimeException ("Could not find recId" );
138- }
139-
140- byte [] rBytes = bigIntegerToBytes (signature .r , 32 );
141- byte [] sBytes = bigIntegerToBytes (signature .s , 32 );
142-
143- byte [] signatureBytes = new byte [64 ];
144- System .arraycopy (rBytes , 0 , signatureBytes , 0 , 32 );
145- System .arraycopy (sBytes , 0 , signatureBytes , 32 , 32 );
146-
147- byte [] signatureWithRecId = new byte [65 ];
148- System .arraycopy (signatureBytes , 0 , signatureWithRecId , 0 , 64 );
149- signatureWithRecId [64 ] = (byte ) recId ;
150-
151- return Hex .encode (signatureWithRecId );
152- }
153-
154- private static byte [] bigIntegerToBytes (BigInteger b , int numBytes ) {
155- byte [] src = b .toByteArray ();
156- byte [] dest = new byte [numBytes ];
157- int srcPos = Math .max (0 , src .length - numBytes );
158- int destPos = Math .max (0 , numBytes - src .length );
159- int length = Math .min (src .length , numBytes );
160- System .arraycopy (src , srcPos , dest , destPos , length );
161- return dest ;
125+ return Hex .encode (PrivateKey .sign (hash , privateKey ));
162126 }
163127
164128 public void recoverSender () {
165129 if (this .signature == null || this .signature .length () != 130 ) {
166130 throw new RuntimeException ("Invalid signature" );
167131 }
168132
169- byte [] signatureWithRecId = Hex .decode (this .signature );
170- if (signatureWithRecId .length != 65 ) {
171- throw new RuntimeException ("Invalid signature length" );
172- }
173-
174- byte recId = signatureWithRecId [64 ];
175- byte [] signatureBytes = Arrays .copyOfRange (signatureWithRecId , 0 , 64 );
176-
177- BigInteger r = new BigInteger (1 , Arrays .copyOfRange (signatureBytes , 0 , 32 ));
178- BigInteger s = new BigInteger (1 , Arrays .copyOfRange (signatureBytes , 32 , 64 ));
179-
180- ECKey .ECDSASignature signature = new ECKey .ECDSASignature (r , s );
181-
182- byte [] hash = this .hash (true );
183-
184- ECKey recoveredKey =
185- ECKey .recoverFromSignature (recId , signature , Sha256Hash .wrap (hash ), true );
186- if (recoveredKey == null ) {
187- throw new RuntimeException ("Could not recover public key from signature" );
188- }
133+ ECKey recoveredKey = PublicKey .recover (this .hash (true ), Hex .decode (this .signature ));
189134
190135 this .senderPublicKey = recoveredKey .getPublicKeyAsHex ();
191136
@@ -199,27 +144,7 @@ public boolean verify() {
199144 }
200145
201146 ECKey keys = ECKey .fromPublicOnly (Hex .decode (this .senderPublicKey ));
202-
203- byte [] signatureWithRecId = Hex .decode (this .signature );
204- if (signatureWithRecId .length != 65 ) {
205- return false ;
206- }
207-
208- byte recId = signatureWithRecId [64 ];
209- byte [] signatureBytes = Arrays .copyOfRange (signatureWithRecId , 0 , 64 );
210-
211- BigInteger r = new BigInteger (1 , Arrays .copyOfRange (signatureBytes , 0 , 32 ));
212- BigInteger s = new BigInteger (1 , Arrays .copyOfRange (signatureBytes , 32 , 64 ));
213-
214- ECKey .ECDSASignature signature = new ECKey .ECDSASignature (r , s );
215-
216- byte [] hash = this .hash (true );
217-
218- ECKey recoveredKey =
219- ECKey .recoverFromSignature (recId , signature , Sha256Hash .wrap (hash ), true );
220- if (recoveredKey == null ) {
221- return false ;
222- }
147+ ECKey recoveredKey = PublicKey .recover (this .hash (true ), Hex .decode (this .signature ));
223148
224149 return recoveredKey .getPubKeyPoint ().equals (keys .getPubKeyPoint ());
225150 } catch (Exception e ) {
0 commit comments