@tomo-inc/wallet-adaptor-base 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/README.md +60 -0
- package/dist/index.cjs +2485 -0
- package/dist/index.d.cts +184 -0
- package/dist/index.d.ts +184 -0
- package/dist/index.js +2471 -0
- package/package.json +31 -0
- package/project.json +67 -0
- package/src/chains/type.ts +3 -0
- package/src/chains/utils.ts +20 -0
- package/src/index.ts +93 -0
- package/src/svg.d.ts +4 -0
- package/src/type.ts +99 -0
- package/src/utils/browsers.ts +39 -0
- package/src/utils/chainId.ts +27 -0
- package/src/utils/isMobile.ts +22 -0
- package/src/utils/utils.ts +14 -0
- package/src/wallet-api/balance.ts +96 -0
- package/src/wallet-api/chain.ts +59 -0
- package/src/wallet-api/connect.ts +65 -0
- package/src/wallet-api/index.ts +5 -0
- package/src/wallet-api/sign-in.ts +103 -0
- package/src/wallet-api/sign-message.ts +60 -0
- package/src/wallets/Wallet.ts +37 -0
- package/src/wallets/default/backpackWallet/backpackWallet.svg +1 -0
- package/src/wallets/default/backpackWallet/backpackWallet.ts +23 -0
- package/src/wallets/default/berasigWallet/berasigWallet.svg +1 -0
- package/src/wallets/default/berasigWallet/berasigWallet.ts +22 -0
- package/src/wallets/default/binanceWallet/binanceWallet.svg +1 -0
- package/src/wallets/default/binanceWallet/binanceWallet.ts +23 -0
- package/src/wallets/default/bitgetWallet/bitgetWallet.svg +4 -0
- package/src/wallets/default/bitgetWallet/bitgetWallet.ts +30 -0
- package/src/wallets/default/bitskiWallet/bitskiWallet.svg +1 -0
- package/src/wallets/default/bitskiWallet/bitskiWallet.ts +17 -0
- package/src/wallets/default/bitverseWallet/bitverseWallet.svg +1 -0
- package/src/wallets/default/bitverseWallet/bitverseWallet.ts +17 -0
- package/src/wallets/default/bloomWallet/bloomWallet.svg +1 -0
- package/src/wallets/default/bloomWallet/bloomWallet.ts +15 -0
- package/src/wallets/default/braveWallet/braveWallet.svg +1 -0
- package/src/wallets/default/braveWallet/braveWallet.ts +20 -0
- package/src/wallets/default/bybitWallet/bybitWallet.svg +1 -0
- package/src/wallets/default/bybitWallet/bybitWallet.ts +23 -0
- package/src/wallets/default/clvWallet/clvWallet.svg +1 -0
- package/src/wallets/default/clvWallet/clvWallet.ts +21 -0
- package/src/wallets/default/coin98Wallet/coin98Wallet.svg +1 -0
- package/src/wallets/default/coin98Wallet/coin98Wallet.ts +25 -0
- package/src/wallets/default/coinbaseWallet/coinbaseWallet.svg +4 -0
- package/src/wallets/default/coinbaseWallet/coinbaseWallet.ts +25 -0
- package/src/wallets/default/compassWallet/compassWallet.svg +1 -0
- package/src/wallets/default/compassWallet/compassWallet.ts +19 -0
- package/src/wallets/default/coreWallet/coreWallet.svg +7 -0
- package/src/wallets/default/coreWallet/coreWallet.ts +24 -0
- package/src/wallets/default/ctrlWallet/ctrlWallet.svg +13 -0
- package/src/wallets/default/ctrlWallet/ctrlWallet.ts +24 -0
- package/src/wallets/default/dawnWallet/dawnWallet.svg +23 -0
- package/src/wallets/default/dawnWallet/dawnWallet.ts +16 -0
- package/src/wallets/default/desigWallet/desigWallet.svg +1 -0
- package/src/wallets/default/desigWallet/desigWallet.ts +21 -0
- package/src/wallets/default/enkryptWallet/enkryptWallet.svg +10 -0
- package/src/wallets/default/enkryptWallet/enkryptWallet.ts +24 -0
- package/src/wallets/default/foxWallet/foxWallet.svg +6 -0
- package/src/wallets/default/foxWallet/foxWallet.ts +19 -0
- package/src/wallets/default/frameWallet/frameWallet.svg +1 -0
- package/src/wallets/default/frameWallet/frameWallet.ts +17 -0
- package/src/wallets/default/gateWallet/gateWallet.svg +1 -0
- package/src/wallets/default/gateWallet/gateWallet.ts +24 -0
- package/src/wallets/default/imTokenWallet/imTokenWallet.svg +10 -0
- package/src/wallets/default/imTokenWallet/imTokenWallet.ts +18 -0
- package/src/wallets/default/injectedWallet/injectedWallet.svg +1 -0
- package/src/wallets/default/injectedWallet/injectedWallet.ts +12 -0
- package/src/wallets/default/iopayWallet/iopayWallet.svg +1 -0
- package/src/wallets/default/iopayWallet/iopayWallet.ts +18 -0
- package/src/wallets/default/kaiaWallet/kaiaWallet.svg +16 -0
- package/src/wallets/default/kaiaWallet/kaiaWallet.ts +22 -0
- package/src/wallets/default/kaikasWallet/kaikasWallet.svg +1 -0
- package/src/wallets/default/kaikasWallet/kaikasWallet.ts +22 -0
- package/src/wallets/default/krakenWallet/krakenWallet.svg +1 -0
- package/src/wallets/default/krakenWallet/krakenWallet.ts +17 -0
- package/src/wallets/default/kresusWallet/kresusWallet.svg +1 -0
- package/src/wallets/default/kresusWallet/kresusWallet.ts +17 -0
- package/src/wallets/default/ledgerWallet/ledgerWallet.svg +1 -0
- package/src/wallets/default/ledgerWallet/ledgerWallet.ts +23 -0
- package/src/wallets/default/magicEdenWallet/magicEden.svg +1 -0
- package/src/wallets/default/magicEdenWallet/magicEdenWallet.ts +25 -0
- package/src/wallets/default/mathWallet/icon.ts +2 -0
- package/src/wallets/default/mathWallet/mathWallet.ts +24 -0
- package/src/wallets/default/metaMaskWallet/icon.ts +2 -0
- package/src/wallets/default/metaMaskWallet/metaMaskWallet.svg +32 -0
- package/src/wallets/default/metaMaskWallet/metaMaskWallet.ts +88 -0
- package/src/wallets/default/myDogeWallet/icon.ts +2 -0
- package/src/wallets/default/myDogeWallet/mydogeWallet.svg +190 -0
- package/src/wallets/default/myDogeWallet/mydogeWallet.ts +20 -0
- package/src/wallets/default/nestWallet/nestWallet.svg +1 -0
- package/src/wallets/default/nestWallet/nestWallet.ts +17 -0
- package/src/wallets/default/novaWallet/novaWallet.svg +19 -0
- package/src/wallets/default/novaWallet/novaWallet.ts +30 -0
- package/src/wallets/default/oktoWallet/oktoWallet.svg +1 -0
- package/src/wallets/default/oktoWallet/oktoWallet.ts +18 -0
- package/src/wallets/default/okxWallet/okxWallet.svg +1 -0
- package/src/wallets/default/okxWallet/okxWallet.ts +32 -0
- package/src/wallets/default/omniWallet/omniWallet.svg +5 -0
- package/src/wallets/default/omniWallet/omniWallet.ts +18 -0
- package/src/wallets/default/oneInchWallet/oneInchWallet.svg +1 -0
- package/src/wallets/default/oneInchWallet/oneInchWallet.ts +18 -0
- package/src/wallets/default/oneKeyWallet/oneKeyWallet.svg +1 -0
- package/src/wallets/default/oneKeyWallet/oneKeyWallet.ts +25 -0
- package/src/wallets/default/paraSwapWallet/paraSwapWallet.svg +1 -0
- package/src/wallets/default/paraSwapWallet/paraswapWallet.ts +17 -0
- package/src/wallets/default/phantomWallet/phantomWallet.svg +1 -0
- package/src/wallets/default/phantomWallet/phantomWallet.ts +30 -0
- package/src/wallets/default/rabbyWallet/rabbyWallet.svg +1 -0
- package/src/wallets/default/rabbyWallet/rabbyWallet.ts +18 -0
- package/src/wallets/default/rainbowWallet/rainbowWallet.svg +54 -0
- package/src/wallets/default/rainbowWallet/rainbowWallet.ts +24 -0
- package/src/wallets/default/ramperWallet/ramperWallet.svg +1 -0
- package/src/wallets/default/ramperWallet/ramperWallet.ts +19 -0
- package/src/wallets/default/readyWallet/readyWallet.svg +4 -0
- package/src/wallets/default/readyWallet/readyWallet.ts +18 -0
- package/src/wallets/default/roninWallet/roninWallet.svg +1 -0
- package/src/wallets/default/roninWallet/roninWallet.ts +25 -0
- package/src/wallets/default/safeWallet/safeWallet.svg +6 -0
- package/src/wallets/default/safeWallet/safeWallet.ts +18 -0
- package/src/wallets/default/safeheronWallet/safeheronWallet.svg +1 -0
- package/src/wallets/default/safeheronWallet/safeheronWallet.ts +17 -0
- package/src/wallets/default/safepalWallet/safepalWallet.svg +1 -0
- package/src/wallets/default/safepalWallet/safepalWallet.ts +24 -0
- package/src/wallets/default/seifWallet/seifWallet.svg +12 -0
- package/src/wallets/default/seifWallet/seifWallet.ts +18 -0
- package/src/wallets/default/solflareWallet/icon.ts +2 -0
- package/src/wallets/default/solflareWallet/solflareWallet.ts +23 -0
- package/src/wallets/default/subWallet/subWallet.svg +1 -0
- package/src/wallets/default/subWallet/subWallet.ts +25 -0
- package/src/wallets/default/tahoWallet/tahoWallet.svg +1 -0
- package/src/wallets/default/tahoWallet/tahoWallet.ts +19 -0
- package/src/wallets/default/talismanWallet/talismanWallet.svg +1 -0
- package/src/wallets/default/talismanWallet/talismanWallet.ts +19 -0
- package/src/wallets/default/tokenPocketWallet/tokenPocketWallet.svg +1 -0
- package/src/wallets/default/tokenPocketWallet/tokenPocketWallet.ts +26 -0
- package/src/wallets/default/tokenaryWallet/tokenaryWallet.svg +1 -0
- package/src/wallets/default/tokenaryWallet/tokenaryWallet.ts +19 -0
- package/src/wallets/default/trezorWallet/icon.ts +2 -0
- package/src/wallets/default/trezorWallet/trezorWallet.ts +19 -0
- package/src/wallets/default/trustWallet/trustWallet.svg +1 -0
- package/src/wallets/default/trustWallet/trustWallet.ts +25 -0
- package/src/wallets/default/uniswapWallet/uniswapWallet.svg +1 -0
- package/src/wallets/default/uniswapWallet/uniswapWallet.ts +17 -0
- package/src/wallets/default/universalProfilesWallet/icon.ts +2 -0
- package/src/wallets/default/universalProfilesWallet/universalProfilesWallet.svg +1353 -0
- package/src/wallets/default/universalProfilesWallet/universalProfilesWallet.ts +22 -0
- package/src/wallets/default/valoraWallet/valoraWallet.svg +1 -0
- package/src/wallets/default/valoraWallet/valoraWallet.ts +18 -0
- package/src/wallets/default/walletConnectWallet/walletConnectWallet.svg +4 -0
- package/src/wallets/default/walletConnectWallet/walletConnectWallet.ts +18 -0
- package/src/wallets/default/wigwamWallet/wigwamWallet.svg +9 -0
- package/src/wallets/default/wigwamWallet/wigwamWallet.ts +20 -0
- package/src/wallets/default/xPortalWallet/xPortalWallet.svg +6 -0
- package/src/wallets/default/xPortalWallet/xPortalWallet.ts +22 -0
- package/src/wallets/default/zealWallet/zealWallet.svg +1 -0
- package/src/wallets/default/zealWallet/zealWallet.ts +25 -0
- package/src/wallets/default/zerionWallet/zerionWallet.svg +1 -0
- package/src/wallets/default/zerionWallet/zerionWallet.ts +24 -0
- package/src/wallets/default/zilPayWallet/zilPayWallet.svg +17 -0
- package/src/wallets/default/zilPayWallet/zilPayWallet.ts +21 -0
- package/src/wallets/detector.ts +136 -0
- package/src/wallets/index.ts +210 -0
- package/src/wallets/providers/WalletConnectProvider.ts +346 -0
- package/src/wallets/providers/WalletConnectSolanaProvider.ts +351 -0
- package/src/wallets/wallet-eip6963.ts +70 -0
- package/src/wallets/wallet-standard.ts +77 -0
- package/src/wallets/wallet-walletconnect.ts +202 -0
- package/tsconfig.json +7 -0
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tomo-inc/wallet-adaptor-base",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "tomo.inc",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"private": false,
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@solana/spl-token": "^0.4.13",
|
|
20
|
+
"@solana/web3.js": "^1.98.0",
|
|
21
|
+
"@aptos-labs/ts-sdk": "^5.1.1",
|
|
22
|
+
"viem": "2.21.54",
|
|
23
|
+
"@tomo-inc/wallet-connect-protocol": "0.0.3"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.0.0",
|
|
27
|
+
"tsup": "^8.0.0",
|
|
28
|
+
"typescript": "^5.0.0",
|
|
29
|
+
"vitest": "^3.2.4"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wallet-adaptor-base",
|
|
3
|
+
"sourceRoot": "packages/wallet-adaptor-base/src",
|
|
4
|
+
"projectType": "library",
|
|
5
|
+
"targets": {
|
|
6
|
+
"build": {
|
|
7
|
+
"executor": "nx:run-commands",
|
|
8
|
+
"outputs": ["{projectRoot}/dist"],
|
|
9
|
+
"options": {
|
|
10
|
+
"command": "tsup src/index.ts --format esm,cjs --dts --treeshake",
|
|
11
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dev": {
|
|
15
|
+
"executor": "nx:run-commands",
|
|
16
|
+
"options": {
|
|
17
|
+
"command": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
18
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"prepare": {
|
|
22
|
+
"executor": "nx:run-commands",
|
|
23
|
+
"dependsOn": ["build"],
|
|
24
|
+
"options": {
|
|
25
|
+
"command": "pnpm build",
|
|
26
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"lint": {
|
|
30
|
+
"executor": "nx:run-commands",
|
|
31
|
+
"options": {
|
|
32
|
+
"command": "eslint src/**/*.ts",
|
|
33
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"lint:fix": {
|
|
37
|
+
"executor": "nx:run-commands",
|
|
38
|
+
"options": {
|
|
39
|
+
"command": "eslint src/**/*.ts --fix",
|
|
40
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"format": {
|
|
44
|
+
"executor": "nx:run-commands",
|
|
45
|
+
"options": {
|
|
46
|
+
"command": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
47
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"test": {
|
|
51
|
+
"executor": "nx:run-commands",
|
|
52
|
+
"outputs": ["{projectRoot}/coverage"],
|
|
53
|
+
"options": {
|
|
54
|
+
"command": "vitest run",
|
|
55
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"test:watch": {
|
|
59
|
+
"executor": "nx:run-commands",
|
|
60
|
+
"options": {
|
|
61
|
+
"command": "vitest",
|
|
62
|
+
"cwd": "packages/wallet-adaptor-base"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"tags": ["npm:public", "scope:wallet-adaptor-base", "type:library"]
|
|
67
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AdaptorChainType } from "../type";
|
|
2
|
+
import { ChainConfig, WagmiChainConfig } from "./type";
|
|
3
|
+
|
|
4
|
+
export function chainInfoAdapter(
|
|
5
|
+
chainConfig: ChainConfig | WagmiChainConfig,
|
|
6
|
+
chainInfoType: string,
|
|
7
|
+
chainType: AdaptorChainType,
|
|
8
|
+
): any {
|
|
9
|
+
if (chainType === "evm") {
|
|
10
|
+
console.log(chainConfig);
|
|
11
|
+
}
|
|
12
|
+
switch (chainInfoType) {
|
|
13
|
+
case "wagmi":
|
|
14
|
+
return null as any;
|
|
15
|
+
case "tomo":
|
|
16
|
+
return null as any;
|
|
17
|
+
default:
|
|
18
|
+
return null as any;
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AdaptorChainType, Connector, WalletConnectorType } from "./type";
|
|
2
|
+
import { uniqueConnectors } from "./utils/utils";
|
|
3
|
+
import { defaultConnectors } from "./wallets";
|
|
4
|
+
import { WagmiWalletConfig, WalletConfig } from "./wallets/Wallet";
|
|
5
|
+
import { connectorDector, supportedWalletConfigTypes, walletConfigAdapter } from "./wallets/detector";
|
|
6
|
+
import { eip6963Wallets } from "./wallets/wallet-eip6963";
|
|
7
|
+
import { walletStandardWallets } from "./wallets/wallet-standard";
|
|
8
|
+
import { setWalletConnectConfig, walletConnectWallets } from "./wallets/wallet-walletconnect";
|
|
9
|
+
export * from "./type";
|
|
10
|
+
export { setWalletConnectConfig };
|
|
11
|
+
export type { WalletConfig };
|
|
12
|
+
|
|
13
|
+
//wallets list
|
|
14
|
+
export const TomoDefaultConnectors = defaultConnectors
|
|
15
|
+
.filter((wallet) => wallet?.info?.links?.chrome_install !== undefined)
|
|
16
|
+
.sort((a, _b) => (a.isInstalled ? -1 : 1));
|
|
17
|
+
|
|
18
|
+
export async function loadConnectors({
|
|
19
|
+
chainType = "all",
|
|
20
|
+
recommonedConnectors,
|
|
21
|
+
connectorTypes = [],
|
|
22
|
+
}: {
|
|
23
|
+
chainType?: AdaptorChainType;
|
|
24
|
+
recommonedConnectors?: (WalletConfig | WagmiWalletConfig)[];
|
|
25
|
+
connectorTypes?: WalletConnectorType[];
|
|
26
|
+
}): Promise<{
|
|
27
|
+
all: Connector[];
|
|
28
|
+
recommoned: Connector[];
|
|
29
|
+
}> {
|
|
30
|
+
const recommonedWalletConfigs = recommonedConnectors || [];
|
|
31
|
+
|
|
32
|
+
const evmWallets = await eip6963Wallets();
|
|
33
|
+
const { solanaWallets, aptosWallets } = (await walletStandardWallets()) as {
|
|
34
|
+
solanaWallets: Connector[];
|
|
35
|
+
aptosWallets: Connector[];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// WalletConnect wallets from WalletConnect Cloud Explorer API
|
|
39
|
+
const wcWallets = await walletConnectWallets();
|
|
40
|
+
|
|
41
|
+
let recommonedConnectorsDetected: Connector[] = [];
|
|
42
|
+
if (recommonedWalletConfigs.length > 0) {
|
|
43
|
+
recommonedConnectorsDetected = recommonedWalletConfigs.map((walletConfig, index) => {
|
|
44
|
+
const connectorType = connectorTypes[index];
|
|
45
|
+
if (!supportedWalletConfigTypes[connectorType]) {
|
|
46
|
+
throw new Error(`Unsupported wallet config type: ${connectorType}`);
|
|
47
|
+
}
|
|
48
|
+
const _walletConfig = walletConfigAdapter(walletConfig, connectorType);
|
|
49
|
+
return {
|
|
50
|
+
...connectorDector(_walletConfig),
|
|
51
|
+
recommoned: true,
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const connectorsFromConfig = uniqueConnectors([...recommonedConnectorsDetected, ...defaultConnectors]);
|
|
57
|
+
|
|
58
|
+
// Include WalletConnect wallets along with other detected wallets
|
|
59
|
+
const connectorsDetected = [...evmWallets, ...solanaWallets, ...aptosWallets, ...wcWallets];
|
|
60
|
+
|
|
61
|
+
for (const connector of connectorsDetected) {
|
|
62
|
+
const connectorsDetected = connectorsFromConfig.find((c) => c.info.name === connector.info.name);
|
|
63
|
+
if (connectorsDetected) {
|
|
64
|
+
connectorsDetected.isInstalled = true;
|
|
65
|
+
connectorsDetected.providers = { ...connectorsDetected.providers, ...connector.providers };
|
|
66
|
+
} else {
|
|
67
|
+
connectorsFromConfig.push(connector);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const allConnectors = connectorsFromConfig
|
|
72
|
+
.filter((wallet) => (chainType !== "all" ? !!wallet.providers[chainType] : true))
|
|
73
|
+
.sort((a, b) => {
|
|
74
|
+
// First priority: recommoned wallets
|
|
75
|
+
if (a.recommoned && !b.recommoned) return -1;
|
|
76
|
+
if (!a.recommoned && b.recommoned) return 1;
|
|
77
|
+
|
|
78
|
+
// Second priority: installed wallets
|
|
79
|
+
if (a.isInstalled && !b.isInstalled) return -1;
|
|
80
|
+
if (!a.isInstalled && b.isInstalled) return 1;
|
|
81
|
+
|
|
82
|
+
// Equal priority: maintain original order
|
|
83
|
+
return 0;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
all: allConnectors,
|
|
88
|
+
recommoned: recommonedConnectorsDetected,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//api
|
|
93
|
+
export * from "./wallet-api";
|
package/src/svg.d.ts
ADDED
package/src/type.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export type WalletProvider = any;
|
|
2
|
+
|
|
3
|
+
export enum ProviderProtocol {
|
|
4
|
+
EIP6963 = "eip6963",
|
|
5
|
+
WALLET_STANDARD = "wallet-standard",
|
|
6
|
+
WALLET_CONNECT = "wallet-connect",
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export enum ProviderChainType {
|
|
10
|
+
inject = "inject",
|
|
11
|
+
deeplink = "deeplink",
|
|
12
|
+
walletConnect = "walletConnect",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type WalletConnectorType = "wagmi" | "tomo";
|
|
16
|
+
|
|
17
|
+
export type AdaptorChainType = "all" | "evm" | "solana" | "aptos";
|
|
18
|
+
|
|
19
|
+
export const SupportedChainTypes = ["evm", "solana", "aptos"];
|
|
20
|
+
|
|
21
|
+
export interface WalletInfo {
|
|
22
|
+
uuid: string;
|
|
23
|
+
name: string;
|
|
24
|
+
namespace?: string;
|
|
25
|
+
icon: string;
|
|
26
|
+
iconBackground?: string;
|
|
27
|
+
rdns?: string;
|
|
28
|
+
isWalletConnect?: boolean;
|
|
29
|
+
links: {
|
|
30
|
+
homepage?: string;
|
|
31
|
+
ios_install?: string;
|
|
32
|
+
android_install?: string;
|
|
33
|
+
chrome_install?: string;
|
|
34
|
+
|
|
35
|
+
mobile?: string;
|
|
36
|
+
qrCode?: string;
|
|
37
|
+
edge?: string;
|
|
38
|
+
firefox?: string;
|
|
39
|
+
opera?: string;
|
|
40
|
+
safari?: string;
|
|
41
|
+
macos?: string;
|
|
42
|
+
windows?: string;
|
|
43
|
+
linux?: string;
|
|
44
|
+
desktop?: string;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ConnectorProvider {
|
|
49
|
+
provider: any;
|
|
50
|
+
protocol: ProviderProtocol;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface Connector {
|
|
54
|
+
info: WalletInfo;
|
|
55
|
+
isInstalled?: boolean;
|
|
56
|
+
recommoned?: boolean;
|
|
57
|
+
|
|
58
|
+
providers: {
|
|
59
|
+
evm?: ConnectorProvider;
|
|
60
|
+
solana?: ConnectorProvider;
|
|
61
|
+
aptos?: ConnectorProvider;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SignInParams {
|
|
66
|
+
scheme?: "https" | "http";
|
|
67
|
+
domain: string;
|
|
68
|
+
uri?: string;
|
|
69
|
+
chainId?: string;
|
|
70
|
+
address?: string;
|
|
71
|
+
statement: string;
|
|
72
|
+
version?: string;
|
|
73
|
+
nonce: string;
|
|
74
|
+
issuedAt?: string;
|
|
75
|
+
resources?: `https://${string}`[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface WalletOptions {
|
|
79
|
+
chainType: AdaptorChainType;
|
|
80
|
+
account: {
|
|
81
|
+
address: string;
|
|
82
|
+
chainId?: string;
|
|
83
|
+
network?: string;
|
|
84
|
+
};
|
|
85
|
+
provider: WalletProvider;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ChainInfo {
|
|
89
|
+
chainId: string;
|
|
90
|
+
chainName: string;
|
|
91
|
+
rpcUrls: string[];
|
|
92
|
+
iconUrls: string[];
|
|
93
|
+
nativeCurrency: {
|
|
94
|
+
name: string;
|
|
95
|
+
symbol: string;
|
|
96
|
+
decimals: number;
|
|
97
|
+
};
|
|
98
|
+
blockExplorerUrls: string[];
|
|
99
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function isSafari(): boolean {
|
|
2
|
+
return (
|
|
3
|
+
typeof navigator !== "undefined" &&
|
|
4
|
+
typeof navigator.userAgent !== "undefined" &&
|
|
5
|
+
/Version\/([0-9._]+).*Safari/.test(navigator.userAgent) // Source: https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isArc(): boolean {
|
|
10
|
+
return (
|
|
11
|
+
typeof document !== "undefined" && getComputedStyle(document.body).getPropertyValue("--arc-palette-focus") !== ""
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export enum BrowserType {
|
|
16
|
+
Arc = "Arc",
|
|
17
|
+
Brave = "Brave",
|
|
18
|
+
Browser = "Browser",
|
|
19
|
+
Chrome = "Chrome",
|
|
20
|
+
Edge = "Edge",
|
|
21
|
+
Firefox = "Firefox",
|
|
22
|
+
Opera = "Opera",
|
|
23
|
+
Safari = "Safari",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getBrowser(): BrowserType {
|
|
27
|
+
// bail out if `navigator` or `navigator.userAgent` is not available
|
|
28
|
+
if (typeof navigator === "undefined") return BrowserType.Browser;
|
|
29
|
+
const ua = navigator.userAgent?.toLowerCase();
|
|
30
|
+
// @ts-expect-error - brave is not in the navigator type
|
|
31
|
+
if (navigator.brave?.isBrave) return BrowserType.Brave;
|
|
32
|
+
if (ua?.indexOf("edg/") > -1) return BrowserType.Edge;
|
|
33
|
+
if (ua?.indexOf("op") > -1) return BrowserType.Opera;
|
|
34
|
+
if (isArc()) return BrowserType.Arc;
|
|
35
|
+
if (ua?.indexOf("chrome") > -1) return BrowserType.Chrome;
|
|
36
|
+
if (ua?.indexOf("firefox") > -1) return BrowserType.Firefox;
|
|
37
|
+
if (isSafari()) return BrowserType.Safari;
|
|
38
|
+
return BrowserType.Browser;
|
|
39
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isHex, toHex, fromHex, isAddressEqual } from "viem";
|
|
2
|
+
|
|
3
|
+
export const isHexChainId = (chainIdHex: string) => {
|
|
4
|
+
return isHex(chainIdHex);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const isSameAddress = (addr1: "0x${string}", addr2: "0x${string}") => {
|
|
8
|
+
return isAddressEqual(addr1, addr2);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const getAllTypeChainIds = ({ chainId, chainType }: { chainId: string | number; chainType: string }) => {
|
|
12
|
+
if (isHex(chainId)) {
|
|
13
|
+
const chainIdHex = chainId;
|
|
14
|
+
chainId = fromHex(chainId, "number").toString();
|
|
15
|
+
return {
|
|
16
|
+
chainId,
|
|
17
|
+
chainIdHex,
|
|
18
|
+
chainUid: `${chainType}:${chainId}`,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const chainIdHex = toHex(Number(chainId));
|
|
22
|
+
return {
|
|
23
|
+
chainId,
|
|
24
|
+
chainIdHex,
|
|
25
|
+
chainUid: `${chainType}:${chainId}`,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function isAndroid(): boolean {
|
|
2
|
+
return typeof navigator !== "undefined" && /android/i.test(navigator.userAgent);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function isSmallIOS(): boolean {
|
|
6
|
+
return typeof navigator !== "undefined" && /iPhone|iPod/.test(navigator.userAgent);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isLargeIOS(): boolean {
|
|
10
|
+
return (
|
|
11
|
+
typeof navigator !== "undefined" &&
|
|
12
|
+
(/iPad/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1))
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isIOS(): boolean {
|
|
17
|
+
return isSmallIOS() || isLargeIOS();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isMobile(): boolean {
|
|
21
|
+
return isAndroid() || isIOS();
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Connector } from "type";
|
|
2
|
+
|
|
3
|
+
export function uniqueConnectors(allConnectors: Connector[]) {
|
|
4
|
+
const connectors: Connector[] = [];
|
|
5
|
+
const seenRdns = new Set();
|
|
6
|
+
allConnectors.forEach((connector) => {
|
|
7
|
+
const key = encodeURIComponent(connector.info.rdns || connector.info.name);
|
|
8
|
+
if (!seenRdns.has(key)) {
|
|
9
|
+
seenRdns.add(key);
|
|
10
|
+
connectors.push(connector);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
return connectors;
|
|
14
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { PublicKey, Connection } from "@solana/web3.js";
|
|
2
|
+
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
|
|
3
|
+
import { WalletOptions } from "type";
|
|
4
|
+
|
|
5
|
+
export interface BalanceParams {
|
|
6
|
+
address?: string;
|
|
7
|
+
chainId: string;
|
|
8
|
+
type?: "erc20" | "spl" | "native";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ITokenInfo {
|
|
12
|
+
name: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
decimals: number;
|
|
15
|
+
address?: string;
|
|
16
|
+
icon: string;
|
|
17
|
+
price?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const getBalance = async (
|
|
21
|
+
token: BalanceParams,
|
|
22
|
+
walletOptions: WalletOptions,
|
|
23
|
+
): Promise<{ total: number; tokenInfo: ITokenInfo }> => {
|
|
24
|
+
const { provider, chainType, account } = walletOptions;
|
|
25
|
+
const { address } = account;
|
|
26
|
+
const { chainId } = token;
|
|
27
|
+
if (!provider) {
|
|
28
|
+
throw new Error("Provider is required");
|
|
29
|
+
}
|
|
30
|
+
if (!chainType) {
|
|
31
|
+
throw new Error("Chain type is required");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const tokenInfo: any = null;
|
|
35
|
+
if (chainType === "evm") {
|
|
36
|
+
if (!chainId || chainId !== account.chainId) {
|
|
37
|
+
throw new Error("Chain id is required and must be current chain id");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const balance = await provider.request({
|
|
41
|
+
method: "eth_getBalance",
|
|
42
|
+
params: [address, "latest"],
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
total: Number(balance || "0") || 0,
|
|
46
|
+
tokenInfo,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (chainType === "solana") {
|
|
51
|
+
const rpcUrl = "https://rpc.ankr.com/solana/c2d7e8a3db5dce62e202db3d28cca25e74da5028abbf20764e2961918ba34dfc";
|
|
52
|
+
const connection = new Connection(rpcUrl);
|
|
53
|
+
|
|
54
|
+
const balances = await connection.getMultipleAccountsInfo([address].map((addr) => new PublicKey(addr)));
|
|
55
|
+
console.log("solana balances", balances);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
total: balances[0]?.lamports || 0,
|
|
59
|
+
tokenInfo,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//https://www.npmjs.com/package/@aptos-labs/ts-sdk
|
|
64
|
+
if (chainType === "aptos") {
|
|
65
|
+
if (!address) {
|
|
66
|
+
throw new Error("Address is required for Aptos balance query");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let balance = 0;
|
|
70
|
+
try {
|
|
71
|
+
const network = account?.network?.toLowerCase() as Network;
|
|
72
|
+
const aptos = new Aptos(new AptosConfig({ network }));
|
|
73
|
+
|
|
74
|
+
const resourceType = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
|
|
75
|
+
|
|
76
|
+
const accountResource = await aptos.getAccountResource<{
|
|
77
|
+
coin: { value: string };
|
|
78
|
+
}>({
|
|
79
|
+
accountAddress: address,
|
|
80
|
+
resourceType,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
balance = Number(accountResource?.coin?.value || "0") || 0;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error("Failed to get Aptos balance:", error);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
total: balance,
|
|
91
|
+
tokenInfo,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw new Error(`getBalance not supported in ${chainType}`);
|
|
96
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ChainInfo, WalletOptions } from "type";
|
|
2
|
+
import { getAllTypeChainIds } from "../utils/chainId";
|
|
3
|
+
|
|
4
|
+
export const switchChain = async (chainId: string | number, walletOptions: WalletOptions) => {
|
|
5
|
+
const { provider, chainType } = walletOptions;
|
|
6
|
+
|
|
7
|
+
if (!provider) {
|
|
8
|
+
throw new Error("Provider is required");
|
|
9
|
+
}
|
|
10
|
+
if (!chainType) {
|
|
11
|
+
throw new Error("Chain type is required");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (chainType === "evm") {
|
|
15
|
+
const { chainIdHex } = getAllTypeChainIds({ chainId, chainType });
|
|
16
|
+
if (!chainId || !chainIdHex) {
|
|
17
|
+
throw new Error("Chain ID is required, or invalid chain ID");
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
await provider.request({
|
|
21
|
+
method: "wallet_switchEthereumChain",
|
|
22
|
+
params: [{ chainId: chainIdHex }],
|
|
23
|
+
});
|
|
24
|
+
return true;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw error as Error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
throw new Error(`switchChain Not supported in ${chainType}`);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const addChain = async (chainInfo: ChainInfo, walletOptions: WalletOptions) => {
|
|
34
|
+
const { provider, chainType } = walletOptions;
|
|
35
|
+
|
|
36
|
+
if (!provider) {
|
|
37
|
+
throw new Error("Provider is required");
|
|
38
|
+
}
|
|
39
|
+
if (!chainType) {
|
|
40
|
+
throw new Error("Chain type is required");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (chainType === "evm") {
|
|
44
|
+
if (!chainInfo?.chainId || !chainInfo?.chainId.startsWith("0x")) {
|
|
45
|
+
throw new Error("Chain ID must starting with 0x");
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
await provider.request({
|
|
49
|
+
method: "wallet_addEthereumChain",
|
|
50
|
+
params: [chainInfo],
|
|
51
|
+
});
|
|
52
|
+
return true;
|
|
53
|
+
} catch (error: any) {
|
|
54
|
+
throw error as Error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
throw new Error(`addChain Not supported in ${chainType}`);
|
|
59
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { WalletOptions } from "type";
|
|
2
|
+
|
|
3
|
+
export const connect = async (
|
|
4
|
+
walletOptions: WalletOptions,
|
|
5
|
+
): Promise<{ address: string; chainId?: string; network?: string }> => {
|
|
6
|
+
const { provider, chainType } = walletOptions;
|
|
7
|
+
|
|
8
|
+
if (!provider) {
|
|
9
|
+
throw new Error("Provider is required");
|
|
10
|
+
}
|
|
11
|
+
if (!chainType) {
|
|
12
|
+
throw new Error("Chain type is required");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let chainId = "";
|
|
16
|
+
let address = "";
|
|
17
|
+
let network = "";
|
|
18
|
+
if (chainType === "evm") {
|
|
19
|
+
const res = await provider.request({ method: "eth_requestAccounts" });
|
|
20
|
+
address = res?.[0] || "";
|
|
21
|
+
chainId = await provider.request({ method: "eth_chainId" });
|
|
22
|
+
}
|
|
23
|
+
if (chainType === "solana") {
|
|
24
|
+
let res = null;
|
|
25
|
+
if (provider?.connect) {
|
|
26
|
+
res = await provider.connect();
|
|
27
|
+
}
|
|
28
|
+
if (provider?.request) {
|
|
29
|
+
res = await provider.request({
|
|
30
|
+
method: "connect",
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
address = provider?.publicKey?.toString() || "";
|
|
34
|
+
network = "mainnet-beta";
|
|
35
|
+
}
|
|
36
|
+
if (chainType === "aptos") {
|
|
37
|
+
await provider?.connect();
|
|
38
|
+
const account = await provider?.account();
|
|
39
|
+
network = ((await provider?.network()) || "Mainnet") as string;
|
|
40
|
+
address = account?.address || "";
|
|
41
|
+
}
|
|
42
|
+
return { address, chainId, network };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const disconnect = async (walletOptions: WalletOptions): Promise<boolean> => {
|
|
46
|
+
const { provider, chainType } = walletOptions;
|
|
47
|
+
|
|
48
|
+
if (!provider) {
|
|
49
|
+
throw new Error("Provider is required");
|
|
50
|
+
}
|
|
51
|
+
if (!chainType) {
|
|
52
|
+
throw new Error("chainType is required");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (chainType === "evm") {
|
|
56
|
+
await provider.request({ method: "wallet_revokePermissions" });
|
|
57
|
+
}
|
|
58
|
+
if (chainType === "solana") {
|
|
59
|
+
await provider.disconnect();
|
|
60
|
+
}
|
|
61
|
+
if (chainType === "aptos") {
|
|
62
|
+
await provider.disconnect();
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
};
|