@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
84 lines (81 loc) • 3.88 kB
JavaScript
import { createSolanaRpc } from '@solana/kit';
import { StandardEvents } from '@wallet-standard/features';
import { SOLANA_METHOD_TYPES } from '@web3auth/ws-embed';
import { ref, shallowRef, onScopeDispose, watch } from 'vue';
import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
import { WALLET_CONNECTORS } from '../../../base/wallet/index.js';
import { useWeb3Auth } from '../../composables/useWeb3Auth.js';
import { useChain } from '../../composables/useChain.js';
const useSolanaWallet = () => {
const {
connection,
web3Auth
} = useWeb3Auth();
const {
chainNamespace
} = useChain();
const accounts = ref(null);
const solanaWallet = shallowRef(null);
const rpc = shallowRef(null);
let unsubscribeStandardEvents = null;
const teardownWalletListeners = () => {
var _unsubscribeStandardE;
(_unsubscribeStandardE = unsubscribeStandardEvents) === null || _unsubscribeStandardE === void 0 || _unsubscribeStandardE();
unsubscribeStandardEvents = null;
};
const syncAccounts = () => {
var _solanaWallet$value$a, _solanaWallet$value;
const accts = (_solanaWallet$value$a = (_solanaWallet$value = solanaWallet.value) === null || _solanaWallet$value === void 0 ? void 0 : _solanaWallet$value.accounts.map(a => a.address)) !== null && _solanaWallet$value$a !== void 0 ? _solanaWallet$value$a : [];
accounts.value = accts.length > 0 ? accts : null;
};
const setupWallet = () => {
var _connection$value$sol, _connection$value, _web3Auth$value;
teardownWalletListeners();
const wallet = (_connection$value$sol = (_connection$value = connection.value) === null || _connection$value === void 0 ? void 0 : _connection$value.solanaWallet) !== null && _connection$value$sol !== void 0 ? _connection$value$sol : null;
if (!wallet) return;
solanaWallet.value = wallet;
syncAccounts();
unsubscribeStandardEvents = wallet.features[StandardEvents].on("change", syncAccounts);
if ((_web3Auth$value = web3Auth.value) !== null && _web3Auth$value !== void 0 && (_web3Auth$value = _web3Auth$value.currentChain) !== null && _web3Auth$value !== void 0 && _web3Auth$value.rpcTarget && chainNamespace.value === CHAIN_NAMESPACES.SOLANA) {
rpc.value = createSolanaRpc(web3Auth.value.currentChain.rpcTarget);
}
};
const resetWallet = () => {
teardownWalletListeners();
solanaWallet.value = null;
accounts.value = null;
rpc.value = null;
};
onScopeDispose(teardownWalletListeners);
const getPrivateKey = async () => {
var _connection$value2, _web3Auth$value$prima;
if (!web3Auth.value) throw new Error("Web3Auth not initialized");
if (((_connection$value2 = connection.value) === null || _connection$value2 === void 0 ? void 0 : _connection$value2.connectorName) !== WALLET_CONNECTORS.AUTH) {
throw new Error("getPrivateKey is only supported with the Auth connector");
}
const provider = (_web3Auth$value$prima = web3Auth.value.primaryConnector) === null || _web3Auth$value$prima === void 0 ? void 0 : _web3Auth$value$prima.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;
};
watch([connection, chainNamespace], ([newConnection, newChainNamespace]) => {
if (!(newConnection !== null && newConnection !== void 0 && newConnection.solanaWallet)) {
if (solanaWallet.value) resetWallet();
return;
}
// setup wallet if not setup or chain namespace is changed to solana
if (!solanaWallet.value || newChainNamespace === CHAIN_NAMESPACES.SOLANA) setupWallet();
}, {
immediate: true
});
return {
solanaWallet,
accounts,
rpc,
getPrivateKey
};
};
export { useSolanaWallet };