FRE-600: Fix code review blockers

- Consolidated duplicate UndoManagers to single instance
- Fixed connection promise to only resolve on 'connected' status
- Fixed WebSocketProvider import (WebsocketProvider)
- Added proper doc.destroy() cleanup
- Renamed isPresenceInitialized property to avoid conflict

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-04-25 00:08:01 -04:00
parent 65b552bb08
commit 7c684a42cc
48450 changed files with 5679671 additions and 383 deletions

View File

@@ -0,0 +1,158 @@
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['SolanaMobileWalletAdapterModule_kotlinVersion']
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:9.0.1'
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}
def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['SolanaMobileWalletAdapterModule_' + name]
}
def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['SolanaMobileWalletAdapterModule_' + name]).toInteger()
}
android {
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
defaultConfig {
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}
buildTypes {
release {
minifyEnabled false
}
}
lintOptions {
disable 'GradleCompatible'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"src/newarch"
]
} else {
java.srcDirs += ["src/oldarch"]
}
}
}
}
repositories {
mavenCentral()
google()
def found = false
def defaultDir = null
def androidSourcesName = 'React Native sources'
if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
} else {
defaultDir = new File(
projectDir,
'/../../../node_modules/react-native/android'
)
}
if (defaultDir.exists()) {
maven {
url defaultDir.toString()
name androidSourcesName
}
logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
found = true
} else {
def parentDir = rootProject.projectDir
1.upto(5, {
if (found) return true
parentDir = parentDir.parentFile
def androidSourcesDir = new File(
parentDir,
'node_modules/react-native'
)
def androidPrebuiltBinaryDir = new File(
parentDir,
'node_modules/react-native/android'
)
if (androidPrebuiltBinaryDir.exists()) {
maven {
url androidPrebuiltBinaryDir.toString()
name androidSourcesName
}
logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
found = true
} else if (androidSourcesDir.exists()) {
maven {
url androidSourcesDir.toString()
name androidSourcesName
}
logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
found = true
}
})
}
if (!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again."
)
}
}
def kotlin_version = getExtOrDefault('kotlinVersion')
dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation "com.solanamobile:mobile-wallet-adapter-clientlib:2.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0"
}
if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "SolanaMobileWalletAdapterModule"
codegenJavaPackageName = "com.solanamobile.mobilewalletadapter.reactnative"
}
}

View File

@@ -0,0 +1,5 @@
SolanaMobileWalletAdapterModule_kotlinVersion=1.9.0
SolanaMobileWalletAdapterModule_minSdkVersion=21
SolanaMobileWalletAdapterModule_targetSdkVersion=33
SolanaMobileWalletAdapterModule_compileSdkVersion=33
SolanaMobileWalletAdapterModule_ndkversion=23.1.7779620

View File

@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -0,0 +1,248 @@
#!/bin/sh
#
# Copyright © 2015 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/2d6327017519d23b96af35865dc997fcb544fb40/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@@ -0,0 +1,93 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solanamobile.mobilewalletadapter.reactnative">
</manifest>

View File

@@ -0,0 +1,84 @@
package com.solanamobile.mobilewalletadapter.reactnative
import com.facebook.react.bridge.*
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
object JSONSerializationUtils {
@Throws(JSONException::class)
fun convertMapToJson(readableMap: ReadableMap?): JSONObject {
val json = JSONObject()
readableMap?.keySetIterator()?.let { iterator ->
while (iterator.hasNextKey()) {
val key = iterator.nextKey()
when (readableMap.getType(key)) {
ReadableType.Array -> json.put(
key,
readableMap.getArray(key)?.let { convertArrayToJson(it) }
)
ReadableType.Boolean -> json.put(key, readableMap.getBoolean(key))
ReadableType.Map -> json.put(key, convertMapToJson(readableMap.getMap(key)))
ReadableType.Null -> json.put(key, JSONObject.NULL)
ReadableType.Number -> json.put(key, readableMap.getDouble(key))
ReadableType.String -> json.put(key, readableMap.getString(key))
}
}
}
return json
}
@Throws(JSONException::class)
private fun convertArrayToJson(readableArray: ReadableArray?): JSONArray {
val array = JSONArray()
readableArray?.let {
for (i in 0 until readableArray.size()) {
when (readableArray.getType(i)) {
ReadableType.Array -> array.put(convertArrayToJson(readableArray.getArray(i)))
ReadableType.Boolean -> array.put(readableArray.getBoolean(i))
ReadableType.Map -> array.put(convertMapToJson(readableArray.getMap(i)))
ReadableType.Null -> {}
ReadableType.Number -> array.put(readableArray.getDouble(i))
ReadableType.String -> array.put(readableArray.getString(i))
}
}
}
return array
}
@Throws(JSONException::class)
fun convertJsonToMap(jsonObject: JSONObject): ReadableMap {
val map: WritableMap = WritableNativeMap()
val iterator = jsonObject.keys()
while (iterator.hasNext()) {
val key = iterator.next()
when (val value = jsonObject[key]) {
is Boolean -> map.putBoolean(key, value)
is Double -> map.putDouble(key, value)
is Int -> map.putInt(key, value)
is JSONArray -> map.putArray(key, convertJsonToArray(value))
is JSONObject -> map.putMap(key, convertJsonToMap(value))
is String -> map.putString(key, value)
else -> map.putString(key, value.toString())
}
}
return map
}
@Throws(JSONException::class)
private fun convertJsonToArray(jsonArray: JSONArray): ReadableArray {
val array: WritableArray = WritableNativeArray()
for (i in 0 until jsonArray.length()) {
when (val value = jsonArray[i]) {
is Boolean -> array.pushBoolean(value)
is Double -> array.pushDouble(value)
is Int -> array.pushInt(value)
is JSONArray -> array.pushArray(convertJsonToArray(value))
is JSONObject -> array.pushMap(convertJsonToMap(value))
is String -> array.pushString(value)
else -> array.pushString(value.toString())
}
}
return array
}
}

