Skip to content

Commit

Permalink
feat: make the unipass host become configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Oct 19, 2023
1 parent 70c4062 commit 13f76a9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
15 changes: 9 additions & 6 deletions apps/ckit-app/src/containers/WalletContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +29,7 @@ function useWallet() {
const [error, setError] = useState<WalletConnectError | null>(null);
const [visible, setVisible] = useState(false);
const [isInitialized, setIsInitialized] = useState(false);
const { host } = useUnipass();

const setModalVisible = useCallback((visible: boolean) => {
setVisible(visible);
Expand All @@ -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(
() =>
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion apps/ckit-app/src/hooks/useUnipass.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ interface UnipassContext {
cacheTx: (tx: string | null) => void;
clearTx: () => void;
cachedTx: string | null;
host: string;
}

export function useUnipass(): UnipassContext {
const [cachedTx, cacheTx] = useLocalStorage<string | null>('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(),
shouldSendTx: adapter.hasSigData(),
cacheTx,
clearTx: () => cacheTx(null),
cachedTx,
host,
};
}
6 changes: 3 additions & 3 deletions apps/ckit-app/src/wallets/ObservableUnipassWallet.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/ckit/src/wallets/UnipassWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 13f76a9

Please sign in to comment.