-
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.
Merge branch 'main' of https://github.com/trustwallet/trust-web3-prov…
- Loading branch information
Showing
36 changed files
with
1,206 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'kotlin-android' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
applicationId "com.trust.web3.demo" | ||
minSdkVersion 23 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.2.0' | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.material:material:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
|
||
implementation "com.louiscad.splitties:splitties-alertdialog-material:3.0.0" | ||
implementation "com.trustwallet:wallet-core:2.9.7" | ||
|
||
implementation 'com.github.trustwallet:trust-web3-provider:2.0.0-alpha' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||
} |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.trust.web3.demo"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:name=".App" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.MyApplication"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
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 | ||
} | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.trust.web3.demo | ||
|
||
import android.graphics.Bitmap | ||
import android.net.http.SslError | ||
import android.os.Bundle | ||
import android.webkit.SslErrorHandler | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class MainActivity : AppCompatActivity() { | ||
companion object { | ||
private const val DAPP_URL = "https://www.magiceden.io/me" | ||
private const val CHAIN_ID = 56 | ||
private const val RPC_URL = "https://bsc-dataseed2.binance.org" | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(R.layout.activity_main) | ||
|
||
val provderJs = loadProviderJs() | ||
val initJs = loadInitJs( | ||
CHAIN_ID, | ||
RPC_URL | ||
) | ||
WebView.setWebContentsDebuggingEnabled(true) | ||
val webview: WebView = findViewById(R.id.webview) | ||
webview.settings.run { | ||
javaScriptEnabled = true | ||
domStorageEnabled = true | ||
} | ||
WebAppInterface(this, webview, DAPP_URL).run { | ||
webview.addJavascriptInterface(this, "_tw_") | ||
|
||
val webViewClient = object : WebViewClient() { | ||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { | ||
super.onPageStarted(view, url, favicon) | ||
view?.evaluateJavascript(provderJs, null) | ||
view?.evaluateJavascript(initJs, null) | ||
} | ||
|
||
override fun onReceivedSslError( | ||
view: WebView?, | ||
handler: SslErrorHandler?, | ||
error: SslError? | ||
) { | ||
// Ignore SSL certificate errors | ||
handler?.proceed() | ||
println(error.toString()) | ||
} | ||
} | ||
webview.webViewClient = webViewClient | ||
webview.loadUrl(DAPP_URL) | ||
} | ||
} | ||
|
||
private fun loadProviderJs(): String { | ||
return resources.openRawResource(R.raw.trust_min).bufferedReader().use { it.readText() } | ||
} | ||
|
||
private fun loadInitJs(chainId: Int, rpcUrl: String): String { | ||
val source = """ | ||
(function() { | ||
var config = { | ||
ethereum: { | ||
chainId: $chainId, | ||
rpcUrl: "$rpcUrl" | ||
}, | ||
solana: { | ||
cluster: "mainnet-beta", | ||
}, | ||
isDebug: true | ||
}; | ||
trustwallet.ethereum = new trustwallet.Provider(config); | ||
trustwallet.solana = new trustwallet.SolanaProvider(config); | ||
trustwallet.postMessage = (json) => { | ||
window._tw_.postMessage(JSON.stringify(json)); | ||
} | ||
window.ethereum = trustwallet.ethereum; | ||
})(); | ||
""" | ||
return source | ||
} | ||
} |
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) | ||
} | ||
} |
Oops, something went wrong.