Skip to content

Commit

Permalink
feat: add imToken wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
xwartz committed Sep 30, 2024
1 parent 071740b commit 3205843
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 46 deletions.
4 changes: 3 additions & 1 deletion packages/web/config/generate-cosmos-kit-wallet-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "node:path";
import { cdcwalletExtensionInfo } from "@cosmos-kit/cdcwallet-extension";
import { Wallet as DefaultWallet } from "@cosmos-kit/core";
import { cosmostationExtensionInfo } from "@cosmos-kit/cosmostation-extension";
import { imTokenWalletInfo } from "@cosmos-kit/imtoken-extension"
import { keplrExtensionInfo } from "@cosmos-kit/keplr-extension";
import { keplrMobileInfo } from "@cosmos-kit/keplr-mobile";
import { leapExtensionInfo } from "@cosmos-kit/leap-extension";
Expand Down Expand Up @@ -38,6 +39,7 @@ const CosmosKitWalletList: Wallet[] = [
cosmostationExtensionInfo,
stationExtensionInfo,
cdcwalletExtensionInfo,
imTokenWalletInfo,
];

function isObject(value: any): value is Record<any, any> {
Expand Down Expand Up @@ -106,7 +108,7 @@ async function generateCosmosKitWalletList() {
).join(",")}}
export const CosmosKitWalletList: Record<AvailableCosmosWallets, Wallet> = ${getStringifiedWallet(
registryObject
)}
)}
`;

const prettierConfig = await prettier.resolveConfig("./");
Expand Down
89 changes: 51 additions & 38 deletions packages/web/config/wallet-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
import("~/integrations/keplr-walletconnect").then(
(m) => m.KeplrMobileWallet
),
supportsChain: async (chainId) => {
supportsChain: async (chainId: string) => {
const keplrMobileAvailableChains: MainnetChainIds[] = [
"cosmoshub-4",
"osmosis-1",
Expand Down Expand Up @@ -54,9 +54,9 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
"gitopia",
"likecoin-mainnet-2",
"akashnet-2",
];
]

return keplrMobileAvailableChains.includes(chainId as MainnetChainIds);
return keplrMobileAvailableChains.includes(chainId as MainnetChainIds)
},
stakeUrl: "https://wallet.keplr.app/chains/osmosis?tab=staking",
governanceUrl: "https://wallet.keplr.app/chains/osmosis?tab=governance",
Expand All @@ -78,7 +78,7 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
logo: "/wallets/leap.svg",
lazyInstall: () =>
import("@cosmos-kit/leap-mobile").then((m) => m.LeapMobileWallet),
supportsChain: async (chainId) => {
supportsChain: async (chainId: string) => {
const leapMobileAvailableChains: MainnetChainIds[] = [
"agoric-3",
"akashnet-2",
Expand Down Expand Up @@ -146,8 +146,8 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
"phoenix-1",
"umee-1",
"dimension_37-1",
];
return leapMobileAvailableChains.includes(chainId as MainnetChainIds);
]
return leapMobileAvailableChains.includes(chainId as MainnetChainIds)
},

stakeUrl: "https://cosmos.leapwallet.io/transact/stake/plain?chain=osmosis",
Expand All @@ -162,27 +162,27 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
(m) => m.OkxwalletExtensionWallet
),
windowPropertyName: "okxwallet",
async supportsChain(chainId, retryCount = 0) {
if (typeof window === "undefined") return true;
async supportsChain(chainId: string, retryCount = 0) {
if (typeof window === "undefined") return true

const okxWallet = (window as any)?.okxwallet?.keplr as {
getKey: (chainId: string) => Promise<boolean>;
};
getKey: (chainId: string) => Promise<boolean>
}

if (!okxWallet) return true;
if (!okxWallet) return true

try {
await okxWallet.getKey(chainId);
return true;
await okxWallet.getKey(chainId)
return true
} catch (e) {
const error = e as { code: number; message: string };
const error = e as { code: number; message: string }

// Check for chain not supported error
if (
error.code === -32603 &&
error.message.includes("There is no chain info")
) {
return false;
return false
}

// Retry if the wallet is already processing
Expand All @@ -199,26 +199,26 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
*/
await new Promise((resolve) =>
setTimeout(resolve, Math.pow(2, retryCount) * 100)
);
)
// @ts-ignore
return this.supportsChain(chainId, retryCount + 1);
return this.supportsChain(chainId, retryCount + 1)
}

return false;
return false
}
},
matchError: (error) => {
if (typeof error !== "string") return error;
if (typeof error !== "string") return error

if (
error.includes(
"Already processing wallet_requestIdentities. Please wait."
)
) {
return new WalletConnectionInProgressError();
return new WalletConnectionInProgressError()
}

return error;
return error
},
signOptions: {
preferNoSetFee: true,
Expand Down Expand Up @@ -262,13 +262,13 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
(m) => m.CdcwalletExtensionWallet
),
mobileDisabled: false,
async supportsChain(chainId) {
async supportsChain(chainId: string) {
const cdcAvailableChains: MainnetChainIds[] = [
"cosmoshub-4",
"osmosis-1",
"crypto-org-chain-mainnet-1",
];
return cdcAvailableChains.includes(chainId as MainnetChainIds);
]
return cdcAvailableChains.includes(chainId as MainnetChainIds)
},
windowPropertyName: "cdc_wallet",
stakeUrl: "https://crypto.com/staking",
Expand All @@ -281,19 +281,19 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
lazyInstall: () =>
import("@cosmos-kit/xdefi-extension").then((m) => m.XDEFIExtensionWallet),
windowPropertyName: "xfi",
async supportsChain(chainId) {
if (typeof window === "undefined") return true;
async supportsChain(chainId: string) {
if (typeof window === "undefined") return true

const xfiWallet = (window as any)?.xfi?.keplr as {
getKey: (chainId: string) => Promise<boolean>;
};
getKey: (chainId: string) => Promise<boolean>
}

if (!xfiWallet) return true;
if (!xfiWallet) return true

return xfiWallet
.getKey(chainId)
.then(() => true)
.catch(() => false);
.catch(() => false)
},
features: [],
},
Expand All @@ -318,21 +318,34 @@ export const CosmosWalletRegistry: CosmosRegistryWallet[] = [
(m) => m.StationExtensionWallet
),
windowPropertyName: "station",
supportsChain: async (chainId) => {
if (typeof window === "undefined") return true;
supportsChain: async (chainId: string) => {
if (typeof window === "undefined") return true

const stationWallet = (window as any)?.station?.keplr as {
getChainInfosWithoutEndpoints: () => Promise<{ chainId: string }[]>;
};
getChainInfosWithoutEndpoints: () => Promise<{ chainId: string }[]>
}

if (!stationWallet) return true;
if (!stationWallet) return true

const chainInfos = await stationWallet.getChainInfosWithoutEndpoints();
return chainInfos.some((info) => info.chainId === chainId);
const chainInfos = await stationWallet.getChainInfosWithoutEndpoints()
return chainInfos.some((info) => info.chainId === chainId)
},
signOptions: {
preferNoSetFee: true,
},
features: [],
},
];
{
...CosmosKitWalletList["imtoken-extension"],
logo: "/wallets/imtoken.svg",
mobileDisabled: false,
windowPropertyName: "cosmos",
features: [],
lazyInstall: () =>
import("@cosmos-kit/imtoken-extension").then((m) => m.IMTokenWallet),
async supportsChain(chainId: string) {
const cdcAvailableChains: MainnetChainIds[] = ["cosmoshub-4", "osmosis-1"]
return cdcAvailableChains.includes(chainId as MainnetChainIds)
},
},
]
30 changes: 23 additions & 7 deletions packages/web/modals/wallet-select/use-selectable-wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export const useSelectableWallets = ({
* Therefore, we should only show that compatible extension wallet.
* */
if (acc.length > 0 && acc[0].name.endsWith("-extension")) {
return acc;
return acc
}

const _window = window as Record<string, any>;
const mobileWebModeName = "mobile-web";
const _window = window as Record<string, any>
const mobileWebModeName = "mobile-web"

/**
* If on mobile and `leap` is in `window`, it means that the user enters
Expand All @@ -99,7 +99,23 @@ export const useSelectableWallets = ({
(wallet) =>
wallet.name === AvailableCosmosWallets.CryptocomWallet
)
.map((wallet) => ({ ...wallet, mobileDisabled: false }));
.map((wallet) => ({ ...wallet, mobileDisabled: false }))
}

/**
* If on mobile and `imToken` is in `window`, it means that the user enters
* the frontend from imToken's app in app browser. So, there is no need
* to use wallet connect, as it resembles the extension's usage.
*/
if (
_window?.imToken &&
_window?.cosmos?.mode === mobileWebModeName
) {
return array
.filter(
(wallet) => wallet.name === AvailableCosmosWallets.imToken
)
.map((wallet) => ({ ...wallet, mobileDisabled: false }))
}

/**
Expand All @@ -110,7 +126,7 @@ export const useSelectableWallets = ({
if (_window?.leap && _window?.leap?.mode === mobileWebModeName) {
return array
.filter((wallet) => wallet.name === AvailableCosmosWallets.Leap)
.map((wallet) => ({ ...wallet, mobileDisabled: false }));
.map((wallet) => ({ ...wallet, mobileDisabled: false }))
}

/**
Expand All @@ -123,13 +139,13 @@ export const useSelectableWallets = ({
.filter(
(wallet) => wallet.name === AvailableCosmosWallets.Keplr
)
.map((wallet) => ({ ...wallet, mobileDisabled: false }));
.map((wallet) => ({ ...wallet, mobileDisabled: false }))
}

/**
* If user is in a normal mobile browser, show only wallet connect
*/
return wallet.name.endsWith("mobile") ? [...acc, wallet] : acc;
return wallet.name.endsWith("mobile") ? [...acc, wallet] : acc
}

return [...acc, wallet];
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@cosmos-kit/cdcwallet": "^2.13.2",
"@cosmos-kit/core": "^2.13.1",
"@cosmos-kit/cosmostation": "^2.11.2",
"@cosmos-kit/imtoken": "^1.0.0",
"@cosmos-kit/keplr": "^2.12.2",
"@cosmos-kit/leap": "^2.12.2",
"@cosmos-kit/okxwallet": "^2.9.2",
Expand Down
20 changes: 20 additions & 0 deletions packages/web/public/wallets/imtoken.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2990,6 +2990,21 @@
"@cosmos-kit/cosmostation-extension" "^2.12.2"
"@cosmos-kit/cosmostation-mobile" "^2.11.2"

"@cosmos-kit/imtoken-extension@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@cosmos-kit/imtoken-extension/-/imtoken-extension-1.1.0.tgz#4d1c4ea2a0bd4548969cf4fd58503321230336c4"
integrity sha512-6vNrjVqLXdFcAEFaPCM0uFQjbLMaKymbahk2YW3qN8fUGopdmfDbFuBJj6IKqKEu9uOijmfvUcmKDZIax+gurg==
dependencies:
"@chain-registry/keplr" "1.68.2"
"@cosmos-kit/core" "^2.13.1"

"@cosmos-kit/imtoken@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@cosmos-kit/imtoken/-/imtoken-1.1.0.tgz#962e281a783a8fd1647fb9aa437d0fcf0159db46"
integrity sha512-PRN8OzMDbT1j5Zs5HgN34laXUUStZgxMs6cDibd9yOa3BBf0LU74CQQe+ucTLhfydrD8uqXxfsq2jg7YVxAPPg==
dependencies:
"@cosmos-kit/imtoken-extension" "^1.1.0"

"@cosmos-kit/keplr-extension@^2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@cosmos-kit/keplr-extension/-/keplr-extension-2.12.2.tgz#d632a1e53eb821b70851ae2aba6c473b4b79dd67"
Expand Down

0 comments on commit 3205843

Please sign in to comment.