import { ChipiBrowserSDK } from "@chipi-stack/backend";
const browserClient = new ChipiBrowserSDK({
apiPublicKey: process.env.VITE_CHIPI_PUBLIC_KEY, // or your framework's env var
});
async function getUserWallet(userId: string) {
try {
const bearerToken = await getBearerToken(); // Your auth implementation
const wallet = await browserClient.getWallet({
externalUserId: userId,
bearerToken: bearerToken,
});
console.log('Wallet found:');
console.log('Address:', wallet.publicKey);
return wallet;
} catch (error) {
if (error.message.includes('not found')) {
console.log('No wallet found for user:', userId);
return null;
}
console.error('Error retrieving wallet:', error);
throw error;
}
}
// Usage example
async function checkUserWallet(userId: string) {
const wallet = await getUserWallet(userId);
if (wallet) {
console.log(`User ${userId} has wallet: ${wallet.publicKey}`);
return wallet;
} else {
console.log(`User ${userId} needs to create a wallet`);
return null;
}
}