管理权限和会话密钥以确保 B3 Global Account 交互的安全性
interface Permissions {
approvedTargets: string[]; // 合约地址
startDate: Date; // 权限激活时间
endDate: Date; // 权限过期时间
nativeTokenLimitPerTransaction: number; // 每笔交易的 ETH 限额
// 可以添加额外的 ERC-20 代币限额
}
import { RequestPermissionsButton } from "@b3dotfun/sdk/global-account/react";
const b3Chain = {
id: 8333,
name: "B3",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpc: "https://mainnet-rpc.b3.fun",
};
function BasicPermissions() {
const permissions = {
approvedTargets: [
"0x9c275ff1634519E9B5449ec79cd939B5F900564d", // 你的游戏合约
],
startDate: new Date(),
endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 天
nativeTokenLimitPerTransaction: 0.01, // 每笔交易 0.01 ETH
};
return (
<RequestPermissionsButton
chain={b3Chain}
sessionKeyAddress="0x..." // MetaMask 或钱包地址
permissions={permissions}
onSuccess={() => {
console.log("权限成功授予!");
}}
onError={(error) => {
console.error("权限请求失败:", error);
}}
/>
);
}
function GamePermissions() {
const gamePermissions = {
approvedTargets: [
"0x...", // 游戏合约
"0x...", // NFT 市场
"0x...", // 代币合约
],
startDate: new Date(),
endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 天
nativeTokenLimitPerTransaction: 0.001, // 小额 ETH 限制以支付燃气费
};
return (
<div className="game-permissions">
<h3>授予游戏权限</h3>
<p>允许此游戏代表您执行操作,有效期为 7 天</p>
<RequestPermissionsButton
chain={b3Chain}
sessionKeyAddress="0x..."
permissions={gamePermissions}
onSuccess={() => {
// 重定向到游戏或更新 UI
console.log("游戏权限授予成功!");
}}
onError={(error) => {
console.error("授予游戏权限失败:", error);
}}
/>
</div>
);
}
import { useRequestPermissions } from "@b3dotfun/sdk/global-account/react";
function CustomPermissionFlow() {
const { requestPermissions, isLoading, error } = useRequestPermissions();
const handlePermissionRequest = async () => {
try {
const result = await requestPermissions({
chain: b3Chain,
sessionKeyAddress: "0x...",
permissions: {
approvedTargets: ["0x..."],
startDate: new Date(),
endDate: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 小时
nativeTokenLimitPerTransaction: 0.005,
},
});
if (result.success) {
console.log("权限成功授予:", result.data);
}
} catch (err) {
console.error("权限请求失败:", err);
}
};
return (
<div>
<button
onClick={handlePermissionRequest}
disabled={isLoading}
>
{isLoading ? "正在请求权限..." : "授予权限"}
</button>
{error && (
<div className="error">
错误:{error.message}
</div>
)}
</div>
);
}
import { usePermissions } from "@b3dotfun/sdk/global-account/react";
function PermissionStatus() {
const { permissions, hasPermissions, isExpired } = usePermissions();
return (
<div className="permission-status">
<h3>权限状态</h3>
{hasPermissions ? (
<div className="permissions-active">
<p>✅ 权限激活</p>
<p>过期时间:{permissions?.endDate.toLocaleDateString()}</p>
<p>已批准的合约:{permissions?.approvedTargets.length}</p>
{isExpired && (
<p className="warning">⚠️ 权限已过期</p>
)}
</div>
) : (
<div className="no-permissions">
<p>❌ 无活动权限</p>
<RequestPermissionsButton {...permissionConfig} />
</div>
)}
</div>
);
}
import { useRevokePermissions } from "@b3dotfun/sdk/global-account/react";
function RevokePermissions() {
const { revokePermissions, isLoading } = useRevokePermissions();
const handleRevoke = async () => {
try {
await revokePermissions({
sessionKeyAddress: "0x...",
chain: b3Chain,
});
console.log("权限成功撤销");
} catch (error) {
console.error("撤销权限失败:", error);
}
};
return (
<button
onClick={handleRevoke}
disabled={isLoading}
className="revoke-button"
>
{isLoading ? "正在撤销..." : "撤销权限"}
</button>
);
}
function DynamicPermissions() {
const [permissionLevel, setPermissionLevel] = useState<'basic' | 'advanced'>('basic');
const getPermissions = (level: string) => {
const basePermissions = {
startDate: new Date(),
endDate: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 小时
};
if (level === 'basic') {
return {
...basePermissions,
approvedTargets: ["0x..."], // 限制合约
nativeTokenLimitPerTransaction: 0.001,
};
} else {
return {
...basePermissions,
approvedTargets: ["0x...", "0x...", "0x..."], // 更多合约
nativeTokenLimitPerTransaction: 0.01,
};
}
};
return (
<div>
<div className="permission-selector">
<button
onClick={() => setPermissionLevel('basic')}
className={permissionLevel === 'basic' ? 'active' : ''}
>
基本权限
</button>
<button
onClick={() => setPermissionLevel('advanced')}
className={permissionLevel === 'advanced' ? 'active' : ''}
>
高级权限
</button>
</div>
<RequestPermissionsButton
chain={b3Chain}
sessionKeyAddress="0x..."
permissions={getPermissions(permissionLevel)}
onSuccess={() => console.log(`${permissionLevel} 权限授予成功`)}
/>
</div>
);
}
function PermissionErrorHandling() {
const handlePermissionError = (error: Error) => {
switch (error.message) {
case 'USER_REJECTED':
console.log('用户拒绝权限请求');
break;
case 'INSUFFICIENT_PERMISSIONS':
console.log('请求的权限超出限制');
break;
case 'EXPIRED_SESSION':
console.log('会话密钥已过期');
// 请求新权限
break;
default:
console.error('未知权限错误:', error);
}
};
return (
<RequestPermissionsButton
// ... 其他属性
onError={handlePermissionError}
/>
);
}