33// import { AddressType, InMemoryKeyAgent } from '@cardano-sdk/key-management'
44import { createWebExtHandler , webExtensionLink } from '@status-im/trpc-webext'
55import { createTRPCClient } from '@trpc/client'
6- import { initTRPC } from '@trpc/server'
6+ import { initTRPC , TRPCError } from '@trpc/server'
77import superjson from 'superjson'
88import { browser } from 'wxt/browser'
99import { z } from 'zod'
@@ -19,6 +19,7 @@ import {
1919} from './wallet'
2020import { runtimePortToClientContextType } from './webext'
2121
22+ import type { ValidPasswordContext } from './trpc/middlewares/password-auth'
2223import type { CreateWebExtContextOptions } from '@status-im/trpc-webext/adapter'
2324
2425const createContext = async ( webextOpts ?: CreateWebExtContextOptions ) => {
@@ -33,7 +34,7 @@ const createContext = async (webextOpts?: CreateWebExtContextOptions) => {
3334 }
3435}
3536
36- type Context = Awaited < ReturnType < typeof createContext > >
37+ type Context = Awaited < ReturnType < typeof createContext > > & ValidPasswordContext
3738
3839const passwordAuthPlugin = createPasswordAuthPlugin < Context > ( )
3940
@@ -46,7 +47,12 @@ const t = initTRPC.context<Context>().create({
4647 allowOutsideOfServer : true ,
4748} )
4849
49- const publicProcedure = t . procedure . concat ( passwordAuthPlugin )
50+ const trpcGlobalPlugins = [ passwordAuthPlugin ]
51+
52+ const publicProcedure = trpcGlobalPlugins . reduce (
53+ ( procedure , plugin ) => procedure . concat ( plugin ) ,
54+ t . procedure ,
55+ )
5056
5157const { createCallerFactory, router } = t
5258
@@ -74,7 +80,6 @@ const apiRouter = router({
7480 )
7581 . mutation ( async ( { input, ctx } ) => {
7682 const { walletCore, keyStore } = ctx
77- console . log ( 'ctx = ' , ctx )
7883
7984 const wallet = walletCore . HDWallet . create ( 256 , input . password )
8085 const mnemonic = wallet . mnemonic ( )
@@ -127,10 +132,16 @@ const apiRouter = router({
127132
128133 const wallet = await keyStore . load ( input . walletId )
129134
135+ if ( ! ctx . validPassword )
136+ throw new TRPCError ( {
137+ message : 'Invalid password' ,
138+ code : 'UNAUTHORIZED' ,
139+ } )
140+
130141 return {
131142 id : wallet . id ,
132143 name : wallet . name ,
133- mnemonic : await keyStore . exportMnemonic ( wallet . id , input . password ) ,
144+ mnemonic : await keyStore . exportMnemonic ( wallet . id , ctx . validPassword ) ,
134145 }
135146 } ) ,
136147
@@ -213,18 +224,24 @@ const apiRouter = router({
213224 . mutation ( async ( { input, ctx } ) => {
214225 const { keyStore, walletCore } = ctx
215226
227+ if ( ! ctx . validPassword )
228+ throw new TRPCError ( {
229+ message : 'Invalid password' ,
230+ code : 'UNAUTHORIZED' ,
231+ } )
232+
216233 const wallet = await keyStore . load ( input . walletId )
217234
218235 // todo!: test calling multiple times
219236 // const { id } = await keyStore.addAccounts(
220237 // wallet.id,
221- // input.password ,
238+ // ctx.validPassword ,
222239 // [walletCore.CoinType.ethereum],
223240 // )
224241
225242 const { id } = await keyStore . addAccountsWithDerivations (
226243 wallet . id ,
227- input . password ,
244+ ctx . validPassword ,
228245 [
229246 {
230247 // coin: wallet.activeAccounts[0].coin,
@@ -237,7 +254,7 @@ const apiRouter = router({
237254 // note: add account with custom derivation path
238255 // const mnemonic = (await keyStore.export(
239256 // wallet.id,
240- // input.password ,
257+ // ctx.validPassword ,
241258 // )) as string
242259 // // fixme: calculate index based on last account
243260 // const index = 0
@@ -246,19 +263,19 @@ const apiRouter = router({
246263 // const key = walletCore.StoredKey.importHDWallet(
247264 // mnemonic,
248265 // input.name,
249- // Buffer.from(input.password ),
266+ // Buffer.from(ctx.validPassword ),
250267 // walletCore.CoinType.ethereum,
251268 // )
252269
253270 // const privateKey = key
254- // .wallet(Buffer.from(input.password ))
271+ // .wallet(Buffer.from(ctx.validPassword ))
255272 // .getKey(walletCore.CoinType.ethereum, derivationPath)
256273
257274 // // note!: would be categorized separatley from mnemonic wallet and as as private key, so if used instead of adding accounts add private keys from the start
258275 // const { id } = await keyStore.importKey(
259276 // privateKey.data(),
260277 // 'untitled',
261- // input.password ,
278+ // ctx.validPassword ,
262279 // walletCore.CoinType.ethereum,
263280 // walletCore.StoredKeyEncryption.aes256Ctr,
264281 // )
@@ -293,25 +310,31 @@ const apiRouter = router({
293310 throw new Error ( 'From address not found' )
294311 }
295312
313+ if ( ! ctx . validPassword )
314+ throw new TRPCError ( {
315+ message : 'Invalid password' ,
316+ code : 'UNAUTHORIZED' ,
317+ } )
318+
296319 // const mnemonic = (await keyStore.export(
297320 // wallet.id,
298- // input.password ,
321+ // ctx.validPassword ,
299322 // )) as string
300323
301324 // const key = walletCore.StoredKey.importHDWallet(
302325 // mnemonic,
303326 // wallet.name,
304- // Buffer.from(input.password ),
327+ // Buffer.from(ctx.validPassword ),
305328 // walletCore.CoinType.ethereum,
306329 // )
307330
308331 // const privateKey = key
309- // .wallet(Buffer.from(input.password ))
332+ // .wallet(Buffer.from(ctx.validPassword ))
310333 // .getKey(walletCore.CoinType.ethereum, account.derivationPath)
311334
312335 const privateKey = await keyStore . getKey (
313336 wallet . id ,
314- input . password ,
337+ ctx . validPassword ,
315338 account ,
316339 )
317340
@@ -344,9 +367,15 @@ const apiRouter = router({
344367
345368 const wallet = await keyStore . load ( input . walletId )
346369
370+ if ( ! ctx . validPassword )
371+ throw new TRPCError ( {
372+ message : 'Invalid password' ,
373+ code : 'UNAUTHORIZED' ,
374+ } )
375+
347376 const { id } = await keyStore . addAccountsWithDerivations (
348377 wallet . id ,
349- input . password ,
378+ ctx . validPassword ,
350379 [
351380 {
352381 coin : walletCore . CoinType . bitcoin ,
@@ -375,7 +404,7 @@ const apiRouter = router({
375404 // note!: second default derivation; does not add new account
376405 // await keyStore.addAccountsWithDerivations(
377406 // wallet.id,
378- // input.password ,
407+ // ctx.validPassword ,
379408 // [
380409 // {
381410 // coin: walletCore.CoinType.bitcoin,
@@ -413,9 +442,15 @@ const apiRouter = router({
413442 throw new Error ( 'From address not found' )
414443 }
415444
445+ if ( ! ctx . validPassword )
446+ throw new TRPCError ( {
447+ message : 'Invalid password' ,
448+ code : 'UNAUTHORIZED' ,
449+ } )
450+
416451 const privateKey = await keyStore . getKey (
417452 wallet . id ,
418- input . password ,
453+ ctx . validPassword ,
419454 account ,
420455 )
421456
@@ -446,9 +481,15 @@ const apiRouter = router({
446481
447482 const wallet = await keyStore . load ( input . walletId )
448483
484+ if ( ! ctx . validPassword )
485+ throw new TRPCError ( {
486+ message : 'Invalid password' ,
487+ code : 'UNAUTHORIZED' ,
488+ } )
489+
449490 const { id } = await keyStore . addAccounts (
450491 wallet . id ,
451- input . password ,
492+ ctx . validPassword ,
452493 [ walletCore . CoinType . solana ] ,
453494 )
454495
@@ -481,9 +522,15 @@ const apiRouter = router({
481522 throw new Error ( 'From address not found' )
482523 }
483524
525+ if ( ! ctx . validPassword )
526+ throw new TRPCError ( {
527+ message : 'Invalid password' ,
528+ code : 'UNAUTHORIZED' ,
529+ } )
530+
484531 const privateKey = await keyStore . getKey (
485532 wallet . id ,
486- input . password ,
533+ ctx . validPassword ,
487534 account ,
488535 )
489536
@@ -514,9 +561,15 @@ const apiRouter = router({
514561
515562 const wallet = await keyStore . load ( input . walletId )
516563
564+ if ( ! ctx . validPassword )
565+ throw new TRPCError ( {
566+ message : 'Invalid password' ,
567+ code : 'UNAUTHORIZED' ,
568+ } )
569+
517570 const { id } = await keyStore . addAccounts (
518571 wallet . id ,
519- input . password ,
572+ ctx . validPassword ,
520573 [ walletCore . CoinType . cardano ] ,
521574 )
522575
0 commit comments