diff --git a/.DS_Store b/.DS_Store
index 4c8349b..bb2a1d4 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/assets/btn_click.ts b/assets/btn_click.ts
index 8b8f05a..11bb6d0 100644
--- a/assets/btn_click.ts
+++ b/assets/btn_click.ts
@@ -1,4 +1,4 @@
-import { _decorator, Component, Node, native } from "cc";
+import { _decorator, Component, Node, native,sys } from "cc";
const { ccclass, property } = _decorator;
import { createRequestData } from "./WalletSdkUtils";
@@ -21,11 +21,20 @@ export class btn_click extends Component {
*/
public btn_onclick1() {
console.info("点击了按钮");
- var ret = native.reflection.callStaticMethod(
- "JSHandleClass",
- "walleConnectFun:",
- "imtokenv2"
- );
+ if (sys.platform == sys.Platform.ANDROID) {
+ var ret = native.reflection.callStaticMethod(
+ 'com/cocos/game/AppActivity',
+ "initWallet",
+ "()V"
+ );
+ } else if (sys.platform == sys.Platform.IOS) {
+ var ret = native.reflection.callStaticMethod(
+ "JSHandleClass",
+ "walleConnectFun:",
+ "imtokenv2"
+ );
+ }
+
}
//发送交易
@@ -37,18 +46,33 @@ export class btn_click extends Component {
"Mint",
"0x4eDE150f62dc21aDC0F39a2A02c95A5Cc1FD7B2C",
);
- var jsonObj = {
- "method": "eth_sendTransaction",
- "params": JSON.stringify(temp)
- };
- var jsonStr = JSON.stringify(jsonObj);
- ///0x2eB535d54382eA5CED9183899916A9d39e093877
-
- var ret = native.reflection.callStaticMethod(
- "JSHandleClass",
- "sendContractFun:",
- jsonStr
- );
+ if (sys.platform == sys.Platform.ANDROID) {
+ var ret = native.reflection.callStaticMethod(
+ 'com/cocos/game/AppActivity',
+ 'sendCustomMessage',
+ '(Ljava/lang/String;)Ljava/lang/String;',
+ JSON.stringify({
+ method: 'eth_sendTransaction',
+ params: [
+ temp
+ ],
+ })
+ );
+ } else if (sys.platform == sys.Platform.IOS) {
+ var jsonObj = {
+ "method": "eth_sendTransaction",
+ "params": JSON.stringify(temp)
+ };
+ var jsonStr = JSON.stringify(jsonObj);
+ ///0x2eB535d54382eA5CED9183899916A9d39e093877
+
+ var ret = native.reflection.callStaticMethod(
+ "JSHandleClass",
+ "sendContractFun:",
+ jsonStr
+ );
+ }
+
}
public async btn_onclick3() {
// console.info("点击了按钮");
diff --git a/native/.DS_Store b/native/.DS_Store
index 139a048..1d97f0f 100644
Binary files a/native/.DS_Store and b/native/.DS_Store differ
diff --git a/native/engine/.DS_Store b/native/engine/.DS_Store
index 4dd4f2e..31e896f 100644
Binary files a/native/engine/.DS_Store and b/native/engine/.DS_Store differ
diff --git a/native/engine/android/.DS_Store b/native/engine/android/.DS_Store
new file mode 100644
index 0000000..99f81d5
Binary files /dev/null and b/native/engine/android/.DS_Store differ
diff --git a/native/engine/android/CMakeLists.txt b/native/engine/android/CMakeLists.txt
new file mode 100755
index 0000000..39a67f3
--- /dev/null
+++ b/native/engine/android/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.8)
+
+option(APP_NAME "Project Name" "cocos_demo_3")
+project(${APP_NAME} CXX)
+set(CC_LIB_NAME cocos)
+set(CC_PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR})
+set(CC_PROJ_SOURCES)
+set(CC_COMMON_SOURCES)
+set(CC_ALL_SOURCES)
+
+include(${CMAKE_CURRENT_LIST_DIR}/../common/CMakeLists.txt)
+
+cc_android_before_target(${CC_LIB_NAME})
+add_library(${CC_LIB_NAME} SHARED ${CC_ALL_SOURCES})
+cc_android_after_target(${CC_LIB_NAME})
diff --git a/native/engine/android/Post-service.cmake b/native/engine/android/Post-service.cmake
new file mode 100644
index 0000000..16fd033
--- /dev/null
+++ b/native/engine/android/Post-service.cmake
@@ -0,0 +1 @@
+# Supported for Cocos Service!
\ No newline at end of file
diff --git a/native/engine/android/Pre-service.cmake b/native/engine/android/Pre-service.cmake
new file mode 100644
index 0000000..16fd033
--- /dev/null
+++ b/native/engine/android/Pre-service.cmake
@@ -0,0 +1 @@
+# Supported for Cocos Service!
\ No newline at end of file
diff --git a/native/engine/android/app/.DS_Store b/native/engine/android/app/.DS_Store
new file mode 100644
index 0000000..ee0ccef
Binary files /dev/null and b/native/engine/android/app/.DS_Store differ
diff --git a/native/engine/android/app/AndroidManifest.xml b/native/engine/android/app/AndroidManifest.xml
new file mode 100644
index 0000000..9f5c4f5
--- /dev/null
+++ b/native/engine/android/app/AndroidManifest.xml
@@ -0,0 +1,42 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/native/engine/android/app/build.gradle b/native/engine/android/app/build.gradle
new file mode 100644
index 0000000..f199209
--- /dev/null
+++ b/native/engine/android/app/build.gradle
@@ -0,0 +1,115 @@
+import org.apache.tools.ant.taskdefs.condition.Os
+
+apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
+RES_PATH = RES_PATH.replace("\\", "/")
+COCOS_ENGINE_PATH = COCOS_ENGINE_PATH.replace("\\", "/")
+
+buildDir = "${RES_PATH}/proj/build/$project.name"
+android {
+ compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
+ buildToolsVersion PROP_BUILD_TOOLS_VERSION
+ ndkPath PROP_NDK_PATH
+
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_1_8
+ targetCompatibility JavaVersion.VERSION_1_8
+ }
+
+ defaultConfig {
+ applicationId APPLICATION_ID
+ minSdkVersion PROP_MIN_SDK_VERSION
+ targetSdkVersion PROP_TARGET_SDK_VERSION
+ versionCode 1
+ versionName "1.0"
+
+ externalNativeBuild {
+ cmake {
+ targets "cocos"
+ arguments "-DRES_DIR=${RES_PATH}", "-DCOCOS_X_PATH=${COCOS_ENGINE_PATH}", "-DANDROID_STL=c++_static", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE", "-DANDROID_LD=gold"
+ }
+ ndk { abiFilters PROP_APP_ABI.split(':') }
+ }
+ }
+
+ sourceSets.main {
+ java.srcDirs "../src", "src"
+ res.srcDirs "../res", 'res'
+ jniLibs.srcDirs "../libs", 'libs'
+ manifest.srcFile "AndroidManifest.xml"
+ assets.srcDir "${RES_PATH}/data"
+ jniLibs {
+ // Vulkan validation layer
+ // srcDir "${android.ndkDirectory}/sources/third_party/vulkan/src/build-android/jniLibs"
+ }
+ }
+
+ externalNativeBuild {
+ cmake {
+ path "../CMakeLists.txt"
+ buildStagingDirectory "${RES_PATH}/proj/build"
+ }
+ }
+
+ signingConfigs {
+
+ release {
+ if (project.hasProperty("RELEASE_STORE_FILE") && !RELEASE_STORE_FILE.isEmpty()) {
+ storeFile file(RELEASE_STORE_FILE)
+ storePassword RELEASE_STORE_PASSWORD
+ keyAlias RELEASE_KEY_ALIAS
+ keyPassword RELEASE_KEY_PASSWORD
+ }
+ }
+ }
+
+ buildTypes {
+ release {
+ debuggable false
+ jniDebuggable false
+ renderscriptDebuggable false
+ minifyEnabled true
+ shrinkResources true
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+ if (project.hasProperty("RELEASE_STORE_FILE")) {
+ signingConfig signingConfigs.release
+ }
+ // resValue "string", "app_name", PROP_APP_NAME
+ }
+
+ debug {
+ debuggable true
+ jniDebuggable true
+ renderscriptDebuggable true
+ // resValue "string", "app_name", "${PROP_APP_NAME}-dbg"
+ // applicationIdSuffix ".debug"
+ }
+ }
+}
+
+dependencies {
+ implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
+ implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
+ implementation fileTree(dir: "${COCOS_ENGINE_PATH}/cocos/platform/android/java/libs", include: ['*.jar'])
+ implementation project(':libservice')
+ implementation project(':libcocos')
+
+ implementation "org.java-websocket:Java-WebSocket:1.4.0"
+
+ implementation "com.github.komputing:khex:1.0.0-RC6"
+
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"
+
+ implementation 'com.github.WalletConnect:kotlin-walletconnect-lib:0.9.6'
+
+ implementation "androidx.appcompat:appcompat:$versions.androidx_app_compat"
+ implementation "androidx.multidex:multidex:$versions.multidex"
+
+ implementation 'com.squareup.moshi:moshi:1.8.0'
+ implementation "com.squareup.okhttp3:okhttp:$versions.okhttp"
+
+ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
+ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
+
+ implementation 'com.google.code.gson:gson:2.9.1'
+}
diff --git a/native/engine/android/app/proguard-rules.pro b/native/engine/android/app/proguard-rules.pro
new file mode 100644
index 0000000..dbb43fb
--- /dev/null
+++ b/native/engine/android/app/proguard-rules.pro
@@ -0,0 +1,42 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in E:\developSoftware\Android\SDK/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# 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 *;
+#}
+
+# Proguard Cocos2d-x-lite for release
+-keep public class com.cocos.** { *; }
+-dontwarn com.cocos.**
+
+# Proguard Apache HTTP for release
+-keep class org.apache.http.** { *; }
+-dontwarn org.apache.http.**
+
+# Proguard okhttp for release
+-keep class okhttp3.** { *; }
+-dontwarn okhttp3.**
+
+-keep class okio.** { *; }
+-dontwarn okio.**
+
+# Proguard Android Webivew for release. you can comment if you are not using a webview
+-keep public class android.net.http.SslError
+-keep public class android.webkit.WebViewClient
+
+-keep public class com.google.** { *; }
+
+-dontwarn android.webkit.WebView
+-dontwarn android.net.http.SslError
+-dontwarn android.webkit.WebViewClient
diff --git a/native/engine/android/app/src/.DS_Store b/native/engine/android/app/src/.DS_Store
new file mode 100644
index 0000000..40a1e2a
Binary files /dev/null and b/native/engine/android/app/src/.DS_Store differ
diff --git a/native/engine/android/app/src/com/.DS_Store b/native/engine/android/app/src/com/.DS_Store
new file mode 100644
index 0000000..9044e66
Binary files /dev/null and b/native/engine/android/app/src/com/.DS_Store differ
diff --git a/native/engine/android/app/src/com/cocos/.DS_Store b/native/engine/android/app/src/com/cocos/.DS_Store
new file mode 100644
index 0000000..c258f59
Binary files /dev/null and b/native/engine/android/app/src/com/cocos/.DS_Store differ
diff --git a/native/engine/android/app/src/com/cocos/game/.DS_Store b/native/engine/android/app/src/com/cocos/game/.DS_Store
new file mode 100644
index 0000000..f3711bc
Binary files /dev/null and b/native/engine/android/app/src/com/cocos/game/.DS_Store differ
diff --git a/native/engine/android/app/src/com/cocos/game/AppActivity.kt b/native/engine/android/app/src/com/cocos/game/AppActivity.kt
new file mode 100644
index 0000000..5a5f747
--- /dev/null
+++ b/native/engine/android/app/src/com/cocos/game/AppActivity.kt
@@ -0,0 +1,250 @@
+@file:JvmName("AppActivity")
+/****************************************************************************
+ * Copyright (c) 2015-2016 Chukong Technologies Inc.
+ * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
+ *
+ * http://www.cocos2d-x.org
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.(
+ */
+package com.cocos.game
+
+import android.content.res.Configuration
+
+import com.cocos.service.SDKWrapper
+import com.cocos.lib.CocosActivity
+
+import android.os.Bundle
+import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import android.view.View
+import com.example.walletconnect_demo.server.BridgeServer
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import okhttp3.OkHttpClient
+import org.komputing.khex.extensions.toNoPrefixHexString
+import org.walletconnect.Session
+import org.walletconnect.impls.*
+import org.walletconnect.nullOnThrow
+import java.io.File
+import java.util.*
+import com.google.gson.Gson
+import kotlinx.coroutines.launch
+import com.squareup.moshi.Moshi
+
+public class AppActivity : CocosActivity(), Session.Callback {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
+ if (!isTaskRoot()) {
+ // Android launched another instance of the root activity into an existing task
+ // so just quietly finish and go away, dropping the user back into the activity
+ // at the top of the stack (ie: the last state of this task)
+ // Don't need to finish it again since it's finished in super.onCreate .
+ return
+ }
+ // DO OTHER INITIALIZATION BELOW
+ SDKWrapper.shared().init(this)
+// initBridge()
+ initMoshi()
+ initClient()
+ initSessionStorage()
+
+ instance = this;
+ }
+
+ override fun onStatus(status: Session.Status) {
+ when(status) {
+ Session.Status.Approved -> sessionApproved()
+ Session.Status.Closed -> sessionClosed()
+ Session.Status.Connected,
+ Session.Status.Disconnected,
+ is Session.Status.Error -> {
+ // Do Stuff
+ }
+ }
+ }
+
+ override fun onMethodCall(call: Session.MethodCall) {
+ }
+
+ private fun sessionApproved() {
+ println("--------sessionApproved--------------")
+ println(session.approvedAccounts())
+
+ }
+
+ private fun sessionClosed() {
+ println("--------sessionClosed--------------")
+ println(session.approvedAccounts())
+ }
+
+ private fun initClient() {
+ client = OkHttpClient.Builder().build()
+ }
+
+ private fun initMoshi() {
+ moshi = Moshi.Builder().build()
+ }
+
+ private fun initBridge() {
+ bridge = BridgeServer(moshi)
+ bridge.start()
+ }
+
+ private fun initSessionStorage() {
+ storage = FileWCSessionStore(File(cacheDir, "session_store.json").apply { createNewFile() }, moshi)
+ }
+
+ override fun onResume() {
+ super.onResume()
+ SDKWrapper.shared().onResume()
+ }
+
+ override fun onPause() {
+ super.onPause()
+ SDKWrapper.shared().onPause()
+ }
+
+ override fun onDestroy() {
+ super.onDestroy()
+ // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
+ if (!isTaskRoot()) {
+ return
+ }
+ SDKWrapper.shared().onDestroy()
+ }
+
+// override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
+// super.onActivityResult(requestCode, resultCode, data)
+// SDKWrapper.shared().onActivityResult(requestCode, resultCode, data)
+// }
+
+ override fun onNewIntent(intent: Intent) {
+ super.onNewIntent(intent)
+ SDKWrapper.shared().onNewIntent(intent)
+ }
+
+ override fun onRestart() {
+ super.onRestart()
+ SDKWrapper.shared().onRestart()
+ }
+
+ override fun onStop() {
+ super.onStop()
+ SDKWrapper.shared().onStop()
+ }
+
+ override fun onBackPressed() {
+ SDKWrapper.shared().onBackPressed()
+ super.onBackPressed()
+ }
+
+ override fun onConfigurationChanged(newConfig: Configuration) {
+ SDKWrapper.shared().onConfigurationChanged(newConfig)
+ super.onConfigurationChanged(newConfig)
+ }
+
+ override protected fun onRestoreInstanceState(savedInstanceState: Bundle) {
+ SDKWrapper.shared().onRestoreInstanceState(savedInstanceState)
+ super.onRestoreInstanceState(savedInstanceState)
+ }
+
+ override protected fun onSaveInstanceState(outState: Bundle) {
+ SDKWrapper.shared().onSaveInstanceState(outState)
+ super.onSaveInstanceState(outState)
+ }
+
+ override protected fun onStart() {
+ SDKWrapper.shared().onStart()
+ super.onStart()
+ }
+
+
+ override fun onLowMemory() {
+ SDKWrapper.shared().onLowMemory()
+ super.onLowMemory()
+ }
+
+ fun test(){
+ }
+
+ companion object {
+ lateinit var instance: AppActivity ;
+ private lateinit var client: OkHttpClient
+ private lateinit var moshi: Moshi
+ private lateinit var bridge: BridgeServer
+ private lateinit var storage: WCSessionStore
+ lateinit var config: Session.Config
+ lateinit var session: Session
+
+ fun resetSession() {
+ nullOnThrow { session }?.clearCallbacks()
+ val key = ByteArray(32).also { Random().nextBytes(it) }.toNoPrefixHexString()
+ config = Session.Config(UUID.randomUUID().toString(), "https://bridge.walletconnect.org/", key)
+ session = WCSession(config,
+ MoshiPayloadAdapter(moshi),
+ storage,
+ OkHttpTransport.Builder(client, moshi),
+ Session.PeerMeta(name = "Example App")
+ )
+ session.offer()
+ }
+ @JvmStatic public fun initWallet() {
+ println("--------initWallet--------------")
+ resetSession()
+ session.addCallback(instance)
+ val i = Intent(Intent.ACTION_VIEW)
+ i.data = Uri.parse(config.toWCUri())
+ instance.startActivity(i)
+ // instance.test()
+ }
+ ///解析庄给的json字符串 并且使用custom方法发送给钱包
+ @JvmStatic public fun sendCustomMessage(jsonStr:String):String{
+ ///解析成实体类
+ var str = Gson().fromJson(jsonStr,argsModel::class.java);
+ session.performMethodCall(
+ Session.MethodCall.Custom(
+ System.currentTimeMillis(),
+ str.method!!,
+ str.params!!,
+ ),
+ ::handleResponse
+ )
+ val intent = Intent(Intent.ACTION_VIEW)
+ intent.data = Uri.parse("wc:")
+ intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
+ instance.startActivity(intent)
+ return "ok"
+ }
+ private fun handleResponse(resp: Session.MethodCall.Response) {
+ println(resp.result as? String)
+ }
+ }
+}
+class argsModel {
+ var method: String? = null
+ var params: List? = null
+
+ override fun toString(): String {
+ return "argsModel(method=$method, params=$params)"
+ }
+}
+
diff --git a/native/engine/android/app/src/com/cocos/game/Server/BridgeServer.kt b/native/engine/android/app/src/com/cocos/game/Server/BridgeServer.kt
new file mode 100644
index 0000000..3a94b83
--- /dev/null
+++ b/native/engine/android/app/src/com/cocos/game/Server/BridgeServer.kt
@@ -0,0 +1,98 @@
+package com.example.walletconnect_demo.server
+
+
+import android.util.Log
+import com.squareup.moshi.Moshi
+import com.squareup.moshi.Types
+import org.java_websocket.WebSocket
+import org.java_websocket.handshake.ClientHandshake
+import org.java_websocket.server.WebSocketServer
+import java.lang.Exception
+import java.lang.ref.WeakReference
+import java.net.InetSocketAddress
+import java.util.*
+import java.util.concurrent.ConcurrentHashMap
+
+class BridgeServer(moshi: Moshi) : WebSocketServer(InetSocketAddress(PORT)) {
+
+ private val adapter = moshi.adapter