Skip to content

Commit

Permalink
feat: Support telegram mini app
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Sep 6, 2024
1 parent 221d30a commit 3c711ae
Show file tree
Hide file tree
Showing 24 changed files with 45 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@node-real/walletkit': patch
---

Refactor walletkit
Support telegram mini app
14 changes: 0 additions & 14 deletions .changeset/pre.json

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/tiny-kings-hunt.md

This file was deleted.

13 changes: 0 additions & 13 deletions packages/walletkit/CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion packages/walletkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@node-real/walletkit",
"version": "2.3.2-alpha.1",
"version": "2.3.5",
"author": "node-real",
"private": false,
"description": "WalletKit is a React component library for easily connecting a wallet to your dApp.",
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/core/configs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface BaseWallet extends WalletConfig {
isVisible?: boolean;
render?: (props: WalletRenderProps) => React.ReactNode;
showQRCode?: boolean;
connectWithUri?: boolean;
isInstalled: () => boolean | undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import { EvmHomeViewWalletConnectUriProvider } from '@/evm/components/EvmHomeVie
import { isMobile, isTMA } from '@/core/base/utils/mobile';

export function HomeView() {
const { wallets, options, evmConfig } = useWalletKit();
const { wallets, options } = useWalletKit();
const { isMobileLayout } = useResponsive();

const visibleWallets = wallets.filter((item) => item.isVisible !== false);
const useGridLayout =
visibleWallets.length >= options.gridLayoutThreshold! ||
(isMobileLayout && options.useGridLayoutOnMobile);

const needPreCreateWcUri =
(!!evmConfig?.wallets.find((item) => item.connectWithUri) || isTMA()) && isMobile();
const needPreCreateWcUri = isTMA() && isMobile();

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import { useState, useRef, useMemo, useCallback } from 'react';
import { RouteContext } from './context';
import { EvmConnectingView } from '@/evm/components/EvmConnectingView';
import { EvmQRCodeView } from '@/evm/components/EvmQRCodeView';
import { EvmWalletConnectURIConnectingView } from '@/evm/components/EvmWalletConnectURIConnectingView';
import { EvmUriConnectingView } from '@/evm/components/EvmUriConnectingView';
import { SolanaConnectingView } from '@/solana/components/SolanaConnectingView';
import { SolanaQRCodeView } from '@/solana/components/SolanaQRCodeView';
import { HomeView } from '../HomeView';
import { EvmMetaMaskURIConnectingView } from '@/evm/components/EvmMetaMaskURIConnectingView';

export enum ViewRoutes {
HOME = 'HOME',
EVM_CONNECTING = 'EVM_CONNECTING',
EVM_QRCODE = 'EVM_QRCODE',
EVM_WALLET_CONNECT_URI_CONNECTING = 'EVM_WALLET_CONNECT_URI_CONNECTING',
EVM_META_MASK_URI_CONNECTING = 'EVM_META_MASK_URI_CONNECTING',
EVM_URI_CONNECTING = 'EVM_URI_CONNECTING',
SOLANA_CONNECTING = 'SOLANA_CONNECTING',
SOLANA_QRCODE = 'SOLANA_QRCODE',
}
Expand All @@ -36,10 +34,8 @@ export function RouteProvider(props: RouteProviderProps) {
return <EvmConnectingView />;
case ViewRoutes.EVM_QRCODE:
return <EvmQRCodeView />;
case ViewRoutes.EVM_WALLET_CONNECT_URI_CONNECTING:
return <EvmWalletConnectURIConnectingView />;
case ViewRoutes.EVM_META_MASK_URI_CONNECTING:
return <EvmMetaMaskURIConnectingView />;
case ViewRoutes.EVM_URI_CONNECTING:
return <EvmUriConnectingView />;
case ViewRoutes.SOLANA_CONNECTING:
return <SolanaConnectingView />;
case ViewRoutes.SOLANA_QRCODE:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function EvmQRCodeView() {

const { wcUri } = useWalletConnectUri();
const wcModal = useWalletConnectModal();
const qrCodeUri = wcUri && ((selectedWallet as EvmWallet).getQrCodeUri?.(wcUri) ?? wcUri);
const qrCodeUri = wcUri && ((selectedWallet as EvmWallet).getUri?.(wcUri) ?? wcUri);
const isConnected = useIsConnected();

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { openLink } from '@/core/utils/common';
import { useWalletConnectUri } from '@/evm/hooks/useWalletConnectUri';
import { useConnectingStatus } from '@/evm/hooks/useConnectingStatus';

export function EvmWalletConnectURIConnectingView() {
export function EvmUriConnectingView() {
const { selectedWallet } = useWalletKit();
const isConnected = useIsConnected();

Expand All @@ -23,7 +23,7 @@ export function EvmWalletConnectURIConnectingView() {
const onTryAgain = () => {
setStatus(CONNECT_STATUS.CONNECTING);

const walletUri = (selectedWallet as EvmWallet).getConnectUri(wcUri!);
const walletUri = (selectedWallet as EvmWallet).getUri(wcUri!);
openLink(walletUri);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAndroid, isMobile, isTMA } from '@/core/base/utils/mobile';
import { isMobile, isTMA } from '@/core/base/utils/mobile';
import { UseWalletRenderProps } from '@/core/hooks/useWalletRender';
import { useConnectModal } from '@/core/modals/ConnectModal/context';
import { ViewRoutes } from '@/core/modals/ConnectModal/RouteProvider';
Expand Down Expand Up @@ -76,22 +76,18 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
}
};

const tryJumpToUriConnectingView = () => {
if (isAndroid()) {
jumpToDeepLink();
} else {
const wcUri = getEvmGlobalData().homeViewWalletConnectUri;
if (wcUri) {
const connectUri = wallet.getUri(wcUri);
if (connectUri) {
openLink(connectUri);
jumpTo(ViewRoutes.EVM_WALLET_CONNECT_URI_CONNECTING);
} else {
options.onError?.(
new Error(`The wallet does not support URI connection`),
`The wallet does not support URI connection`,
);
}
const jumpToUriConnectingView = () => {
const wcUri = getEvmGlobalData().homeViewWalletConnectUri;
if (wcUri) {
const connectUri = wallet.getUri(wcUri);
if (connectUri) {
openLink(connectUri);
jumpTo(ViewRoutes.EVM_URI_CONNECTING);
} else {
options.onError?.(
new Error(`The wallet does not support URI connection`),
`The wallet does not support URI connection`,
);
}
}
};
Expand All @@ -107,7 +103,7 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
if (isWalletConnect(walletId)) {
wcModal.onOpen();
} else {
tryJumpToUriConnectingView();
jumpToUriConnectingView();
}
} else {
// 1.2 pc
Expand All @@ -117,8 +113,6 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
// 2. mobile
if (isWalletConnect(walletId)) {
wcModal.onOpen();
} else if (wallet.connectWithUri) {
tryJumpToUriConnectingView();
} else if (wallet.isInstalled()) {
jumpToConnectingView();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEvmConnect } from '@/evm/hooks/useEvmConnect';
import { useWalletKit } from '../../core/providers/WalletKitProvider/context';

export function useConnectEvmWallet() {
export function useConnectWallet() {
const { log, evmConfig } = useWalletKit();

const { connect, connectors } = useEvmConnect();
Expand Down
2 changes: 1 addition & 1 deletion packages/walletkit/src/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export * from './components/EthereumScript';
export * from './utils/defaultEvmConfig';

// hooks
export * from './hooks/useConnectEvmWallet';
export * from './hooks/useConnectWallet';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function binanceWeb3Wallet(props: InjectedEvmWalletOptions = {}): EvmWall
id: 'binanceWeb3Wallet',
walletType: 'evm',
showQRCode: true,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
Expand Down
8 changes: 6 additions & 2 deletions packages/walletkit/src/evm/wallets/bitgetWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { bitgetWalletConfig } from '@/core/configs/bitgetWallet';
import { EvmWallet, InjectedEvmWalletOptions } from '../types';
import { injected } from '../injected';
import { getEvmInjectedProvider } from '../utils';
import { isAndroid, isTMA } from '@/core/base/utils/mobile';

export function bitgetWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
const { connectorOptions, ...restProps } = props;
Expand All @@ -11,15 +12,18 @@ export function bitgetWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'bitgetWallet',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
getDeepLink() {
return `https://bkcode.vip?action=dapp&url=${window.location.href}`;
},
getUri(uri) {
return `https://bkcode.vip/wc?uri=${encodeURIComponent(uri)}`;
let encodedUri = encodeURIComponent(uri);
if (isTMA() && isAndroid()) {
encodedUri = encodeURIComponent(encodedUri);
}
return `https://bkcode.vip/wc?uri=${encodedUri}`;
},
getCreateConnectorFn() {
return injected({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function coinbaseWallet(props: CoinbaseWalletOptions = {}): EvmWallet {
id: 'coinbaseWallet',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
if (
connectorOptions &&
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/wallets/mathWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function mathWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'mathWallet',
walletType: 'evm',
spinnerColor: undefined,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
Expand Down
8 changes: 6 additions & 2 deletions packages/walletkit/src/evm/wallets/metaMask/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { metaMaskConfig } from '@/core/configs/metaMask';
import { hasEvmInjectedProvider } from '../utils';
import { injected } from '../injected';
import { InjectedEvmWalletOptions, EvmWallet } from '../types';
import { isAndroid, isTMA } from '@/core/base/utils/mobile';

export function metaMask(props: InjectedEvmWalletOptions = {}): EvmWallet {
const { connectorOptions, ...restProps } = props;
Expand All @@ -11,7 +12,6 @@ export function metaMask(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'metaMask',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return hasEvmInjectedProvider('isMetaMask');
},
Expand All @@ -20,7 +20,11 @@ export function metaMask(props: InjectedEvmWalletOptions = {}): EvmWallet {
return `https://metamask.app.link/dapp/${dappPath}`;
},
getUri(uri) {
return `https://metamask.app.link/wc?uri=${encodeURIComponent(uri)}`;
let encodedUri = encodeURIComponent(uri);
if (isTMA() && isAndroid()) {
encodedUri = encodeURIComponent(encodedUri);
}
return `https://metamask.app.link/wc?uri=${encodedUri}`;
},
getCreateConnectorFn() {
return injected({
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/wallets/okxWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function okxWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'okxWallet',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/wallets/safe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function safe(props: SafeOptions = {}): EvmWallet {
id: 'safe',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return !(typeof window === 'undefined') && window?.parent !== window;
},
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/wallets/tokenPocket/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function tokenPocket(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'tokenPocket',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
Expand Down
8 changes: 6 additions & 2 deletions packages/walletkit/src/evm/wallets/trustWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { trustWalletConfig } from '@/core/configs/trustWallet';
import { injected } from '../injected';
import { EvmWallet, InjectedEvmWalletOptions } from '../types';
import { getEvmInjectedProvider } from '../utils';
import { isAndroid, isTMA } from '@/core/base/utils/mobile';

export function trustWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
const { connectorOptions, ...restProps } = props;
Expand All @@ -12,7 +13,6 @@ export function trustWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
id: 'trust',
walletType: 'evm',
showQRCode: false,
connectWithUri: false,
isInstalled() {
return !!getProvider();
},
Expand All @@ -21,7 +21,11 @@ export function trustWallet(props: InjectedEvmWalletOptions = {}): EvmWallet {
return `https://link.trustwallet.com/open_url?coin_id=60&url=${encodedUrl}`;
},
getUri(uri) {
return `https://link.trustwallet.com/wc?uri=${encodeURIComponent(uri)}`;
let encodedUri = encodeURIComponent(uri);
if (isTMA() && isAndroid()) {
encodedUri = encodeURIComponent(encodedUri);
}
return `https://link.trustwallet.com/wc?uri=${encodedUri}`;
},
getCreateConnectorFn() {
return injected({
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/wallets/walletConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function walletConnect(props: WalletConnectOptions = {}): EvmWallet {
id: 'walletConnect',
walletType: 'evm',
showQRCode: !connectorOptions?.showQrModal,
connectWithUri: false,
isInstalled() {
return false;
},
Expand Down

0 comments on commit 3c711ae

Please sign in to comment.