@@ -33,17 +33,16 @@ import {
3333} from '@/wallets/client/fragments'
3434import { gql , useApolloClient , useMutation , useQuery } from '@apollo/client'
3535import { useDecryption , useEncryption , useSetKey , useWalletLoggerFactory , useWalletsUpdatedAt , WalletStatus } from '@/wallets/client/hooks'
36- import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
36+ import { useCallback , useEffect , useMemo , useState } from 'react'
3737import {
38- isEncryptedField , isTemplate , isWallet , protocolAvailable , protocolClientSchema , protocolLogName , reverseProtocolRelationName ,
39- walletLud16Domain
38+ isEncryptedField , isTemplate , isWallet , protocolAvailable , protocolLogName , reverseProtocolRelationName , walletLud16Domain
4039} from '@/wallets/lib/util'
4140import { protocolTestSendPayment } from '@/wallets/client/protocols'
4241import { timeoutSignal } from '@/lib/time'
4342import { FAST_POLL_INTERVAL_MS , WALLET_SEND_PAYMENT_TIMEOUT_MS } from '@/lib/constants'
4443import { useToast } from '@/components/toast'
4544import { useMe } from '@/components/me'
46- import { useTemplates , useWallets , useWalletsLoading } from '@/wallets/client/context'
45+ import { useTemplates , useWallets } from '@/wallets/client/context'
4746import { requestPersistentStorage } from '@/components/use-indexeddb'
4847
4948export function useWalletsQuery ( ) {
@@ -453,127 +452,10 @@ function useEncryptConfig (defaultProtocol, options = {}) {
453452 return useMemo ( ( ) => ( { encryptConfig, ready } ) , [ encryptConfig , ready ] )
454453}
455454
456- // TODO(wallet-v2): remove migration code
457- // =============================================================
458- // ****** Below is the migration code for WALLET v1 -> v2 ******
459- // remove when we can assume migration is complete (if ever)
460- // =============================================================
461-
462- export function useWalletMigrationMutation ( ) {
463- const wallets = useWallets ( )
464- const loading = useWalletsLoading ( )
465- const client = useApolloClient ( )
466- const { encryptConfig, ready } = useEncryptConfig ( )
467-
468- // XXX We use a ref for the wallets to avoid duplicate wallets
469- // Without a ref, the migrate callback would depend on the wallets and thus update every time the migration creates a wallet.
470- // This update would then cause the useEffect in wallets/client/context/hooks that triggers the migration to run again before the first migration is complete.
471- const walletsRef = useRef ( wallets )
472- useEffect ( ( ) => {
473- if ( ! loading ) walletsRef . current = wallets
474- } , [ loading ] )
475-
476- const migrate = useCallback ( async ( { name, enabled, ...configV1 } ) => {
477- const protocol = { name, send : true }
478-
479- const configV2 = migrateConfig ( protocol , configV1 )
480-
481- const isSameProtocol = ( p ) => {
482- const sameName = p . name === protocol . name
483- const sameSend = p . send === protocol . send
484- const sameConfig = Object . keys ( p . config )
485- . filter ( k => ! [ '__typename' , 'id' ] . includes ( k ) )
486- . every ( k => p . config [ k ] === configV2 [ k ] )
487- return sameName && sameSend && sameConfig
488- }
489-
490- const exists = walletsRef . current . some ( w => w . name === name && w . protocols . some ( isSameProtocol ) )
491- if ( exists ) return
492-
493- const schema = protocolClientSchema ( protocol )
494- await schema . validate ( configV2 )
495-
496- const encrypted = await encryptConfig ( configV2 , { protocol } )
497-
498- // decide if we create a new wallet (templateName) or use an existing one (walletId)
499- const templateName = getWalletTemplateName ( protocol )
500- let walletId
501- const wallet = walletsRef . current . find ( w =>
502- w . name === name && ! w . protocols . some ( p => p . name === protocol . name && p . send )
503- )
504- if ( wallet ) {
505- walletId = Number ( wallet . id )
506- }
507-
508- await client . mutate ( {
509- mutation : protocolUpsertMutation ( protocol ) ,
510- variables : {
511- ...( walletId ? { walletId } : { templateName } ) ,
512- enabled,
513- ...encrypted
514- }
515- } )
516- } , [ client , encryptConfig ] )
517-
518- return useMemo ( ( ) => ( { migrate, ready : ready && ! loading } ) , [ migrate , ready , loading ] )
519- }
520-
521455export function useUpdateKeyHash ( ) {
522456 const [ mutate ] = useMutation ( UPDATE_KEY_HASH )
523457
524458 return useCallback ( async ( keyHash ) => {
525459 await mutate ( { variables : { keyHash } } )
526460 } , [ mutate ] )
527461}
528-
529- function migrateConfig ( protocol , config ) {
530- switch ( protocol . name ) {
531- case 'LNBITS' :
532- return {
533- url : config . url ,
534- apiKey : config . adminKey
535- }
536- case 'PHOENIXD' :
537- return {
538- url : config . url ,
539- apiKey : config . primaryPassword
540- }
541- case 'BLINK' :
542- return {
543- url : config . url ,
544- apiKey : config . apiKey ,
545- currency : config . currency
546- }
547- case 'LNC' :
548- return {
549- pairingPhrase : config . pairingPhrase ,
550- localKey : config . localKey ,
551- remoteKey : config . remoteKey ,
552- serverHost : config . serverHost
553- }
554- case 'WEBLN' :
555- return { }
556- case 'NWC' :
557- return {
558- url : config . nwcUrl
559- }
560- default :
561- return config
562- }
563- }
564-
565- function getWalletTemplateName ( protocol ) {
566- switch ( protocol . name ) {
567- case 'LNBITS' :
568- case 'PHOENIXD' :
569- case 'BLINK' :
570- case 'NWC' :
571- return protocol . name
572- case 'LNC' :
573- return 'LND'
574- case 'WEBLN' :
575- return 'ALBY'
576- default :
577- return null
578- }
579- }
0 commit comments