cruzo-web3 0.1.1 → 0.1.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.
package/README.md CHANGED
@@ -83,12 +83,14 @@ Without a Project ID, the **Mobile wallet (WalletConnect)** option stays disable
83
83
  TON wallets (extension and mobile app) require a public `tonconnect-manifest.json`.
84
84
 
85
85
  1. Add the manifest file to your site root (must be reachable over HTTPS in production).
86
- 2. Point `web3Service` to it:
86
+ 2. Point `web3Service` to the absolute manifest URL at app bootstrap:
87
87
 
88
88
  ```ts
89
- web3Service.setTonManifestUrl(
90
- new URL("/tonconnect-manifest.json", window.location.href).href
91
- );
89
+ import { web3Service } from "cruzo-web3";
90
+
91
+ const tonManifestUrl = new URL("/tonconnect-manifest.json", window.location.href).href;
92
+
93
+ web3Service.setTonManifestUrl(tonManifestUrl);
92
94
  ```
93
95
 
94
96
  Example manifest:
@@ -85,7 +85,6 @@ export class Web3SigningComponent extends AbstractComponent {
85
85
  connectedCallback() {
86
86
  componentsRegistryService.connectBucket(this.innerBucket);
87
87
  super.connectedCallback();
88
- this.ensureTonManifest();
89
88
  this.updateWalletHint();
90
89
  this.setupUrlSync();
91
90
  }
@@ -218,12 +217,6 @@ export class Web3SigningComponent extends AbstractComponent {
218
217
  return { pubKey: null, signed: false, wallet: null };
219
218
  }
220
219
 
221
- private ensureTonManifest() {
222
- if (web3Service.getTonManifestUrl()) return;
223
-
224
- web3Service.setTonManifestUrl(new URL("/tonconnect-manifest.json", window.location.href).href);
225
- }
226
-
227
220
  private updateWalletHint() {
228
221
  const extensions = detectInjectedWallets();
229
222
  const hints = ["Click Connect wallet to choose a provider (extension or mobile app)."];
@@ -80,6 +80,7 @@ export class TonConnectProvider implements Web3Provider {
80
80
  }
81
81
 
82
82
  async signMessage(message: string | Uint8Array) {
83
+ await this.ui.connectionRestored;
83
84
  const account = this.requireAccount();
84
85
  const text = new TextDecoder().decode(toMessageBytes(message));
85
86
 
@@ -118,6 +118,7 @@ export class Web3Service extends AbstractService {
118
118
  readonly setup$ = this.newRx(0);
119
119
 
120
120
  private provider: Web3Provider | null = null;
121
+ private activeProviderKey: string | null = null;
121
122
  private tonManifestUrl: string | null = null;
122
123
  private walletConnectProjectId: string | null = null;
123
124
  private builtinProviders: Web3WalletSlot[] | null = null;
@@ -170,11 +171,20 @@ export class Web3Service extends AbstractService {
170
171
  return getWalletModeLabel(wallet.kind, wallet.transport);
171
172
  }
172
173
 
173
- useProvider(provider: Web3Provider) {
174
+ useProvider(provider: Web3Provider, key: string | null = null) {
174
175
  this.provider = provider;
176
+ this.activeProviderKey = key;
175
177
  return this;
176
178
  }
177
179
 
180
+ private walletProviderKey(kind: WalletKind, transport: WalletTransport) {
181
+ return `${kind}:${transport}`;
182
+ }
183
+
184
+ private customProviderKey(providerId: string) {
185
+ return `custom:${providerId}`;
186
+ }
187
+
178
188
  getProvider() {
179
189
  return this.provider;
180
190
  }
@@ -218,6 +228,12 @@ export class Web3Service extends AbstractService {
218
228
  kind: InjectedWalletKind = "ethereum",
219
229
  options: InjectedProviderOptions = {},
220
230
  ) {
231
+ const key = this.walletProviderKey(kind, "extension");
232
+
233
+ if (this.provider && this.activeProviderKey === key) {
234
+ return this;
235
+ }
236
+
221
237
  const provider = createInjectedProvider(
222
238
  kind,
223
239
  (pubKey) => {
@@ -228,7 +244,7 @@ export class Web3Service extends AbstractService {
228
244
  },
229
245
  );
230
246
 
231
- return this.useProvider(provider);
247
+ return this.useProvider(provider, key);
232
248
  }
233
249
 
234
250
  async useWalletProvider(
@@ -236,6 +252,12 @@ export class Web3Service extends AbstractService {
236
252
  transport: WalletTransport = "auto",
237
253
  options: WalletProviderOptions = {},
238
254
  ) {
255
+ const key = this.walletProviderKey(kind, transport);
256
+
257
+ if (this.provider && this.activeProviderKey === key) {
258
+ return this;
259
+ }
260
+
239
261
  const provider = await createWalletProvider(
240
262
  kind,
241
263
  transport,
@@ -245,7 +267,7 @@ export class Web3Service extends AbstractService {
245
267
  this.walletOptions(options),
246
268
  );
247
269
 
248
- return this.useProvider(provider);
270
+ return this.useProvider(provider, key);
249
271
  }
250
272
 
251
273
  ensureInjectedProvider(
@@ -286,6 +308,8 @@ export class Web3Service extends AbstractService {
286
308
  }
287
309
 
288
310
  await this.provider.disconnect();
311
+ this.provider = null;
312
+ this.activeProviderKey = null;
289
313
  this.userPubKey$.update(null);
290
314
  }
291
315
 
@@ -314,8 +338,14 @@ export class Web3Service extends AbstractService {
314
338
  }
315
339
 
316
340
  async useCustomProvider(providerId: string) {
341
+ const key = this.customProviderKey(providerId);
342
+
343
+ if (this.provider && this.activeProviderKey === key) {
344
+ return this;
345
+ }
346
+
317
347
  const provider = await this.resolveCustomProvider(providerId);
318
- return this.useProvider(provider);
348
+ return this.useProvider(provider, key);
319
349
  }
320
350
 
321
351
  async connectCustom(providerId: string) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cruzo-web3",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Web3 addon for cruzo: wallet providers, sign, verify",
5
5
  "license": "MIT",
6
6
  "type": "module",