Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
y3fers0n committed Jul 12, 2024
2 parents 908deec + 5954f82 commit c1ce6ac
Show file tree
Hide file tree
Showing 36 changed files with 1,206 additions and 4 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,8 @@ dist

# Finder (MacOS) folder config
.DS_Store


android/build
android/app/build/*
android/lib/build/*
android/.gradle
local.properties
49 changes: 49 additions & 0 deletions android/app/build.gradle
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'
}
21 changes: 21 additions & 0 deletions android/app/proguard-rules.pro
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
22 changes: 22 additions & 0 deletions android/app/src/main/AndroidManifest.xml
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>
9 changes: 9 additions & 0 deletions android/app/src/main/java/com/trust/web3/demo/App.kt
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 android/app/src/main/java/com/trust/web3/demo/DAppMethod.kt
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 android/app/src/main/java/com/trust/web3/demo/MainActivity.kt
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
}
}
65 changes: 65 additions & 0 deletions android/app/src/main/java/com/trust/web3/demo/Numeric.kt
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)
}
}
Loading

0 comments on commit c1ce6ac

Please sign in to comment.