Skip to content

Commit 7ef9a60

Browse files
authored
Desktop Launcher Connect support (#132)
* Started launcher package * Use launcher package in react package * prevent useConnect when using launcher * fixing launcher package * set auth token and user with launcher token * README * package fix * improve search function * works in electron * README * refactoring getAuthTokenOverride * moving launcher code from treasure.tsx to useLauncher.tsx * moving everything into useLauncher * fix * Add setup for adding a modal for launcher account * Launcher account modal * Use electron icon, remove log * Update README * make changes * remove TODO * Added changeset * Address additional changes * memoize onAuthTokenUpdated
1 parent d468a1b commit 7ef9a60

File tree

20 files changed

+497
-14
lines changed

20 files changed

+497
-14
lines changed

.changeset/large-ears-rhyme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@treasure-dev/launcher": patch
3+
"@treasure-dev/tdk-react": patch
4+
---
5+
6+
Added @treasure-dev/launcher package for usage with the launcher

examples/connect-electron/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ VITE_TDK_CLIENT_ID=
33
VITE_TDK_ECOSYSTEM_ID=ecosystem.treasure
44
VITE_TDK_ECOSYSTEM_PARTNER_ID=
55
VITE_TDK_BACKEND_WALLET=
6+
VITE_TDK_AUTH_TOKEN=

examples/connect-electron/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
1010
"typecheck": "pnpm typecheck:node && pnpm typecheck:web",
1111
"start": "pnpm install:electron && electron-vite preview",
12-
"dev": "pnpm install:electron && electron-vite dev",
12+
"dev:vite": "electron-vite dev -- --tdk-auth-token=$VITE_TDK_AUTH_TOKEN",
13+
"dev": "pnpm install:electron && dotenv -e .env -- pnpm dev:vite",
1314
"build": "pnpm typecheck && electron-vite build",
1415
"build:unpack": "pnpm build && electron-builder --dir",
1516
"build:win": "pnpm build && electron-builder --win",
@@ -28,6 +29,7 @@
2829
"@types/react": "catalog:",
2930
"@types/react-dom": "catalog:",
3031
"@vitejs/plugin-react": "catalog:",
32+
"dotenv-cli": "catalog:",
3133
"electron": "catalog:",
3234
"electron-builder": "catalog:",
3335
"electron-vite": "catalog:",

examples/connect-electron/src/main/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from "node:path";
22
import { electronApp, is, optimizer } from "@electron-toolkit/utils";
3+
import { getTreasureLauncherAuthToken } from "@treasure-dev/launcher";
34
import { BrowserWindow, app, ipcMain, shell } from "electron";
45
import icon from "../../resources/icon.png?asset";
56
import { startRedirectApp } from "./app";
@@ -66,6 +67,10 @@ app.whenReady().then(() => {
6667
startRedirectApp(mainWindow);
6768
});
6869

70+
ipcMain.on("get-auth-token", (event, _arg) => {
71+
event.returnValue = getTreasureLauncherAuthToken();
72+
});
73+
6974
// Quit when all windows are closed, except on macOS. There, it's common
7075
// for applications and their menu bar to stay active until the user quits
7176
// explicitly with Cmd + Q.

examples/connect-electron/src/renderer/src/main.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import { ThirdwebProvider } from "thirdweb/react";
88

99
import { toWei } from "thirdweb";
1010
import App from "./App";
11+
import icon from "./assets/electron.svg";
12+
13+
const getAuthToken = () =>
14+
window.electron.ipcRenderer.sendSync("get-auth-token");
1115

1216
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
1317
<React.StrictMode>
1418
<ThirdwebProvider>
1519
<TreasureProvider
1620
appName="Electron App"
21+
appIconUri={icon}
1722
apiUri={import.meta.env.VITE_TDK_API_URL}
1823
defaultChainId={421614}
1924
clientId={import.meta.env.VITE_TDK_CLIENT_ID}
@@ -29,6 +34,9 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
2934
],
3035
nativeTokenLimitPerTransaction: toWei("1"),
3136
}}
37+
launcherOptions={{
38+
getAuthTokenOverride: getAuthToken,
39+
}}
3240
>
3341
<App />
3442
</TreasureProvider>

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
"private": true,
33
"type": "module",
44
"scripts": {
5-
"build": "pnpm build:auth && pnpm build:core && pnpm build:react && pnpm build:api",
5+
"build": "pnpm build:auth && pnpm build:core && pnpm build:launcher && pnpm build:react && pnpm build:api",
66
"build:auth": "pnpm --filter ./packages/auth build",
77
"build:core": "pnpm --filter ./packages/core build",
8+
"build:launcher": "pnpm --filter ./packages/launcher build",
89
"build:react": "pnpm --filter ./packages/react build",
910
"build:api": "pnpm --filter ./apps/api build",
1011
"generate": "pnpm -r generate",
11-
"dev": "concurrently \"pnpm dev:auth\" \"pnpm dev:core\" \"pnpm dev:api\" \"pnpm dev:react\"",
12+
"dev": "concurrently \"pnpm dev:auth\" \"pnpm dev:core\" \"pnpm dev:launcher\" \"pnpm dev:api\" \"pnpm dev:react\"",
1213
"dev:auth": "pnpm --filter ./packages/auth dev",
1314
"dev:core": "pnpm --filter ./packages/core dev",
15+
"dev:launcher": "pnpm --filter ./packages/launcher dev",
1416
"dev:api": "pnpm --filter ./apps/api dev",
1517
"dev:react": "pnpm --filter ./packages/react dev",
1618
"dev:connect-electron": "pnpm --filter ./examples/connect-electron dev",
@@ -23,7 +25,7 @@
2325
"test": "TZ=UTC vitest run",
2426
"package:changeset": "changeset",
2527
"package:version": "changeset version",
26-
"package:publish": "pnpm build:auth && pnpm build:core && pnpm build:react && changeset publish",
28+
"package:publish": "pnpm build:auth && pnpm build:core && pnpm build:launcher && pnpm build:react && changeset publish",
2729
"prepare": "husky"
2830
},
2931
"devDependencies": {

packages/launcher/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Treasure Launcher
2+
3+
Treasure Launcher utilities for the Treasure ecosystem
4+
5+
## Prerequisites
6+
7+
- [>= Node 20.11.0](https://nodejs.org/en)
8+
9+
## Installation
10+
11+
```bash
12+
pnpm add @treasure-dev/launcher
13+
```
14+
15+
## Usage
16+
17+
### Using vanilla JavaScript
18+
19+
#### Get auth token
20+
21+
```ts
22+
import { getTreasureLauncherAuthToken } from "@treasure-dev/launcher";
23+
24+
const authToken = getTreasureLauncherAuthToken();
25+
```
26+
27+
#### Check if using Treasure Launcher
28+
29+
```ts
30+
import { isUsingTreasureLauncher } from "@treasure-dev/launcher";
31+
32+
const usingLauncher = isUsingTreasureLauncher();
33+
```
34+
35+
#### Start user session
36+
37+
```ts
38+
import { startUserSessionViaLauncher } from "@treasure-dev/launcher";
39+
40+
await startUserSessionViaLauncher({
41+
backendWallet: "0x...",
42+
approvedTargets: ["0x..."],
43+
nativeTokenLimitPerTransaction: 100,
44+
sessionDurationSec: 60 * 60 * 24 * 7,
45+
sessionMinDurationLeftSec: 60 * 60 * 24 * 3,
46+
});
47+
```
48+
49+
### Using React
50+
51+
#### Check if using Treasure Launcher
52+
53+
```ts
54+
import { useTreasure } from "@treasure-dev/react";
55+
56+
const { isUsingTreasureLauncher } = useTreasure();
57+
```
58+
59+
#### Start user session via Treasure Launcher
60+
61+
Use `startUserSession` as usual:
62+
63+
```ts
64+
import { useTreasure } from "@treasure-dev/react";
65+
66+
const { startUserSession } = useTreasure();
67+
```
68+
69+
### Using Electron + React
70+
71+
```ts Main.ts
72+
ipcMain.on("get-auth-token", (event, _arg) => {
73+
event.returnValue = getTreasureLauncherAuthToken();
74+
});
75+
```
76+
77+
```ts Main.tsx
78+
const getAuthToken = () =>
79+
window.electron.ipcRenderer.sendSync("get-auth-token");
80+
81+
<TreasureProvider
82+
...
83+
getAuthTokenOverride={getAuthToken}
84+
>
85+
```
86+
87+
## Deployment
88+
89+
Merge [changeset-bot](https://github.com/apps/changeset-bot)'s versioning PR to the `main` branch to push a new package version to npm.

packages/launcher/package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@treasure-dev/launcher",
3+
"version": "1.0.0",
4+
"description": "Treasure Launcher utilities for the Treasure ecosystem",
5+
"keywords": [
6+
"treasure"
7+
],
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/TreasureProject/tdk-js.git",
11+
"directory": "packages/launcher"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/TreasureProject/tdk-js/issues",
15+
"email": "[email protected]"
16+
},
17+
"main": "./dist/index.js",
18+
"module": "./dist/index.mjs",
19+
"types": "./dist/index.d.ts",
20+
"exports": {
21+
".": {
22+
"require": "./dist/index.js",
23+
"import": "./dist/index.mjs",
24+
"types": "./dist/index.d.ts"
25+
}
26+
},
27+
"files": [
28+
"./dist"
29+
],
30+
"scripts": {
31+
"typecheck": "tsc --noEmit",
32+
"build": "pnpm typecheck && tsup",
33+
"dev": "pnpm build --watch"
34+
},
35+
"dependencies": {
36+
"@treasure-dev/tdk-core": "*"
37+
},
38+
"engines": {
39+
"node": ">=14.0.0"
40+
}
41+
}

packages/launcher/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { getTreasureLauncherAuthToken, isUsingTreasureLauncher } from "./utils";
2+
export { startUserSessionViaLauncher } from "./session";

packages/launcher/src/session.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { SessionOptions } from "@treasure-dev/tdk-core";
2+
import { isUsingTreasureLauncher } from "./utils";
3+
4+
export function startUserSessionViaLauncher({
5+
backendWallet,
6+
approvedTargets,
7+
nativeTokenLimitPerTransaction,
8+
sessionDurationSec,
9+
sessionMinDurationLeftSec,
10+
}: SessionOptions): Promise<void> {
11+
if (!isUsingTreasureLauncher()) {
12+
return Promise.reject(
13+
new Error(
14+
"startUserSessionViaLauncher can only be used with Treasure Launcher",
15+
),
16+
);
17+
}
18+
19+
return fetch("http://localhost:16001/tdk-start-session", {
20+
method: "POST",
21+
headers: {
22+
"Content-Type": "application/json",
23+
},
24+
body: JSON.stringify({
25+
backendWallet,
26+
approvedTargets,
27+
nativeTokenLimitPerTransaction,
28+
sessionDurationSec,
29+
sessionMinDurationLeftSec,
30+
}),
31+
}).then((response) => {
32+
if (!response.ok) {
33+
throw new Error("Failed to start session");
34+
}
35+
});
36+
}

0 commit comments

Comments
 (0)