@dynamic-labs/global-wallet 3.0.0-alpha.46 → 3.0.0-alpha.47

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/index.js CHANGED
@@ -33,37 +33,73 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
33
33
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
34
34
  };
35
35
 
36
+ const buildCombinedNamespaces = (params) => {
37
+ var _a, _b;
38
+ const requiredEip155 = ((_a = params.requiredNamespaces) === null || _a === void 0 ? void 0 : _a.eip155) || {};
39
+ const optionalEip155 = ((_b = params.optionalNamespaces) === null || _b === void 0 ? void 0 : _b.eip155) || {};
40
+ const chains = new Set([
41
+ ...(requiredEip155.chains || []),
42
+ ...(optionalEip155.chains || []),
43
+ ]);
44
+ const events = new Set([
45
+ ...(requiredEip155.events || []),
46
+ ...(optionalEip155.events || []),
47
+ ]);
48
+ const methods = new Set([
49
+ ...(requiredEip155.methods || []),
50
+ ...(optionalEip155.methods || []),
51
+ ]);
52
+ return {
53
+ chains: Array.from(chains),
54
+ events: Array.from(events),
55
+ methods: Array.from(methods),
56
+ };
57
+ };
36
58
  const GlobalWalletExtension = {
37
59
  extend: (connector) => __awaiter(void 0, void 0, void 0, function* () {
38
60
  let web3wallet;
39
61
  let WCListenersOn = false;
40
62
  const signer = (yield connector.getSigner());
63
+ let pendingSessionProposal;
41
64
  const globalWallet = {
65
+ confirmPairing: (confirm) => __awaiter(void 0, void 0, void 0, function* () {
66
+ const { params, id } = pendingSessionProposal;
67
+ if (!confirm) {
68
+ yield web3wallet.rejectSession({
69
+ id,
70
+ reason: getSdkError('USER_REJECTED'),
71
+ });
72
+ return;
73
+ }
74
+ const { chains, events, methods } = buildCombinedNamespaces(params);
75
+ const address = yield connector.getAddress();
76
+ const approvedNamespaces = buildApprovedNamespaces({
77
+ proposal: params,
78
+ supportedNamespaces: {
79
+ eip155: {
80
+ accounts: chains.map((chain) => `${chain}:${address}`),
81
+ chains,
82
+ events,
83
+ methods,
84
+ },
85
+ },
86
+ });
87
+ yield web3wallet.approveSession({
88
+ id,
89
+ namespaces: approvedNamespaces,
90
+ });
91
+ }),
42
92
  disconnectWCSession: (topic) => __awaiter(void 0, void 0, void 0, function* () {
43
93
  if (web3wallet) {
44
- yield web3wallet.disconnect({
94
+ yield web3wallet.disconnectSession({
45
95
  reason: getSdkError('USER_DISCONNECTED'),
46
96
  topic,
47
97
  });
48
98
  }
49
99
  }),
50
- getConnectedWCSessions: () => __awaiter(void 0, void 0, void 0, function* () {
51
- try {
52
- const request = indexedDB.open('WALLET_CONNECT_V2_INDEXED_DB');
53
- return new Promise((resolve) => {
54
- request.onsuccess = (event) => {
55
- const db = event.target.result;
56
- const transaction = db.transaction('keyvaluestorage', 'readonly');
57
- const objectStore = transaction.objectStore('keyvaluestorage');
58
- const getRequest = objectStore.get('wc@2:core:0.3:pairing');
59
- getRequest.onsuccess = () => resolve(getRequest.result);
60
- };
61
- });
62
- }
63
- catch (e) {
64
- return undefined;
65
- }
66
- }),
100
+ // topic => session mapping, with peer metadata in the session
101
+ getConnectedWCSessions: () => __awaiter(void 0, void 0, void 0, function* () { return web3wallet.getActiveSessions(); }),
102
+ getPendingPairing: () => pendingSessionProposal,
67
103
  initWeb3Wallet: () => __awaiter(void 0, void 0, void 0, function* () {
68
104
  if (!web3wallet) {
69
105
  const core = new Core({
@@ -84,34 +120,16 @@ const GlobalWalletExtension = {
84
120
  if (WCListenersOn)
85
121
  return;
86
122
  const onSessionProposal = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, id }) {
87
- const { chains, events, methods } = params.optionalNamespaces.eip155;
88
- const address = yield connector.getAddress();
89
- const approvedNamespaces = buildApprovedNamespaces({
90
- proposal: params,
91
- supportedNamespaces: {
92
- eip155: {
93
- accounts: chains.map((chain) => `${chain}:${address}`),
94
- chains,
95
- events,
96
- methods,
97
- },
98
- },
99
- });
100
- try {
101
- yield web3wallet.approveSession({
102
- id,
103
- namespaces: approvedNamespaces,
104
- });
105
- }
106
- catch (error) {
107
- yield web3wallet.rejectSession({
108
- id,
109
- reason: getSdkError('USER_REJECTED'),
110
- });
111
- }
123
+ pendingSessionProposal = { id, params };
112
124
  });
113
125
  const sessionMessageListener = (_b) => __awaiter(void 0, [_b], void 0, function* ({ params, id, topic }) {
114
- const { request } = params;
126
+ const { request, chainId } = params;
127
+ // since we cant listen to chain events from the dapp, just change when a request comes in
128
+ if (chainId) {
129
+ yield connector.switchNetwork({
130
+ networkChainId: chainId.split(':')[1],
131
+ });
132
+ }
115
133
  const [requestParamsMessage, typedDataToSign] = request.params;
116
134
  const handleRequest = (handler) => __awaiter(void 0, void 0, void 0, function* () {
117
135
  try {
@@ -146,14 +164,31 @@ const GlobalWalletExtension = {
146
164
  : BigInt(0) }));
147
165
  }));
148
166
  }
149
- yield web3wallet.respondSessionRequest({
150
- response,
151
- topic,
152
- });
167
+ if (response) {
168
+ yield web3wallet.respondSessionRequest({
169
+ response,
170
+ topic,
171
+ });
172
+ }
153
173
  });
154
174
  yield globalWallet.initWeb3Wallet();
155
175
  web3wallet.on('session_proposal', onSessionProposal);
156
176
  web3wallet.on('session_request', sessionMessageListener);
177
+ connector.on('chainChange', (_c) => __awaiter(void 0, [_c], void 0, function* ({ chain }) {
178
+ const connections = yield web3wallet.getActiveSessions();
179
+ const connectionTopics = Object.keys(connections !== null && connections !== void 0 ? connections : {});
180
+ // tell all connected dapps that the chain has changed
181
+ yield Promise.all(connectionTopics.map((topic) => __awaiter(void 0, void 0, void 0, function* () {
182
+ yield web3wallet.emitSessionEvent({
183
+ chainId: `eip155:${chain}`,
184
+ event: {
185
+ data: chain,
186
+ name: 'chainChanged',
187
+ },
188
+ topic,
189
+ });
190
+ })));
191
+ }));
157
192
  WCListenersOn = true;
158
193
  }),
159
194
  pairWithWC: (uri) => __awaiter(void 0, void 0, void 0, function* () {
@@ -166,4 +201,4 @@ const GlobalWalletExtension = {
166
201
  name: 'global-wallet-extension',
167
202
  };
168
203
 
169
- export { GlobalWalletExtension };
204
+ export { GlobalWalletExtension, buildCombinedNamespaces };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/global-wallet",
3
- "version": "3.0.0-alpha.46",
3
+ "version": "3.0.0-alpha.47",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/dynamic-labs/dynamic-auth.git",
@@ -32,6 +32,6 @@
32
32
  },
33
33
  "peerDependencies": {
34
34
  "viem": "2.7.6",
35
- "@dynamic-labs/wallet-connector-core": "3.0.0-alpha.46"
35
+ "@dynamic-labs/wallet-connector-core": "3.0.0-alpha.47"
36
36
  }
37
37
  }
@@ -7,10 +7,17 @@ declare module '@dynamic-labs/wallet-connector-core' {
7
7
  interface IGlobalWalletExtension {
8
8
  initializeListeners: () => Promise<void>;
9
9
  pairWithWC: (uri: string) => Promise<void>;
10
- getConnectedWCSessions: () => Promise<unknown>;
10
+ getConnectedWCSessions: () => Promise<Record<string, object> | undefined>;
11
11
  disconnectWCSession: (topic: string) => Promise<void>;
12
12
  initWeb3Wallet: () => Promise<void>;
13
+ confirmPairing: (confirm: boolean) => Promise<void>;
14
+ getPendingPairing: () => object;
13
15
  }
14
16
  }
15
17
  }
18
+ export declare const buildCombinedNamespaces: (params: any) => {
19
+ chains: any[];
20
+ events: any[];
21
+ methods: any[];
22
+ };
16
23
  export declare const GlobalWalletExtension: WalletConnectorExtension;