View File

@@ -0,0 +1,233 @@
package com.solanamobile.mobilewalletadapter.reactnative
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.util.Log
import com.facebook.react.bridge.*
import com.facebook.react.jstasks.HeadlessJsTaskConfig
import com.facebook.react.jstasks.HeadlessJsTaskContext
import com.solana.mobilewalletadapter.clientlib.protocol.JsonRpc20Client
import com.solana.mobilewalletadapter.clientlib.protocol.MobileWalletAdapterClient
import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationIntentCreator
import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationScenario
import com.solana.mobilewalletadapter.common.protocol.SessionProperties.ProtocolVersion
import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertJsonToMap
import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertMapToJson
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import org.json.JSONObject
class SolanaMobileWalletAdapterModule(reactContext: ReactApplicationContext) :
SolanaMobileWalletAdapterSpec(reactContext), CoroutineScope {
data class SessionState(
val client: MobileWalletAdapterClient,
val localAssociation: LocalAssociationScenario,
)
override val coroutineContext =
Dispatchers.IO + CoroutineName("SolanaMobileWalletAdapterModuleScope") + SupervisorJob()
companion object {
const val NAME = "SolanaMobileWalletAdapter"
private const val TAG = "SolanaMobileWalletAdapterModule"
private const val ASSOCIATION_TIMEOUT_MS = 10000
private const val CLIENT_TIMEOUT_MS = 90000
private const val REQUEST_LOCAL_ASSOCIATION = 0
}
// Used to ensure that you can't start more than one session at a time.
private val mutex: Mutex = Mutex()
private var sessionState: SessionState? = null
private var associationResultCallback: ((Int) -> Unit)? = null
private val mActivityEventListener: ActivityEventListener =
object : BaseActivityEventListener() {
override fun onActivityResult(
activity: Activity,
requestCode: Int,
resultCode: Int,
data: Intent?
) {
if (requestCode == REQUEST_LOCAL_ASSOCIATION)
associationResultCallback?.invoke(resultCode)
}
}
private val sessionBackgroundTaskConfig
get() = HeadlessJsTaskConfig(
"SolanaMobileWalletAdapterSessionBackgroundTask",
Arguments.createMap(),
0,
true
)
init {
reactContext.addActivityEventListener(mActivityEventListener)
}
override fun getName(): String {
return NAME
}
@ReactMethod
override fun startSession(config: ReadableMap?, promise: Promise): Unit {
launch {
mutex.lock()
Log.d(TAG, "startSession with config $config")
var sessionTaskId: Int? = null
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactApplicationContext)
val finishHeadlessTask = { taskId: Int? ->
try {
if (taskId != null && headlessJsTaskContext.isTaskRunning(taskId)) {
headlessJsTaskContext.finishTask(taskId)
}
// fix for Expo 52/RN 0.72/0.73 where the older kotlin/gradle toolchain complains
// about the above if statement being used as an expression. Explicitly returning
// Unit here tells the compiler that the above if is not an expression
Unit
} catch (e: Exception) {
Log.w(TAG, "Failed to finish headless JS task", e)
}
}
try {
val uriPrefix = config?.getString("baseUri")?.let { Uri.parse(it) }
val localAssociation =
LocalAssociationScenario(
CLIENT_TIMEOUT_MS,
)
val intent =
LocalAssociationIntentCreator.createAssociationIntent(
uriPrefix,
localAssociation.port,
localAssociation.session
)
withContext(Dispatchers.Main) {
sessionTaskId = headlessJsTaskContext.startTask(sessionBackgroundTaskConfig)
}
associationResultCallback = { resultCode ->
if (resultCode == Activity.RESULT_CANCELED) {
Log.d(TAG, "Local association cancelled by user, ending session")
promise.reject(
"Session not established: Local association cancelled by user",
LocalAssociationScenario.ConnectionFailedException(
"Local association cancelled by user"
)
)
localAssociation.close()
}
// stop the headless js task, regardless if the association was successful or not
finishHeadlessTask(sessionTaskId)
}
reactApplicationContext.currentActivity?.apply {
startActivityForResult(intent, REQUEST_LOCAL_ASSOCIATION)
} ?: throw NullPointerException(
"Could not find a current activity from which to launch a local association"
)
val client = localAssociation.start()
.get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
sessionState = SessionState(client, localAssociation)
val sessionPropertiesMap: WritableMap = WritableNativeMap()
sessionPropertiesMap.putString(
"protocol_version",
when (localAssociation.session.sessionProperties.protocolVersion) {
ProtocolVersion.LEGACY -> "legacy"
ProtocolVersion.V1 -> "v1"
}
)
promise.resolve(sessionPropertiesMap)
} catch (e: ActivityNotFoundException) {
Log.e(TAG, "Found no installed wallet that supports the mobile wallet protocol", e)
finishHeadlessTask(sessionTaskId)
cleanup()
promise.reject("ERROR_WALLET_NOT_FOUND", e)
} catch (e: TimeoutException) {
Log.e(TAG, "Timed out waiting for local association to be ready", e)
finishHeadlessTask(sessionTaskId)
cleanup()
promise.reject("Timed out waiting for local association to be ready", e)
} catch (e: InterruptedException) {
Log.w(TAG, "Interrupted while waiting for local association to be ready", e)
finishHeadlessTask(sessionTaskId)
cleanup()
promise.reject(e)
} catch (e: ExecutionException) {
Log.e(TAG, "Failed establishing local association with wallet", e.cause)
finishHeadlessTask(sessionTaskId)
cleanup()
promise.reject(e)
} catch (e: Throwable) {
Log.e(TAG, "Failed to start session", e)
finishHeadlessTask(sessionTaskId)
cleanup()
promise.reject(e)
}
}
}
@ReactMethod
override fun invoke(method: String, params: ReadableMap?, promise: Promise): Unit =
sessionState?.let {
Log.d(TAG, "invoke `$method` with params $params")
try {
val result = it.client
.methodCall(method, convertMapToJson(params), CLIENT_TIMEOUT_MS)
.get() as JSONObject
promise.resolve(convertJsonToMap(result))
} catch (e: ExecutionException) {
val cause = e.cause
if (cause is JsonRpc20Client.JsonRpc20RemoteException) {
val userInfo = Arguments.createMap()
userInfo.putInt("jsonRpcErrorCode", cause.code)
promise.reject("JSON_RPC_ERROR", cause, userInfo)
} else if (cause is TimeoutException) {
promise.reject("Timed out waiting for response", e)
} else {
throw e
}
} catch (e: Throwable) {
Log.e(TAG, "Failed to invoke `$method` with params $params", e)
promise.reject(e)
}
} ?: throw NullPointerException(
"Tried to invoke `$method` without an active session"
)
@ReactMethod
override fun endSession(promise: Promise): Unit {
sessionState?.let {
launch {
Log.d(TAG, "endSession")
try {
it.localAssociation
.close()
.get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
cleanup()
promise.resolve(true)
} catch (e: TimeoutException) {
Log.e(TAG, "Timed out waiting for local association to close", e)
cleanup()
promise.reject("Failed to end session", e)
} catch (e: Throwable) {
Log.e(TAG, "Failed to end session", e)
cleanup()
promise.reject("Failed to end session", e)
}
}
} ?: throw NullPointerException("Tried to end a session without an active session")
}
private fun cleanup() {
sessionState = null
associationResultCallback = null
if (mutex.isLocked) {
mutex.unlock()
}
}
}

