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