All Downloads are FREE. Search and download functionalities are using the official Maven repository.

exchange.apexpro.connector.impl.utils.eip712.CryptoUtils Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package exchange.apexpro.connector.impl.utils.eip712;

import lombok.extern.slf4j.Slf4j;
import org.web3j.crypto.ECDSASignature;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Keys;
import org.web3j.crypto.Sign;
import org.web3j.crypto.Sign.SignatureData;
import org.web3j.utils.Numeric;

import javax.xml.bind.DatatypeConverter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CryptoUtils {
    private static final String PERSONAL_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n";
    
    private CryptoUtils() {}
    

    public static Map ecrecoverPersonalSignature(String signature, String message) {
        
        log.debug("ecrecoverPersonalSignature(signature="+signature+", message="+message+")");
    
         // Message
         String pref = PERSONAL_MESSAGE_PREFIX + message.length();

         byte[] msgHash = Hash.sha3((pref+message).getBytes());
         log.trace("msgHash="+Numeric.toHexString(msgHash));

         return ecrecover(signature, msgHash);
    }

    public static Map ecrecover(String signature, byte[] msgHash) {
        
        log.debug("ecrecover(signature="+signature+", msgHash="+DatatypeConverter.printHexBinary(msgHash)+")");
    
         // Signature
         byte[] array = Numeric.hexStringToByteArray(signature);
         byte v = array[64];
         if (v < 27) { v += 27; }
            
         SignatureData sd = new SignatureData(v, (byte[]) Arrays.copyOfRange(array, 0, 32), (byte[])  Arrays.copyOfRange(array, 32, 64));
         
         Map addresses = new HashMap<>();
         
         // Iterate for each possible key to recover
         for(int i=0; i<4; i++) {
             BigInteger publicKey = Sign.recoverFromSignature((byte)i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash);
             log.trace("publicKey ("+i+") = "+publicKey);
                
             if(publicKey != null) {
                 addresses.put(i, "0x" + Keys.getAddress(publicKey));
             }
         }
         
         log.debug("checkSig(signature="+signature+", msgHash="+DatatypeConverter.printHexBinary(msgHash)+") => addresses recovered " + addresses);
 
         return addresses;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy