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