From 13f76a9e0e4801dec547560d34163ecb3a00bfa7 Mon Sep 17 00:00:00 2001 From: homura Date: Thu, 19 Oct 2023 11:16:17 +0800 Subject: [PATCH] feat: make the unipass host become configurable --- .../src/containers/WalletContainer/index.tsx | 15 +++++++++------ apps/ckit-app/src/hooks/useUnipass.tsx | 6 +++++- .../src/wallets/ObservableUnipassWallet.ts | 6 +++--- packages/ckit/src/wallets/UnipassWallet.ts | 2 ++ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/apps/ckit-app/src/containers/WalletContainer/index.tsx b/apps/ckit-app/src/containers/WalletContainer/index.tsx index a39ad8c..2020d56 100644 --- a/apps/ckit-app/src/containers/WalletContainer/index.tsx +++ b/apps/ckit-app/src/containers/WalletContainer/index.tsx @@ -4,6 +4,7 @@ import { autorun, runInAction } from 'mobx'; import { useLocalObservable } from 'mobx-react-lite'; import { useCallback, useEffect, useState } from 'react'; import { createContainer } from 'unstated-next'; +import { useUnipass } from '../../hooks/useUnipass'; import { CkitProviderContainer } from '../CkitProviderContainer'; import { ObservableAcpPwLockWallet, @@ -28,6 +29,7 @@ function useWallet() { const [error, setError] = useState(null); const [visible, setVisible] = useState(false); const [isInitialized, setIsInitialized] = useState(false); + const { host } = useUnipass(); const setModalVisible = useCallback((visible: boolean) => { setVisible(visible); @@ -40,13 +42,13 @@ function useWallet() { runInAction(() => { wallets.splice(0); wallets.push( - new ObservableUnipassWallet(ckitProvider), + new ObservableUnipassWallet(ckitProvider, { host, loginDataCacheKey: '__unipass__' }), new ObservableOmnilockWallet(ckitProvider), new ObservableAcpOmnilockWallet(ckitProvider), ); wallets.find((value) => value.descriptor.name === currentWalletName)?.connect(); }); - }, [ckitProvider]); + }, [ckitProvider, currentWalletName, host]); useEffect( () => @@ -81,21 +83,22 @@ function useWallet() { ); }); }), - [setModalVisible, wallets], + [setCurrentWalletName, setModalVisible, wallets], ); const currentWallet = wallets.find((value) => value.descriptor.name === currentWalletName); useEffect(() => { autorun(() => { + if (!currentWallet || !currentWallet.signer) return; + setSignerAddress(undefined); - const wallet = wallets.find((value) => value.descriptor.name === currentWalletName); - void Promise.resolve(wallet?.signer?.getAddress()).then((address) => { + void Promise.resolve(currentWallet.signer.getAddress()).then((address) => { setSignerAddress(address); setIsInitialized(true); }); }); - }, [currentWalletName]); + }, [currentWallet, currentWalletName, wallets]); return { isInitialized, diff --git a/apps/ckit-app/src/hooks/useUnipass.tsx b/apps/ckit-app/src/hooks/useUnipass.tsx index 1e81901..47dad5d 100644 --- a/apps/ckit-app/src/hooks/useUnipass.tsx +++ b/apps/ckit-app/src/hooks/useUnipass.tsx @@ -8,11 +8,14 @@ interface UnipassContext { cacheTx: (tx: string | null) => void; clearTx: () => void; cachedTx: string | null; + host: string; } export function useUnipass(): UnipassContext { const [cachedTx, cacheTx] = useLocalStorage('unipassTx'); - const adapter = new UnipassWallet.UnipassRedirectAdapter({ host: 'https://unipass.xyz' }); // TODO use the config + // TODO unipass.xyz is deprecated, change it when the new host is ready + const host = 'https://unipass.xyz'; + const adapter = new UnipassWallet.UnipassRedirectAdapter({ host }); // TODO use the config return { shouldLogin: adapter.hasLoginInfo(), cacheLogin: () => adapter.saveLoginInfo(), @@ -20,5 +23,6 @@ export function useUnipass(): UnipassContext { cacheTx, clearTx: () => cacheTx(null), cachedTx, + host, }; } diff --git a/apps/ckit-app/src/wallets/ObservableUnipassWallet.ts b/apps/ckit-app/src/wallets/ObservableUnipassWallet.ts index 9cd4eca..6cae5ce 100644 --- a/apps/ckit-app/src/wallets/ObservableUnipassWallet.ts +++ b/apps/ckit-app/src/wallets/ObservableUnipassWallet.ts @@ -1,9 +1,9 @@ -import { UnipassWallet, CkitProvider } from '@ckitjs/ckit'; +import { UnipassWallet, CkitProvider, UnipassAdapterConfig } from '@ckitjs/ckit'; import { makeObservable, observable } from 'mobx'; export class ObservableUnipassWallet extends UnipassWallet { - constructor(provider: CkitProvider) { - super(provider); + constructor(provider: CkitProvider, adapterConfig?: UnipassAdapterConfig) { + super(provider, adapterConfig); this.setDescriptor({ name: 'UniPass' }); makeObservable(this, { connectStatus: observable, diff --git a/packages/ckit/src/wallets/UnipassWallet.ts b/packages/ckit/src/wallets/UnipassWallet.ts index 08ad90c..430ba6c 100644 --- a/packages/ckit/src/wallets/UnipassWallet.ts +++ b/packages/ckit/src/wallets/UnipassWallet.ts @@ -4,6 +4,8 @@ import { CkitProvider } from '../providers'; import { AbstractSingleEntrySigner } from './AbstractSingleEntrySigner'; import { AdapterConfig, UnipassRedirectAdapter } from './unipass/UnipassAdapter'; +export { AdapterConfig as UnipassAdapterConfig } from './unipass/UnipassAdapter'; + export class UnipassWallet extends AbstractWallet { static UnipassRedirectAdapter: typeof UnipassRedirectAdapter = UnipassRedirectAdapter;