UNPKG

@treasure-dev/launcher

Version:

Treasure Launcher utilities for the Treasure ecosystem

118 lines (116 loc) 3.08 kB
// src/utils.ts function getTreasureLauncherAuthToken() { let args; if (typeof process !== "undefined" && Array.isArray(process.argv)) { args = process.argv; } else if (typeof window !== "undefined" && window.process && Array.isArray(window.process.argv)) { args = window.process.argv; } else { return void 0; } const arg = args.find((arg2) => arg2.startsWith("--tdk-auth-token=")); return arg ? arg.split("=")[1] : void 0; } function isUsingTreasureLauncher() { return !!getTreasureLauncherAuthToken(); } function getTreasureLauncherWalletComponents() { let args; if (typeof process !== "undefined" && Array.isArray(process.argv)) { args = process.argv; } else if (typeof window !== "undefined" && window.process && Array.isArray(window.process.argv)) { args = window.process.argv; } else { return void 0; } let walletId = args.find( (arg) => arg.startsWith("--tdk-wallet-id=") ); if (walletId) { walletId = walletId.split("=")[1]; } let authProvider = args.find( (arg) => arg.startsWith("--tdk-auth-provider=") ); if (authProvider) { authProvider = authProvider.split("=")[1]; } let authCookie = args.find( (arg) => arg.startsWith("--tdk-auth-cookie=") ); if (authCookie) { authCookie = authCookie.split("=")[1]; } if (!walletId || !authProvider || !authCookie) { return void 0; } return { walletId, authProvider, authCookie }; } function getTreasureLauncherPort() { let args; if (typeof process !== "undefined" && Array.isArray(process.argv)) { args = process.argv; } else if (typeof window !== "undefined" && window.process && Array.isArray(window.process.argv)) { args = window.process.argv; } else { return 16001; } let serverPort = args.find( (arg) => arg.startsWith("--server-port=") ); if (serverPort) { serverPort = serverPort.split("=")[1]; } if (!serverPort) { return 16001; } return Number.parseInt(serverPort, 10); } // src/session.ts async function startUserSessionViaLauncher({ sessionOptions, getPort }) { if (!isUsingTreasureLauncher()) { return Promise.reject( new Error( "startUserSessionViaLauncher can only be used with Treasure Launcher" ) ); } const { backendWallet, approvedTargets, nativeTokenLimitPerTransaction, sessionDurationSec, sessionMinDurationLeftSec } = sessionOptions; const port = getPort?.() ?? getTreasureLauncherPort(); const response = await fetch(`http://localhost:${port}/tdk-start-session`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ backendWallet, approvedTargets, nativeTokenLimitPerTransaction, sessionDurationSec, sessionMinDurationLeftSec }) }); if (!response.ok) { throw new Error("Failed to start session"); } } export { getTreasureLauncherAuthToken, getTreasureLauncherPort, getTreasureLauncherWalletComponents, isUsingTreasureLauncher, startUserSessionViaLauncher };