@txwallet/graz-connector 0.1.0

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/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@txwallet/graz-connector",
3
+ "version": "0.1.0",
4
+ "description": "Thin-slice Graz compatibility connector for TX Wallet.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./index.d.ts",
14
+ "default": "./index.js"
15
+ },
16
+ "./react": {
17
+ "types": "./react.d.ts",
18
+ "default": "./react.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "index.js",
23
+ "index.d.ts",
24
+ "react.js",
25
+ "react.d.ts",
26
+ "README.md"
27
+ ],
28
+ "peerDependencies": {
29
+ "react": ">=18"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "react": {
33
+ "optional": true
34
+ }
35
+ },
36
+ "keywords": [
37
+ "tx-wallet",
38
+ "graz",
39
+ "cosmos",
40
+ "wallet-connector"
41
+ ]
42
+ }
package/react.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type {
2
+ TxWalletGrazConnector,
3
+ TxWalletGrazConnectorError,
4
+ TxWalletGrazConnectedAccount,
5
+ TxWalletGrazAccount,
6
+ TxWalletGrazSignResult,
7
+ TxWalletGrazTransactionResult,
8
+ TxWalletGrazAuthHandlerOptions,
9
+ } from './index';
10
+
11
+ export interface UseTxWalletGrazConnectorResult {
12
+ connector: TxWalletGrazConnector;
13
+ status: TxWalletGrazConnector['state'];
14
+ account: TxWalletGrazAccount | null;
15
+ error: TxWalletGrazConnectorError | null;
16
+ isIdle: boolean;
17
+ isConnecting: boolean;
18
+ isConnected: boolean;
19
+ isSigning: boolean;
20
+ isRequesting: boolean;
21
+ connect(input: {
22
+ chainId: string;
23
+ appId: string;
24
+ callbackUrl: string;
25
+ statement?: string;
26
+ }): Promise<TxWalletGrazConnectedAccount | undefined>;
27
+ signAdr36(input: {
28
+ appId: string;
29
+ requestId?: string;
30
+ callbackUrl: string;
31
+ challenge?: Record<string, unknown>;
32
+ }): Promise<TxWalletGrazSignResult | undefined>;
33
+ requestTransaction(input: {
34
+ appId: string;
35
+ requestId?: string;
36
+ callbackUrl: string;
37
+ chainId?: string;
38
+ body?: Record<string, unknown>;
39
+ statusUrl?: string;
40
+ fallbackUrl?: string;
41
+ }): Promise<TxWalletGrazTransactionResult | undefined>;
42
+ disconnect(): Promise<void>;
43
+ }
44
+
45
+ export function useTxWalletGrazConnector(
46
+ connectorOrOptions?: TxWalletGrazConnector | (TxWalletGrazAuthHandlerOptions & { initialChainId?: string }),
47
+ ): UseTxWalletGrazConnectorResult;
package/react.js ADDED
@@ -0,0 +1,106 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import { createTxWalletGrazAuthConnector } from './index.js';
3
+
4
+ const isConnectorInstance = (value) => (
5
+ Boolean(value)
6
+ && typeof value === 'object'
7
+ && typeof value.connect === 'function'
8
+ && typeof value.on === 'function'
9
+ && typeof value.getState === 'function'
10
+ );
11
+
12
+ const readAccount = (connector) => (
13
+ connector.accountAddress
14
+ ? { accountAddress: connector.accountAddress, chainId: connector.chainId }
15
+ : null
16
+ );
17
+
18
+ /**
19
+ * React hook that wraps a TX Wallet Graz connector with graz-style ergonomics:
20
+ * reactive `status`/`account`/`error` plus fire-and-forget-safe actions.
21
+ *
22
+ * Pass an existing connector instance, or auth-handler options to build one
23
+ * (an auth-backed connector is created once on mount). The action methods never
24
+ * throw — failures surface through `error`/`status`; use the returned `connector`
25
+ * directly if you need the raw throwing/awaitable calls.
26
+ */
27
+ export function useTxWalletGrazConnector(connectorOrOptions = {}) {
28
+ const connectorRef = useRef(null);
29
+ if (connectorRef.current === null) {
30
+ connectorRef.current = isConnectorInstance(connectorOrOptions)
31
+ ? connectorOrOptions
32
+ : createTxWalletGrazAuthConnector(connectorOrOptions);
33
+ }
34
+ const connector = connectorRef.current;
35
+
36
+ const [status, setStatus] = useState(() => connector.getState());
37
+ const [account, setAccount] = useState(() => readAccount(connector));
38
+ const [error, setError] = useState(null);
39
+
40
+ useEffect(() => {
41
+ const offState = connector.on('state', (next) => setStatus(next));
42
+ const offConnected = connector.on('connected', (detail) => {
43
+ setAccount({ accountAddress: detail.accountAddress, chainId: detail.chainId });
44
+ setError(null);
45
+ });
46
+ const offDisconnected = connector.on('disconnected', () => setAccount(null));
47
+ const offError = connector.on('error', (normalizedError) => setError(normalizedError));
48
+
49
+ // Re-sync in case the connector changed state between render and subscription.
50
+ setStatus(connector.getState());
51
+ setAccount(readAccount(connector));
52
+
53
+ return () => {
54
+ offState();
55
+ offConnected();
56
+ offDisconnected();
57
+ offError();
58
+ };
59
+ }, [connector]);
60
+
61
+ const connect = useCallback(async (input) => {
62
+ setError(null);
63
+ try {
64
+ return await connector.connect(input);
65
+ } catch {
66
+ // Normalized error is surfaced via the 'error' event -> error state.
67
+ return undefined;
68
+ }
69
+ }, [connector]);
70
+
71
+ const signAdr36 = useCallback(async (input) => {
72
+ setError(null);
73
+ try {
74
+ return await connector.signAdr36(input);
75
+ } catch {
76
+ return undefined;
77
+ }
78
+ }, [connector]);
79
+
80
+ const requestTransaction = useCallback(async (input) => {
81
+ setError(null);
82
+ try {
83
+ return await connector.requestTransaction(input);
84
+ } catch {
85
+ return undefined;
86
+ }
87
+ }, [connector]);
88
+
89
+ const disconnect = useCallback(() => connector.disconnect(), [connector]);
90
+
91
+ return {
92
+ connector,
93
+ status,
94
+ account,
95
+ error,
96
+ isIdle: status === 'idle',
97
+ isConnecting: status === 'connecting',
98
+ isConnected: status === 'connected',
99
+ isSigning: status === 'signing',
100
+ isRequesting: status === 'requesting',
101
+ connect,
102
+ signAdr36,
103
+ requestTransaction,
104
+ disconnect,
105
+ };
106
+ }