diff --git a/app/build.gradle b/app/build.gradle index a9db5cb..346e290 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,6 +54,9 @@ dependencies { implementation 'com.squareup.moshi:moshi:1.9.1' implementation 'com.squareup.okhttp3:okhttp:4.2.1' implementation 'com.squareup.moshi:moshi-kotlin:1.9.1' + implementation "com.github.komputing.kethereum:extensions:$kethereum_version" + implementation "com.github.komputing.kethereum:rlp:$kethereum_version" + implementation 'com.github.komputing:khex:0.6' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/java/im/status/keycard/connect/walletconnect/WalletConnect.kt b/app/src/main/java/im/status/keycard/connect/walletconnect/WalletConnect.kt index 8f2cbdc..913360e 100644 --- a/app/src/main/java/im/status/keycard/connect/walletconnect/WalletConnect.kt +++ b/app/src/main/java/im/status/keycard/connect/walletconnect/WalletConnect.kt @@ -17,11 +17,12 @@ import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import okhttp3.OkHttpClient import org.bouncycastle.jcajce.provider.digest.Keccak -import org.bouncycastle.util.encoders.Hex -import org.bouncycastle.util.encoders.Hex.toHexString import org.walletconnect.Session import org.walletconnect.Session.Config.Companion.fromWCUri import org.walletconnect.impls.* +import org.walleth.khex.hexToByteArray +import org.walleth.khex.toHexString +import org.walleth.khex.toNoPrefixHexString import java.io.File class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { @@ -72,7 +73,14 @@ class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { } "eth_signTypedData" -> { - session?.rejectRequest(call.id, 1L, "Not implemented yet") + val message = call.params?.get(1) + + if (message is Map<*, *>) { + @Suppress("UNCHECKED_CAST") + signTypedData(call.id, message as Map) + } else { + session?.rejectRequest(call.id, 1L, "Invalid params") + } } "eth_signTransaction" -> { @@ -80,7 +88,13 @@ class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { } "eth_sendRawTransaction" -> { - session?.rejectRequest(call.id, 1L, "Not implemented yet") + val signedTx = call.params?.first() + + if (signedTx is String) { + relayTX(call.id, signedTx) + } else { + session?.rejectRequest(call.id, 1L, "Invalid params") + } } else -> session?.rejectRequest(call.id, 1L, "Not implemented") @@ -88,8 +102,14 @@ class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { } + private fun relayTX(id: Long, signedTx: Any) { + // Ask confirmation and forward tx as-is through Infura + println(signedTx) + session?.rejectRequest(id, 1L, "Not implemented yet") + } + private fun signText(id: Long, message: String) { - val msg = Hex.decode(if (message.startsWith("0x", true)) message.drop(2) else message) + val msg = message.hexToByteArray() val text = String(msg) requestId = id @@ -105,6 +125,10 @@ class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { Registry.mainActivity.startActivityForResult(intent, REQ_WALLETCONNECT) } + + private fun signTypedData(id: Long, message: Map) { + session?.rejectRequest(id, 1L, "Not implemented yet") + } } private fun nop(@Suppress("UNUSED_PARAMETER") data: Intent?) { } @@ -138,14 +162,14 @@ class WalletConnect : ExportKeyCommand.Listener, SignCommand.Listener { override fun onResponse(keyPair: BIP32KeyPair) { scope.launch { - val addr = "0x${toHexString(keyPair.toEthereumAddress())}" + val addr = keyPair.toEthereumAddress().toHexString() session?.approve(listOf(addr), chainID) } } override fun onResponse(signature: RecoverableSignature) { scope.launch { - session?.approveRequest(requestId, "0x${toHexString(signature.r)}${toHexString(signature.s)}${toHexString(byteArrayOf(signature.recId.toByte()))}") + session?.approveRequest(requestId, "0x${signature.r.toNoPrefixHexString()}${signature.s.toNoPrefixHexString()}${(signature.recId + 27).toByte().toHexString()}") } } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 83a1d1b..7e5205f 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,8 @@ buildscript { ext.kotlin_version = '1.3.50' + ext.kethereum_version = '0.76.9' + repositories { google() jcenter()