@solana-mobile/mobile-wallet-adapter-walletlib 1.3.0 → 1.4.0-beta2

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.
package/README.md CHANGED
@@ -10,46 +10,102 @@ This package is still in alpha and is not production ready. However, the API is
10
10
 
11
11
  ## Quickstart
12
12
 
13
- ### 1. Start listening and handling MWA requests
13
+ ### 1. Initialize the MWA event listener
14
14
 
15
- Use this API to start a session and start handling requests:
16
- ```typescript
15
+ Use the following API to start listening for MWA requests and events, and register request handlers.
16
+
17
+ ```ts
17
18
  import {
18
- MobileWalletAdapterConfig,
19
+ initializeMWAEventListener,
19
20
  MWARequest,
20
21
  MWASessionEvent,
21
- useMobileWalletAdapterSession,
22
- } from '@solana-mobile/mobile-wallet-adapter-protocol-walletlib';
23
-
24
- const config: MobileWalletAdapterConfig = useMemo(() => {
25
- return {
26
- supportsSignAndSendTransactions: true,
27
- maxTransactionsPerSigningRequest: 10,
28
- maxMessagesPerSigningRequest: 10,
29
- supportedTransactionVersions: [0, 'legacy'],
30
- noConnectionWarningTimeoutMs: 3000,
31
- };
32
- }, []);
22
+ } from '@solana-mobile/mobile-wallet-adapter-walletlib';
33
23
 
34
- // MWA Session Handlers
35
- const handleRequest = useCallback((request: MWARequest) => {
36
- /* ... */
37
- }, []);
24
+ const listener: EmitterSubscription = initializeMWAEventListener(
25
+ (request: MWARequest) => { /* ... */ },
26
+ (sessionEvent: MWASessionEvent) => { /* ... */ },
27
+ );
38
28
 
39
- const handleSessionEvent = useCallback((sessionEvent: MWASessionEvent) => {
40
- /* ... */
41
- }, []);
29
+ /* ... */
42
30
 
43
- // Connect to the calling dApp and begin handling dApp requests
44
- useMobileWalletAdapterSession(
45
- 'Example Wallet Label',
46
- config,
47
- handleRequest,
48
- handleSessionEvent,
49
- );
31
+ // Clean up the listener when it is out of scope
32
+ listener.remove()
33
+ ```
34
+
35
+ You should ensure the listener is cleaned up with `listener.remove()` when it goes out of scope (e.g `listener.remove()` on component lifecycle unmount).
36
+
37
+ ### 2. Initialize the MWA session
38
+
39
+ Define your wallet config and use `initializeMWASession` to establish a session with the dApp endpoint and begin emission of MWA requests/events.
40
+
41
+ > **Note:** This should be called *after* `initializeMWAEventListener` is called, to ensure no events are missed.
42
+
43
+ ```ts
44
+ const config: MobileWalletAdapterConfig = {
45
+ supportsSignAndSendTransactions: true,
46
+ maxTransactionsPerSigningRequest: 10,
47
+ maxMessagesPerSigningRequest: 10,
48
+ supportedTransactionVersions: [0, 'legacy'],
49
+ noConnectionWarningTimeoutMs: 3000,
50
+ optionalFeatures: ['solana:signInWithSolana']
51
+ };
52
+
53
+ try {
54
+ const sessionId = await initializeMobileWalletAdapterSession(
55
+ 'Wallet Name',
56
+ config,
57
+ );
58
+ console.log('sessionId: ' + sessionId);
59
+ } catch (e: any) {
60
+ if (e instanceof SolanaMWAWalletLibError) {
61
+ console.error(e.name, e.code, e.message);
62
+ } else {
63
+ console.error(e);
64
+ }
65
+ }
66
+ ```
67
+
68
+ > **Note**: Although, the `initializeMobileWalletAdapterSession` method returns a `sessionId`, this library only supports one active session for now.
69
+
70
+ ### Example usage:
71
+
72
+ ```ts
73
+ // When your MWA entrypoint is loaded, call a `useEffect` to kick off the listener and session.
74
+ useEffect(() => {
75
+ async function initializeMWASession() {
76
+ const config: MobileWalletAdapterConfig = {
77
+ supportsSignAndSendTransactions: true,
78
+ maxTransactionsPerSigningRequest: 10,
79
+ maxMessagesPerSigningRequest: 10,
80
+ supportedTransactionVersions: [0, 'legacy'],
81
+ noConnectionWarningTimeoutMs: 3000,
82
+ };
83
+ try {
84
+ const sessionId = await initializeMobileWalletAdapterSession(
85
+ 'Wallet Name',
86
+ config,
87
+ );
88
+ console.log('sessionId: ' + sessionId);
89
+ } catch (e: any) {
90
+ if (e instanceof SolanaMWAWalletLibError) {
91
+ console.error(e.name, e.code, e.message);
92
+ } else {
93
+ console.error(e);
94
+ }
95
+ }
96
+ }
97
+ const listener = initializeMWAEventListener(
98
+ (request: MWARequest) => { /* ... */ },
99
+ (sessionEvent: MWASessionEvent) => { /* ... */ },
100
+ );
101
+ initializeMWASession();
102
+
103
+ // When the component is unmounted, clean up the listener.
104
+ return () => listener.remove();
105
+ }, []);
50
106
  ```
51
107
 
52
- ### 2. Handling requests
108
+ ### 3. Handling requests and events
53
109
 
54
110
  A `MWARequest` is handled by calling `resolve(request, response)` and each request have their appropriate response types.
55
111
 
@@ -57,7 +113,7 @@ An example of handling an `AuthorizationRequest`:
57
113
  ```typescript
