@solana-mobile/mobile-wallet-adapter-protocol 2.1.3 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/android/build.gradle +158 -146
  2. package/android/gradle/wrapper/gradle-wrapper.properties +5 -5
  3. package/android/gradle.properties +5 -5
  4. package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/JSONSerializationUtils.kt +11 -9
  5. package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterModule.kt +201 -178
  6. package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterPackage.kt +26 -7
  7. package/android/src/newarch/SolanaMobileWalletAdapter.kt +7 -0
  8. package/android/src/oldarch/SolanaMobileWalletAdapter.kt +16 -0
  9. package/lib/cjs/index.browser.js +266 -5
  10. package/lib/cjs/index.js +266 -5
  11. package/lib/cjs/index.native.js +5 -2
  12. package/lib/esm/index.browser.js +266 -6
  13. package/lib/esm/index.js +266 -6
  14. package/lib/types/index.browser.d.ts +17 -1
  15. package/lib/types/index.browser.d.ts.map +1 -1
  16. package/lib/types/index.d.ts +17 -1
  17. package/lib/types/index.d.ts.map +1 -1
  18. package/lib/types/index.native.d.ts +17 -1
  19. package/lib/types/index.native.d.ts.map +1 -1
  20. package/package.json +70 -58
  21. package/.gitignore +0 -2
  22. package/android/.gitignore +0 -14
  23. package/src/__forks__/react-native/base64Utils.ts +0 -1
  24. package/src/__forks__/react-native/transact.ts +0 -92
  25. package/src/arrayBufferToBase64String.ts +0 -10
  26. package/src/associationPort.ts +0 -19
  27. package/src/base64Utils.ts +0 -3
  28. package/src/createHelloReq.ts +0 -12
  29. package/src/createMobileWalletProxy.ts +0 -175
  30. package/src/createSIWSMessage.ts +0 -14
  31. package/src/createSequenceNumberVector.ts +0 -11
  32. package/src/encryptedMessage.ts +0 -60
  33. package/src/errors.ts +0 -95
  34. package/src/generateAssociationKeypair.ts +0 -10
  35. package/src/generateECDHKeypair.ts +0 -10
  36. package/src/getAssociateAndroidIntentURL.ts +0 -57
  37. package/src/getJWS.ts +0 -19
  38. package/src/getStringWithURLUnsafeBase64CharactersReplaced.ts +0 -11
  39. package/src/index.ts +0 -3
  40. package/src/jsonRpcMessage.ts +0 -38
  41. package/src/parseHelloRsp.ts +0 -46
  42. package/src/parseSessionProps.ts +0 -33
  43. package/src/startSession.ts +0 -94
  44. package/src/transact.ts +0 -266
  45. package/src/types.ts +0 -181
  46. package/tsconfig.cjs.json +0 -7
  47. package/tsconfig.json +0 -8
