Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: maitain v1 login status while use sdk #112

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ const config: IConfigProps = {
};
```

_IConfigProps Clarifications_

```ts
import { GlobalConfigProps } from '@portkey/did-ui-react/dist/_types/src/components/config-provider/types';
import { SignInProps, ISignIn, PortkeyProvider } from '@portkey/did-ui-react';
import { WalletAdapter } from '@aelf-web-login/wallet-adapter-base';

interface IConfirmLogoutDialogProps {
title: string;
subTitle: string[];
okTxt: string;
cancelTxt: string;
visible: boolean;
onOk: () => void;
onCancel: () => void;
width: number;
mobileWidth: number;
}

interface IConfigProps {
didConfig: GlobalConfigProps;
baseConfig: IBaseConfig;
wallets: WalletAdapter[];
}
interface IBaseConfig {
networkType: NetworkEnum;
chainId: TChainId;
keyboard?: boolean;
design?: SignInDesignEnum;
iconSrcForSocialDesign?: string;
titleForSocialDesign?: string;
noCommonBaseModal?: boolean;
showVconsole?: boolean;
SignInComponent?: React.FC<SignInProps & RefAttributes<ISignIn>>;
PortkeyProviderProps?: Partial<Omit<React.ComponentProps<typeof PortkeyProvider>, 'children'>>;
ConfirmLogoutDialog?: React.FC<Partial<IConfirmLogoutDialogProps>>;
}
```

# Usage

1. Import `WebLoginProvider`, `init` and `useConnectWallet`
Expand Down
2 changes: 1 addition & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-base",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-bridge",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
13 changes: 9 additions & 4 deletions packages/bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { DIDWalletInfo, TelegramPlatform } from '@portkey/did-ui-react';
import { isPortkeyApp } from '@aelf-web-login/utils';
import { IBaseConfig } from '.';

let isDisconnectClicked = false;
class Bridge {
private _wallets: WalletAdapter[];
private _activeWallet: WalletAdapter | undefined;
Expand Down Expand Up @@ -80,19 +81,24 @@ class Bridge {
return new Promise((resolve, reject) => {
this._loginResolve = resolve;
this._loginReject = reject;
console.log('connect');
this.openLoginPanel();
});
};

disConnect = async (isDoubleCheck = false) => {
console.log('disconnect', isDoubleCheck, this.isAAWallet);
console.log('disconnect, isDoubleCheck: isAAWallet:', isDoubleCheck, this.isAAWallet);
try {
if (isDoubleCheck || (this.isAAWallet && this.activeWallet!.noNeedForConfirm)) {
if (isDisconnectClicked) {
return;
}
isDisconnectClicked = true;

// only click confirmLogout button/or noNeedForConfirm can enter here
await this.activeWallet?.logout();
this.closeConfirmLogoutPanel();
this.closeLockPanel();
isDisconnectClicked = false;
} else {
if (this.isAAWallet) {
this.openConfirmLogoutPanel();
Expand Down Expand Up @@ -170,7 +176,6 @@ class Bridge {
};

onConnectedHandler = (walletInfo: TWalletInfo) => {
console.log('xxxxxxxxxxxxx-------');
dispatch(setWalletInfo(walletInfo));
dispatch(setWalletType(this.activeWallet?.name));
dispatch(clearLoginError());
Expand Down Expand Up @@ -256,9 +261,9 @@ class Bridge {
console.log('onUniqueWalletClick--', e);
} finally {
if (!this._noCommonBaseModal) {
this.closeLoadingModal();
this.closeLoginPanel();
}
this.closeLoadingModal();
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { store, AppStore } from './store';
import { GlobalConfigProps } from '@portkey/did-ui-react/dist/_types/src/components/config-provider/types';
import { ConfigProvider, SignInProps, ISignIn, PortkeyProvider } from '@portkey/did-ui-react';
import { RefAttributes } from 'react';
import { IConfirmLogoutDialogProps } from './ui';

type BaseConfigProviderProps = Omit<React.ComponentProps<typeof PortkeyProvider>, 'children'>;

Expand All @@ -25,6 +26,7 @@ export interface IBaseConfig {
showVconsole?: boolean;
SignInComponent?: React.FC<SignInProps & RefAttributes<ISignIn>>;
PortkeyProviderProps?: Partial<BaseConfigProviderProps>;
ConfirmLogoutDialog?: React.FC<Partial<IConfirmLogoutDialogProps>>;
}
export interface IConfigProps {
didConfig: GlobalConfigProps;
Expand Down
4 changes: 4 additions & 0 deletions packages/bridge/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ export const store: EnhancedStore<TState> = configureStore({
'walletInfo.extraInfo.provider',
'walletInfo.extraInfo.portkeyInfo.walletInfo',
'walletInfo.extraInfo.nightElfInfo',
'loginError.nativeError',
'payload.nativeError',
],
// Ignore these paths in the state
ignoredPaths: [
'walletInfo.extraInfo.provider',
'walletInfo.extraInfo.portkeyInfo.walletInfo',
'walletInfo.extraInfo.nightElfInfo',
'loginError.nativeError',
'payload.nativeError',
],
},
}),
Expand Down
15 changes: 10 additions & 5 deletions packages/bridge/src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Modal, Button, Typography, Drawer } from 'antd';
import useTelegram from './useTelegram';
import './ui.css';

interface ConfirmLogoutDialogProps {
export interface IConfirmLogoutDialogProps {
title: string;
subTitle: string[];
okTxt: string;
Expand Down Expand Up @@ -49,7 +49,7 @@ const constant = {
connectWallet: 'Connect Wallet',
};

const defaultProps: Partial<ConfirmLogoutDialogProps> = {
const defaultProps: Partial<IConfirmLogoutDialogProps> = {
title: 'Are you sure you want to exit your wallet?',
subTitle: [
'Your current wallet and assets will be removed from this app permanently. This action cannot be undone.',
Expand All @@ -71,7 +71,7 @@ interface ISignInModalProps {
}
const { isMobile } = utils;

const ConfirmLogoutDialog = (props: Partial<ConfirmLogoutDialogProps>) => {
const ConfirmLogoutDialog = (props: Partial<IConfirmLogoutDialogProps>) => {
const { title, subTitle, okTxt, cancelTxt, visible, onOk, onCancel, width, mobileWidth } = {
...defaultProps,
...props,
Expand Down Expand Up @@ -235,8 +235,13 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
);
const filteredWallets = wallets.filter((ele) => ele.name !== PORTKEYAA);
const isMobileDevice = isMobile();
const { noCommonBaseModal = false, SignInComponent } = baseConfig;
const {
noCommonBaseModal = false,
SignInComponent,
ConfirmLogoutDialog: CustomizedConfirmLogoutDialog,
} = baseConfig;
const FinalSignInComponent = SignInComponent || SignIn;
const FinalConfirmLogoutDialog = CustomizedConfirmLogoutDialog || ConfirmLogoutDialog;
// const isLocking = store.getState().isLocking;
// console.log('isLocking', isLocking);

Expand Down Expand Up @@ -446,7 +451,7 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
</DynamicWrapper>
)}

<ConfirmLogoutDialog
<FinalConfirmLogoutDialog
visible={isShowConfirmLogoutPanel}
onOk={confirmLogoutHandler}
onCancel={cancelLogoutHandler}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-react",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
5 changes: 3 additions & 2 deletions packages/starter/src/Demo/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import ContractDemo from './ContractDemo';
import SignatureDemo from './SignatureDemo';
import UtilsDemo from './UtilsDemo';

const APP_NAME = 'explorer.aelf.io';
const APP_NAME = 'forest';
const WEBSITE_ICON = 'https://explorer.aelf.io/favicon.main.ico';
const CHAIN_ID = 'AELF' as TChainId;
const CHAIN_ID = 'tDVW' as TChainId;
const NETWORK_TYPE = NetworkEnum.TESTNET;
const RPC_SERVER_AELF = 'https://explorer-test.aelf.io/chain';
const RPC_SERVER_TDVV = 'https://explorer-test-side02.aelf.io/chain';
Expand Down Expand Up @@ -55,6 +55,7 @@ const didConfig = {
// };

const baseConfig = {
// ConfirmLogoutDialog: CustomizedConfirmLogoutDialog,
// SignInComponent: SignInProxy,
PortkeyProviderProps: {
theme: 'dark' as any,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/utils",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/night-elf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-night-elf",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
1 change: 0 additions & 1 deletion packages/wallets/night-elf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ export class NightElfWallet extends BaseWalletAdapter {
}
const finalChainId = chainId || this._config.chainId;
const bridge = this._wallet.extraInfo?.nightElfInfo?.aelfBridges?.[finalChainId];
console.log(finalChainId, bridge, '---');
if (!bridge) {
throw makeError(ERR_CODE.NIGHT_ELF_NOT_CONNECTED);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/portkey-aa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-portkey-aa",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
20 changes: 16 additions & 4 deletions packages/wallets/portkey-aa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,19 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
get wallet() {
return this._wallet as TWalletInfo;
}
get appName() {
const compatibleAppName = `V2-${this._config.appName}`;
if (enhancedLocalStorage.getItem(compatibleAppName)) {
return compatibleAppName;
} else {
return this._config.appName;
}
}

async autoRequestAccountHandler() {
const canLoginEargly = !!enhancedLocalStorage.getItem(this._config.appName);
const canLoginEargly =
!!enhancedLocalStorage.getItem(this._config.appName) ||
!!enhancedLocalStorage.getItem(`V2-${this._config.appName}`);
if (!canLoginEargly) {
return;
}
Expand Down Expand Up @@ -86,7 +96,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
const holderInfo = await did.getCAHolderInfo(didWalletInfo.chainId);
nickName = holderInfo.nickName;

await did.save(didWalletInfo.pin, this._config.appName);
await did.save(didWalletInfo.pin, this.appName);

const portkeyInfo = {
...didWalletInfo,
Expand Down Expand Up @@ -149,7 +159,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
this._loginState = LoginStateEnum.INITIAL;
enhancedLocalStorage.removeItem(ConnectedWallet);
enhancedLocalStorage.removeItem(PORTKEY_ORIGIN_CHAIN_ID_KEY);
enhancedLocalStorage.removeItem(this._config.appName);
enhancedLocalStorage.removeItem(this.appName);

this.emit('disconnected');
} catch (error) {
Expand All @@ -171,13 +181,15 @@ export class PortkeyAAWallet extends BaseWalletAdapter {

async onUnlock(password: string): Promise<TWalletInfo> {
try {
const appName = this._config.appName;
const appName = this.appName;
const chainId = this._config.chainId;
const isValidPinCode = await this.checkPassword(appName, password);
if (!isValidPinCode) {
return;
}

const localWallet = await did.load(password, appName);

let caInfo = localWallet.didWallet.caInfo[chainId];
let caHash = caInfo?.caHash;
if (!caInfo) {
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/portkey-discover/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aelf-web-login/wallet-adapter-portkey-discover",
"version": "0.1.3-alpha.2",
"version": "0.1.3-alpha.5",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down
Loading