58
114
  import {
59
115
  AuthorizeDappResponse
60
- } from '@solana-mobile/mobile-wallet-adapter-protocol-walletlib';
116
+ } from '@solana-mobile/mobile-wallet-adapter-walletlib';
61
117
 
62
118
  const response = {
63
119
  publicKey: Keypair.generate().publicKey.toBytes(),
@@ -71,7 +127,7 @@ There are a a selection of "fail" responses that you can return to the dApp. The
71
127
  ```typescript
72
128
  import {
73
129
  UserDeclinedResponse
74
- } from '@solana-mobile/mobile-wallet-adapter-protocol-walletlib';
130
+ } from '@solana-mobile/mobile-wallet-adapter-walletlib';
75
131
 
76
132
  const response = {
77
133
  failReason: MWARequestFailReason.UserDeclined,
@@ -137,23 +193,4 @@ Fields:
137
193
 
138
194
  - `SignAndSendTransactionsRequest`
139
195
  - [Spec](https://solana-mobile.github.io/mobile-wallet-adapter/spec/spec.html#sign_and_send_transactions)
140
- - Interfaces: `IMWARequest`, `IVerifiableIdentityRequest`
141
-
142
-
143
- # Changelog
144
-
145
- ## 1.0.3
146
- - Fixed a rerender bug within `useMobileWalletAdapterSession` where `initializeScenario` was needlessly called on rerender.
147
-
148
- - Added `DeauthorizeDappRequest` type, so Javascript side now knows when a dApp requests for deauthorization.
149
-
150
- - Added `ReauthorizeDappRequest` type, so Javascript side now knows when a dApp requests for reauthorization.
151
-
152
- - Refactored `IMWARequest` to only include fields `requestId`, `sessionId`, and `__type`.
153
-
154
- - Added `IVerifableIdentityRequest` to take on the fields `authorizationScope`, `cluster`, and `appIdentity`.
155
-
156
- - `AuthorizeDappRequest` now no longer includes `authorizationScope`. This was mistakenly included previously.
157
-
158
- - Updated documentation in the README. See "Properties of an MWA Request".
159
-
196
+ - Interfaces: `IMWARequest`, `IVerifiableIdentityRequest`