@txnlab/use-wallet-solid 4.6.0 → 5.0.0-rc.1

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/dist/index.jsx CHANGED
@@ -1,717 +1,172 @@
1
- // src/index.tsx
2
1
  import { useStore } from "@tanstack/solid-store";
3
2
  import algosdk from "algosdk";
4
-
5
- // ../../node_modules/.pnpm/solid-js@1.9.11/node_modules/solid-js/dist/solid.js
6
- var sharedConfig = {
7
- context: void 0,
8
- registry: void 0,
9
- effects: void 0,
10
- done: false,
11
- getContextId() {
12
- return getContextId(this.context.count);
13
- },
14
- getNextContextId() {
15
- return getContextId(this.context.count++);
16
- }
17
- };
18
- function getContextId(count) {
19
- const num = String(count), len = num.length - 1;
20
- return sharedConfig.context.id + (len ? String.fromCharCode(96 + len) : "") + num;
21
- }
22
- function setHydrateContext(context) {
23
- sharedConfig.context = context;
24
- }
25
- var IS_DEV = false;
26
- var equalFn = (a, b) => a === b;
27
- var signalOptions = {
28
- equals: equalFn
29
- };
30
- var ERROR = null;
31
- var runEffects = runQueue;
32
- var STALE = 1;
33
- var PENDING = 2;
34
- var UNOWNED = {
35
- owned: null,
36
- cleanups: null,
37
- context: null,
38
- owner: null
39
- };
40
- var Owner = null;
41
- var Transition = null;
42
- var Scheduler = null;
43
- var ExternalSourceConfig = null;
44
- var Listener = null;
45
- var Updates = null;
46
- var Effects = null;
47
- var ExecCount = 0;
48
- function createSignal(value, options) {
49
- options = options ? Object.assign({}, signalOptions, options) : signalOptions;
50
- const s = {
51
- value,
52
- observers: null,
53
- observerSlots: null,
54
- comparator: options.equals || void 0
55
- };
56
- const setter = (value2) => {
57
- if (typeof value2 === "function") {
58
- if (Transition && Transition.running && Transition.sources.has(s)) value2 = value2(s.tValue);
59
- else value2 = value2(s.value);
60
- }
61
- return writeSignal(s, value2);
62
- };
63
- return [readSignal.bind(s), setter];
64
- }
65
- function createRenderEffect(fn, value, options) {
66
- const c = createComputation(fn, value, false, STALE);
67
- if (Scheduler && Transition && Transition.running) Updates.push(c);
68
- else updateComputation(c);
69
- }
70
- function createEffect(fn, value, options) {
71
- runEffects = runUserEffects;
72
- const c = createComputation(fn, value, false, STALE), s = SuspenseContext && useContext(SuspenseContext);
73
- if (s) c.suspense = s;
74
- if (!options || !options.render) c.user = true;
75
- Effects ? Effects.push(c) : updateComputation(c);
76
- }
77
- function createMemo(fn, value, options) {
78
- options = options ? Object.assign({}, signalOptions, options) : signalOptions;
79
- const c = createComputation(fn, value, true, 0);
80
- c.observers = null;
81
- c.observerSlots = null;
82
- c.comparator = options.equals || void 0;
83
- if (Scheduler && Transition && Transition.running) {
84
- c.tState = STALE;
85
- Updates.push(c);
86
- } else updateComputation(c);
87
- return readSignal.bind(c);
88
- }
89
- function untrack(fn) {
90
- if (!ExternalSourceConfig && Listener === null) return fn();
91
- const listener = Listener;
92
- Listener = null;
93
- try {
94
- if (ExternalSourceConfig) return ExternalSourceConfig.untrack(fn);
95
- return fn();
96
- } finally {
97
- Listener = listener;
98
- }
99
- }
100
- function onMount(fn) {
101
- createEffect(() => untrack(fn));
102
- }
103
- function onCleanup(fn) {
104
- if (Owner === null) ;
105
- else if (Owner.cleanups === null) Owner.cleanups = [fn];
106
- else Owner.cleanups.push(fn);
107
- return fn;
108
- }
109
- function startTransition(fn) {
110
- if (Transition && Transition.running) {
111
- fn();
112
- return Transition.done;
113
- }
114
- const l = Listener;
115
- const o = Owner;
116
- return Promise.resolve().then(() => {
117
- Listener = l;
118
- Owner = o;
119
- let t;
120
- if (Scheduler || SuspenseContext) {
121
- t = Transition || (Transition = {
122
- sources: /* @__PURE__ */ new Set(),
123
- effects: [],
124
- promises: /* @__PURE__ */ new Set(),
125
- disposed: /* @__PURE__ */ new Set(),
126
- queue: /* @__PURE__ */ new Set(),
127
- running: true
128
- });
129
- t.done || (t.done = new Promise((res) => t.resolve = res));
130
- t.running = true;
131
- }
132
- runUpdates(fn, false);
133
- Listener = Owner = null;
134
- return t ? t.done : void 0;
135
- });
136
- }
137
- var [transPending, setTransPending] = /* @__PURE__ */ createSignal(false);
138
- function createContext(defaultValue, options) {
139
- const id = /* @__PURE__ */ Symbol("context");
140
- return {
141
- id,
142
- Provider: createProvider(id),
143
- defaultValue
144
- };
145
- }
146
- function useContext(context) {
147
- let value;
148
- return Owner && Owner.context && (value = Owner.context[context.id]) !== void 0 ? value : context.defaultValue;
149
- }
150
- function children(fn) {
151
- const children2 = createMemo(fn);
152
- const memo = createMemo(() => resolveChildren(children2()));
153
- memo.toArray = () => {
154
- const c = memo();
155
- return Array.isArray(c) ? c : c != null ? [c] : [];
156
- };
157
- return memo;
158
- }
159
- var SuspenseContext;
160
- function readSignal() {
161
- const runningTransition = Transition && Transition.running;
162
- if (this.sources && (runningTransition ? this.tState : this.state)) {
163
- if ((runningTransition ? this.tState : this.state) === STALE) updateComputation(this);
164
- else {
165
- const updates = Updates;
166
- Updates = null;
167
- runUpdates(() => lookUpstream(this), false);
168
- Updates = updates;
169
- }
170
- }
171
- if (Listener) {
172
- const sSlot = this.observers ? this.observers.length : 0;
173
- if (!Listener.sources) {
174
- Listener.sources = [this];
175
- Listener.sourceSlots = [sSlot];
176
- } else {
177
- Listener.sources.push(this);
178
- Listener.sourceSlots.push(sSlot);
179
- }
180
- if (!this.observers) {
181
- this.observers = [Listener];
182
- this.observerSlots = [Listener.sources.length - 1];
183
- } else {
184
- this.observers.push(Listener);
185
- this.observerSlots.push(Listener.sources.length - 1);
186
- }
187
- }
188
- if (runningTransition && Transition.sources.has(this)) return this.tValue;
189
- return this.value;
190
- }
191
- function writeSignal(node, value, isComp) {
192
- let current = Transition && Transition.running && Transition.sources.has(node) ? node.tValue : node.value;
193
- if (!node.comparator || !node.comparator(current, value)) {
194
- if (Transition) {
195
- const TransitionRunning = Transition.running;
196
- if (TransitionRunning || !isComp && Transition.sources.has(node)) {
197
- Transition.sources.add(node);
198
- node.tValue = value;
199
- }
200
- if (!TransitionRunning) node.value = value;
201
- } else node.value = value;
202
- if (node.observers && node.observers.length) {
203
- runUpdates(() => {
204
- for (let i = 0; i < node.observers.length; i += 1) {
205
- const o = node.observers[i];
206
- const TransitionRunning = Transition && Transition.running;
207
- if (TransitionRunning && Transition.disposed.has(o)) continue;
208
- if (TransitionRunning ? !o.tState : !o.state) {
209
- if (o.pure) Updates.push(o);
210
- else Effects.push(o);
211
- if (o.observers) markDownstream(o);
212
- }
213
- if (!TransitionRunning) o.state = STALE;
214
- else o.tState = STALE;
215
- }
216
- if (Updates.length > 1e6) {
217
- Updates = [];
218
- if (IS_DEV) ;
219
- throw new Error();
220
- }
221
- }, false);
222
- }
223
- }
224
- return value;
225
- }
226
- function updateComputation(node) {
227
- if (!node.fn) return;
228
- cleanNode(node);
229
- const time = ExecCount;
230
- runComputation(node, Transition && Transition.running && Transition.sources.has(node) ? node.tValue : node.value, time);
231
- if (Transition && !Transition.running && Transition.sources.has(node)) {
232
- queueMicrotask(() => {
233
- runUpdates(() => {
234
- Transition && (Transition.running = true);
235
- Listener = Owner = node;
236
- runComputation(node, node.tValue, time);
237
- Listener = Owner = null;
238
- }, false);
239
- });
240
- }
241
- }
242
- function runComputation(node, value, time) {
243
- let nextValue;
244
- const owner = Owner, listener = Listener;
245
- Listener = Owner = node;
246
- try {
247
- nextValue = node.fn(value);
248
- } catch (err) {
249
- if (node.pure) {
250
- if (Transition && Transition.running) {
251
- node.tState = STALE;
252
- node.tOwned && node.tOwned.forEach(cleanNode);
253
- node.tOwned = void 0;
254
- } else {
255
- node.state = STALE;
256
- node.owned && node.owned.forEach(cleanNode);
257
- node.owned = null;
258
- }
259
- }
260
- node.updatedAt = time + 1;
261
- return handleError(err);
262
- } finally {
263
- Listener = listener;
264
- Owner = owner;
265
- }
266
- if (!node.updatedAt || node.updatedAt <= time) {
267
- if (node.updatedAt != null && "observers" in node) {
268
- writeSignal(node, nextValue, true);
269
- } else if (Transition && Transition.running && node.pure) {
270
- Transition.sources.add(node);
271
- node.tValue = nextValue;
272
- } else node.value = nextValue;
273
- node.updatedAt = time;
274
- }
275
- }
276
- function createComputation(fn, init, pure, state = STALE, options) {
277
- const c = {
278
- fn,
279
- state,
280
- updatedAt: null,
281
- owned: null,
282
- sources: null,
283
- sourceSlots: null,
284
- cleanups: null,
285
- value: init,
286
- owner: Owner,
287
- context: Owner ? Owner.context : null,
288
- pure
289
- };
290
- if (Transition && Transition.running) {
291
- c.state = 0;
292
- c.tState = state;
293
- }
294
- if (Owner === null) ;
295
- else if (Owner !== UNOWNED) {
296
- if (Transition && Transition.running && Owner.pure) {
297
- if (!Owner.tOwned) Owner.tOwned = [c];
298
- else Owner.tOwned.push(c);
299
- } else {
300
- if (!Owner.owned) Owner.owned = [c];
301
- else Owner.owned.push(c);
302
- }
303
- }
304
- if (ExternalSourceConfig && c.fn) {
305
- const [track, trigger] = createSignal(void 0, {
306
- equals: false
307
- });
308
- const ordinary = ExternalSourceConfig.factory(c.fn, trigger);
309
- onCleanup(() => ordinary.dispose());
310
- const triggerInTransition = () => startTransition(trigger).then(() => inTransition.dispose());
311
- const inTransition = ExternalSourceConfig.factory(c.fn, triggerInTransition);
312
- c.fn = (x) => {
313
- track();
314
- return Transition && Transition.running ? inTransition.track(x) : ordinary.track(x);
315
- };
316
- }
317
- return c;
318
- }
319
- function runTop(node) {
320
- const runningTransition = Transition && Transition.running;
321
- if ((runningTransition ? node.tState : node.state) === 0) return;
322
- if ((runningTransition ? node.tState : node.state) === PENDING) return lookUpstream(node);
323
- if (node.suspense && untrack(node.suspense.inFallback)) return node.suspense.effects.push(node);
324
- const ancestors = [node];
325
- while ((node = node.owner) && (!node.updatedAt || node.updatedAt < ExecCount)) {
326
- if (runningTransition && Transition.disposed.has(node)) return;
327
- if (runningTransition ? node.tState : node.state) ancestors.push(node);
328
- }
329
- for (let i = ancestors.length - 1; i >= 0; i--) {
330
- node = ancestors[i];
331
- if (runningTransition) {
332
- let top = node, prev = ancestors[i + 1];
333
- while ((top = top.owner) && top !== prev) {
334
- if (Transition.disposed.has(top)) return;
335
- }
336
- }
337
- if ((runningTransition ? node.tState : node.state) === STALE) {
338
- updateComputation(node);
339
- } else if ((runningTransition ? node.tState : node.state) === PENDING) {
340
- const updates = Updates;
341
- Updates = null;
342
- runUpdates(() => lookUpstream(node, ancestors[0]), false);
343
- Updates = updates;
344
- }
345
- }
346
- }
347
- function runUpdates(fn, init) {
348
- if (Updates) return fn();
349
- let wait = false;
350
- if (!init) Updates = [];
351
- if (Effects) wait = true;
352
- else Effects = [];
353
- ExecCount++;
354
- try {
355
- const res = fn();
356
- completeUpdates(wait);
357
- return res;
358
- } catch (err) {
359
- if (!wait) Effects = null;
360
- Updates = null;
361
- handleError(err);
362
- }
363
- }
364
- function completeUpdates(wait) {
365
- if (Updates) {
366
- if (Scheduler && Transition && Transition.running) scheduleQueue(Updates);
367
- else runQueue(Updates);
368
- Updates = null;
369
- }
370
- if (wait) return;
371
- let res;
372
- if (Transition) {
373
- if (!Transition.promises.size && !Transition.queue.size) {
374
- const sources = Transition.sources;
375
- const disposed = Transition.disposed;
376
- Effects.push.apply(Effects, Transition.effects);
377
- res = Transition.resolve;
378
- for (const e2 of Effects) {
379
- "tState" in e2 && (e2.state = e2.tState);
380
- delete e2.tState;
381
- }
382
- Transition = null;
383
- runUpdates(() => {
384
- for (const d of disposed) cleanNode(d);
385
- for (const v of sources) {
386
- v.value = v.tValue;
387
- if (v.owned) {
388
- for (let i = 0, len = v.owned.length; i < len; i++) cleanNode(v.owned[i]);
389
- }
390
- if (v.tOwned) v.owned = v.tOwned;
391
- delete v.tValue;
392
- delete v.tOwned;
393
- v.tState = 0;
394
- }
395
- setTransPending(false);
396
- }, false);
397
- } else if (Transition.running) {
398
- Transition.running = false;
399
- Transition.effects.push.apply(Transition.effects, Effects);
400
- Effects = null;
401
- setTransPending(true);
402
- return;
403
- }
404
- }
405
- const e = Effects;
406
- Effects = null;
407
- if (e.length) runUpdates(() => runEffects(e), false);
408
- if (res) res();
409
- }
410
- function runQueue(queue) {
411
- for (let i = 0; i < queue.length; i++) runTop(queue[i]);
412
- }
413
- function scheduleQueue(queue) {
414
- for (let i = 0; i < queue.length; i++) {
415
- const item = queue[i];
416
- const tasks = Transition.queue;
417
- if (!tasks.has(item)) {
418
- tasks.add(item);
419
- Scheduler(() => {
420
- tasks.delete(item);
421
- runUpdates(() => {
422
- Transition.running = true;
423
- runTop(item);
424
- }, false);
425
- Transition && (Transition.running = false);
426
- });
427
- }
428
- }
429
- }
430
- function runUserEffects(queue) {
431
- let i, userLength = 0;
432
- for (i = 0; i < queue.length; i++) {
433
- const e = queue[i];
434
- if (!e.user) runTop(e);
435
- else queue[userLength++] = e;
436
- }
437
- if (sharedConfig.context) {
438
- if (sharedConfig.count) {
439
- sharedConfig.effects || (sharedConfig.effects = []);
440
- sharedConfig.effects.push(...queue.slice(0, userLength));
441
- return;
442
- }
443
- setHydrateContext();
444
- }
445
- if (sharedConfig.effects && (sharedConfig.done || !sharedConfig.count)) {
446
- queue = [...sharedConfig.effects, ...queue];
447
- userLength += sharedConfig.effects.length;
448
- delete sharedConfig.effects;
449
- }
450
- for (i = 0; i < userLength; i++) runTop(queue[i]);
451
- }
452
- function lookUpstream(node, ignore) {
453
- const runningTransition = Transition && Transition.running;
454
- if (runningTransition) node.tState = 0;
455
- else node.state = 0;
456
- for (let i = 0; i < node.sources.length; i += 1) {
457
- const source = node.sources[i];
458
- if (source.sources) {
459
- const state = runningTransition ? source.tState : source.state;
460
- if (state === STALE) {
461
- if (source !== ignore && (!source.updatedAt || source.updatedAt < ExecCount)) runTop(source);
462
- } else if (state === PENDING) lookUpstream(source, ignore);
463
- }
464
- }
465
- }
466
- function markDownstream(node) {
467
- const runningTransition = Transition && Transition.running;
468
- for (let i = 0; i < node.observers.length; i += 1) {
469
- const o = node.observers[i];
470
- if (runningTransition ? !o.tState : !o.state) {
471
- if (runningTransition) o.tState = PENDING;
472
- else o.state = PENDING;
473
- if (o.pure) Updates.push(o);
474
- else Effects.push(o);
475
- o.observers && markDownstream(o);
476
- }
477
- }
478
- }
479
- function cleanNode(node) {
480
- let i;
481
- if (node.sources) {
482
- while (node.sources.length) {
483
- const source = node.sources.pop(), index = node.sourceSlots.pop(), obs = source.observers;
484
- if (obs && obs.length) {
485
- const n = obs.pop(), s = source.observerSlots.pop();
486
- if (index < obs.length) {
487
- n.sourceSlots[s] = index;
488
- obs[index] = n;
489
- source.observerSlots[index] = s;
490
- }
491
- }
492
- }
493
- }
494
- if (node.tOwned) {
495
- for (i = node.tOwned.length - 1; i >= 0; i--) cleanNode(node.tOwned[i]);
496
- delete node.tOwned;
497
- }
498
- if (Transition && Transition.running && node.pure) {
499
- reset(node, true);
500
- } else if (node.owned) {
501
- for (i = node.owned.length - 1; i >= 0; i--) cleanNode(node.owned[i]);
502
- node.owned = null;
503
- }
504
- if (node.cleanups) {
505
- for (i = node.cleanups.length - 1; i >= 0; i--) node.cleanups[i]();
506
- node.cleanups = null;
507
- }
508
- if (Transition && Transition.running) node.tState = 0;
509
- else node.state = 0;
510
- }
511
- function reset(node, top) {
512
- if (!top) {
513
- node.tState = 0;
514
- Transition.disposed.add(node);
515
- }
516
- if (node.owned) {
517
- for (let i = 0; i < node.owned.length; i++) reset(node.owned[i]);
518
- }
519
- }
520
- function castError(err) {
521
- if (err instanceof Error) return err;
522
- return new Error(typeof err === "string" ? err : "Unknown error", {
523
- cause: err
524
- });
525
- }
526
- function runErrors(err, fns, owner) {
527
- try {
528
- for (const f of fns) f(err);
529
- } catch (e) {
530
- handleError(e, owner && owner.owner || null);
531
- }
532
- }
533
- function handleError(err, owner = Owner) {
534
- const fns = ERROR && owner && owner.context && owner.context[ERROR];
535
- const error = castError(err);
536
- if (!fns) throw error;
537
- if (Effects) Effects.push({
538
- fn() {
539
- runErrors(error, fns, owner);
540
- },
541
- state: STALE
542
- });
543
- else runErrors(error, fns, owner);
544
- }
545
- function resolveChildren(children2) {
546
- if (typeof children2 === "function" && !children2.length) return resolveChildren(children2());
547
- if (Array.isArray(children2)) {
548
- const results = [];
549
- for (let i = 0; i < children2.length; i++) {
550
- const result = resolveChildren(children2[i]);
551
- Array.isArray(result) ? results.push.apply(results, result) : results.push(result);
552
- }
553
- return results;
554
- }
555
- return children2;
556
- }
557
- function createProvider(id, options) {
558
- return function provider(props) {
559
- let res;
560
- createRenderEffect(() => res = untrack(() => {
561
- Owner.context = {
562
- ...Owner.context,
563
- [id]: props.value
564
- };
565
- return children(() => props.children);
566
- }), void 0);
567
- return res;
568
- };
569
- }
570
-
571
- // src/index.tsx
3
+ import { createContext, createMemo, onMount, useContext } from "solid-js";
572
4
  export * from "@txnlab/use-wallet";
