Skip to content

Commit eef3c00

Browse files
committed
feat: add @status-im/trpc-webext
1 parent aca00b0 commit eef3c00

File tree

16 files changed

+596
-73
lines changed

16 files changed

+596
-73
lines changed

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
layout node

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ web-build/
104104

105105
# Contentlayer
106106
.contentlayer
107+
108+
/.lsp/

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"./packages/components",
99
"./packages/status-js",
1010
"./packages/wallet",
11+
"./packages/trpc-webext",
1112
"./apps/connector",
1213
"./apps/portfolio",
1314
"./apps/wallet",

apps/api/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
},
1717
"dependencies": {
1818
"@status-im/wallet": "workspace:*",
19-
"@trpc/client": "10.45.2",
20-
"@trpc/server": "10.45.2",
21-
"@trpc/next": "10.45.2",
19+
"@trpc/client": "11.3.0",
20+
"@trpc/server": "11.3.0",
21+
"@trpc/next": "11.3.0",
2222
"react": "^19.0.0",
2323
"react-dom": "^19.0.0",
2424
"next": "15.3.0",

apps/wallet/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
"@status-im/components": "workspace:*",
3636
"@status-im/icons": "workspace:*",
3737
"@status-im/wallet": "workspace:*",
38+
"@status-im/trpc-webext": "workspace:*",
3839
"@tanstack/react-query": "^5.66.0",
3940
"@tanstack/react-query-devtools": "^5.66.0",
4041
"@tanstack/react-router": "^1.109.2",
41-
"@trpc/client": "10.45.2",
42-
"@trpc/server": "10.45.2",
42+
"@trpc/client": "11.3.0",
43+
"@trpc/server": "11.3.0",
4344
"@trustwallet/wallet-core": "^4.2.10",
4445
"@wxt-dev/storage": "^1.1.0",
4546
"bitcoinjs-lib": "^6.1.7",

apps/wallet/src/data/api.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// import { Cardano } from '@cardano-sdk/core'
22
// import { SodiumBip32Ed25519 } from '@cardano-sdk/crypto'
33
// import { AddressType, InMemoryKeyAgent } from '@cardano-sdk/key-management'
4-
import { createTRPCProxyClient } from '@trpc/client'
4+
import { createWebExtHandler, webExtensionLink } from '@status-im/trpc-webext'
5+
import { createTRPCClient } from '@trpc/client'
56
import { initTRPC } from '@trpc/server'
67
import superjson from 'superjson'
7-
import { createChromeHandler } from 'trpc-chrome/adapter'
8-
import { chromeLink } from 'trpc-chrome/link'
8+
import { browser } from 'wxt/browser'
99
import { z } from 'zod'
1010

1111
import * as bitcoin from './bitcoin/bitcoin'
@@ -16,12 +16,17 @@ import {
1616
getWalletCore,
1717
// type WalletCore
1818
} from './wallet'
19+
import { runtimePortToClientContextType } from './webext'
1920

20-
const createContext = async () => {
21+
import type { CreateWebExtContextOptions } from '@status-im/trpc-webext/adapter'
22+
23+
const createContext = async (webextOpts?: CreateWebExtContextOptions) => {
2124
const keyStore = await getKeystore()
2225
const walletCore = await getWalletCore()
2326

2427
return {
28+
...webextOpts,
29+
contextType: runtimePortToClientContextType(webextOpts?.req),
2530
keyStore,
2631
walletCore,
2732
}
@@ -591,8 +596,11 @@ const apiRouter = router({
591596
export type APIRouter = typeof apiRouter
592597

593598
export async function createAPI() {
594-
// @ts-expect-error: fixme!:
595-
createChromeHandler({ router: apiRouter, createContext })
599+
createWebExtHandler({
600+
router: apiRouter,
601+
createContext,
602+
runtime: browser.runtime,
603+
})
596604

597605
const ctx = await createContext()
598606
const api = createCallerFactory(apiRouter)(ctx)
@@ -601,10 +609,13 @@ export async function createAPI() {
601609
}
602610

603611
export function createAPIClient() {
604-
const port = chrome.runtime.connect()
605-
606-
return createTRPCProxyClient<APIRouter>({
607-
links: [chromeLink({ port })],
608-
transformer: superjson,
612+
return createTRPCClient<APIRouter>({
613+
links: [
614+
webExtensionLink({
615+
runtime: browser.runtime,
616+
timeoutMS: 30000,
617+
transformer: superjson,
618+
}),
619+
],
609620
})
610621
}

apps/wallet/src/data/webext.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Runtime } from 'wxt/browser'
2+
3+
export type TRPCClientContextType = 'POPUP' | 'SIDE_PANEL' | 'PAGE' | 'TAB'
4+
5+
export function runtimePortToClientContextType(
6+
port?: Runtime.Port,
7+
): TRPCClientContextType | undefined {
8+
const { origin } = globalThis.location
9+
if (!port) return
10+
if (port.sender?.url?.startsWith(`${origin}/sidepanel.html`))
11+
return 'SIDE_PANEL'
12+
if (port.sender?.url?.startsWith(`${origin}/popup.html`)) return 'POPUP'
13+
if (port.sender?.url?.startsWith(`${origin}/page.html`)) return 'PAGE'
14+
return 'TAB'
15+
}

packages/trpc-webext/.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"arrowParens": "avoid",
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import configs from '@status-im/eslint-config'
2+
3+
/** @type {import('eslint').Linter.Config[]} */
4+
export default [
5+
...configs,
6+
{
7+
files: ['**/*.ts', '**/*.mts', '**/*.mjs', '**/*.tsx'],
8+
},
9+
]

packages/trpc-webext/package.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@status-im/trpc-webext",
3+
"description": "description",
4+
"version": "0.0.1",
5+
"license": "MIT",
6+
"keywords": [
7+
"trpc",
8+
"extension",
9+
"webext",
10+
"webextension"
11+
],
12+
"main": "./dist/index.js",
13+
"module": "./dist/index.js",
14+
"types": "./dist/index.d.ts",
15+
"exports": {
16+
".": {
17+
"types": "./dist/index.d.ts",
18+
"import": "./dist/index.js",
19+
"require": "./dist/index.js"
20+
},
21+
"./adapter": {
22+
"types": "./dist/adapter/index.d.ts",
23+
"import": "./dist/adapter/index.js",
24+
"require": "./dist/adapter/index.js"
25+
},
26+
"./link": {
27+
"types": "./dist/link/index.d.ts",
28+
"import": "./dist/link/index.js",
29+
"require": "./dist/link/index.js"
30+
}
31+
},
32+
"files": [
33+
"dist"
34+
],
35+
"scripts": {
36+
"preinstall": "npx only-allow pnpm",
37+
"dev": "tsc -w",
38+
"build": "tsc",
39+
"lint": "eslint src",
40+
"format": "prettier --write ."
41+
},
42+
"peerDependencies": {
43+
"@trpc/client": "^11.0.0",
44+
"@trpc/server": "^11.0.0"
45+
},
46+
"devDependencies": {
47+
"@types/webextension-polyfill": "^0.12.3"
48+
}
49+
}

0 commit comments

Comments
 (0)