-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement message signing for android demo (#177)
* add android small demo * update for comment suggestion * consolidate configuration info * Update android/app/build.gradle * implement personal / typed sign * Add unknown method Co-authored-by: Dougie <[email protected]> Co-authored-by: Viktor Radchenko <[email protected]>
- Loading branch information
1 parent
0d0f9a0
commit 105ad0b
Showing
12 changed files
with
300 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.trust.web3.demo | ||
|
||
import android.app.Application | ||
|
||
class App: Application() { | ||
init { | ||
System.loadLibrary("TrustWalletCore") | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
android/app/src/main/java/com/trust/web3/demo/DAppMethod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.trust.web3.demo | ||
|
||
enum class DAppMethod { | ||
SIGNTRANSACTION, | ||
SIGNPERSONALMESSAGE, | ||
SIGNMESSAGE, | ||
SIGNTYPEDMESSAGE, | ||
ECRECOVER, | ||
REQUESTACCOUNTS, | ||
WATCHASSET, | ||
ADDETHEREUMCHAIN, | ||
UNKNOWN; | ||
|
||
companion object { | ||
fun fromValue(value: String): DAppMethod { | ||
return when (value) { | ||
"signTransaction" -> SIGNTRANSACTION | ||
"signPersonalMessage" -> SIGNPERSONALMESSAGE | ||
"signMessage" -> SIGNMESSAGE | ||
"signTypedMessage" -> SIGNTYPEDMESSAGE | ||
"ecRecover" -> ECRECOVER | ||
"requestAccounts" -> REQUESTACCOUNTS | ||
"watchAsset" -> WATCHASSET | ||
"addEthereumChain" -> ADDETHEREUMCHAIN | ||
else -> UNKNOWN | ||
} | ||
} | ||
} | ||
} |
46 changes: 28 additions & 18 deletions
46
android/app/src/main/java/com/trust/web3/demo/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.trust.web3.demo | ||
|
||
import kotlin.experimental.and | ||
|
||
object Numeric { | ||
fun containsHexPrefix(input: String): Boolean { | ||
return input.length > 1 && input[0] == '0' && input[1] == 'x' | ||
} | ||
|
||
fun cleanHexPrefix(input: String): String { | ||
return if (containsHexPrefix(input)) { | ||
input.substring(2) | ||
} else { | ||
input | ||
} | ||
} | ||
|
||
fun hexStringToByteArray(input: String): ByteArray { | ||
val cleanInput = cleanHexPrefix(input) | ||
|
||
val len = cleanInput.length | ||
|
||
if (len == 0) { | ||
return byteArrayOf() | ||
} | ||
|
||
val data: ByteArray | ||
val startIdx: Int | ||
if (len % 2 != 0) { | ||
data = ByteArray(len / 2 + 1) | ||
data[0] = Character.digit(cleanInput[0], 16).toByte() | ||
startIdx = 1 | ||
} else { | ||
data = ByteArray(len / 2) | ||
startIdx = 0 | ||
} | ||
|
||
var i = startIdx | ||
while (i < len) { | ||
data[(i + 1) / 2] = | ||
((Character.digit(cleanInput[i], 16) shl 4) + Character.digit( | ||
cleanInput[i + 1], | ||
16 | ||
)).toByte() | ||
i += 2 | ||
} | ||
return data | ||
} | ||
|
||
fun toHexString(input: ByteArray?, offset: Int, length: Int, withPrefix: Boolean): String { | ||
val stringBuilder = StringBuilder() | ||
if (withPrefix) { | ||
stringBuilder.append("0x") | ||
} | ||
for (i in offset until offset + length) { | ||
stringBuilder.append(String.format("%02x", input!![i] and 0xFF.toByte())) | ||
} | ||
|
||
return stringBuilder.toString() | ||
} | ||
|
||
fun toHexString(input: ByteArray?): String { | ||
return toHexString(input, 0, input!!.size, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
android/app/src/main/java/com/trust/web3/demo/WebViewExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.trust.web3.demo | ||
|
||
import android.webkit.WebView | ||
|
||
fun WebView.sendError(message: String, methodId: Long) { | ||
val script = "window.ethereum.sendError($methodId, \"$message\")" | ||
this.post { | ||
this.evaluateJavascript(script) {} | ||
} | ||
} | ||
|
||
fun WebView.sendResult(message: String, methodId: Long) { | ||
val script = "window.ethereum.sendResponse($methodId, \"$message\")" | ||
this.post { | ||
this.evaluateJavascript(script) {} | ||
} | ||
} | ||
|
||
fun WebView.sendResults(messages: List<String>, methodId: Long) { | ||
val message = messages.joinToString(separator = ",") | ||
val script = "window.ethereum.sendResponse($methodId, \"$message\")" | ||
this.post { | ||
this.evaluateJavascript(script) {} | ||
} | ||
} | ||
|
||
fun String.toHexByteArray(): ByteArray { | ||
return Numeric.hexStringToByteArray(this) | ||
} |
Oops, something went wrong.