@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
53 lines (50 loc) • 2.23 kB
JavaScript
import { createSolanaRpc } from '@solana/kit';
import { SOLANA_METHOD_TYPES } from '@web3auth/ws-embed';
import { useMemo, useCallback } from 'react';
import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
import { WALLET_CONNECTORS } from '../../../base/wallet/index.js';
import { useChain } from '../../hooks/useChain.js';
import { useWeb3Auth } from '../../hooks/useWeb3Auth.js';
const useSolanaWallet = () => {
const {
connection,
web3Auth
} = useWeb3Auth();
const {
chainNamespace
} = useChain();
const solanaWallet = useMemo(() => {
var _connection$solanaWal;
return (_connection$solanaWal = connection === null || connection === void 0 ? void 0 : connection.solanaWallet) !== null && _connection$solanaWal !== void 0 ? _connection$solanaWal : null;
}, [connection]);
const accounts = useMemo(() => {
if (chainNamespace !== CHAIN_NAMESPACES.SOLANA || !solanaWallet) return null;
const accts = solanaWallet.accounts.map(a => a.address);
return accts.length > 0 ? accts : null;
}, [solanaWallet, chainNamespace]);
const rpc = useMemo(() => {
if (!web3Auth || !solanaWallet || chainNamespace !== CHAIN_NAMESPACES.SOLANA) return null;
return createSolanaRpc(web3Auth.currentChain.rpcTarget);
}, [web3Auth, solanaWallet, chainNamespace]);
const getPrivateKey = useCallback(async () => {
var _web3Auth$primaryConn;
if (!web3Auth) throw new Error("Web3Auth not initialized");
if ((connection === null || connection === void 0 ? void 0 : connection.connectorName) !== WALLET_CONNECTORS.AUTH) {
throw new Error("getPrivateKey is only supported with the Auth connector");
}
const provider = (_web3Auth$primaryConn = web3Auth.primaryConnector) === null || _web3Auth$primaryConn === void 0 ? void 0 : _web3Auth$primaryConn.provider;
if (!provider) throw new Error("Provider not available");
const privateKey = await provider.request({
method: SOLANA_METHOD_TYPES.SOLANA_PRIVATE_KEY
});
if (!privateKey) throw new Error("Failed to retrieve private key");
return privateKey;
}, [web3Auth, connection]);
return {
solanaWallet,
accounts,
rpc,
getPrivateKey
};
};
export { useSolanaWallet };