@@ -1,178 +1,201 @@
1
- package com.solanamobile.mobilewalletadapter.reactnative
2
-
3
- import android.app.Activity
4
- import android.content.ActivityNotFoundException
5
- import android.content.Intent
6
- import android.net.Uri
7
- import android.util.Log
8
- import com.facebook.react.bridge.*
9
- import com.solana.mobilewalletadapter.clientlib.protocol.JsonRpc20Client
10
- import com.solana.mobilewalletadapter.clientlib.protocol.MobileWalletAdapterClient
11
- import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationIntentCreator
12
- import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationScenario
13
- import com.solana.mobilewalletadapter.common.protocol.SessionProperties.ProtocolVersion
14
- import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertJsonToMap
15
- import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertMapToJson
16
- import kotlinx.coroutines.*
17
- import kotlinx.coroutines.sync.Mutex
18
- import org.json.JSONObject
19
- import java.util.concurrent.ExecutionException
20
- import java.util.concurrent.TimeUnit
21
- import java.util.concurrent.TimeoutException
22
-
23
- class SolanaMobileWalletAdapterModule(reactContext: ReactApplicationContext) :
24
- ReactContextBaseJavaModule(reactContext), CoroutineScope {
25
-
26
- data class SessionState(
27
- val client: MobileWalletAdapterClient,
28
- val localAssociation: LocalAssociationScenario,
29
- )
30
-
31
- override val coroutineContext =
32
- Dispatchers.IO + CoroutineName("SolanaMobileWalletAdapterModuleScope") + SupervisorJob()
33
-
34
- companion object {
35
- private const val ASSOCIATION_TIMEOUT_MS = 10000
36
- private const val CLIENT_TIMEOUT_MS = 90000
37
- private const val REQUEST_LOCAL_ASSOCIATION = 0
38
-
39
- // Used to ensure that you can't start more than one session at a time.
40
- private val mutex: Mutex = Mutex()
41
- private var sessionState: SessionState? = null
42
- private var associationResultCallback: ((Int) -> Unit)? = null
43
- }
44
-
45
- private val mActivityEventListener: ActivityEventListener =
46
- object : BaseActivityEventListener() {
47
- override fun onActivityResult(
48
- activity: Activity?,
49
- requestCode: Int,
50
- resultCode: Int,
51
- data: Intent?
52
- ) {
53
- if (requestCode == REQUEST_LOCAL_ASSOCIATION)
54
- associationResultCallback?.invoke(resultCode)
55
- }
56
- }
57
-
58
- init {
59
- reactContext.addActivityEventListener(mActivityEventListener)
60
- }
61
-
62
- override fun getName(): String {
63
- return "SolanaMobileWalletAdapter"
64
- }
65
-
66
- @ReactMethod
67
- fun startSession(config: ReadableMap?, promise: Promise) = launch {
68
- mutex.lock()
69
- Log.d(name, "startSession with config $config")
70
- try {
71
- val uriPrefix = config?.getString("baseUri")?.let { Uri.parse(it) }
72
- val localAssociation = LocalAssociationScenario(
73
- CLIENT_TIMEOUT_MS,
74
- )
75
- val intent = LocalAssociationIntentCreator.createAssociationIntent(
76
- uriPrefix,
77
- localAssociation.port,
78
- localAssociation.session
79
- )
80
- associationResultCallback = { resultCode ->
81
- if (resultCode == Activity.RESULT_CANCELED) {
82
- Log.d(name, "Local association cancelled by user, ending session")
83
- promise.reject("Session not established: Local association cancelled by user",
84
- LocalAssociationScenario.ConnectionFailedException("Local association cancelled by user"))
85
- localAssociation.close()
86
- }
87
- }
88
- currentActivity?.startActivityForResult(intent, REQUEST_LOCAL_ASSOCIATION)
89
- ?: throw NullPointerException("Could not find a current activity from which to launch a local association")
90
- val client =
91
- localAssociation.start().get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
92
- sessionState = SessionState(client, localAssociation)
93
- val sessionPropertiesMap: WritableMap = WritableNativeMap()
94
- sessionPropertiesMap.putString("protocol_version",
95
- when (localAssociation.session.sessionProperties.protocolVersion) {
96
- ProtocolVersion.LEGACY -> "legacy"
97
- ProtocolVersion.V1 -> "v1"
98
- })
99
- promise.resolve(sessionPropertiesMap)
100
- } catch (e: ActivityNotFoundException) {
101
- Log.e(name, "Found no installed wallet that supports the mobile wallet protocol", e)
102
- cleanup()
103
- promise.reject("ERROR_WALLET_NOT_FOUND", e)
104
- } catch (e: TimeoutException) {
105
- Log.e(name, "Timed out waiting for local association to be ready", e)
106
- cleanup()
107
- promise.reject("Timed out waiting for local association to be ready", e)
108
- } catch (e: InterruptedException) {
109
- Log.w(name, "Interrupted while waiting for local association to be ready", e)
110
- cleanup()
111
- promise.reject(e)
112
- } catch (e: ExecutionException) {
113
- Log.e(name, "Failed establishing local association with wallet", e.cause)
114
- cleanup()
115
- promise.reject(e)
116
- } catch (e: Throwable) {
117
- Log.e(name, "Failed to start session", e)
118
- cleanup()
119
- promise.reject(e)
120
- }
121
- }
122
-
123
- @ReactMethod
124
- fun invoke(method: String, params: ReadableMap, promise: Promise) = sessionState?.let {
125
- Log.d(name, "invoke `$method` with params $params")
126
- try {
127
- val result = it.client.methodCall(
128
- method,
129
- convertMapToJson(params),
130
- CLIENT_TIMEOUT_MS
131
- ).get() as JSONObject
132
- promise.resolve(convertJsonToMap(result))
133
- } catch (e: ExecutionException) {
134
- val cause = e.cause
135
- if (cause is JsonRpc20Client.JsonRpc20RemoteException) {
136
- val userInfo = Arguments.createMap()
137
- userInfo.putInt("jsonRpcErrorCode", cause.code)
138
- promise.reject("JSON_RPC_ERROR", cause, userInfo)
139
- } else if (cause is TimeoutException) {
140
- promise.reject("Timed out waiting for response", e)
141
- } else {
142
- throw e
143
- }
144
- } catch (e: Throwable) {
145
- Log.e(name, "Failed to invoke `$method` with params $params", e)
146
- promise.reject(e)
147
- }
148
- } ?: throw NullPointerException("Tried to invoke `$method` without an active session")
149
-
150
- @ReactMethod
151
- fun endSession(promise: Promise) = sessionState?.let {
152
- launch {
153
- Log.d(name, "endSession")
154
- try {
155
- it.localAssociation.close()
156
- .get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
157
- cleanup()
158
- promise.resolve(true)
159
- } catch (e: TimeoutException) {
160
- Log.e(name, "Timed out waiting for local association to close", e)
161
- cleanup()
162
- promise.reject("Failed to end session", e)
163
- } catch (e: Throwable) {
164
- Log.e(name, "Failed to end session", e)
165
- cleanup()
166
- promise.reject("Failed to end session", e)
167
- }
168
- }
169
- } ?: throw NullPointerException("Tried to end a session without an active session")
170
-
171
- private fun cleanup() {
172
- sessionState = null
173
- associationResultCallback = null
174
- if (mutex.isLocked) {
175
- mutex.unlock()
176
- }
177
- }
178
- }
1
+ package com.solanamobile.mobilewalletadapter.reactnative
2
+
3
+ import android.app.Activity
4
+ import android.content.ActivityNotFoundException
5
+ import android.content.Intent
6
+ import android.net.Uri
7
+ import android.util.Log
8
+ import com.facebook.react.bridge.*
9
+ import com.solana.mobilewalletadapter.clientlib.protocol.JsonRpc20Client
10
+ import com.solana.mobilewalletadapter.clientlib.protocol.MobileWalletAdapterClient
11
+ import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationIntentCreator
12
+ import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationScenario
13
+ import com.solana.mobilewalletadapter.common.protocol.SessionProperties.ProtocolVersion
14
+ import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertJsonToMap
15
+ import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertMapToJson
16
+ import java.util.concurrent.ExecutionException
17
+ import java.util.concurrent.TimeUnit
18
+ import java.util.concurrent.TimeoutException
19
+ import kotlinx.coroutines.*
20
+ import kotlinx.coroutines.sync.Mutex
21
+ import org.json.JSONObject
22
+
23
+ class SolanaMobileWalletAdapterModule(reactContext: ReactApplicationContext) :
24
+ SolanaMobileWalletAdapterSpec(reactContext), CoroutineScope {
25
+
26
+ data class SessionState(
27
+ val client: MobileWalletAdapterClient,
28
+ val localAssociation: LocalAssociationScenario,
29
+ )
30
+
31
+ override val coroutineContext =
32
+ Dispatchers.IO + CoroutineName("SolanaMobileWalletAdapterModuleScope") + SupervisorJob()
33
+
34
+ companion object {
35
+ const val NAME = "SolanaMobileWalletAdapter"
36
+ private const val ASSOCIATION_TIMEOUT_MS = 10000
37
+ private const val CLIENT_TIMEOUT_MS = 90000
38
+ private const val REQUEST_LOCAL_ASSOCIATION = 0
39
+
40
+ // Used to ensure that you can't start more than one session at a time.
41
+ private val mutex: Mutex = Mutex()
42
+ private var sessionState: SessionState? = null
43
+ private var associationResultCallback: ((Int) -> Unit)? = null
44
+ }
45
+
46
+ private val mActivityEventListener: ActivityEventListener =
47
+ object : BaseActivityEventListener() {
48
+ override fun onActivityResult(
49
+ activity: Activity?,
50
+ requestCode: Int,
51
+ resultCode: Int,
52
+ data: Intent?
53
+ ) {
54
+ if (requestCode == REQUEST_LOCAL_ASSOCIATION)
55
+ associationResultCallback?.invoke(resultCode)
56
+ }
57
+ }
58
+
59
+ init {
60
+ reactContext.addActivityEventListener(mActivityEventListener)
61
+ }
62
+
63
+ override fun getName(): String {
64
+ return NAME
65
+ }
66
+
67
+ @ReactMethod
68
+ override fun startSession(config: ReadableMap?, promise: Promise): Unit {
69
+ launch {
70
+ mutex.lock()
71
+ Log.d(name, "startSession with config $config")
72
+ try {
73
+ val uriPrefix = config?.getString("baseUri")?.let { Uri.parse(it) }
74
+ val localAssociation =
75
+ LocalAssociationScenario(
76
+ CLIENT_TIMEOUT_MS,
77
+ )
78
+ val intent =
79
+ LocalAssociationIntentCreator.createAssociationIntent(
80
+ uriPrefix,
81
+ localAssociation.port,
82
+ localAssociation.session
83
+ )
84
+ associationResultCallback = { resultCode ->
85
+ if (resultCode == Activity.RESULT_CANCELED) {
86
+ Log.d(name, "Local association cancelled by user, ending session")
87
+ promise.reject(
88
+ "Session not established: Local association cancelled by user",
89
+ LocalAssociationScenario.ConnectionFailedException(
90
+ "Local association cancelled by user"
91
+ )
92
+ )
93
+ localAssociation.close()
94
+ }
95
+ }
96
+ currentActivity?.startActivityForResult(intent, REQUEST_LOCAL_ASSOCIATION)
97
+ ?: throw NullPointerException(
98
+ "Could not find a current activity from which to launch a local association"
99
+ )
100
+ val client =
101
+ localAssociation
102
+ .start()
103
+ .get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
104
+ sessionState = SessionState(client, localAssociation)
105
+ val sessionPropertiesMap: WritableMap = WritableNativeMap()
106
+ sessionPropertiesMap.putString(
107
+ "protocol_version",
108
+ when (localAssociation.session.sessionProperties.protocolVersion) {
109
+ ProtocolVersion.LEGACY -> "legacy"
110
+ ProtocolVersion.V1 -> "v1"
111
+ }
112
+ )
113
+ promise.resolve(sessionPropertiesMap)
114
+ } catch (e: ActivityNotFoundException) {
115
+ Log.e(name, "Found no installed wallet that supports the mobile wallet protocol", e)
116
+ cleanup()
117
+ promise.reject("ERROR_WALLET_NOT_FOUND", e)
118
+ } catch (e: TimeoutException) {
119
+ Log.e(name, "Timed out waiting for local association to be ready", e)
120
+ cleanup()
121
+ promise.reject("Timed out waiting for local association to be ready", e)
122
+ } catch (e: InterruptedException) {
123
+ Log.w(name, "Interrupted while waiting for local association to be ready", e)
124
+ cleanup()
125
+ promise.reject(e)
126
+ } catch (e: ExecutionException) {
127
+ Log.e(name, "Failed establishing local association with wallet", e.cause)
128
+ cleanup()
129
+ promise.reject(e)
130
+ } catch (e: Throwable) {
131
+ Log.e(name, "Failed to start session", e)
132
+ cleanup()
133
+ promise.reject(e)
134
+ }
135
+ }
136
+ }
137
+
138
+ @ReactMethod
139
+ override fun invoke(method: String, params: ReadableMap?, promise: Promise): Unit =
140
+ sessionState?.let {
141
+ Log.d(name, "invoke `$method` with params $params")
142
+ try {
143
+ val result =
144
+ it.client
145
+ .methodCall(method, convertMapToJson(params), CLIENT_TIMEOUT_MS)
146
+ .get() as
147
+ JSONObject
148
+ promise.resolve(convertJsonToMap(result))
149
+ } catch (e: ExecutionException) {
150
+ val cause = e.cause
151
+ if (cause is JsonRpc20Client.JsonRpc20RemoteException) {
152
+ val userInfo = Arguments.createMap()
153
+ userInfo.putInt("jsonRpcErrorCode", cause.code)
154
+ promise.reject("JSON_RPC_ERROR", cause, userInfo)
155
+ } else if (cause is TimeoutException) {
156
+ promise.reject("Timed out waiting for response", e)
157
+ } else {
158
+ throw e
159
+ }
160
+ } catch (e: Throwable) {
161
+ Log.e(name, "Failed to invoke `$method` with params $params", e)
162
+ promise.reject(e)
163
+ }
164
+ }
165
+ ?: throw NullPointerException(
166
+ "Tried to invoke `$method` without an active session"
167
+ )
168
+
169
+ @ReactMethod
170
+ override fun endSession(promise: Promise): Unit {
171
+ sessionState?.let {
172
+ launch {
173
+ Log.d(name, "endSession")
174
+ try {
175
+ it.localAssociation
176
+ .close()
177
+ .get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
178
+ cleanup()
179
+ promise.resolve(true)
180
+ } catch (e: TimeoutException) {
181
+ Log.e(name, "Timed out waiting for local association to close", e)
182
+ cleanup()
183
+ promise.reject("Failed to end session", e)
184
+ } catch (e: Throwable) {
185
+ Log.e(name, "Failed to end session", e)
186
+ cleanup()
187
+ promise.reject("Failed to end session", e)
188
+ }
189
+ }
190
+ }
191
+ ?: throw NullPointerException("Tried to end a session without an active session")
192
+ }
193
+
194
+ private fun cleanup() {
195
+ sessionState = null
196
+ associationResultCallback = null
197
+ if (mutex.isLocked) {
198
+ mutex.unlock()
199
+ }
200
+ }
201
+ }
@@ -1,16 +1,35 @@
1
1
  package com.solanamobile.mobilewalletadapter.reactnative
