@solana-mobile/mobile-wallet-adapter-protocol 2.0.0 → 2.0.2

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 (70) hide show
  1. package/.gitignore +2 -0
  2. package/README.md +69 -62
  3. package/android/.gitignore +14 -0
  4. package/android/.gradle/4.4.1/fileChanges/last-build.bin +0 -0
  5. package/android/.gradle/4.4.1/fileHashes/fileHashes.bin +0 -0
  6. package/android/.gradle/4.4.1/fileHashes/fileHashes.lock +0 -0
  7. package/android/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  8. package/android/.gradle/7.1/dependencies-accessors/gc.properties +0 -0
  9. package/android/.gradle/7.1/fileChanges/last-build.bin +0 -0
  10. package/android/.gradle/7.1/fileHashes/fileHashes.lock +0 -0
  11. package/android/.gradle/7.1/gc.properties +0 -0
  12. package/android/.gradle/7.5/checksums/checksums.lock +0 -0
  13. package/android/.gradle/7.5/dependencies-accessors/dependencies-accessors.lock +0 -0
  14. package/android/.gradle/7.5/dependencies-accessors/gc.properties +0 -0
  15. package/android/.gradle/7.5/fileChanges/last-build.bin +0 -0
  16. package/android/.gradle/7.5/fileHashes/fileHashes.lock +0 -0
  17. package/android/.gradle/7.5/gc.properties +0 -0
  18. package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
  19. package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
  20. package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  21. package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
  22. package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
  23. package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
  24. package/android/.gradle/7.5.1/gc.properties +0 -0
  25. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  26. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  27. package/android/.gradle/checksums/checksums.lock +0 -0
  28. package/android/.gradle/checksums/md5-checksums.bin +0 -0
  29. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  30. package/android/.gradle/vcs-1/gc.properties +0 -0
  31. package/android/build.gradle +146 -146
  32. package/android/gradle/wrapper/gradle-wrapper.properties +5 -5
  33. package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterModule.kt +178 -139
  34. package/lib/cjs/index.browser.js +290 -81
  35. package/lib/cjs/index.js +290 -81
  36. package/lib/cjs/index.native.js +180 -28
  37. package/lib/esm/index.browser.js +288 -82
  38. package/lib/esm/index.js +288 -82
  39. package/lib/types/index.browser.d.ts +68 -5
  40. package/lib/types/index.browser.d.ts.map +1 -1
  41. package/lib/types/index.d.ts +68 -5
  42. package/lib/types/index.d.ts.map +1 -1
  43. package/lib/types/index.native.d.ts +68 -5
  44. package/lib/types/index.native.d.ts.map +1 -1
  45. package/package.json +56 -51
  46. package/src/__forks__/react-native/base64Utils.ts +1 -0
  47. package/src/__forks__/react-native/transact.ts +92 -0
  48. package/src/arrayBufferToBase64String.ts +10 -0
  49. package/src/associationPort.ts +19 -0
  50. package/src/base64Utils.ts +3 -0
  51. package/src/createHelloReq.ts +12 -0
  52. package/src/createMobileWalletProxy.ts +175 -0
  53. package/src/createSIWSMessage.ts +14 -0
  54. package/src/createSequenceNumberVector.ts +11 -0
  55. package/src/encryptedMessage.ts +60 -0
  56. package/src/errors.ts +95 -0
  57. package/src/generateAssociationKeypair.ts +10 -0
  58. package/src/generateECDHKeypair.ts +10 -0
  59. package/src/getAssociateAndroidIntentURL.ts +57 -0
  60. package/src/getJWS.ts +19 -0
  61. package/src/getStringWithURLUnsafeBase64CharactersReplaced.ts +11 -0
  62. package/src/index.ts +3 -0
  63. package/src/jsonRpcMessage.ts +38 -0
  64. package/src/parseHelloRsp.ts +46 -0
  65. package/src/parseSessionProps.ts +33 -0
  66. package/src/startSession.ts +94 -0
  67. package/src/transact.ts +266 -0
  68. package/src/types.ts +181 -0
  69. package/tsconfig.cjs.json +7 -0
  70. package/tsconfig.json +8 -0
