Skip to content

Commit

Permalink
Merge pull request #137 from AElfProject/feature/update-aelf-bridge
Browse files Browse the repository at this point in the history
Feature/update aelf bridge
  • Loading branch information
aelf-lxy authored Aug 7, 2024
2 parents 4381bf5 + 76090a8 commit db2bb43
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/login/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-web-login",
"version": "2.1.3",
"version": "2.1.5",
"main": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"exports": {
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GlobalConfigProps as GlobalConfigPropsV1 } from '@portkey-v1/did-ui-rea
import { GlobalConfigProps } from '@portkey/did-ui-react/dist/_types/src/components/config-provider/types';
import { EventEmitter } from 'ahooks/lib/useEventEmitter';
import { PortkeyDidV1, PortkeyDid } from './index';
import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage';

// copy from @aelf-react/core, cause it's not exported
export type AelfNode = {
Expand Down
53 changes: 27 additions & 26 deletions packages/login/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import clsx from 'clsx';
import { PortkeyDid, PortkeyDidV1 } from './index';
import { getStorageVersion } from './utils/getUrl';
import CommonErrorBoundary from './components/ErrorBoundary';
import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage';

const INITIAL_STATE = {
loginState: WebLoginState.initial,
Expand Down Expand Up @@ -589,32 +590,32 @@ function WebLoginProvider({

export default function Provider({ children, ...props }: WebLoginProviderProps) {
const aelfReactConfig = getConfig().aelfReact;
const TELEGRAM_SRC = 'https://telegram.org/js/telegram-web-app.js';

const [scriptLoaded, setScriptLoaded] = useState(false);

useEffect(() => {
if (typeof window !== 'undefined' && typeof location !== 'undefined') {
const script = document.createElement('script');
script.src = TELEGRAM_SRC;
script.type = 'text/javascript';
script.onload = () => {
setScriptLoaded(true);
};

script.onerror = () => {
console.error('Failed to load the Telegram script');
setScriptLoaded(false);
};
document.head.appendChild(script);
return () => {
document.head.removeChild(script);
};
}
}, []);
if (!scriptLoaded) {
return null;
}
// const TELEGRAM_SRC = 'https://telegram.org/js/telegram-web-app.js';

// const [scriptLoaded, setScriptLoaded] = useState(false);

// useEffect(() => {
// if (typeof window !== 'undefined' && typeof location !== 'undefined') {
// const script = document.createElement('script');
// script.src = TELEGRAM_SRC;
// script.type = 'text/javascript';
// script.onload = () => {
// setScriptLoaded(true);
// };

// script.onerror = () => {
// console.error('Failed to load the Telegram script');
// setScriptLoaded(false);
// };
// document.head.appendChild(script);
// return () => {
// document.head.removeChild(script);
// };
// }
// }, []);
// if (typeof window !== 'undefined' && !scriptLoaded) {
// return null;
// }

return (
<CommonErrorBoundary>
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/hooks/useCallContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SendOptions } from '@portkey/types';
import { SendOptions as SendOptionsV1 } from '@portkey-v1/types';
import useWebLoginEvent from './useWebLoginEvent';
import { getFaviconUrl, getStorageVersion, getUrl } from '../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage';

const getAElfInstance = (() => {
const instances = new Map<string, any>();
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/hooks/useGetAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChainId } from '@portkey/provider-types';
import { did } from '@portkey/did-ui-react';
import { did as didV1 } from '@portkey-v1/did-ui-react';
import { getStorageVersion } from '../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage';

export default function useGetAccount(chainId: string) {
const WEB_LOGIN_VERSION = getStorageVersion();
Expand Down
44 changes: 44 additions & 0 deletions packages/login/src/utils/enhancedLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export interface Storage {
getItem(key: string, ...args: Array<any>): any;
setItem(key: string, value: any, ...args: Array<any>): any;
removeItem(key: string, ...args: Array<any>): any;
clear(): void;
}

const localStorageMock = (function () {
let store: { [key: string]: string } = {};

return {
getItem: function (key: string): string | null {
return store[key] || null;
},
setItem: function (key: string, value: string) {
store[key] = value.toString();
},
removeItem: function (key: string) {
delete store[key];
},
clear: function () {
store = {};
},
};
})();

// let enhancedLocalStorage: Storage = {} as Storage;
// if (typeof window !== 'undefined') {
// enhancedLocalStorage = window.localStorage;
// } else {
// import('node-localstorage').then(({ LocalStorage }) => {
// enhancedLocalStorage = new LocalStorage('./scratch');
// });
// }
// export { enhancedLocalStorage };

let enhancedLocalStorage: Storage;
if (typeof window !== 'undefined') {
console.log('enhancedLocalStorage in window');
enhancedLocalStorage = window.localStorage;
} else {
enhancedLocalStorage = localStorageMock;
}
export { enhancedLocalStorage, localStorageMock };
1 change: 1 addition & 0 deletions packages/login/src/utils/getUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOGIN_VERSION } from '../constants';
import { getConfig } from '../config';
import { enhancedLocalStorage as localStorage } from './enhancedLocalStorage';

export function getUrl() {
return new URL(location.href);
Expand Down
3 changes: 3 additions & 0 deletions packages/login/src/utils/isMobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const notMobileRE = /CrOS/;
const tabletRE = /android|ipad|playbook|silk/i;

export default function isMobile() {
if (typeof navigator === 'undefined') {
return false;
}
const ua = navigator.userAgent;
if (typeof ua !== 'string') return false;

Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/discover/detectProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import detectProvider from '@portkey/detect-provider';
import { IPortkeyProvider } from '@portkey/provider-types';
import { getStorageVersion } from '../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export default async function detectDiscoverProvider(version?: string | null): Promise<IPortkeyProvider | null> {
const WEB_LOGIN_VERSION = getStorageVersion();
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/discover/useDiscover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useWebLoginEvent from '../../hooks/useWebLoginEvent';
import { removeOtherKey, useWebLogin } from '../../context';
import isPortkeyApp, { isPortkeyV2 } from '../../utils/isPortkeyApp';
import { getStorageVersion } from '../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export type DiscoverDetectState = 'unknown' | 'detected' | 'not-detected';
export type DiscoverInterface = WalletHookInterface & {
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/elf/useElf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import checkSignatureParams from '../../utils/signatureParams';
import detectNightElf from './detectNightElf';
import { zeroFill } from '../../utils/zeroFill';
import { removeOtherKey } from '../../context';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export function useElf({
options,
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/Portkey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { changePortkeyVersion } from '../../../utils/isPortkeyApp';
import isMobile from '../../../utils/isMobile';
import { useWebLogin } from '../../../context';
import { getStorageVersion } from '../../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

interface IPortkeyPanelProps {
open: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/Portkey/indexV1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FetchRequest } from '@portkey-v1/request';
import { changePortkeyVersion } from '../../../utils/isPortkeyApp';
import isMobile from '../../../utils/isMobile';
import { getStorageVersion } from '../../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export default function Portkey({
open,
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/usePortkey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { addPrefix } from '../../../utils/getDidAndVersion';
import { aes } from '@portkey/utils';
import useTelegram from './useTelegram';
import { NetworkType } from '@portkey/provider-types';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export type PortkeyInterface = WalletHookInterface & {
isManagerExists: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/usePortkey/indexV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import checkSignatureParams from '../../../utils/signatureParams';
import { PortkeyOptions } from 'src/types';
import { sendAdapter } from '../../../hooks/useCallContract';
import { message } from 'antd';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export type PortkeyInterface = WalletHookInterface & {
isManagerExists: boolean;
Expand Down

0 comments on commit db2bb43

Please sign in to comment.