2
2
 
3
- import com.facebook.react.ReactPackage
3
+ import com.facebook.react.TurboReactPackage
4
4
  import com.facebook.react.bridge.NativeModule
5
5
  import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.uimanager.ViewManager
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
7
9
 
8
- class SolanaMobileWalletAdapterPackage : ReactPackage {
9
- override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
10
- return listOf(SolanaMobileWalletAdapterModule(reactContext))
10
+ class SolanaMobileWalletAdapterModulePackage : TurboReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == SolanaMobileWalletAdapterModule.NAME) {
13
+ SolanaMobileWalletAdapterModule(reactContext)
14
+ } else {
15
+ null
16
+ }
11
17
  }
12
18
 
13
- override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
14
- return emptyList()
19
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20
+ return ReactModuleInfoProvider {
21
+ val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22
+ moduleInfos[SolanaMobileWalletAdapterModule.NAME] =
23
+ ReactModuleInfo(
24
+ SolanaMobileWalletAdapterModule.NAME,
25
+ SolanaMobileWalletAdapterModule.NAME,
26
+ false, // canOverrideExistingModule
27
+ false, // needsEagerInit
28
+ true, // hasConstants
29
+ false, // isCxxModule
30
+ true // isTurboModule
31
+ )
32
+ moduleInfos
33
+ }
15
34
  }
16
35
  }
@@ -0,0 +1,7 @@
1
+ package com.solanamobile.mobilewalletadapter.reactnative
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext
4
+
5
+ abstract class SolanaMobileWalletAdapterSpec
6
+ internal constructor(context: ReactApplicationContext) :
7
+ NativeSolanaMobileWalletAdapterSpec(context) {}
@@ -0,0 +1,16 @@
1
+ package com.solanamobile.mobilewalletadapter.reactnative
2
+
3
+ import com.facebook.react.bridge.Promise
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
6
+ import com.facebook.react.bridge.ReadableMap
7
+
8
+ abstract class SolanaMobileWalletAdapterSpec
9
+ internal constructor(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
10
+
11
+ abstract fun startSession(config: ReadableMap?, promise: Promise)
12
+
13
+ abstract fun invoke(method: String, params: ReadableMap?, promise: Promise)
14
+
15
+ abstract fun endSession(promise: Promise)
16
+ }