androidethereumtrust

How to recover an address from the signed message?


I use Trust SDK for signing messages and transactions.

I recover a transaction (Trust.signTransaction()) like this:

val tx = Geth.newTransactionFromRLP(Numeric.hexStringToByteArray(response.result))

Where Geth - framework (org.ethereum.geth), version 1.8.11.

How can I recover a data (and an address of the user's wallet) from the signed message (Trust.signMessage())?

Developer of Trust SDK said to look at 'recovery'. But what is it? Framework?

Thank you very much in advance.


Solution

  • I've solved the problem.

    1) Need to use this approach:

    signPersonalMessageCall = Trust.signPersonalMessage()
                    .message("my message to be signed")
                    .call(this)
    

    2) To get the address of the user need to use:

    const val PERSONAL_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n"    
    
    //...
    
    fun recoverAddressFromSignature(message: String, signature: String): String? {
                val prefix = PERSONAL_MESSAGE_PREFIX + message.length
                val msgHash = Hash.sha3((prefix + message).toByteArray())
    
                val signatureBytes = Numeric.hexStringToByteArray(signature)
                var v = signatureBytes[64]
    
                if (v < 27) {
                    v = (v + 27).toByte()
                }
    
                val sd = SignatureData(
                        v,
                        Arrays.copyOfRange(signatureBytes, 0, 32) as ByteArray,
                        Arrays.copyOfRange(signatureBytes, 32, 64) as ByteArray)
    
                var addressRecovered: String? = null
    
                // Iterate for each possible key to recover
                for (i in 0..3) {
                    val publicKey = Sign.recoverFromSignature(
                            i,
                            ECDSASignature(BigInteger(1, sd.r), BigInteger(1, sd.s)),
                            msgHash)
    
                    if (publicKey != null) {
                        addressRecovered = "0x" + Keys.getAddress(publicKey)
    
                        Log.i(Core.APP_TAG, "*-*-* addressRecovered = $addressRecovered")
                    }
                }
    
                return addressRecovered
            }
    

    The method (on Java) for recovering was grabbed from here.