Skip to content

Commit 387e134

Browse files
committed
feat: password auth
resolve #698
1 parent 2557d88 commit 387e134

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

apps/wallet/src/data/api.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { browser } from 'wxt/browser'
99
import { z } from 'zod'
1010

1111
import * as bitcoin from './bitcoin/bitcoin'
12-
import { chromeLinkWithRetries } from './chromeLink'
1312
import * as ethereum from './ethereum/ethereum'
1413
import { getKeystore } from './keystore'
1514
import * as solana from './solana/solana'
15+
import { createPasswordAuthPlugin } from './trpc/middlewares/password-auth'
1616
import {
1717
getWalletCore,
1818
// type WalletCore
@@ -35,6 +35,8 @@ const createContext = async (webextOpts?: CreateWebExtContextOptions) => {
3535

3636
type Context = Awaited<ReturnType<typeof createContext>>
3737

38+
const passwordAuthPlugin = createPasswordAuthPlugin<Context>()
39+
3840
/**
3941
* @see https://trpc.io/docs/server/routers#runtime-configuration
4042
*/
@@ -44,14 +46,15 @@ const t = initTRPC.context<Context>().create({
4446
allowOutsideOfServer: true,
4547
})
4648

47-
// const publicProcedure = t.procedure
49+
const publicProcedure = t.procedure.concat(passwordAuthPlugin)
50+
4851
const { createCallerFactory, router } = t
4952

5053
// todo: lock with password as trpc auth procedure
5154
// todo?: expose password in context or use other (session) token derived from it for encrypting and storing
5255
const apiRouter = router({
5356
wallet: router({
54-
all: t.procedure.query(async ({ ctx }) => {
57+
all: publicProcedure.query(async ({ ctx }) => {
5558
const { keyStore } = ctx
5659

5760
const wallets = await keyStore.loadAll()
@@ -62,7 +65,7 @@ const apiRouter = router({
6265
// todo: validation (e.g. password, mnemonic, already exists)
6366
// todo: words count option
6467
// todo: handle cancelation
65-
add: t.procedure
68+
add: publicProcedure
6669
.input(
6770
z.object({
6871
password: z.string(),
@@ -71,6 +74,7 @@ const apiRouter = router({
7174
)
7275
.mutation(async ({ input, ctx }) => {
7376
const { walletCore, keyStore } = ctx
77+
console.log('ctx = ', ctx)
7478

7579
const wallet = walletCore.HDWallet.create(128, input.password)
7680
const mnemonic = wallet.mnemonic()
@@ -111,7 +115,7 @@ const apiRouter = router({
111115
}
112116
}),
113117

114-
get: t.procedure
118+
get: publicProcedure
115119
.input(
116120
z.object({
117121
walletId: z.string(),
@@ -130,7 +134,7 @@ const apiRouter = router({
130134
}
131135
}),
132136

133-
import: t.procedure
137+
import: publicProcedure
134138
.input(
135139
z.object({
136140
mnemonic: z.string(),
@@ -183,7 +187,7 @@ const apiRouter = router({
183187
}),
184188

185189
account: router({
186-
all: t.procedure
190+
all: publicProcedure
187191
.input(
188192
z.object({
189193
walletId: z.string(),
@@ -198,7 +202,7 @@ const apiRouter = router({
198202
}),
199203

200204
ethereum: router({
201-
add: t.procedure
205+
add: publicProcedure
202206
.input(
203207
z.object({
204208
walletId: z.string(),
@@ -266,7 +270,7 @@ const apiRouter = router({
266270
}),
267271

268272
// note: our first tx https://holesky.etherscan.io/tx/0xdc2aa244933260c50e665aa816767dce6b76d5d498e6358392d5f79bfc9626d5
269-
send: t.procedure
273+
send: publicProcedure
270274
.input(
271275
z.object({
272276
walletId: z.string(),
@@ -334,7 +338,7 @@ const apiRouter = router({
334338

335339
bitcoin: router({
336340
// note?: create all variants (e.g. segwit, nested segwit, legacy, taproot) for each added account by default
337-
add: t.procedure
341+
add: publicProcedure
338342
.input(
339343
z.object({
340344
walletId: z.string(),
@@ -392,7 +396,7 @@ const apiRouter = router({
392396
}),
393397

394398
// note: our first tx https://mempool.space/testnet4/tx/4d1797f4a6e92ab5164cfa8030e5954670f162e2aae792c8d6d6a81aae32fbd4
395-
send: t.procedure
399+
send: publicProcedure
396400
.input(
397401
z.object({
398402
walletId: z.string(),
@@ -436,7 +440,7 @@ const apiRouter = router({
436440
}),
437441

438442
solana: router({
439-
add: t.procedure
443+
add: publicProcedure
440444
.input(
441445
z.object({
442446
walletId: z.string(),
@@ -460,7 +464,7 @@ const apiRouter = router({
460464
}),
461465

462466
// note: our first tx https://solscan.io/tx/LNgKUb6bewbcgVXi9NBF4qYNJC5kjMPpH5GDVZBsVXFC7MDhYtdygkuP1avq7c31bHDkr9pkKYvMSdT16mt294g?cluster=devnet
463-
send: t.procedure
467+
send: publicProcedure
464468
.input(
465469
z.object({
466470
walletId: z.string(),
@@ -504,7 +508,7 @@ const apiRouter = router({
504508
}),
505509

506510
cardano: router({
507-
add: t.procedure
511+
add: publicProcedure
508512
.input(
509513
z.object({
510514
walletId: z.string(),
@@ -558,7 +562,7 @@ const apiRouter = router({
558562
}),
559563

560564
privateKey: router({
561-
import: t.procedure
565+
import: publicProcedure
562566
.input(
563567
z.object({
564568
privateKey: z.string(),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { initTRPC } from '@trpc/server'
2+
3+
import type { KeyStore } from '@trustwallet/wallet-core'
4+
5+
export interface PasswordAuthParams {
6+
password?: string
7+
walletId?: string
8+
}
9+
10+
type Context = {
11+
keyStore: KeyStore.Default
12+
}
13+
14+
export function createPasswordAuthPlugin<TContext extends Context>() {
15+
const t = initTRPC.context<TContext>().create({
16+
isServer: false,
17+
allowOutsideOfServer: true,
18+
})
19+
20+
return t.procedure.use(async opts => {
21+
const { ctx } = opts
22+
const { keyStore } = ctx
23+
const params = (await opts.getRawInput()) as PasswordAuthParams
24+
if (
25+
typeof params?.password !== 'string' ||
26+
typeof params?.walletId !== 'string'
27+
)
28+
return opts.next()
29+
30+
let validPassword: undefined | string
31+
32+
await keyStore
33+
.export(params.walletId, params.password)
34+
.then(() => {
35+
validPassword = params.password
36+
})
37+
.catch(() => {})
38+
39+
return opts.next({ ctx: { validPassword } })
40+
})
41+
}

0 commit comments

Comments
 (0)