-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionHandler.js
More file actions
27 lines (20 loc) · 1019 Bytes
/
sessionHandler.js
File metadata and controls
27 lines (20 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ethers } from 'ethers';
export const createGameSession = async (mainSigner, sessionManagerAddress) => {
// 1. Generate an ephemeral (temporary) wallet
const ephemeralWallet = ethers.Wallet.createRandom();
// 2. Define session duration (e.g., 1 hour)
const duration = 3600;
const abi = ["function createSession(address _sessionKey, uint256 _duration) external"];
const contract = new ethers.Contract(sessionManagerAddress, abi, mainSigner);
// 3. Register the session key on-chain
const tx = await contract.createSession(ephemeralWallet.address, duration);
await tx.wait();
// 4. Store the private key in sessionStorage (temporary browser memory)
sessionStorage.setItem('fof_session_key', ephemeralWallet.privateKey);
return ephemeralWallet.address;
};
export const getSessionSigner = (provider) => {
const privKey = sessionStorage.getItem('fof_session_key');
if (!privKey) return null;
return new ethers.Wallet(privKey, provider);
};