70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
// hooks/useWallet.ts
|
|
"use client";
|
|
import { useState, useEffect } from 'react';
|
|
import { Keypair } from 'stellar-sdk'; // Assuming Stellar SDK is used
|
|
type Wallet = {
|
|
publicKey: string;
|
|
secretKey: string;
|
|
};
|
|
|
|
export const useWallet = () => {
|
|
const [publicKey, setPublicKey] = useState<string | null>(null);
|
|
const [secretKey, setSecretKey] = useState<string | null>(null);
|
|
const [isConnected, setIsConnected] = useState(false);
|
|
|
|
useEffect(() => {
|
|
// Retrieve wallet info from localStorage on load
|
|
const storedPublicKey = localStorage.getItem('stellarPublicKey');
|
|
const storedSecretKey = localStorage.getItem('stellarSecretKey');
|
|
if (storedPublicKey && storedSecretKey) {
|
|
setPublicKey(storedPublicKey);
|
|
setSecretKey(storedSecretKey);
|
|
setIsConnected(true);
|
|
}
|
|
}, []);
|
|
|
|
const connect = () => {
|
|
const keypair = Keypair.random();
|
|
const publicKey = keypair.publicKey();
|
|
const secret = keypair.secret();
|
|
|
|
setPublicKey(publicKey);
|
|
setSecretKey(secret);
|
|
setIsConnected(true);
|
|
|
|
localStorage.setItem('stellarPublicKey', publicKey);
|
|
localStorage.setItem('stellarSecretKey', secret);
|
|
};
|
|
|
|
const disconnect = () => {
|
|
setPublicKey(null);
|
|
setSecretKey(null);
|
|
setIsConnected(false);
|
|
|
|
localStorage.removeItem('stellarPublicKey');
|
|
localStorage.removeItem('stellarSecretKey');
|
|
};
|
|
|
|
const signTransaction = async (transaction: any) => {
|
|
if (!publicKey || !secretKey) {
|
|
throw new Error('Wallet not connected');
|
|
}
|
|
// Use external wallet service for signing transaction
|
|
const wallet: Wallet = { publicKey, secretKey };
|
|
return await signTransactionWithWallet(wallet, transaction);
|
|
};
|
|
|
|
return {
|
|
publicKey,
|
|
secretKey,
|
|
isConnected,
|
|
connect,
|
|
disconnect,
|
|
signTransaction,
|
|
};
|
|
};
|
|
function signTransactionWithWallet(wallet: Wallet, transaction: any) {
|
|
throw new Error('Function not implemented.');
|
|
}
|
|
|