573
- var WalletContext = createContext();
574
- var WalletProvider = (props) => {
575
- const store = () => props.manager;
576
- onMount(async () => {
577
- try {
578
- await props.manager.resumeSessions();
579
- } catch (error) {
580
- }
581
- });
582
- return <WalletContext.Provider value={store}>{props.children}</WalletContext.Provider>;
583
- };
584
- var useWalletManager = () => {
585
- const manager = useContext(WalletContext);
586
- if (!manager) {
587
- throw new Error("useWalletManager must be used within a WalletProvider");
588
- }
589
- return manager();
5
+ //#region src/index.tsx
6
+ const WalletContext = createContext();
7
+ const WalletProvider = (props) => {
8
+ const store = () => props.manager;
9
+ onMount(async () => {
10
+ try {
11
+ await props.manager.resumeSessions();
12
+ } catch (error) {
13
+ console.error("Error resuming sessions:", error);
14
+ }
15
+ });
16
+ return <WalletContext.Provider value={store}>{props.children}</WalletContext.Provider>;
590
17
  };
591
- var useNetwork = () => {
592
- const manager = createMemo(() => useWalletManager());
593
- const activeNetwork = useStore(manager().store, (state) => state.activeNetwork);
594
- const activeNetworkConfig = () => {
595
- const store = useStore(manager().store);
596
- return store().networkConfig[activeNetwork()];
597
- };
598
- const setActiveNetwork = async (networkId) => {
599
- if (networkId === activeNetwork()) {
600
- return;
601
- }
602
- if (!manager().networkConfig[networkId]) {
603
- throw new Error(`Network "${networkId}" not found in network configuration`);
604
- }
605
- const { algod } = manager().networkConfig[networkId];
606
- const { token = "", baseServer, port = "", headers = {} } = algod;
607
- const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
608
- await manager().setActiveNetwork(networkId);
609
- manager().store.setState((state) => ({
610
- ...state,
611
- activeNetwork: networkId,
612
- algodClient: newClient
613
- }));
614
- };
615
- const updateAlgodConfig = (networkId, config) => {
616
- manager().updateAlgodConfig(networkId, config);
617
- if (networkId === activeNetwork()) {
618
- const { algod } = manager().networkConfig[networkId];
619
- const { token = "", baseServer, port = "", headers = {} } = algod;
620
- const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
621
- manager().store.setState((state) => ({
622
- ...state,
623
- algodClient: newClient
624
- }));
625
- }
626
- };
627
- const resetNetworkConfig = (networkId) => {
628
- manager().resetNetworkConfig(networkId);
629
- if (networkId === activeNetwork()) {
630
- const { algod } = manager().networkConfig[networkId];
631
- const { token = "", baseServer, port = "", headers = {} } = algod;
632
- const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
633
- manager().store.setState((state) => ({
634
- ...state,
635
- algodClient: newClient
636
- }));
637
- }
638
- };
639
- return {
640
- activeNetwork,
641
- networkConfig: () => manager().networkConfig,
642
- activeNetworkConfig,
643
- setActiveNetwork,
644
- updateAlgodConfig,
645
- resetNetworkConfig
646
- };
18
+ const useWalletManager = () => {
19
+ const manager = useContext(WalletContext);
20
+ if (!manager) throw new Error("useWalletManager must be used within a WalletProvider");
21
+ return manager();
647
22
  };
648
- var useWallet = () => {
649
- const manager = createMemo(() => useWalletManager());
650
- const managerStatus = useStore(manager().store, (state) => state.managerStatus);
651
- const isReady = createMemo(() => managerStatus() === "ready");
652
- const algodClient = useStore(manager().store, (state) => state.algodClient);
653
- const walletStore = useStore(manager().store, (state) => state.wallets);
654
- const walletState = (walletKey) => walletStore()[walletKey] || null;
655
- const activeWalletId = useStore(manager().store, (state) => state.activeWallet);
656
- const activeWallet = () => manager().getWallet(activeWalletId()) || null;
657
- const activeWalletState = () => walletState(activeWalletId());
658
- const activeWalletAccounts = () => activeWalletState()?.accounts ?? null;
659
- const activeWalletAddresses = () => activeWalletAccounts()?.map((account) => account.address) ?? null;
660
- const activeAccount = () => activeWalletState()?.activeAccount ?? null;
661
- const activeAddress = () => activeAccount()?.address ?? null;
662
- const isWalletActive = (walletKey) => walletKey === activeWalletId();
663
- const isWalletConnected = (walletKey) => !!walletState(walletKey)?.accounts.length || false;
664
- const signTransactions = (txnGroup, indexesToSign) => {
665
- const wallet = activeWallet();
666
- if (!wallet) {
667
- throw new Error("No active wallet");
668
- }
669
- return wallet.signTransactions(txnGroup, indexesToSign);
670
- };
671
- const transactionSigner = (txnGroup, indexesToSign) => {
672
- const wallet = activeWallet();
673
- if (!wallet) {
674
- throw new Error("No active wallet");
675
- }
676
- return wallet.transactionSigner(txnGroup, indexesToSign);
677
- };
678
- const signData = (data, metadata) => {
679
- const wallet = activeWallet();
680
- if (!wallet) {
681
- throw new Error("No active wallet");
682
- }
683
- return wallet.signData(data, metadata);
684
- };
685
- const withPrivateKey = (callback) => {
686
- const wallet = activeWallet();
687
- if (!wallet) {
688
- throw new Error("No active wallet");
689
- }
690
- return wallet.withPrivateKey(callback);
691
- };
692
- return {
693
- wallets: manager().wallets,
694
- isReady,
695
- algodClient,
696
- activeWallet,
697
- activeWalletAccounts,
698
- activeWalletAddresses,
699
- activeWalletState,
700
- activeAccount,
701
- activeAddress,
702
- activeWalletId,
703
- isWalletActive,
704
- isWalletConnected,
705
- signData,
706
- withPrivateKey,
707
- signTransactions,
708
- transactionSigner,
709
- walletStore
710
- };
23
+ const useNetwork = () => {
24
+ const manager = createMemo(() => useWalletManager());
25
+ const activeNetwork = useStore(manager().store, (state) => state.activeNetwork);
26
+ const activeNetworkConfig = () => {
27
+ return useStore(manager().store)().networkConfig[activeNetwork()];
28
+ };
29
+ const setActiveNetwork = async (networkId) => {
30
+ if (networkId === activeNetwork()) return;
31
+ if (!manager().networkConfig[networkId]) throw new Error(`Network "${networkId}" not found in network configuration`);
32
+ console.info(`[Solid] Creating new Algodv2 client...`);
33
+ const { algod } = manager().networkConfig[networkId];
34
+ const { token = "", baseServer, port = "", headers = {} } = algod;
35
+ const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
36
+ await manager().setActiveNetwork(networkId);
37
+ manager().store.setState((state) => ({
38
+ ...state,
39
+ activeNetwork: networkId,
40
+ algodClient: newClient
41
+ }));
42
+ console.info(`[Solid] Active network set to ${networkId}.`);
43
+ };
44
+ const updateAlgodConfig = (networkId, config) => {
45
+ manager().updateAlgodConfig(networkId, config);
46
+ if (networkId === activeNetwork()) {
47
+ console.info(`[Solid] Creating new Algodv2 client...`);
48
+ const { algod } = manager().networkConfig[networkId];
49
+ const { token = "", baseServer, port = "", headers = {} } = algod;
50
+ const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
51
+ manager().store.setState((state) => ({
52
+ ...state,
53
+ algodClient: newClient
54
+ }));
55
+ }
56
+ };
57
+ const resetNetworkConfig = (networkId) => {
58
+ manager().resetNetworkConfig(networkId);
59
+ if (networkId === activeNetwork()) {
60
+ console.info(`[Solid] Creating new Algodv2 client...`);
61
+ const { algod } = manager().networkConfig[networkId];
62
+ const { token = "", baseServer, port = "", headers = {} } = algod;
63
+ const newClient = new algosdk.Algodv2(token, baseServer, port, headers);
64
+ manager().store.setState((state) => ({
65
+ ...state,
66
+ algodClient: newClient
67
+ }));
68
+ }
69
+ };
70
+ return {
71
+ activeNetwork,
72
+ networkConfig: () => manager().networkConfig,
73
+ activeNetworkConfig,
74
+ setActiveNetwork,
75
+ updateAlgodConfig,
76
+ resetNetworkConfig
77
+ };
711
78
  };
712
- export {
713
- WalletProvider,
714
- useNetwork,
715
- useWallet,
716
- useWalletManager
79
+ const useWallet = () => {
80
+ const manager = createMemo(() => useWalletManager());
81
+ const managerStatus = useStore(manager().store, (state) => state.managerStatus);
82
+ const isReady = createMemo(() => managerStatus() === "ready");
83
+ const algodClient = useStore(manager().store, (state) => state.algodClient);
84
+ const walletStateMap = useStore(manager().store, (state) => state.wallets);
85
+ const activeWalletId = useStore(manager().store, (state) => state.activeWallet);
86
+ const transformToWallet = (wallet) => {
87
+ return {
88
+ id: wallet.id,
89
+ walletKey: wallet.walletKey,
90
+ metadata: wallet.metadata,
91
+ get accounts() {
92
+ return walletStateMap()[wallet.walletKey]?.accounts ?? [];
93
+ },
94
+ get activeAccount() {
95
+ return walletStateMap()[wallet.walletKey]?.activeAccount ?? null;
96
+ },
97
+ get isConnected() {
98
+ return !!walletStateMap()[wallet.walletKey];
99
+ },
100
+ get isActive() {
101
+ return wallet.walletKey === activeWalletId();
102
+ },
103
+ canSignData: wallet.canSignData ?? false,
104
+ canUsePrivateKey: wallet.canUsePrivateKey ?? false,
105
+ connect: (args) => wallet.connect(args),
106
+ disconnect: () => wallet.disconnect(),
107
+ setActive: () => wallet.setActive(),
108
+ setActiveAccount: (addr) => wallet.setActiveAccount(addr)
109
+ };
110
+ };
111
+ const wallets = [...manager().wallets].map(transformToWallet);
112
+ const activeNetwork = useStore(manager().store, (state) => state.activeNetwork);
113
+ const availableWallets = createMemo(() => {
114
+ activeNetwork();
115
+ return wallets.filter((w) => manager().availableWallets.some((aw) => aw.walletKey === w.walletKey));
116
+ });
117
+ const activeBaseWallet = createMemo(() => {
118
+ const id = activeWalletId();
119
+ return id ? manager().getWallet(id) || null : null;
120
+ });
121
+ const activeWallet = createMemo(() => {
122
+ const id = activeWalletId();
123
+ return id ? wallets.find((w) => w.walletKey === id) ?? null : null;
124
+ });
125
+ const activeWalletAccounts = createMemo(() => {
126
+ return walletStateMap()[activeWalletId()]?.accounts ?? null;
127
+ });
128
+ const activeWalletAddresses = createMemo(() => activeWalletAccounts()?.map((account) => account.address) ?? null);
129
+ const activeAccount = createMemo(() => {
130
+ return walletStateMap()[activeWalletId()]?.activeAccount ?? null;
131
+ });
132
+ const activeAddress = createMemo(() => activeAccount()?.address ?? null);
133
+ const signTransactions = (txnGroup, indexesToSign) => {
134
+ const wallet = activeBaseWallet();
135
+ if (!wallet) throw new Error("No active wallet");
136
+ return wallet.signTransactions(txnGroup, indexesToSign);
137
+ };
138
+ const transactionSigner = (txnGroup, indexesToSign) => {
139
+ const wallet = activeBaseWallet();
140
+ if (!wallet) throw new Error("No active wallet");
141
+ return wallet.transactionSigner(txnGroup, indexesToSign);
142
+ };
143
+ const signData = (data, metadata) => {
144
+ const wallet = activeBaseWallet();
145
+ if (!wallet) throw new Error("No active wallet");
146
+ return wallet.signData(data, metadata);
147
+ };
148
+ const withPrivateKey = (callback) => {
149
+ const wallet = activeBaseWallet();
150
+ if (!wallet) throw new Error("No active wallet");
151
+ return wallet.withPrivateKey(callback);
152
+ };
153
+ return {
154
+ wallets: () => wallets,
155
+ availableWallets,
156
+ isReady,
157
+ algodClient,
158
+ activeWallet,
159
+ activeWalletAccounts,
160
+ activeWalletAddresses,
161
+ activeAccount,
162
+ activeAddress,
163
+ signData,
164
+ withPrivateKey,
165
+ signTransactions,
166
+ transactionSigner
167
+ };
717
168
  };
169
+ //#endregion
170
+ export { WalletProvider, useNetwork, useWallet, useWalletManager };
171
+
172
+ //# sourceMappingURL=index.jsx.map