@@ -1,139 +1,178 @@
1
- package com.solanamobile.mobilewalletadapter.reactnative
2
-
3
- import android.content.ActivityNotFoundException
4
- import android.net.Uri
5
- import android.util.Log
6
- import com.facebook.react.bridge.*
7
- import com.solana.mobilewalletadapter.clientlib.protocol.JsonRpc20Client
8
- import com.solana.mobilewalletadapter.clientlib.protocol.MobileWalletAdapterClient
9
- import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationIntentCreator
10
- import com.solana.mobilewalletadapter.clientlib.scenario.LocalAssociationScenario
11
- import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertJsonToMap
12
- import com.solanamobile.mobilewalletadapter.reactnative.JSONSerializationUtils.convertMapToJson
13
- import kotlinx.coroutines.*
14
- import kotlinx.coroutines.sync.Mutex
15
- import org.json.JSONObject
16
- import java.util.concurrent.ExecutionException
17
- import java.util.concurrent.TimeUnit
18
- import java.util.concurrent.TimeoutException
19
-
20
- class SolanaMobileWalletAdapterModule(reactContext: ReactApplicationContext) :
21
- ReactContextBaseJavaModule(reactContext), CoroutineScope {
22
-
23
- data class SessionState(
24
- val client: MobileWalletAdapterClient,
25
- val localAssociation: LocalAssociationScenario,
26
- )
27
-
28
- override val coroutineContext =
29
- Dispatchers.IO + CoroutineName("SolanaMobileWalletAdapterModuleScope") + SupervisorJob()
30
-
31
- companion object {
32
- private const val ASSOCIATION_TIMEOUT_MS = 10000
33
- private const val CLIENT_TIMEOUT_MS = 90000
34
-
35
- // Used to ensure that you can't start more than one session at a time.
36
- private val mutex: Mutex = Mutex()
37
- private var sessionState: SessionState? = null
38
- }
39
-
40
- override fun getName(): String {
41
- return "SolanaMobileWalletAdapter"
42
- }
43
-
44
- @ReactMethod
45
- fun startSession(config: ReadableMap?, promise: Promise) = launch {
46
- mutex.lock()
47
- Log.d(name, "startSession with config $config")
48
- try {
49
- val uriPrefix = config?.getString("baseUri")?.let { Uri.parse(it) }
50
- val localAssociation = LocalAssociationScenario(
51
- CLIENT_TIMEOUT_MS,
52
- )
53
- val intent = LocalAssociationIntentCreator.createAssociationIntent(
54
- uriPrefix,
55
- localAssociation.port,
56
- localAssociation.session
57
- )
58
- currentActivity?.startActivityForResult(intent, 0)
59
- ?: throw NullPointerException("Could not find a current activity from which to launch a local association")
60
- val client =
61
- localAssociation.start().get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
62
- sessionState = SessionState(client, localAssociation)
63
- promise.resolve(true)
64
- } catch (e: ActivityNotFoundException) {
65
- Log.e(name, "Found no installed wallet that supports the mobile wallet protocol", e)
66
- cleanup()
67
- promise.reject("ERROR_WALLET_NOT_FOUND", e)
68
- } catch (e: TimeoutException) {
69
- Log.e(name, "Timed out waiting for local association to be ready", e)
70
- cleanup()
71
- promise.reject("Timed out waiting for local association to be ready", e)
72
- } catch (e: InterruptedException) {
73
- Log.w(name, "Interrupted while waiting for local association to be ready", e)
74
- cleanup()
75
- promise.reject(e)
76
- } catch (e: ExecutionException) {
77
- Log.e(name, "Failed establishing local association with wallet", e.cause)
78
- cleanup()
79
- promise.reject(e)
80
- } catch (e: Throwable) {
81
- Log.e(name, "Failed to start session", e)
82
- cleanup()
83
- promise.reject(e)
84
- }
85
- }
86
-
87
- @ReactMethod
88
- fun invoke(method: String, params: ReadableMap, promise: Promise) = sessionState?.let {
89
- Log.d(name, "invoke `$method` with params $params")
90
- try {
91
- val result = it.client.methodCall(
92
- method,
93
- convertMapToJson(params),
94
- CLIENT_TIMEOUT_MS
95
- ).get() as JSONObject
96
- promise.resolve(convertJsonToMap(result))
97
- } catch (e: ExecutionException) {
98
- val cause = e.cause
99
- if (cause is JsonRpc20Client.JsonRpc20RemoteException) {
100
- val userInfo = Arguments.createMap()
101
- userInfo.putInt("jsonRpcErrorCode", cause.code)
102
- promise.reject("JSON_RPC_ERROR", cause, userInfo)
103
- } else {
104
- throw e
105
- }
106
- } catch (e: Throwable) {
107
- Log.e(name, "Failed to invoke `$method` with params $params", e)
108
- promise.reject(e)
109
- }
110
- } ?: throw NullPointerException("Tried to invoke `$method` without an active session")
111
-
112
- @ReactMethod
113
- fun endSession(promise: Promise) = sessionState?.let {
114
- launch {
115
- Log.d(name, "endSession")
116
- try {
117
- it.localAssociation.close()
118
- .get(ASSOCIATION_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS)
119
- cleanup()
120
- promise.resolve(true)
121
- } catch (e: TimeoutException) {
122
- Log.e(name, "Timed out waiting for local association to close", e)
123
- cleanup()
124
- promise.reject("Failed to end session", e)
125
- } catch (e: Throwable) {
126
- Log.e(name, "Failed to end session", e)
127
- cleanup()
128
- promise.reject("Failed to end session", e)
129
- }
130
- }
131
- } ?: throw NullPointerException("Tried to end a session without an active session")
132
-
133
- private fun cleanup() {
134
- sessionState = null
135
- if (mutex.isLocked) {
136
- mutex.unlock()
137
- }
138
- }
139
- }
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
+ }