@xyo-network/react-chain-client 1.20.15 → 1.20.17
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/LICENSE +165 -0
- package/README.md +24 -0
- package/dist/browser/hooks/client/permissions/usePermissions.d.ts +1 -1
- package/dist/browser/hooks/client/story/GatewayFromWalletStatus.d.ts +7 -0
- package/dist/browser/hooks/client/story/GatewayFromWalletStatus.d.ts.map +1 -0
- package/dist/browser/hooks/client/story/mockWalletDecorator.d.ts +3 -0
- package/dist/browser/hooks/client/story/mockWalletDecorator.d.ts.map +1 -0
- package/dist/browser/hooks/client/useClientFromWallet.d.ts.map +1 -1
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts +14 -0
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts.map +1 -1
- package/dist/browser/index.mjs +61 -72
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +84 -24
- package/src/global.d.ts +0 -9
- package/src/hooks/client/helpers/findCaveat.ts +0 -20
- package/src/hooks/client/helpers/index.ts +0 -1
- package/src/hooks/client/index.ts +0 -4
- package/src/hooks/client/permissions/index.ts +0 -2
- package/src/hooks/client/permissions/usePermissions.ts +0 -14
- package/src/hooks/client/permissions/usePermissionsAccounts.ts +0 -38
- package/src/hooks/client/useClientFromWallet.ts +0 -98
- package/src/hooks/client/useGatewayFromWallet.ts +0 -30
- package/src/hooks/helpers/getXyoClient.ts +0 -46
- package/src/hooks/helpers/index.ts +0 -1
- package/src/hooks/index.ts +0 -2
- package/src/index.ts +0 -2
- package/src/types/GatewayFromWallet.ts +0 -8
- package/src/types/index.ts +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @xyo-network/react-chain-client
|
|
2
|
+
|
|
3
|
+
[![npm][npm-badge]][npm-link]
|
|
4
|
+
[![license][license-badge]][license-link]
|
|
5
|
+
|
|
6
|
+
> XYO Layer One React SDK - Client/Wallet Hooks
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
{{pm}} add {{name}}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
See the [LICENSE](LICENSE) file for license rights and limitations (LGPL-3.0-only).
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
[npm-badge]: https://img.shields.io/npm/v/@xyo-network/react-chain-client.svg
|
|
22
|
+
[npm-link]: https://www.npmjs.com/package/@xyo-network/react-chain-client
|
|
23
|
+
[license-badge]: https://img.shields.io/npm/l/@xyo-network/react-chain-client.svg
|
|
24
|
+
[license-link]: https://github.com/xylabs/sdk-js/blob/main/LICENSE
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface GatewayFromWalletStatusProps {
|
|
4
|
+
gatewayName?: GatewayName;
|
|
5
|
+
}
|
|
6
|
+
export declare const GatewayFromWalletStatus: React.FC<GatewayFromWalletStatusProps>;
|
|
7
|
+
//# sourceMappingURL=GatewayFromWalletStatus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GatewayFromWalletStatus.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/client/story/GatewayFromWalletStatus.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,MAAM,WAAW,4BAA4B;IAC3C,WAAW,CAAC,EAAE,WAAW,CAAA;CAC1B;AAaD,eAAO,MAAM,uBAAuB,EAAE,KAAK,CAAC,EAAE,CAAC,4BAA4B,CAkC1E,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockWalletDecorator.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/client/story/mockWalletDecorator.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAyGtD,eAAO,MAAM,cAAc,EAAE,SAa5B,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useClientFromWallet.d.ts","sourceRoot":"","sources":["../../../../src/hooks/client/useClientFromWallet.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"useClientFromWallet.d.ts","sourceRoot":"","sources":["../../../../src/hooks/client/useClientFromWallet.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAKrD,UAAU,WAAW;IACnB,MAAM,CAAC,EAAE,SAAS,GAAG,IAAI,CAAA;IACzB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IACnB,SAAS,EAAE,OAAO,CAAA;IAClB,QAAQ,EAAE,OAAO,CAAA;CAClB;AASD,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,WAqDtD,CAAA;AAED,oDAAoD;AACpD,eAAO,MAAM,SAAS,aAxDwB,MAAM,KAAG,WAwDX,CAAA"}
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import type { GatewayName } from '@xyo-network/xl1-sdk';
|
|
2
2
|
import type { GatewayFromWallet } from '../../types/index.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a gateway from the injected XL1 Wallet extension.
|
|
5
|
+
*
|
|
6
|
+
* The returned `gateway` uses a three-state contract:
|
|
7
|
+
* - `undefined` — wallet detection is still in progress (isLoading=true),
|
|
8
|
+
* or no gatewayName was provided. Consumers should treat this as "not yet known"
|
|
9
|
+
* and avoid committing to either a present or absent UI state.
|
|
10
|
+
* - `null` — wallet is definitively absent. Either globalThis.xyo was never set,
|
|
11
|
+
* or detection timed out. Safe to show fallback UI (e.g. "Get XL1 Wallet").
|
|
12
|
+
* A late-recovery listener may still transition this to a real gateway if
|
|
13
|
+
* the extension loads after the timeout.
|
|
14
|
+
* - `XyoGatewayRunner` — wallet is present and exposes this gateway for the
|
|
15
|
+
* requested network. Ready to use for transactions and queries.
|
|
16
|
+
*/
|
|
3
17
|
export declare const useGatewayFromWallet: (gatewayName?: GatewayName, timeout?: number) => GatewayFromWallet;
|
|
4
18
|
/** @deprecated - useGatewayFromWallet */
|
|
5
19
|
export declare const useGateway: (gatewayName?: GatewayName, timeout?: number) => GatewayFromWallet;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGatewayFromWallet.d.ts","sourceRoot":"","sources":["../../../../src/hooks/client/useGatewayFromWallet.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAG7D,eAAO,MAAM,oBAAoB,GAAI,cAAc,WAAW,EAAE,UAAU,MAAM,KAAG,iBAoBlF,CAAA;AAED,yCAAyC;AACzC,eAAO,MAAM,UAAU,iBAvB4B,WAAW,YAAY,MAAM,KAAG,iBAuBrC,CAAA"}
|
|
1
|
+
{"version":3,"file":"useGatewayFromWallet.d.ts","sourceRoot":"","sources":["../../../../src/hooks/client/useGatewayFromWallet.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAG7D;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,oBAAoB,GAAI,cAAc,WAAW,EAAE,UAAU,MAAM,KAAG,iBAoBlF,CAAA;AAED,yCAAyC;AACzC,eAAO,MAAM,UAAU,iBAvB4B,WAAW,YAAY,MAAM,KAAG,iBAuBrC,CAAA"}
|
package/dist/browser/index.mjs
CHANGED
|
@@ -15,12 +15,12 @@ var findCaveat = /* @__PURE__ */ __name(async (permissions, targetCapability, ta
|
|
|
15
15
|
}, "findCaveat");
|
|
16
16
|
|
|
17
17
|
// src/hooks/client/useClientFromWallet.ts
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
18
|
+
import { isUndefined as isUndefined2 } from "@xylabs/sdk-js";
|
|
19
|
+
import { useEffect, useState } from "react";
|
|
20
20
|
|
|
21
21
|
// src/hooks/helpers/getXyoClient.ts
|
|
22
22
|
import { isUndefined } from "@xylabs/sdk-js";
|
|
23
|
-
var CLIENT_LISTENER_TIMEOUT =
|
|
23
|
+
var CLIENT_LISTENER_TIMEOUT = 2e3;
|
|
24
24
|
var hasXyoClient = /* @__PURE__ */ __name(() => {
|
|
25
25
|
return "client" in globalThis.xyo;
|
|
26
26
|
}, "hasXyoClient");
|
|
@@ -53,82 +53,73 @@ async function getXyoClient(timeout = CLIENT_LISTENER_TIMEOUT) {
|
|
|
53
53
|
__name(getXyoClient, "getXyoClient");
|
|
54
54
|
|
|
55
55
|
// src/hooks/client/useClientFromWallet.ts
|
|
56
|
-
var
|
|
56
|
+
var initialState = {
|
|
57
57
|
client: void 0,
|
|
58
58
|
error: null,
|
|
59
59
|
isLoading: false,
|
|
60
60
|
timedout: false
|
|
61
61
|
};
|
|
62
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
63
|
-
var emitChange = /* @__PURE__ */ __name(() => {
|
|
64
|
-
for (const listener of listeners) listener();
|
|
65
|
-
}, "emitChange");
|
|
66
|
-
var updateState = /* @__PURE__ */ __name((newState) => {
|
|
67
|
-
currentState = {
|
|
68
|
-
...currentState,
|
|
69
|
-
...newState
|
|
70
|
-
};
|
|
71
|
-
emitChange();
|
|
72
|
-
}, "updateState");
|
|
73
|
-
var initializeClient = /* @__PURE__ */ __name(async (timeout) => {
|
|
74
|
-
if (currentState.isLoading || currentState.client) return;
|
|
75
|
-
updateState({
|
|
76
|
-
isLoading: true,
|
|
77
|
-
error: null
|
|
78
|
-
});
|
|
79
|
-
try {
|
|
80
|
-
const client = await getXyoClient(timeout);
|
|
81
|
-
if (client === null) {
|
|
82
|
-
updateState({
|
|
83
|
-
client: null,
|
|
84
|
-
timedout: true,
|
|
85
|
-
isLoading: false
|
|
86
|
-
});
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
updateState({
|
|
90
|
-
client,
|
|
91
|
-
isLoading: false,
|
|
92
|
-
error: null
|
|
93
|
-
});
|
|
94
|
-
} catch (error) {
|
|
95
|
-
console.error("Error initializing XyoClient", error);
|
|
96
|
-
updateState({
|
|
97
|
-
error,
|
|
98
|
-
isLoading: false
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}, "initializeClient");
|
|
102
|
-
var subscribe = /* @__PURE__ */ __name((listener, timeout) => {
|
|
103
|
-
listeners.add(listener);
|
|
104
|
-
void initializeClient(timeout);
|
|
105
|
-
return () => {
|
|
106
|
-
listeners.delete(listener);
|
|
107
|
-
};
|
|
108
|
-
}, "subscribe");
|
|
109
|
-
var getSnapshot = /* @__PURE__ */ __name(() => currentState, "getSnapshot");
|
|
110
62
|
var useClientFromWallet = /* @__PURE__ */ __name((timeout) => {
|
|
111
|
-
const
|
|
112
|
-
timeout
|
|
113
|
-
]);
|
|
114
|
-
const clientState = useSyncExternalStore(subscribeWithTimeout, getSnapshot);
|
|
63
|
+
const [state, setState] = useState(initialState);
|
|
115
64
|
useEffect(() => {
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
|
|
65
|
+
let cancelled = false;
|
|
66
|
+
const initialize = /* @__PURE__ */ __name(async () => {
|
|
67
|
+
setState((prev) => {
|
|
68
|
+
if (prev.isLoading) return prev;
|
|
69
|
+
return {
|
|
70
|
+
...prev,
|
|
71
|
+
isLoading: true,
|
|
72
|
+
error: null
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
try {
|
|
76
|
+
const client = await getXyoClient(timeout);
|
|
77
|
+
if (cancelled) return;
|
|
78
|
+
if (client === null) {
|
|
79
|
+
setState({
|
|
80
|
+
client: null,
|
|
81
|
+
timedout: true,
|
|
82
|
+
isLoading: false,
|
|
83
|
+
error: null
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
setState({
|
|
87
|
+
client,
|
|
88
|
+
isLoading: false,
|
|
89
|
+
error: null,
|
|
90
|
+
timedout: false
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (cancelled) return;
|
|
95
|
+
setState({
|
|
96
|
+
client: void 0,
|
|
97
|
+
isLoading: false,
|
|
98
|
+
error,
|
|
99
|
+
timedout: false
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}, "initialize");
|
|
103
|
+
void initialize();
|
|
104
|
+
const onPluginReady = /* @__PURE__ */ __name(() => {
|
|
105
|
+
if (cancelled) return;
|
|
106
|
+
const client = isUndefined2(globalThis.xyo) ? null : globalThis.xyo?.client ?? null;
|
|
107
|
+
setState({
|
|
108
|
+
client,
|
|
119
109
|
isLoading: false,
|
|
110
|
+
timedout: false,
|
|
120
111
|
error: null
|
|
121
112
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (clientState.timedout && isNull(clientState.client) && !clientState.isLoading) {
|
|
125
|
-
globalThis.addEventListener("xyo:plugin-ready", listener);
|
|
126
|
-
}
|
|
113
|
+
}, "onPluginReady");
|
|
114
|
+
globalThis.addEventListener("xyo:plugin-ready", onPluginReady);
|
|
127
115
|
return () => {
|
|
128
|
-
|
|
116
|
+
cancelled = true;
|
|
117
|
+
globalThis.removeEventListener("xyo:plugin-ready", onPluginReady);
|
|
129
118
|
};
|
|
130
|
-
}
|
|
131
|
-
|
|
119
|
+
}, [
|
|
120
|
+
timeout
|
|
121
|
+
]);
|
|
122
|
+
return state;
|
|
132
123
|
}, "useClientFromWallet");
|
|
133
124
|
var useClient = useClientFromWallet;
|
|
134
125
|
|
|
@@ -177,17 +168,15 @@ var useAccountPermissions = /* @__PURE__ */ __name(() => {
|
|
|
177
168
|
}, "useAccountPermissions");
|
|
178
169
|
|
|
179
170
|
// src/hooks/client/useGatewayFromWallet.ts
|
|
180
|
-
import {
|
|
171
|
+
import { isNull } from "@xylabs/sdk-js";
|
|
181
172
|
var useGatewayFromWallet = /* @__PURE__ */ __name((gatewayName, timeout) => {
|
|
182
173
|
const { client, isLoading, error, timedout } = useClientFromWallet(timeout);
|
|
183
174
|
const resolveGateway = /* @__PURE__ */ __name(() => {
|
|
184
|
-
if (
|
|
185
|
-
if (timedout && !isLoading && isDefined3(gatewayName)) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
175
|
+
if (isNull(client)) return null;
|
|
188
176
|
return client?.gateways?.[gatewayName];
|
|
189
177
|
}, "resolveGateway");
|
|
190
178
|
return {
|
|
179
|
+
// no gatewayName requested = undefined (opt-out, not "absent")
|
|
191
180
|
gateway: gatewayName ? resolveGateway() : void 0,
|
|
192
181
|
isLoading,
|
|
193
182
|
error,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/client/helpers/findCaveat.ts","../../src/hooks/client/useClientFromWallet.ts","../../src/hooks/helpers/getXyoClient.ts","../../src/hooks/client/permissions/usePermissions.ts","../../src/hooks/client/permissions/usePermissionsAccounts.ts","../../src/hooks/client/useGatewayFromWallet.ts"],"sourcesContent":["import { isDefined } from '@xylabs/sdk-js'\nimport type {\n CaveatTypes, ParentCapability, XyoPermissions,\n} from '@xyo-network/xl1-sdk'\n\nexport const findCaveat = async (\n permissions: XyoPermissions,\n targetCapability: ParentCapability,\n targetCaveatType: CaveatTypes,\n) => {\n const existingPermissions = await permissions.getPermissions()\n if (isDefined(existingPermissions) && existingPermissions.length > 0) {\n const foundPermissions = existingPermissions\n .find(p => p.parentCapability === targetCapability)\n if (isDefined(foundPermissions)) {\n return foundPermissions.caveats?.find(caveat => caveat.type === targetCaveatType)?.value ?? []\n }\n }\n return existingPermissions\n}\n","import { isNull } from '@xylabs/sdk-js'\nimport type { XyoClient } from '@xyo-network/xl1-sdk'\nimport {\n useCallback, useEffect, useSyncExternalStore,\n} from 'react'\n\nimport { getXyoClient } from '../helpers/index.ts'\n\ninterface ClientState {\n client?: XyoClient | null\n error: Error | null\n isLoading: boolean\n timedout: boolean\n}\n\nlet currentState: ClientState = {\n client: undefined,\n error: null,\n isLoading: false,\n timedout: false,\n}\n\nconst listeners = new Set<() => void>()\n\nconst emitChange = () => {\n for (const listener of listeners) listener()\n}\n\nconst updateState = (newState: Partial<ClientState>) => {\n currentState = { ...currentState, ...newState }\n emitChange()\n}\n\nconst initializeClient = async (timeout?: number) => {\n if (currentState.isLoading || currentState.client) return\n\n updateState({ isLoading: true, error: null })\n\n try {\n const client = await getXyoClient(timeout)\n if (client === null) {\n updateState({\n client: null, timedout: true, isLoading: false,\n })\n return\n }\n updateState({\n client, isLoading: false, error: null,\n })\n } catch (error) {\n console.error('Error initializing XyoClient', error)\n updateState({ error: error as Error, isLoading: false })\n }\n}\n\nconst subscribe = (listener: () => void, timeout?: number) => {\n listeners.add(listener)\n\n void initializeClient(timeout)\n\n return () => {\n listeners.delete(listener)\n }\n}\n\nconst getSnapshot = (): ClientState => currentState\n\nexport const useClientFromWallet = (timeout?: number) => {\n const subscribeWithTimeout = useCallback((listener: () => void) => subscribe(listener, timeout), [timeout])\n const clientState = useSyncExternalStore(subscribeWithTimeout, getSnapshot)\n\n useEffect(() => {\n // if client appears after timeout\n let listener = () => {\n // Update state when client becomes available\n updateState({\n client: globalThis.xyo?.client,\n isLoading: false,\n error: null,\n })\n // Notify listeners of the change\n emitChange()\n }\n // If we have timed out and still no client, listen for the plugin-ready event\n if (clientState.timedout && isNull(clientState.client) && !clientState.isLoading) {\n globalThis.addEventListener('xyo:plugin-ready', listener)\n }\n\n return () => {\n globalThis.removeEventListener('xyo:plugin-ready', listener)\n }\n })\n\n return clientState\n}\n\n/** @deprecated - use useClientFromWallet instead */\nexport const useClient = useClientFromWallet\n","import { isUndefined } from '@xylabs/sdk-js'\nimport type { XyoClient } from '@xyo-network/xl1-sdk'\n\nconst CLIENT_LISTENER_TIMEOUT = 500\n\nconst hasXyoClient = () => {\n return 'client' in globalThis.xyo\n}\n\nexport const listenForClientInjection = (onClientReady: () => void, timeout: number, onTimeout: () => void) => {\n let resolved = false\n const listener: EventListener = () => {\n onClientReady()\n resolved = true\n }\n globalThis.addEventListener('xyo:plugin-ready', listener)\n setTimeout(() => {\n if (!resolved) {\n globalThis.removeEventListener('xyo:plugin-ready', listener)\n onTimeout()\n }\n }, timeout)\n}\n\ntype ReturnType = XyoClient | undefined | null\n\nexport async function getXyoClient(timeout = CLIENT_LISTENER_TIMEOUT): Promise<ReturnType> {\n // if no xyo object, we can bail early\n if (isUndefined(globalThis.xyo)) {\n return null\n }\n return hasXyoClient()\n ? globalThis.xyo.client\n // listen for the XyoWallet to be injected\n : await new Promise<ReturnType>((resolve) => {\n listenForClientInjection(\n () => {\n resolve(globalThis.xyo.client)\n },\n timeout,\n () => {\n resolve(null)\n },\n )\n })\n}\n","import { useClientFromWallet } from '../useClientFromWallet.ts'\n\nexport const usePermissions = () => {\n const {\n client, isLoading, error, timedout,\n } = useClientFromWallet()\n const permissions = client?.permissions\n return {\n permissions,\n isLoading,\n error,\n timedout,\n }\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport type { JsonValue } from '@xylabs/sdk-js'\nimport {\n isArray, isDefined, isDefinedNotNull, isString,\n} from '@xylabs/sdk-js'\n\nimport { findCaveat } from '../helpers/index.ts'\nimport { usePermissions } from './usePermissions.ts'\n\nconst validateRestrictedAccounts = (restrictedAccounts: JsonValue): readonly string [] => {\n if (isDefined(restrictedAccounts)) {\n if (isArray(restrictedAccounts) && restrictedAccounts.every(isString)) {\n return restrictedAccounts\n }\n throw new Error(\n `Unrecognized format for restrictReturnedAccounts caveat found on permissions. \\n\n Should be an array of strings: ${JSON.stringify(restrictedAccounts)}`,\n )\n }\n return []\n}\n\nexport const useAccountPermissions = () => {\n const { permissions, error } = usePermissions()\n\n return usePromise(async () => {\n if (isDefinedNotNull(error)) throw error\n if (isDefined(permissions)) {\n const restrictedAccounts = await findCaveat(\n permissions,\n // TODO - extract to constant in protocol package\n 'xyoWallet_getAccounts',\n 'restrictReturnedAccounts',\n )\n return validateRestrictedAccounts(restrictedAccounts)\n }\n }, [permissions])\n}\n","import { isDefined, isNull } from '@xylabs/sdk-js'\nimport type { GatewayName } from '@xyo-network/xl1-sdk'\n\nimport type { GatewayFromWallet } from '../../types/index.ts'\nimport { useClientFromWallet } from './useClientFromWallet.ts'\n\nexport const useGatewayFromWallet = (gatewayName?: GatewayName, timeout?: number): GatewayFromWallet => {\n const {\n client, isLoading, error, timedout,\n } = useClientFromWallet(timeout)\n\n const resolveGateway = () => {\n // null client means we do not have to wait for timedout\n if (isNull(client)) return null\n if (timedout && !isLoading && isDefined(gatewayName)) {\n return null\n }\n return client?.gateways?.[gatewayName!]\n }\n\n return {\n gateway: gatewayName ? resolveGateway() : undefined,\n isLoading,\n error,\n timedout,\n }\n}\n\n/** @deprecated - useGatewayFromWallet */\nexport const useGateway = useGatewayFromWallet\n"],"mappings":";;;;AAAA,SAASA,iBAAiB;AAKnB,IAAMC,aAAa,8BACxBC,aACAC,kBACAC,qBAAAA;AAEA,QAAMC,sBAAsB,MAAMH,YAAYI,eAAc;AAC5D,MAAIC,UAAUF,mBAAAA,KAAwBA,oBAAoBG,SAAS,GAAG;AACpE,UAAMC,mBAAmBJ,oBACtBK,KAAKC,CAAAA,MAAKA,EAAEC,qBAAqBT,gBAAAA;AACpC,QAAII,UAAUE,gBAAAA,GAAmB;AAC/B,aAAOA,iBAAiBI,SAASH,KAAKI,CAAAA,WAAUA,OAAOC,SAASX,gBAAAA,GAAmBY,SAAS,CAAA;IAC9F;EACF;AACA,SAAOX;AACT,GAd0B;;;ACL1B,SAASY,cAAc;AAEvB,SACEC,aAAaC,WAAWC,4BACnB;;;ACJP,SAASC,mBAAmB;AAG5B,IAAMC,0BAA0B;AAEhC,IAAMC,eAAe,6BAAA;AACnB,SAAO,YAAYC,WAAWC;AAChC,GAFqB;AAId,IAAMC,2BAA2B,wBAACC,eAA2BC,SAAiBC,cAAAA;AACnF,MAAIC,WAAW;AACf,QAAMC,WAA0B,6BAAA;AAC9BJ,kBAAAA;AACAG,eAAW;EACb,GAHgC;AAIhCN,aAAWQ,iBAAiB,oBAAoBD,QAAAA;AAChDE,aAAW,MAAA;AACT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AqB;EAEzE;AACA,SAAO,CAAA;AACT,GAXmC;AAa5B,IAAMQ,wBAAwB,6BAAA;AACnC,QAAM,EAAEC,aAAaC,MAAK,IAAKC,eAAAA;AAE/B,SAAOC,WAAW,YAAA;AAChB,QAAIC,iBAAiBH,KAAAA,EAAQ,OAAMA;AACnC,QAAIT,WAAUQ,WAAAA,GAAc;AAC1B,YAAMT,qBAAqB,MAAMc;QAC/BL;;QAEA;QACA;MAAA;AAEF,aAAOV,2BAA2BC,kBAAAA;IACpC;EACF,GAAG;IAACS;GAAY;AAClB,GAfqC;;;ACtBrC,SAASM,aAAAA,YAAWC,UAAAA,eAAc;AAM3B,IAAMC,uBAAuB,wBAACC,aAA2BC,YAAAA;AAC9D,QAAM,EACJC,QAAQC,WAAWC,OAAOC,SAAQ,IAChCC,oBAAoBL,OAAAA;AAExB,QAAMM,iBAAiB,6BAAA;AAErB,QAAIC,QAAON,MAAAA,EAAS,QAAO;AAC3B,QAAIG,YAAY,CAACF,aAAaM,WAAUT,WAAAA,GAAc;AACpD,aAAO;IACT;AACA,WAAOE,QAAQQ,WAAWV,WAAAA;EAC5B,GAPuB;AASvB,SAAO;IACLW,SAASX,cAAcO,eAAAA,IAAmBK;IAC1CT;IACAC;IACAC;EACF;AACF,GApBoC;AAuB7B,IAAMQ,aAAad;","names":["isDefined","findCaveat","permissions","targetCapability","targetCaveatType","existingPermissions","getPermissions","isDefined","length","foundPermissions","find","p","parentCapability","caveats","caveat","type","value","isNull","useCallback","useEffect","useSyncExternalStore","isUndefined","CLIENT_LISTENER_TIMEOUT","hasXyoClient","globalThis","xyo","listenForClientInjection","onClientReady","timeout","onTimeout","resolved","listener","addEventListener","setTimeout","removeEventListener","getXyoClient","isUndefined","client","Promise","resolve","currentState","client","undefined","error","isLoading","timedout","listeners","Set","emitChange","listener","updateState","newState","initializeClient","timeout","getXyoClient","console","subscribe","add","delete","getSnapshot","useClientFromWallet","subscribeWithTimeout","useCallback","clientState","useSyncExternalStore","useEffect","globalThis","xyo","isNull","addEventListener","removeEventListener","useClient","usePermissions","client","isLoading","error","timedout","useClientFromWallet","permissions","usePromise","isArray","isDefined","isDefinedNotNull","isString","validateRestrictedAccounts","restrictedAccounts","isDefined","isArray","every","isString","Error","JSON","stringify","useAccountPermissions","permissions","error","usePermissions","usePromise","isDefinedNotNull","findCaveat","isDefined","isNull","useGatewayFromWallet","gatewayName","timeout","client","isLoading","error","timedout","useClientFromWallet","resolveGateway","isNull","isDefined","gateways","gateway","undefined","useGateway"]}
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/client/helpers/findCaveat.ts","../../src/hooks/client/useClientFromWallet.ts","../../src/hooks/helpers/getXyoClient.ts","../../src/hooks/client/permissions/usePermissions.ts","../../src/hooks/client/permissions/usePermissionsAccounts.ts","../../src/hooks/client/useGatewayFromWallet.ts"],"sourcesContent":["import { isDefined } from '@xylabs/sdk-js'\nimport type {\n CaveatTypes, ParentCapability, XyoPermissions,\n} from '@xyo-network/xl1-sdk'\n\nexport const findCaveat = async (\n permissions: XyoPermissions,\n targetCapability: ParentCapability,\n targetCaveatType: CaveatTypes,\n) => {\n const existingPermissions = await permissions.getPermissions()\n if (isDefined(existingPermissions) && existingPermissions.length > 0) {\n const foundPermissions = existingPermissions\n .find(p => p.parentCapability === targetCapability)\n if (isDefined(foundPermissions)) {\n return foundPermissions.caveats?.find(caveat => caveat.type === targetCaveatType)?.value ?? []\n }\n }\n return existingPermissions\n}\n","import { isUndefined } from '@xylabs/sdk-js'\nimport type { XyoClient } from '@xyo-network/xl1-sdk'\nimport { useEffect, useState } from 'react'\n\nimport { getXyoClient } from '../helpers/index.ts'\n\ninterface ClientState {\n client?: XyoClient | null\n error: Error | null\n isLoading: boolean\n timedout: boolean\n}\n\nconst initialState: ClientState = {\n client: undefined,\n error: null,\n isLoading: false,\n timedout: false,\n}\n\nexport const useClientFromWallet = (timeout?: number): ClientState => {\n const [state, setState] = useState<ClientState>(initialState)\n\n useEffect(() => {\n let cancelled = false\n\n const initialize = async () => {\n setState((prev) => {\n if (prev.isLoading) return prev\n return {\n ...prev, isLoading: true, error: null,\n }\n })\n\n try {\n const client = await getXyoClient(timeout)\n if (cancelled) return\n if (client === null) {\n setState({\n client: null, timedout: true, isLoading: false, error: null,\n })\n } else {\n setState({\n client, isLoading: false, error: null, timedout: false,\n })\n }\n } catch (error) {\n if (cancelled) return\n setState({\n client: undefined, isLoading: false, error: error as Error, timedout: false,\n })\n }\n }\n\n void initialize()\n\n // Late recovery: if wallet loads after timeout, update state\n const onPluginReady = () => {\n if (cancelled) return\n const client = isUndefined(globalThis.xyo) ? null : globalThis.xyo?.client ?? null\n setState({\n client, isLoading: false, timedout: false, error: null,\n })\n }\n globalThis.addEventListener('xyo:plugin-ready', onPluginReady)\n\n return () => {\n cancelled = true\n globalThis.removeEventListener('xyo:plugin-ready', onPluginReady)\n }\n }, [timeout])\n\n return state\n}\n\n/** @deprecated - use useClientFromWallet instead */\nexport const useClient = useClientFromWallet\n","import { isUndefined } from '@xylabs/sdk-js'\nimport type { XyoClient } from '@xyo-network/xl1-sdk'\n\nconst CLIENT_LISTENER_TIMEOUT = 2000\n\nconst hasXyoClient = () => {\n return 'client' in globalThis.xyo\n}\n\nexport const listenForClientInjection = (onClientReady: () => void, timeout: number, onTimeout: () => void) => {\n let resolved = false\n const listener: EventListener = () => {\n onClientReady()\n resolved = true\n }\n globalThis.addEventListener('xyo:plugin-ready', listener)\n setTimeout(() => {\n if (!resolved) {\n globalThis.removeEventListener('xyo:plugin-ready', listener)\n onTimeout()\n }\n }, timeout)\n}\n\ntype ReturnType = XyoClient | undefined | null\n\nexport async function getXyoClient(timeout = CLIENT_LISTENER_TIMEOUT): Promise<ReturnType> {\n // if no xyo object, we can bail early\n if (isUndefined(globalThis.xyo)) {\n return null\n }\n return hasXyoClient()\n ? globalThis.xyo.client\n // listen for the XyoWallet to be injected\n : await new Promise<ReturnType>((resolve) => {\n listenForClientInjection(\n () => {\n resolve(globalThis.xyo.client)\n },\n timeout,\n () => {\n resolve(null)\n },\n )\n })\n}\n","import { useClientFromWallet } from '../useClientFromWallet.ts'\n\nexport const usePermissions = () => {\n const {\n client, isLoading, error, timedout,\n } = useClientFromWallet()\n const permissions = client?.permissions\n return {\n permissions,\n isLoading,\n error,\n timedout,\n }\n}\n","import { usePromise } from '@xylabs/react-promise'\nimport type { JsonValue } from '@xylabs/sdk-js'\nimport {\n isArray, isDefined, isDefinedNotNull, isString,\n} from '@xylabs/sdk-js'\n\nimport { findCaveat } from '../helpers/index.ts'\nimport { usePermissions } from './usePermissions.ts'\n\nconst validateRestrictedAccounts = (restrictedAccounts: JsonValue): readonly string [] => {\n if (isDefined(restrictedAccounts)) {\n if (isArray(restrictedAccounts) && restrictedAccounts.every(isString)) {\n return restrictedAccounts\n }\n throw new Error(\n `Unrecognized format for restrictReturnedAccounts caveat found on permissions. \\n\n Should be an array of strings: ${JSON.stringify(restrictedAccounts)}`,\n )\n }\n return []\n}\n\nexport const useAccountPermissions = () => {\n const { permissions, error } = usePermissions()\n\n return usePromise(async () => {\n if (isDefinedNotNull(error)) throw error\n if (isDefined(permissions)) {\n const restrictedAccounts = await findCaveat(\n permissions,\n // TODO - extract to constant in protocol package\n 'xyoWallet_getAccounts',\n 'restrictReturnedAccounts',\n )\n return validateRestrictedAccounts(restrictedAccounts)\n }\n }, [permissions])\n}\n","import { isNull } from '@xylabs/sdk-js'\nimport type { GatewayName } from '@xyo-network/xl1-sdk'\n\nimport type { GatewayFromWallet } from '../../types/index.ts'\nimport { useClientFromWallet } from './useClientFromWallet.ts'\n\n/**\n * Resolves a gateway from the injected XL1 Wallet extension.\n *\n * The returned `gateway` uses a three-state contract:\n * - `undefined` — wallet detection is still in progress (isLoading=true),\n * or no gatewayName was provided. Consumers should treat this as \"not yet known\"\n * and avoid committing to either a present or absent UI state.\n * - `null` — wallet is definitively absent. Either globalThis.xyo was never set,\n * or detection timed out. Safe to show fallback UI (e.g. \"Get XL1 Wallet\").\n * A late-recovery listener may still transition this to a real gateway if\n * the extension loads after the timeout.\n * - `XyoGatewayRunner` — wallet is present and exposes this gateway for the\n * requested network. Ready to use for transactions and queries.\n */\nexport const useGatewayFromWallet = (gatewayName?: GatewayName, timeout?: number): GatewayFromWallet => {\n const {\n client, isLoading, error, timedout,\n } = useClientFromWallet(timeout)\n\n const resolveGateway = () => {\n // null client = wallet confirmed absent (no globalThis.xyo or timed out)\n if (isNull(client)) return null\n // client is undefined (still loading) or defined (wallet present) —\n // optional chain returns undefined while loading, or the gateway if available\n return client?.gateways?.[gatewayName!]\n }\n\n return {\n // no gatewayName requested = undefined (opt-out, not \"absent\")\n gateway: gatewayName ? resolveGateway() : undefined,\n isLoading,\n error,\n timedout,\n }\n}\n\n/** @deprecated - useGatewayFromWallet */\nexport const useGateway = useGatewayFromWallet\n"],"mappings":";;;;AAAA,SAASA,iBAAiB;AAKnB,IAAMC,aAAa,8BACxBC,aACAC,kBACAC,qBAAAA;AAEA,QAAMC,sBAAsB,MAAMH,YAAYI,eAAc;AAC5D,MAAIC,UAAUF,mBAAAA,KAAwBA,oBAAoBG,SAAS,GAAG;AACpE,UAAMC,mBAAmBJ,oBACtBK,KAAKC,CAAAA,MAAKA,EAAEC,qBAAqBT,gBAAAA;AACpC,QAAII,UAAUE,gBAAAA,GAAmB;AAC/B,aAAOA,iBAAiBI,SAASH,KAAKI,CAAAA,WAAUA,OAAOC,SAASX,gBAAAA,GAAmBY,SAAS,CAAA;IAC9F;EACF;AACA,SAAOX;AACT,GAd0B;;;ACL1B,SAASY,eAAAA,oBAAmB;AAE5B,SAASC,WAAWC,gBAAgB;;;ACFpC,SAASC,mBAAmB;AAG5B,IAAMC,0BAA0B;AAEhC,IAAMC,eAAe,6BAAA;AACnB,SAAO,YAAYC,WAAWC;AAChC,GAFqB;AAId,IAAMC,2BAA2B,wBAACC,eAA2BC,SAAiBC,cAAAA;AACnF,MAAIC,WAAW;AACf,QAAMC,WAA0B,6BAAA;AAC9BJ,kBAAAA;AACAG,eAAW;EACb,GAHgC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AAAA;IACpC;EACF,GAAG;IAACS;GAAY;AAClB,GAfqC;;;ACtBrC,SAASM,cAAc;AAoBhB,IAAMC,uBAAuB,wBAACC,aAA2BC,YAAAA;AAC9D,QAAM,EACJC,QAAQC,WAAWC,OAAOC,SAAQ,IAChCC,oBAAoBL,OAAAA;AAExB,QAAMM,iBAAiB,6BAAA;AAErB,QAAIC,OAAON,MAAAA,EAAS,QAAO;AAG3B,WAAOA,QAAQO,WAAWT,WAAAA;EAC5B,GANuB;AAQvB,SAAO;;IAELU,SAASV,cAAcO,eAAAA,IAAmBI;IAC1CR;IACAC;IACAC;EACF;AACF,GApBoC;AAuB7B,IAAMO,aAAab;","names":["isDefined","findCaveat","permissions","targetCapability","targetCaveatType","existingPermissions","getPermissions","isDefined","length","foundPermissions","find","p","parentCapability","caveats","caveat","type","value","isUndefined","useEffect","useState","isUndefined","CLIENT_LISTENER_TIMEOUT","hasXyoClient","globalThis","xyo","listenForClientInjection","onClientReady","timeout","onTimeout","resolved","listener","addEventListener","setTimeout","removeEventListener","getXyoClient","isUndefined","client","Promise","resolve","initialState","client","undefined","error","isLoading","timedout","useClientFromWallet","timeout","state","setState","useState","useEffect","cancelled","initialize","prev","getXyoClient","onPluginReady","isUndefined","globalThis","xyo","addEventListener","removeEventListener","useClient","usePermissions","client","isLoading","error","timedout","useClientFromWallet","permissions","usePromise","isArray","isDefined","isDefinedNotNull","isString","validateRestrictedAccounts","restrictedAccounts","isDefined","isArray","every","isString","Error","JSON","stringify","useAccountPermissions","permissions","error","usePermissions","usePromise","isDefinedNotNull","findCaveat","isNull","useGatewayFromWallet","gatewayName","timeout","client","isLoading","error","timedout","useClientFromWallet","resolveGateway","isNull","gateways","gateway","undefined","useGateway"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/react-chain-client",
|
|
3
|
-
"version": "1.20.
|
|
3
|
+
"version": "1.20.17",
|
|
4
4
|
"description": "XYO Layer One React SDK - Client/Wallet Hooks",
|
|
5
5
|
"homepage": "https://xylabs.com",
|
|
6
6
|
"bugs": {
|
|
@@ -24,53 +24,113 @@
|
|
|
24
24
|
"types": "./dist/browser/index.d.ts",
|
|
25
25
|
"browser": {
|
|
26
26
|
"types": "./dist/browser/index.d.ts",
|
|
27
|
-
"source": "./src/index.ts",
|
|
28
27
|
"default": "./dist/browser/index.mjs"
|
|
29
28
|
},
|
|
30
|
-
"source": "./src/index.ts",
|
|
31
29
|
"default": "./dist/browser/index.mjs"
|
|
32
30
|
},
|
|
33
31
|
"./package.json": "./package.json"
|
|
34
32
|
},
|
|
35
33
|
"module": "./dist/browser/index.mjs",
|
|
36
|
-
"source": "./src/index.ts",
|
|
37
34
|
"types": "./dist/browser/index.d.ts",
|
|
38
35
|
"files": [
|
|
39
36
|
"dist",
|
|
40
|
-
"src",
|
|
41
37
|
"!**/*.bench.*",
|
|
42
38
|
"!**/*.spec.*",
|
|
43
|
-
"!**/*.test.*"
|
|
39
|
+
"!**/*.test.*",
|
|
40
|
+
"README.md"
|
|
44
41
|
],
|
|
45
42
|
"dependencies": {
|
|
46
|
-
"@
|
|
43
|
+
"@storybook/react-vite": "~10.3.5",
|
|
44
|
+
"@xyo-network/xl1-sdk": "^1.26.20"
|
|
47
45
|
},
|
|
48
46
|
"devDependencies": {
|
|
47
|
+
"@emotion/react": "^11.5.0",
|
|
48
|
+
"@emotion/styled": "^11.3.0",
|
|
49
|
+
"@mui/material": "^7.3.9",
|
|
50
|
+
"@opentelemetry/api": "^1",
|
|
51
|
+
"@types/node": "^25.5.2",
|
|
49
52
|
"@types/react": "~19.2.14",
|
|
50
|
-
"@xylabs/
|
|
51
|
-
"@xylabs/
|
|
52
|
-
"@xylabs/ts-scripts-
|
|
53
|
-
"@xylabs/
|
|
54
|
-
"@xylabs/tsconfig
|
|
55
|
-
"@xylabs/tsconfig-
|
|
56
|
-
"@
|
|
57
|
-
"@xyo-network/
|
|
58
|
-
"@xyo-network/
|
|
59
|
-
"
|
|
53
|
+
"@xylabs/react-promise": "~7.1.20",
|
|
54
|
+
"@xylabs/sdk-js": "~5.0.95",
|
|
55
|
+
"@xylabs/ts-scripts-common": "~7.9.6",
|
|
56
|
+
"@xylabs/ts-scripts-pnpm": "~7.9.6",
|
|
57
|
+
"@xylabs/tsconfig": "~7.9.6",
|
|
58
|
+
"@xylabs/tsconfig-dom": "~7.9.6",
|
|
59
|
+
"@xylabs/tsconfig-react": "~7.9.6",
|
|
60
|
+
"@xyo-network/account": "~5.3.30",
|
|
61
|
+
"@xyo-network/account-model": "~5.3.30",
|
|
62
|
+
"@xyo-network/api-models": "~5.3.30",
|
|
63
|
+
"@xyo-network/archivist-abstract": "~5.3.30",
|
|
64
|
+
"@xyo-network/archivist-generic": "~5.3.30",
|
|
65
|
+
"@xyo-network/archivist-memory": "~5.3.30",
|
|
66
|
+
"@xyo-network/archivist-model": "~5.3.30",
|
|
67
|
+
"@xyo-network/archivist-view": "~5.3.30",
|
|
68
|
+
"@xyo-network/archivist-wrapper": "~5.3",
|
|
69
|
+
"@xyo-network/boundwitness-builder": "~5.3.30",
|
|
70
|
+
"@xyo-network/boundwitness-validator": "~5.3.30",
|
|
71
|
+
"@xyo-network/bridge-abstract": "~5.3.30",
|
|
72
|
+
"@xyo-network/bridge-model": "~5.3.30",
|
|
73
|
+
"@xyo-network/config-payload-plugin": "~5.3.30",
|
|
74
|
+
"@xyo-network/data": "~5.3.30",
|
|
75
|
+
"@xyo-network/diviner-abstract": "~5.3",
|
|
76
|
+
"@xyo-network/diviner-boundwitness-memory": "~5.3.30",
|
|
77
|
+
"@xyo-network/diviner-identity": "~5.3.30",
|
|
78
|
+
"@xyo-network/diviner-model": "~5.3.30",
|
|
79
|
+
"@xyo-network/diviner-payload-generic": "~5.3.30",
|
|
80
|
+
"@xyo-network/diviner-payload-model": "~5.3.30",
|
|
81
|
+
"@xyo-network/diviner-wrapper": "~5.3",
|
|
82
|
+
"@xyo-network/dns": "~5.3.30",
|
|
83
|
+
"@xyo-network/domain-payload-plugin": "~5.3.30",
|
|
84
|
+
"@xyo-network/elliptic": "~5.3.30",
|
|
85
|
+
"@xyo-network/hash": "~5.3.30",
|
|
86
|
+
"@xyo-network/huri": "~5.3.30",
|
|
87
|
+
"@xyo-network/manifest-model": "~5.3.30",
|
|
88
|
+
"@xyo-network/module-abstract": "~5.3.30",
|
|
89
|
+
"@xyo-network/module-model": "~5.3.30",
|
|
90
|
+
"@xyo-network/module-resolver": "~5.3.30",
|
|
91
|
+
"@xyo-network/module-wrapper": "~5.3",
|
|
92
|
+
"@xyo-network/network": "~5.3.30",
|
|
93
|
+
"@xyo-network/node-abstract": "~5.3.30",
|
|
94
|
+
"@xyo-network/node-memory": "~5.3.30",
|
|
95
|
+
"@xyo-network/node-model": "~5.3.30",
|
|
96
|
+
"@xyo-network/node-view": "~5.3.30",
|
|
97
|
+
"@xyo-network/node-wrapper": "~5.3",
|
|
98
|
+
"@xyo-network/payload-builder": "~5.3.30",
|
|
99
|
+
"@xyo-network/payload-model": "~5.3.30",
|
|
100
|
+
"@xyo-network/payload-validator": "~5.3.30",
|
|
101
|
+
"@xyo-network/previous-hash-store-model": "~5.3.30",
|
|
102
|
+
"@xyo-network/sdk-js": "~5.3.30",
|
|
103
|
+
"@xyo-network/sentinel-abstract": "~5.3.30",
|
|
104
|
+
"@xyo-network/sentinel-memory": "~5.3.30",
|
|
105
|
+
"@xyo-network/sentinel-model": "~5.3.30",
|
|
106
|
+
"@xyo-network/wallet-model": "~5.3.30",
|
|
107
|
+
"@xyo-network/wasm": "~5.3.30",
|
|
108
|
+
"@xyo-network/witness-adhoc": "~5.3.30",
|
|
109
|
+
"@xyo-network/witness-model": "~5.3.30",
|
|
110
|
+
"ajv": "^8",
|
|
111
|
+
"axios": "^1.15.0",
|
|
112
|
+
"esbuild": "*",
|
|
60
113
|
"ethers": "^6.16.0",
|
|
61
|
-
"
|
|
114
|
+
"mongodb": "^7.1.1",
|
|
115
|
+
"pako": "~2.1.0",
|
|
116
|
+
"react": "~19.2.5",
|
|
117
|
+
"react-dom": "^19.2.5",
|
|
118
|
+
"storybook": "~10.3.5",
|
|
62
119
|
"typescript": "~5.9.3",
|
|
63
|
-
"
|
|
120
|
+
"vite": "^8.0.8",
|
|
121
|
+
"zod": "~4.3.6",
|
|
122
|
+
"@xyo-network/react-chain-model": "~1.20.17",
|
|
123
|
+
"@xyo-network/react-chain-shared": "~1.20.17"
|
|
64
124
|
},
|
|
65
125
|
"peerDependencies": {
|
|
66
|
-
"@xylabs/
|
|
67
|
-
"@
|
|
68
|
-
"react": "
|
|
126
|
+
"@xylabs/react-promise": "~7.1.20",
|
|
127
|
+
"@xylabs/sdk-js": "~5.0.93",
|
|
128
|
+
"react": "~19.2.4"
|
|
69
129
|
},
|
|
70
130
|
"engines": {
|
|
71
|
-
"node": ">=
|
|
131
|
+
"node": ">=24"
|
|
72
132
|
},
|
|
73
133
|
"publishConfig": {
|
|
74
134
|
"access": "public"
|
|
75
135
|
}
|
|
76
|
-
}
|
|
136
|
+
}
|
package/src/global.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { isDefined } from '@xylabs/sdk-js'
|
|
2
|
-
import type {
|
|
3
|
-
CaveatTypes, ParentCapability, XyoPermissions,
|
|
4
|
-
} from '@xyo-network/xl1-sdk'
|
|
5
|
-
|
|
6
|
-
export const findCaveat = async (
|
|
7
|
-
permissions: XyoPermissions,
|
|
8
|
-
targetCapability: ParentCapability,
|
|
9
|
-
targetCaveatType: CaveatTypes,
|
|
10
|
-
) => {
|
|
11
|
-
const existingPermissions = await permissions.getPermissions()
|
|
12
|
-
if (isDefined(existingPermissions) && existingPermissions.length > 0) {
|
|
13
|
-
const foundPermissions = existingPermissions
|
|
14
|
-
.find(p => p.parentCapability === targetCapability)
|
|
15
|
-
if (isDefined(foundPermissions)) {
|
|
16
|
-
return foundPermissions.caveats?.find(caveat => caveat.type === targetCaveatType)?.value ?? []
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return existingPermissions
|
|
20
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './findCaveat.ts'
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { useClientFromWallet } from '../useClientFromWallet.ts'
|
|
2
|
-
|
|
3
|
-
export const usePermissions = () => {
|
|
4
|
-
const {
|
|
5
|
-
client, isLoading, error, timedout,
|
|
6
|
-
} = useClientFromWallet()
|
|
7
|
-
const permissions = client?.permissions
|
|
8
|
-
return {
|
|
9
|
-
permissions,
|
|
10
|
-
isLoading,
|
|
11
|
-
error,
|
|
12
|
-
timedout,
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { JsonValue } from '@xylabs/sdk-js'
|
|
3
|
-
import {
|
|
4
|
-
isArray, isDefined, isDefinedNotNull, isString,
|
|
5
|
-
} from '@xylabs/sdk-js'
|
|
6
|
-
|
|
7
|
-
import { findCaveat } from '../helpers/index.ts'
|
|
8
|
-
import { usePermissions } from './usePermissions.ts'
|
|
9
|
-
|
|
10
|
-
const validateRestrictedAccounts = (restrictedAccounts: JsonValue): readonly string [] => {
|
|
11
|
-
if (isDefined(restrictedAccounts)) {
|
|
12
|
-
if (isArray(restrictedAccounts) && restrictedAccounts.every(isString)) {
|
|
13
|
-
return restrictedAccounts
|
|
14
|
-
}
|
|
15
|
-
throw new Error(
|
|
16
|
-
`Unrecognized format for restrictReturnedAccounts caveat found on permissions. \n
|
|
17
|
-
Should be an array of strings: ${JSON.stringify(restrictedAccounts)}`,
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
return []
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const useAccountPermissions = () => {
|
|
24
|
-
const { permissions, error } = usePermissions()
|
|
25
|
-
|
|
26
|
-
return usePromise(async () => {
|
|
27
|
-
if (isDefinedNotNull(error)) throw error
|
|
28
|
-
if (isDefined(permissions)) {
|
|
29
|
-
const restrictedAccounts = await findCaveat(
|
|
30
|
-
permissions,
|
|
31
|
-
// TODO - extract to constant in protocol package
|
|
32
|
-
'xyoWallet_getAccounts',
|
|
33
|
-
'restrictReturnedAccounts',
|
|
34
|
-
)
|
|
35
|
-
return validateRestrictedAccounts(restrictedAccounts)
|
|
36
|
-
}
|
|
37
|
-
}, [permissions])
|
|
38
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { isNull } from '@xylabs/sdk-js'
|
|
2
|
-
import type { XyoClient } from '@xyo-network/xl1-sdk'
|
|
3
|
-
import {
|
|
4
|
-
useCallback, useEffect, useSyncExternalStore,
|
|
5
|
-
} from 'react'
|
|
6
|
-
|
|
7
|
-
import { getXyoClient } from '../helpers/index.ts'
|
|
8
|
-
|
|
9
|
-
interface ClientState {
|
|
10
|
-
client?: XyoClient | null
|
|
11
|
-
error: Error | null
|
|
12
|
-
isLoading: boolean
|
|
13
|
-
timedout: boolean
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
let currentState: ClientState = {
|
|
17
|
-
client: undefined,
|
|
18
|
-
error: null,
|
|
19
|
-
isLoading: false,
|
|
20
|
-
timedout: false,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const listeners = new Set<() => void>()
|
|
24
|
-
|
|
25
|
-
const emitChange = () => {
|
|
26
|
-
for (const listener of listeners) listener()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const updateState = (newState: Partial<ClientState>) => {
|
|
30
|
-
currentState = { ...currentState, ...newState }
|
|
31
|
-
emitChange()
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const initializeClient = async (timeout?: number) => {
|
|
35
|
-
if (currentState.isLoading || currentState.client) return
|
|
36
|
-
|
|
37
|
-
updateState({ isLoading: true, error: null })
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
const client = await getXyoClient(timeout)
|
|
41
|
-
if (client === null) {
|
|
42
|
-
updateState({
|
|
43
|
-
client: null, timedout: true, isLoading: false,
|
|
44
|
-
})
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
updateState({
|
|
48
|
-
client, isLoading: false, error: null,
|
|
49
|
-
})
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('Error initializing XyoClient', error)
|
|
52
|
-
updateState({ error: error as Error, isLoading: false })
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const subscribe = (listener: () => void, timeout?: number) => {
|
|
57
|
-
listeners.add(listener)
|
|
58
|
-
|
|
59
|
-
void initializeClient(timeout)
|
|
60
|
-
|
|
61
|
-
return () => {
|
|
62
|
-
listeners.delete(listener)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const getSnapshot = (): ClientState => currentState
|
|
67
|
-
|
|
68
|
-
export const useClientFromWallet = (timeout?: number) => {
|
|
69
|
-
const subscribeWithTimeout = useCallback((listener: () => void) => subscribe(listener, timeout), [timeout])
|
|
70
|
-
const clientState = useSyncExternalStore(subscribeWithTimeout, getSnapshot)
|
|
71
|
-
|
|
72
|
-
useEffect(() => {
|
|
73
|
-
// if client appears after timeout
|
|
74
|
-
let listener = () => {
|
|
75
|
-
// Update state when client becomes available
|
|
76
|
-
updateState({
|
|
77
|
-
client: globalThis.xyo?.client,
|
|
78
|
-
isLoading: false,
|
|
79
|
-
error: null,
|
|
80
|
-
})
|
|
81
|
-
// Notify listeners of the change
|
|
82
|
-
emitChange()
|
|
83
|
-
}
|
|
84
|
-
// If we have timed out and still no client, listen for the plugin-ready event
|
|
85
|
-
if (clientState.timedout && isNull(clientState.client) && !clientState.isLoading) {
|
|
86
|
-
globalThis.addEventListener('xyo:plugin-ready', listener)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return () => {
|
|
90
|
-
globalThis.removeEventListener('xyo:plugin-ready', listener)
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return clientState
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** @deprecated - use useClientFromWallet instead */
|
|
98
|
-
export const useClient = useClientFromWallet
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { isDefined, isNull } from '@xylabs/sdk-js'
|
|
2
|
-
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
3
|
-
|
|
4
|
-
import type { GatewayFromWallet } from '../../types/index.ts'
|
|
5
|
-
import { useClientFromWallet } from './useClientFromWallet.ts'
|
|
6
|
-
|
|
7
|
-
export const useGatewayFromWallet = (gatewayName?: GatewayName, timeout?: number): GatewayFromWallet => {
|
|
8
|
-
const {
|
|
9
|
-
client, isLoading, error, timedout,
|
|
10
|
-
} = useClientFromWallet(timeout)
|
|
11
|
-
|
|
12
|
-
const resolveGateway = () => {
|
|
13
|
-
// null client means we do not have to wait for timedout
|
|
14
|
-
if (isNull(client)) return null
|
|
15
|
-
if (timedout && !isLoading && isDefined(gatewayName)) {
|
|
16
|
-
return null
|
|
17
|
-
}
|
|
18
|
-
return client?.gateways?.[gatewayName!]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
gateway: gatewayName ? resolveGateway() : undefined,
|
|
23
|
-
isLoading,
|
|
24
|
-
error,
|
|
25
|
-
timedout,
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** @deprecated - useGatewayFromWallet */
|
|
30
|
-
export const useGateway = useGatewayFromWallet
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { isUndefined } from '@xylabs/sdk-js'
|
|
2
|
-
import type { XyoClient } from '@xyo-network/xl1-sdk'
|
|
3
|
-
|
|
4
|
-
const CLIENT_LISTENER_TIMEOUT = 500
|
|
5
|
-
|
|
6
|
-
const hasXyoClient = () => {
|
|
7
|
-
return 'client' in globalThis.xyo
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const listenForClientInjection = (onClientReady: () => void, timeout: number, onTimeout: () => void) => {
|
|
11
|
-
let resolved = false
|
|
12
|
-
const listener: EventListener = () => {
|
|
13
|
-
onClientReady()
|
|
14
|
-
resolved = true
|
|
15
|
-
}
|
|
16
|
-
globalThis.addEventListener('xyo:plugin-ready', listener)
|
|
17
|
-
setTimeout(() => {
|
|
18
|
-
if (!resolved) {
|
|
19
|
-
globalThis.removeEventListener('xyo:plugin-ready', listener)
|
|
20
|
-
onTimeout()
|
|
21
|
-
}
|
|
22
|
-
}, timeout)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type ReturnType = XyoClient | undefined | null
|
|
26
|
-
|
|
27
|
-
export async function getXyoClient(timeout = CLIENT_LISTENER_TIMEOUT): Promise<ReturnType> {
|
|
28
|
-
// if no xyo object, we can bail early
|
|
29
|
-
if (isUndefined(globalThis.xyo)) {
|
|
30
|
-
return null
|
|
31
|
-
}
|
|
32
|
-
return hasXyoClient()
|
|
33
|
-
? globalThis.xyo.client
|
|
34
|
-
// listen for the XyoWallet to be injected
|
|
35
|
-
: await new Promise<ReturnType>((resolve) => {
|
|
36
|
-
listenForClientInjection(
|
|
37
|
-
() => {
|
|
38
|
-
resolve(globalThis.xyo.client)
|
|
39
|
-
},
|
|
40
|
-
timeout,
|
|
41
|
-
() => {
|
|
42
|
-
resolve(null)
|
|
43
|
-
},
|
|
44
|
-
)
|
|
45
|
-
})
|
|
46
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './getXyoClient.ts'
|
package/src/hooks/index.ts
DELETED
package/src/index.ts
DELETED
package/src/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './GatewayFromWallet.ts'
|