@meshconnect/uwc-react 0.2.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/README.md +222 -0
- package/dist/hooks/index.d.ts +9 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +9 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useConnection.d.ts +22 -0
- package/dist/hooks/useConnection.d.ts.map +1 -0
- package/dist/hooks/useConnection.js +98 -0
- package/dist/hooks/useConnection.js.map +1 -0
- package/dist/hooks/useDisconnect.d.ts +27 -0
- package/dist/hooks/useDisconnect.d.ts.map +1 -0
- package/dist/hooks/useDisconnect.js +44 -0
- package/dist/hooks/useDisconnect.js.map +1 -0
- package/dist/hooks/useNetworks.d.ts +16 -0
- package/dist/hooks/useNetworks.d.ts.map +1 -0
- package/dist/hooks/useNetworks.js +23 -0
- package/dist/hooks/useNetworks.js.map +1 -0
- package/dist/hooks/useSession.d.ts +18 -0
- package/dist/hooks/useSession.d.ts.map +1 -0
- package/dist/hooks/useSession.js +33 -0
- package/dist/hooks/useSession.js.map +1 -0
- package/dist/hooks/useSignMessage.d.ts +22 -0
- package/dist/hooks/useSignMessage.d.ts.map +1 -0
- package/dist/hooks/useSignMessage.js +61 -0
- package/dist/hooks/useSignMessage.js.map +1 -0
- package/dist/hooks/useSwitchNetwork.d.ts +28 -0
- package/dist/hooks/useSwitchNetwork.d.ts.map +1 -0
- package/dist/hooks/useSwitchNetwork.js +68 -0
- package/dist/hooks/useSwitchNetwork.js.map +1 -0
- package/dist/hooks/useTransaction.d.ts +25 -0
- package/dist/hooks/useTransaction.d.ts.map +1 -0
- package/dist/hooks/useTransaction.js +64 -0
- package/dist/hooks/useTransaction.js.map +1 -0
- package/dist/hooks/useWallets.d.ts +16 -0
- package/dist/hooks/useWallets.d.ts.map +1 -0
- package/dist/hooks/useWallets.js +23 -0
- package/dist/hooks/useWallets.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/ConnectionProvider.d.ts +21 -0
- package/dist/providers/ConnectionProvider.d.ts.map +1 -0
- package/dist/providers/ConnectionProvider.js +31 -0
- package/dist/providers/ConnectionProvider.js.map +1 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +2 -0
- package/dist/providers/index.js.map +1 -0
- package/package.json +43 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useConnection.ts +123 -0
- package/src/hooks/useDisconnect.ts +52 -0
- package/src/hooks/useNetworks.ts +26 -0
- package/src/hooks/useSession.ts +37 -0
- package/src/hooks/useSignMessage.ts +69 -0
- package/src/hooks/useSwitchNetwork.ts +84 -0
- package/src/hooks/useTransaction.ts +79 -0
- package/src/hooks/useWallets.ts +26 -0
- package/src/index.ts +2 -0
- package/src/providers/ConnectionProvider.tsx +76 -0
- package/src/providers/index.ts +1 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { UseSwitchNetworkReturn } from '@meshconnect/uwc-types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook for switching networks on the connected wallet
|
|
4
|
+
* @returns Network switching function and loading states
|
|
5
|
+
* @throws Error if used outside of ConnectionProvider
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const { switchNetwork, isLoading, isWaitingForUserApproval } = useSwitchNetwork()
|
|
9
|
+
*
|
|
10
|
+
* const handleSwitch = async () => {
|
|
11
|
+
* try {
|
|
12
|
+
* await switchNetwork('eip155:8453') // Switch to Base
|
|
13
|
+
* } catch (error) {
|
|
14
|
+
* console.error('Network switch failed:', error)
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* // Show loading UI
|
|
19
|
+
* if (isWaitingForUserApproval) {
|
|
20
|
+
* return <div>Please approve in your wallet...</div>
|
|
21
|
+
* }
|
|
22
|
+
* if (isLoading) {
|
|
23
|
+
* return <div>Switching network...</div>
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function useSwitchNetwork(): UseSwitchNetworkReturn;
|
|
28
|
+
//# sourceMappingURL=useSwitchNetwork.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSwitchNetwork.d.ts","sourceRoot":"","sources":["../../src/hooks/useSwitchNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,sBAAsB,EAEvB,MAAM,wBAAwB,CAAA;AAE/B;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAkDzD"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useContext, useCallback, useState, useEffect } from 'react';
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider';
|
|
3
|
+
/**
|
|
4
|
+
* Hook for switching networks on the connected wallet
|
|
5
|
+
* @returns Network switching function and loading states
|
|
6
|
+
* @throws Error if used outside of ConnectionProvider
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const { switchNetwork, isLoading, isWaitingForUserApproval } = useSwitchNetwork()
|
|
10
|
+
*
|
|
11
|
+
* const handleSwitch = async () => {
|
|
12
|
+
* try {
|
|
13
|
+
* await switchNetwork('eip155:8453') // Switch to Base
|
|
14
|
+
* } catch (error) {
|
|
15
|
+
* console.error('Network switch failed:', error)
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Show loading UI
|
|
20
|
+
* if (isWaitingForUserApproval) {
|
|
21
|
+
* return <div>Please approve in your wallet...</div>
|
|
22
|
+
* }
|
|
23
|
+
* if (isLoading) {
|
|
24
|
+
* return <div>Switching network...</div>
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useSwitchNetwork() {
|
|
29
|
+
const context = useContext(ConnectionContext);
|
|
30
|
+
if (!context) {
|
|
31
|
+
throw new Error('useSwitchNetwork must be used within a ConnectionProvider');
|
|
32
|
+
}
|
|
33
|
+
const { connector } = context;
|
|
34
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
35
|
+
const [isWaitingForUserApproval, setIsWaitingForUserApproval] = useState(false);
|
|
36
|
+
const [error, setError] = useState();
|
|
37
|
+
// Subscribe to loading state changes from the core
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const updateLoadingState = () => {
|
|
40
|
+
const state = connector.getNetworkSwitchLoadingState();
|
|
41
|
+
setIsLoading(state.isLoading);
|
|
42
|
+
setIsWaitingForUserApproval(state.isWaitingForUserApproval);
|
|
43
|
+
};
|
|
44
|
+
// Check initial state
|
|
45
|
+
updateLoadingState();
|
|
46
|
+
// Subscribe to changes
|
|
47
|
+
const unsubscribe = connector.subscribe(updateLoadingState);
|
|
48
|
+
return unsubscribe;
|
|
49
|
+
}, [connector]);
|
|
50
|
+
const switchNetwork = useCallback(async (networkId) => {
|
|
51
|
+
setError(undefined);
|
|
52
|
+
try {
|
|
53
|
+
await connector.switchNetwork(networkId);
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
const walletError = err;
|
|
57
|
+
setError(walletError);
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
}, [connector]);
|
|
61
|
+
return {
|
|
62
|
+
switchNetwork,
|
|
63
|
+
isLoading,
|
|
64
|
+
isWaitingForUserApproval,
|
|
65
|
+
error
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=useSwitchNetwork.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSwitchNetwork.js","sourceRoot":"","sources":["../../src/hooks/useSwitchNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAOnE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,gBAAgB;IAC9B,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAA;IAE7C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAA;IAC9E,CAAC;IAED,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;IAC7B,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IACjD,MAAM,CAAC,wBAAwB,EAAE,2BAA2B,CAAC,GAC3D,QAAQ,CAAC,KAAK,CAAC,CAAA;IACjB,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAA2B,CAAA;IAE7D,mDAAmD;IACnD,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,GAAG,EAAE;YAC9B,MAAM,KAAK,GAAG,SAAS,CAAC,4BAA4B,EAAE,CAAA;YACtD,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;YAC7B,2BAA2B,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;QAC7D,CAAC,CAAA;QAED,sBAAsB;QACtB,kBAAkB,EAAE,CAAA;QAEpB,uBAAuB;QACvB,MAAM,WAAW,GAAG,SAAS,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAA;QAE3D,OAAO,WAAW,CAAA;IACpB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAA;IAEf,MAAM,aAAa,GAAG,WAAW,CAC/B,KAAK,EAAE,SAAoB,EAAE,EAAE;QAC7B,QAAQ,CAAC,SAAS,CAAC,CAAA;QACnB,IAAI,CAAC;YACH,MAAM,SAAS,CAAC,aAAa,CAAC,SAAS,CAAC,CAAA;QAC1C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,WAAW,GAAG,GAAkB,CAAA;YACtC,QAAQ,CAAC,WAAW,CAAC,CAAA;YACrB,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC,EACD,CAAC,SAAS,CAAC,CACZ,CAAA;IAED,OAAO;QACL,aAAa;QACb,SAAS;QACT,wBAAwB;QACxB,KAAK;KACN,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { UseTransactionReturn } from '@meshconnect/uwc-types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook for sending transactions with the connected wallet
|
|
4
|
+
* @returns Object containing sendTransaction function, loading state, and transaction result
|
|
5
|
+
* @throws Error if used outside of ConnectionProvider
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const { sendTransaction, isLoading, transactionResult } = useTransaction()
|
|
9
|
+
*
|
|
10
|
+
* const handleSend = async () => {
|
|
11
|
+
* try {
|
|
12
|
+
* const result = await sendTransaction({
|
|
13
|
+
* to: '0x...',
|
|
14
|
+
* amount: BigInt('1000000000000000000'),
|
|
15
|
+
* from: session.activeAddress
|
|
16
|
+
* })
|
|
17
|
+
* console.log('Transaction hash:', result.hash)
|
|
18
|
+
* } catch (error) {
|
|
19
|
+
* console.error('Transaction failed:', error)
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function useTransaction(): UseTransactionReturn;
|
|
25
|
+
//# sourceMappingURL=useTransaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransaction.d.ts","sourceRoot":"","sources":["../../src/hooks/useTransaction.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,oBAAoB,EAErB,MAAM,wBAAwB,CAAA;AAE/B;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,IAAI,oBAAoB,CA+CrD"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useContext, useCallback, useState } from 'react';
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider';
|
|
3
|
+
/**
|
|
4
|
+
* Hook for sending transactions with the connected wallet
|
|
5
|
+
* @returns Object containing sendTransaction function, loading state, and transaction result
|
|
6
|
+
* @throws Error if used outside of ConnectionProvider
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const { sendTransaction, isLoading, transactionResult } = useTransaction()
|
|
10
|
+
*
|
|
11
|
+
* const handleSend = async () => {
|
|
12
|
+
* try {
|
|
13
|
+
* const result = await sendTransaction({
|
|
14
|
+
* to: '0x...',
|
|
15
|
+
* amount: BigInt('1000000000000000000'),
|
|
16
|
+
* from: session.activeAddress
|
|
17
|
+
* })
|
|
18
|
+
* console.log('Transaction hash:', result.hash)
|
|
19
|
+
* } catch (error) {
|
|
20
|
+
* console.error('Transaction failed:', error)
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useTransaction() {
|
|
26
|
+
const context = useContext(ConnectionContext);
|
|
27
|
+
if (!context) {
|
|
28
|
+
throw new Error('useTransaction must be used within a ConnectionProvider');
|
|
29
|
+
}
|
|
30
|
+
const { connector, session } = context;
|
|
31
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
32
|
+
const [transactionResult, setTransactionResult] = useState();
|
|
33
|
+
const [error, setError] = useState();
|
|
34
|
+
const sendTransaction = useCallback(async (request) => {
|
|
35
|
+
if (!session.activeAddress) {
|
|
36
|
+
throw new Error('No wallet connected');
|
|
37
|
+
}
|
|
38
|
+
if (!request) {
|
|
39
|
+
throw new Error('Transaction request is required');
|
|
40
|
+
}
|
|
41
|
+
setIsLoading(true);
|
|
42
|
+
setError(undefined);
|
|
43
|
+
try {
|
|
44
|
+
const result = await connector.sendTransaction(request);
|
|
45
|
+
setTransactionResult(result);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
const walletError = err;
|
|
50
|
+
setError(walletError);
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
setIsLoading(false);
|
|
55
|
+
}
|
|
56
|
+
}, [connector, session.activeAddress]);
|
|
57
|
+
return {
|
|
58
|
+
sendTransaction,
|
|
59
|
+
isLoading,
|
|
60
|
+
transactionResult,
|
|
61
|
+
error
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=useTransaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransaction.js","sourceRoot":"","sources":["../../src/hooks/useTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAQnE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,cAAc;IAC5B,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAA;IAE7C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAA;IAC5E,CAAC;IAED,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;IACtC,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IACjD,MAAM,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,GAAG,QAAQ,EAEvD,CAAA;IACH,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAA2B,CAAA;IAE7D,MAAM,eAAe,GAAG,WAAW,CACjC,KAAK,EAAE,OAA2B,EAA8B,EAAE;QAChE,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QACxC,CAAC;QAED,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAA;QACpD,CAAC;QAED,YAAY,CAAC,IAAI,CAAC,CAAA;QAClB,QAAQ,CAAC,SAAS,CAAC,CAAA;QACnB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,CAAA;YACvD,oBAAoB,CAAC,MAAM,CAAC,CAAA;YAC5B,OAAO,MAAM,CAAA;QACf,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,WAAW,GAAG,GAAkB,CAAA;YACtC,QAAQ,CAAC,WAAW,CAAC,CAAA;YACrB,MAAM,GAAG,CAAA;QACX,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAA;QACrB,CAAC;IACH,CAAC,EACD,CAAC,SAAS,EAAE,OAAO,CAAC,aAAa,CAAC,CACnC,CAAA;IAED,OAAO;QACL,eAAe;QACf,SAAS;QACT,iBAAiB;QACjB,KAAK;KACN,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WalletMetadata } from '@meshconnect/uwc-types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook for accessing the list of configured wallets
|
|
4
|
+
* @returns Array of wallet metadata including names, IDs, and supported networks
|
|
5
|
+
* @throws Error if used outside of ConnectionProvider
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const wallets = useWallets()
|
|
9
|
+
*
|
|
10
|
+
* wallets.forEach(wallet => {
|
|
11
|
+
* console.log(`${wallet.name}: ${wallet.id}`)
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function useWallets(): WalletMetadata[];
|
|
16
|
+
//# sourceMappingURL=useWallets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWallets.d.ts","sourceRoot":"","sources":["../../src/hooks/useWallets.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAE5D;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,IAAI,cAAc,EAAE,CAQ7C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider';
|
|
3
|
+
/**
|
|
4
|
+
* Hook for accessing the list of configured wallets
|
|
5
|
+
* @returns Array of wallet metadata including names, IDs, and supported networks
|
|
6
|
+
* @throws Error if used outside of ConnectionProvider
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const wallets = useWallets()
|
|
10
|
+
*
|
|
11
|
+
* wallets.forEach(wallet => {
|
|
12
|
+
* console.log(`${wallet.name}: ${wallet.id}`)
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function useWallets() {
|
|
17
|
+
const context = useContext(ConnectionContext);
|
|
18
|
+
if (!context) {
|
|
19
|
+
throw new Error('useWallets must be used within a ConnectionProvider');
|
|
20
|
+
}
|
|
21
|
+
return context.wallets;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=useWallets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWallets.js","sourceRoot":"","sources":["../../src/hooks/useWallets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAGnE;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU;IACxB,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAA;IAE7C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAA;IACxE,CAAC;IAED,OAAO,OAAO,CAAC,OAAO,CAAA;AACxB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { UniversalWalletConnector } from '@meshconnect/uwc-core';
|
|
3
|
+
import type { Session, Network, WalletMetadata, WalletConnectConfig } from '@meshconnect/uwc-types';
|
|
4
|
+
interface ConnectionContextValue {
|
|
5
|
+
connector: UniversalWalletConnector;
|
|
6
|
+
session: Session;
|
|
7
|
+
wallets: WalletMetadata[];
|
|
8
|
+
networks: Network[];
|
|
9
|
+
isReady: boolean;
|
|
10
|
+
}
|
|
11
|
+
declare const ConnectionContext: React.Context<ConnectionContextValue | null>;
|
|
12
|
+
interface ConnectionProviderProps {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
networks: Network[];
|
|
15
|
+
wallets: WalletMetadata[];
|
|
16
|
+
usingIntegratedBrowser?: boolean;
|
|
17
|
+
walletConnectConfig?: WalletConnectConfig;
|
|
18
|
+
}
|
|
19
|
+
export declare function ConnectionProvider({ children, networks, wallets, usingIntegratedBrowser, walletConnectConfig }: ConnectionProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
20
|
+
export { ConnectionContext };
|
|
21
|
+
//# sourceMappingURL=ConnectionProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectionProvider.d.ts","sourceRoot":"","sources":["../../src/providers/ConnectionProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAqD,MAAM,OAAO,CAAA;AACzE,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,KAAK,EACV,OAAO,EACP,OAAO,EACP,cAAc,EACd,mBAAmB,EACpB,MAAM,wBAAwB,CAAA;AAE/B,UAAU,sBAAsB;IAC9B,SAAS,EAAE,wBAAwB,CAAA;IACnC,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,cAAc,EAAE,CAAA;IACzB,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,QAAA,MAAM,iBAAiB,8CAAqD,CAAA;AAE5E,UAAU,uBAAuB;IAC/B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;IACzB,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,OAAO,EAAE,cAAc,EAAE,CAAA;IACzB,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,mBAAmB,CAAC,EAAE,mBAAmB,CAAA;CAC1C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,sBAA8B,EAC9B,mBAAmB,EACpB,EAAE,uBAAuB,2CAwCzB;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { createContext, useEffect, useState, useRef } from 'react';
|
|
3
|
+
import { UniversalWalletConnector } from '@meshconnect/uwc-core';
|
|
4
|
+
const ConnectionContext = createContext(null);
|
|
5
|
+
export function ConnectionProvider({ children, networks, wallets, usingIntegratedBrowser = false, walletConnectConfig }) {
|
|
6
|
+
// Create the connector instance only once using useRef
|
|
7
|
+
const connectorRef = useRef(null);
|
|
8
|
+
if (!connectorRef.current) {
|
|
9
|
+
connectorRef.current = new UniversalWalletConnector(networks, wallets, usingIntegratedBrowser, walletConnectConfig);
|
|
10
|
+
}
|
|
11
|
+
const connector = connectorRef.current;
|
|
12
|
+
const [session, setSession] = useState(() => connector.getSession());
|
|
13
|
+
const [isReady, setIsReady] = useState(() => connector.isReady());
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const unsubscribe = connector.subscribe(() => {
|
|
16
|
+
setSession(connector.getSession());
|
|
17
|
+
setIsReady(connector.isReady());
|
|
18
|
+
});
|
|
19
|
+
return unsubscribe;
|
|
20
|
+
}, [connector]);
|
|
21
|
+
const value = {
|
|
22
|
+
connector,
|
|
23
|
+
session,
|
|
24
|
+
wallets: connector.getWallets(),
|
|
25
|
+
networks: connector.getNetworks(),
|
|
26
|
+
isReady
|
|
27
|
+
};
|
|
28
|
+
return (_jsx(ConnectionContext.Provider, { value: value, children: children }));
|
|
29
|
+
}
|
|
30
|
+
export { ConnectionContext };
|
|
31
|
+
//# sourceMappingURL=ConnectionProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectionProvider.js","sourceRoot":"","sources":["../../src/providers/ConnectionProvider.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AACzE,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAA;AAgBhE,MAAM,iBAAiB,GAAG,aAAa,CAAgC,IAAI,CAAC,CAAA;AAU5E,MAAM,UAAU,kBAAkB,CAAC,EACjC,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,sBAAsB,GAAG,KAAK,EAC9B,mBAAmB,EACK;IACxB,uDAAuD;IACvD,MAAM,YAAY,GAAG,MAAM,CAAkC,IAAI,CAAC,CAAA;IAElE,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;QAC1B,YAAY,CAAC,OAAO,GAAG,IAAI,wBAAwB,CACjD,QAAQ,EACR,OAAO,EACP,sBAAsB,EACtB,mBAAmB,CACpB,CAAA;IACH,CAAC;IAED,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAA;IAEtC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAU,GAAG,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAA;IAC7E,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAA;IAEjE,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,WAAW,GAAG,SAAS,CAAC,SAAS,CAAC,GAAG,EAAE;YAC3C,UAAU,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAA;YAClC,UAAU,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;QAEF,OAAO,WAAW,CAAA;IACpB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAA;IAEf,MAAM,KAAK,GAA2B;QACpC,SAAS;QACT,OAAO;QACP,OAAO,EAAE,SAAS,CAAC,UAAU,EAAE;QAC/B,QAAQ,EAAE,SAAS,CAAC,WAAW,EAAE;QACjC,OAAO;KACR,CAAA;IAED,OAAO,CACL,KAAC,iBAAiB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YACrC,QAAQ,GACkB,CAC9B,CAAA;AACH,CAAC;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meshconnect/uwc-react",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "React hooks and components for Universal Wallet Connector",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@meshconnect/uwc-core": "0.2.0",
|
|
20
|
+
"@meshconnect/uwc-types": "0.2.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "^18.0.0",
|
|
24
|
+
"react-dom": "^18.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/react": "^18.3.12",
|
|
28
|
+
"@types/react-dom": "^18.3.1",
|
|
29
|
+
"react": "^18.3.1",
|
|
30
|
+
"react-dom": "^18.3.1",
|
|
31
|
+
"typescript": "^5.9.2"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc",
|
|
38
|
+
"dev": "tsc --watch",
|
|
39
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
40
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
|
41
|
+
"type-check": "tsc --noEmit"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './useConnection'
|
|
2
|
+
export * from './useSwitchNetwork'
|
|
3
|
+
export * from './useDisconnect'
|
|
4
|
+
export * from './useSession'
|
|
5
|
+
export * from './useWallets'
|
|
6
|
+
export * from './useNetworks'
|
|
7
|
+
export * from './useSignMessage'
|
|
8
|
+
export * from './useTransaction'
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { useContext, useCallback, useState, useEffect } from 'react'
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider'
|
|
3
|
+
import type {
|
|
4
|
+
NetworkId,
|
|
5
|
+
UseConnectionReturn,
|
|
6
|
+
WalletError
|
|
7
|
+
} from '@meshconnect/uwc-types'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hook for managing wallet connections with both injected and WalletConnect modes
|
|
11
|
+
* @param walletId - The ID of the wallet to connect to (e.g., 'metamask', 'trust')
|
|
12
|
+
* @returns Connection methods and state for both injected and WalletConnect modes
|
|
13
|
+
* @throws Error if used outside of ConnectionProvider
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const { walletConnect, injected } = useConnection('metamask')
|
|
17
|
+
*
|
|
18
|
+
* // Connect with browser extension
|
|
19
|
+
* await injected.connect('eip155:1')
|
|
20
|
+
*
|
|
21
|
+
* // Connect with WalletConnect QR code
|
|
22
|
+
* await walletConnect.connect('eip155:1')
|
|
23
|
+
* if (walletConnect.connectionURI) {
|
|
24
|
+
* // Display QR code with walletConnect.connectionURI
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useConnection(walletId: string): UseConnectionReturn {
|
|
29
|
+
const context = useContext(ConnectionContext)
|
|
30
|
+
|
|
31
|
+
if (!context) {
|
|
32
|
+
throw new Error('useConnection must be used within a ConnectionProvider')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { connector, isReady } = context
|
|
36
|
+
|
|
37
|
+
const [walletConnectLoading, setWalletConnectLoading] = useState(false)
|
|
38
|
+
const [injectedLoading, setInjectedLoading] = useState(false)
|
|
39
|
+
const [walletConnectError, setWalletConnectError] = useState<
|
|
40
|
+
WalletError | undefined
|
|
41
|
+
>()
|
|
42
|
+
const [injectedError, setInjectedError] = useState<WalletError | undefined>()
|
|
43
|
+
const [connectionURI, setConnectionURI] = useState<string | undefined>()
|
|
44
|
+
const [walletConnectAvailable, setWalletConnectAvailable] = useState(false)
|
|
45
|
+
const [injectedAvailable, setInjectedAvailable] = useState(false)
|
|
46
|
+
|
|
47
|
+
// Update connection URI and availability when they change
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const checkState = () => {
|
|
50
|
+
const uri = connector.getConnectionURI()
|
|
51
|
+
setConnectionURI(uri)
|
|
52
|
+
|
|
53
|
+
// Check availability for both connection modes
|
|
54
|
+
const wcAvailable = connector.isConnectionModeAvailable(
|
|
55
|
+
'walletConnect',
|
|
56
|
+
walletId
|
|
57
|
+
)
|
|
58
|
+
const injAvailable = connector.isConnectionModeAvailable(
|
|
59
|
+
'injected',
|
|
60
|
+
walletId
|
|
61
|
+
)
|
|
62
|
+
setWalletConnectAvailable(wcAvailable)
|
|
63
|
+
setInjectedAvailable(injAvailable)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Check initially and on state changes
|
|
67
|
+
checkState()
|
|
68
|
+
const unsubscribe = connector.subscribe(checkState)
|
|
69
|
+
|
|
70
|
+
return unsubscribe
|
|
71
|
+
}, [connector, walletId])
|
|
72
|
+
|
|
73
|
+
const connectWalletConnect = useCallback(
|
|
74
|
+
async (networkId?: NetworkId) => {
|
|
75
|
+
setWalletConnectLoading(true)
|
|
76
|
+
setWalletConnectError(undefined)
|
|
77
|
+
try {
|
|
78
|
+
await connector.connect('walletConnect', walletId, networkId)
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const walletError = error as WalletError
|
|
81
|
+
setWalletConnectError(walletError)
|
|
82
|
+
throw error
|
|
83
|
+
} finally {
|
|
84
|
+
setWalletConnectLoading(false)
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
[connector, walletId]
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
const connectInjected = useCallback(
|
|
91
|
+
async (networkId?: NetworkId) => {
|
|
92
|
+
setInjectedLoading(true)
|
|
93
|
+
setInjectedError(undefined)
|
|
94
|
+
try {
|
|
95
|
+
await connector.connect('injected', walletId, networkId)
|
|
96
|
+
} catch (error) {
|
|
97
|
+
const walletError = error as WalletError
|
|
98
|
+
setInjectedError(walletError)
|
|
99
|
+
throw error
|
|
100
|
+
} finally {
|
|
101
|
+
setInjectedLoading(false)
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
[connector, walletId]
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
walletConnect: {
|
|
109
|
+
connect: connectWalletConnect,
|
|
110
|
+
connectionURI,
|
|
111
|
+
isLoading: walletConnectLoading,
|
|
112
|
+
error: walletConnectError,
|
|
113
|
+
available: walletConnectAvailable
|
|
114
|
+
},
|
|
115
|
+
injected: {
|
|
116
|
+
connect: connectInjected,
|
|
117
|
+
isLoading: injectedLoading,
|
|
118
|
+
error: injectedError,
|
|
119
|
+
available: injectedAvailable
|
|
120
|
+
},
|
|
121
|
+
isReady
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useContext, useCallback, useState } from 'react'
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider'
|
|
3
|
+
|
|
4
|
+
interface UseDisconnectReturn {
|
|
5
|
+
disconnect: () => Promise<void>
|
|
6
|
+
isLoading: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hook for disconnecting the current wallet
|
|
11
|
+
* @returns Disconnect function and loading state
|
|
12
|
+
* @throws Error if used outside of ConnectionProvider
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const { disconnect, isLoading } = useDisconnect()
|
|
16
|
+
*
|
|
17
|
+
* const handleDisconnect = async () => {
|
|
18
|
+
* await disconnect()
|
|
19
|
+
* console.log('Wallet disconnected')
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <button onClick={handleDisconnect} disabled={isLoading}>
|
|
24
|
+
* {isLoading ? 'Disconnecting...' : 'Disconnect'}
|
|
25
|
+
* </button>
|
|
26
|
+
* )
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useDisconnect(): UseDisconnectReturn {
|
|
30
|
+
const context = useContext(ConnectionContext)
|
|
31
|
+
|
|
32
|
+
if (!context) {
|
|
33
|
+
throw new Error('useDisconnect must be used within a ConnectionProvider')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { connector } = context
|
|
37
|
+
const [isLoading, setIsLoading] = useState(false)
|
|
38
|
+
|
|
39
|
+
const disconnect = useCallback(async () => {
|
|
40
|
+
setIsLoading(true)
|
|
41
|
+
try {
|
|
42
|
+
await connector.disconnect()
|
|
43
|
+
} finally {
|
|
44
|
+
setIsLoading(false)
|
|
45
|
+
}
|
|
46
|
+
}, [connector])
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
disconnect,
|
|
50
|
+
isLoading
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useContext } from 'react'
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider'
|
|
3
|
+
import type { Network } from '@meshconnect/uwc-types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for accessing the list of configured networks
|
|
7
|
+
* @returns Array of network configurations
|
|
8
|
+
* @throws Error if used outside of ConnectionProvider
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* const networks = useNetworks()
|
|
12
|
+
*
|
|
13
|
+
* networks.forEach(network => {
|
|
14
|
+
* console.log(`${network.name}: ${network.id}`)
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function useNetworks(): Network[] {
|
|
19
|
+
const context = useContext(ConnectionContext)
|
|
20
|
+
|
|
21
|
+
if (!context) {
|
|
22
|
+
throw new Error('useNetworks must be used within a ConnectionProvider')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return context.networks
|
|
26
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useContext } from 'react'
|
|
2
|
+
import { ConnectionContext } from '../providers/ConnectionProvider'
|
|
3
|
+
import type { UseSessionReturn } from '@meshconnect/uwc-types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for accessing the current wallet session state
|
|
7
|
+
* @returns Session information including active address, network, and connection details
|
|
8
|
+
* @throws Error if used outside of ConnectionProvider
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* const { activeAddress, activeNetwork, availableNetworks } = useSession()
|
|
12
|
+
*
|
|
13
|
+
* if (activeAddress) {
|
|
14
|
+
* console.log(`Connected: ${activeAddress}`)
|
|
15
|
+
* console.log(`Network: ${activeNetwork?.name}`)
|
|
16
|
+
* console.log(`Available networks: ${availableNetworks.length}`)
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function useSession(): UseSessionReturn {
|
|
21
|
+
const context = useContext(ConnectionContext)
|
|
22
|
+
|
|
23
|
+
if (!context) {
|
|
24
|
+
throw new Error('useSession must be used within a ConnectionProvider')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { session } = context
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
session,
|
|
31
|
+
activeAddress: session.activeAddress,
|
|
32
|
+
activeNetwork: session.activeNetwork,
|
|
33
|
+
availableNetworks: session.availableNetworks,
|
|
34
|
+
activeConnector: session.connectionMode,
|
|
35
|
+
availableAddresses: session.availableAddresses
|
|
36
|
+
}
|
|
37
|
+
}
|