View File

@@ -0,0 +1,35 @@
package com.solanamobile.mobilewalletadapter.reactnative
import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import java.util.HashMap
class SolanaMobileWalletAdapterModulePackage : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == SolanaMobileWalletAdapterModule.NAME) {
SolanaMobileWalletAdapterModule(reactContext)
} else {
null
}
}
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
moduleInfos[SolanaMobileWalletAdapterModule.NAME] =
ReactModuleInfo(
SolanaMobileWalletAdapterModule.NAME,
SolanaMobileWalletAdapterModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
)
moduleInfos
}
}
}

View File

@@ -0,0 +1,7 @@
package com.solanamobile.mobilewalletadapter.reactnative
import com.facebook.react.bridge.ReactApplicationContext
abstract class SolanaMobileWalletAdapterSpec
internal constructor(context: ReactApplicationContext) :
NativeSolanaMobileWalletAdapterSpec(context) {}

View File

@@ -0,0 +1,16 @@
package com.solanamobile.mobilewalletadapter.reactnative
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReadableMap
abstract class SolanaMobileWalletAdapterSpec
internal constructor(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
abstract fun startSession(config: ReadableMap?, promise: Promise)
abstract fun invoke(method: String, params: ReadableMap?, promise: Promise)
abstract fun endSession(promise: Promise)
}