@tcswap/wallet-extensions 4.1.16
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/dist/chunk-7y3ffnf3.js +4 -0
- package/dist/chunk-7y3ffnf3.js.map +10 -0
- package/dist/chunk-fazw0jvt.js +4 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-kx2thqhk.js +5 -0
- package/dist/chunk-kx2thqhk.js.map +9 -0
- package/dist/chunk-swh9jwp7.js +4 -0
- package/dist/chunk-swh9jwp7.js.map +10 -0
- package/dist/src/bitget/index.cjs +4 -0
- package/dist/src/bitget/index.cjs.map +11 -0
- package/dist/src/bitget/index.js +4 -0
- package/dist/src/bitget/index.js.map +11 -0
- package/dist/src/cosmostation/index.cjs +4 -0
- package/dist/src/cosmostation/index.cjs.map +10 -0
- package/dist/src/cosmostation/index.js +4 -0
- package/dist/src/cosmostation/index.js.map +10 -0
- package/dist/src/ctrl/index.cjs +4 -0
- package/dist/src/ctrl/index.cjs.map +11 -0
- package/dist/src/ctrl/index.js +4 -0
- package/dist/src/ctrl/index.js.map +11 -0
- package/dist/src/evm-extensions/index.cjs +4 -0
- package/dist/src/evm-extensions/index.cjs.map +10 -0
- package/dist/src/evm-extensions/index.js +4 -0
- package/dist/src/evm-extensions/index.js.map +10 -0
- package/dist/src/index.cjs +4 -0
- package/dist/src/index.cjs.map +9 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +9 -0
- package/dist/src/keepkey-bex/index.cjs +4 -0
- package/dist/src/keepkey-bex/index.cjs.map +11 -0
- package/dist/src/keepkey-bex/index.js +4 -0
- package/dist/src/keepkey-bex/index.js.map +11 -0
- package/dist/src/keplr/index.cjs +4 -0
- package/dist/src/keplr/index.cjs.map +11 -0
- package/dist/src/keplr/index.js +4 -0
- package/dist/src/keplr/index.js.map +11 -0
- package/dist/src/okx/index.cjs +4 -0
- package/dist/src/okx/index.cjs.map +11 -0
- package/dist/src/okx/index.js +4 -0
- package/dist/src/okx/index.js.map +11 -0
- package/dist/src/onekey/index.cjs +4 -0
- package/dist/src/onekey/index.cjs.map +10 -0
- package/dist/src/onekey/index.js +4 -0
- package/dist/src/onekey/index.js.map +10 -0
- package/dist/src/phantom/index.cjs +4 -0
- package/dist/src/phantom/index.cjs.map +10 -0
- package/dist/src/phantom/index.js +4 -0
- package/dist/src/phantom/index.js.map +10 -0
- package/dist/src/polkadotjs/index.cjs +4 -0
- package/dist/src/polkadotjs/index.cjs.map +10 -0
- package/dist/src/polkadotjs/index.js +4 -0
- package/dist/src/polkadotjs/index.js.map +10 -0
- package/dist/src/talisman/index.cjs +4 -0
- package/dist/src/talisman/index.cjs.map +10 -0
- package/dist/src/talisman/index.js +4 -0
- package/dist/src/talisman/index.js.map +10 -0
- package/dist/src/tronlink/index.cjs +4 -0
- package/dist/src/tronlink/index.cjs.map +12 -0
- package/dist/src/tronlink/index.js +4 -0
- package/dist/src/tronlink/index.js.map +12 -0
- package/dist/src/vultisig/index.cjs +4 -0
- package/dist/src/vultisig/index.cjs.map +11 -0
- package/dist/src/vultisig/index.js +4 -0
- package/dist/src/vultisig/index.js.map +11 -0
- package/dist/types/bitget/helpers.d.ts +1607 -0
- package/dist/types/bitget/helpers.d.ts.map +1 -0
- package/dist/types/bitget/index.d.ts +11 -0
- package/dist/types/bitget/index.d.ts.map +1 -0
- package/dist/types/cosmostation/index.d.ts +24 -0
- package/dist/types/cosmostation/index.d.ts.map +1 -0
- package/dist/types/ctrl/index.d.ts +14 -0
- package/dist/types/ctrl/index.d.ts.map +1 -0
- package/dist/types/ctrl/walletHelpers.d.ts +23 -0
- package/dist/types/ctrl/walletHelpers.d.ts.map +1 -0
- package/dist/types/evm-extensions/index.d.ts +747 -0
- package/dist/types/evm-extensions/index.d.ts.map +1 -0
- package/dist/types/evm-extensions/types.d.ts +18 -0
- package/dist/types/evm-extensions/types.d.ts.map +1 -0
- package/dist/types/helpers/near.d.ts +60 -0
- package/dist/types/helpers/near.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/keepkey-bex/index.d.ts +14 -0
- package/dist/types/keepkey-bex/index.d.ts.map +1 -0
- package/dist/types/keepkey-bex/walletHelpers.d.ts +48 -0
- package/dist/types/keepkey-bex/walletHelpers.d.ts.map +1 -0
- package/dist/types/keplr/chainRegistry.d.ts +4 -0
- package/dist/types/keplr/chainRegistry.d.ts.map +1 -0
- package/dist/types/keplr/index.d.ts +14 -0
- package/dist/types/keplr/index.d.ts.map +1 -0
- package/dist/types/okx/helpers.d.ts +855 -0
- package/dist/types/okx/helpers.d.ts.map +1 -0
- package/dist/types/okx/index.d.ts +11 -0
- package/dist/types/okx/index.d.ts.map +1 -0
- package/dist/types/okx/types.d.ts +104 -0
- package/dist/types/okx/types.d.ts.map +1 -0
- package/dist/types/onekey/evmSigner.d.ts +32 -0
- package/dist/types/onekey/evmSigner.d.ts.map +1 -0
- package/dist/types/onekey/index.d.ts +14 -0
- package/dist/types/onekey/index.d.ts.map +1 -0
- package/dist/types/phantom/index.d.ts +15 -0
- package/dist/types/phantom/index.d.ts.map +1 -0
- package/dist/types/polkadotjs/index.d.ts +14 -0
- package/dist/types/polkadotjs/index.d.ts.map +1 -0
- package/dist/types/talisman/index.d.ts +14 -0
- package/dist/types/talisman/index.d.ts.map +1 -0
- package/dist/types/tronlink/helpers.d.ts +29 -0
- package/dist/types/tronlink/helpers.d.ts.map +1 -0
- package/dist/types/tronlink/index.d.ts +13 -0
- package/dist/types/tronlink/index.d.ts.map +1 -0
- package/dist/types/tronlink/types.d.ts +55 -0
- package/dist/types/tronlink/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +166 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/vultisig/index.d.ts +14 -0
- package/dist/types/vultisig/index.d.ts.map +1 -0
- package/dist/types/vultisig/walletHelpers.d.ts +37 -0
- package/dist/types/vultisig/walletHelpers.d.ts.map +1 -0
- package/package.json +133 -0
- package/src/bitget/helpers.ts +181 -0
- package/src/bitget/index.ts +26 -0
- package/src/cosmostation/index.ts +122 -0
- package/src/ctrl/index.ts +194 -0
- package/src/ctrl/walletHelpers.ts +186 -0
- package/src/evm-extensions/index.ts +122 -0
- package/src/evm-extensions/types.ts +15 -0
- package/src/helpers/near-browser-provider.d.ts +270 -0
- package/src/helpers/near.ts +174 -0
- package/src/index.ts +1 -0
- package/src/keepkey-bex/index.ts +151 -0
- package/src/keepkey-bex/walletHelpers.ts +241 -0
- package/src/keplr/chainRegistry.ts +240 -0
- package/src/keplr/index.ts +54 -0
- package/src/okx/helpers.ts +192 -0
- package/src/okx/index.ts +43 -0
- package/src/okx/types.ts +117 -0
- package/src/onekey/evmSigner.ts +116 -0
- package/src/onekey/index.ts +182 -0
- package/src/phantom/index.ts +136 -0
- package/src/polkadotjs/index.ts +61 -0
- package/src/talisman/index.ts +132 -0
- package/src/tronlink/helpers.ts +153 -0
- package/src/tronlink/index.ts +49 -0
- package/src/tronlink/types.ts +44 -0
- package/src/types.ts +157 -0
- package/src/vultisig/index.ts +179 -0
- package/src/vultisig/walletHelpers.ts +295 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
//TBD @towan to be moved somewhere else
|
|
6
|
+
|
|
7
|
+
import type { Account } from "@near-js/accounts";
|
|
8
|
+
import { type Action, SignedTransaction, type Transaction } from "@near-js/transactions";
|
|
9
|
+
import { USwapError } from "@tcswap/helpers";
|
|
10
|
+
import type { NearSigner } from "@tcswap/toolboxes/near";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* NEAR Browser Wallet Provider Interface
|
|
14
|
+
* Common interface implemented by browser extension wallets
|
|
15
|
+
*/
|
|
16
|
+
export interface NearBrowserWalletProvider {
|
|
17
|
+
connect(params?: { contractId?: string; methodNames?: string[] }): Promise<Account[] | { accountId: string }>;
|
|
18
|
+
disconnect?(): Promise<void>;
|
|
19
|
+
signOut?(): Promise<void>; // Alternative to disconnect
|
|
20
|
+
|
|
21
|
+
getAccountId(): string | Promise<string>;
|
|
22
|
+
getAccounts?(): Promise<Account[]>;
|
|
23
|
+
isSignedIn(): boolean;
|
|
24
|
+
getPublicKey?(): Promise<string>;
|
|
25
|
+
|
|
26
|
+
signMessage?(params: any): Promise<any>;
|
|
27
|
+
signAndSendTransaction(params: { receiverId: string; actions: Action[]; signerId?: string }): Promise<any>;
|
|
28
|
+
signAndSendTransactions?(params: { transactions: Transaction[] }): Promise<any[]>;
|
|
29
|
+
|
|
30
|
+
request<T>(params: { method: string; params?: any }): Promise<T>;
|
|
31
|
+
verifyOwner?(params: { message: string; callbackUrl?: string }): Promise<any>;
|
|
32
|
+
getNetwork?(): Promise<{ networkId: string; nodeUrl: string }>;
|
|
33
|
+
|
|
34
|
+
on?(event: string, handler: (...args: any[]) => void): void;
|
|
35
|
+
off?(event: string, handler: (...args: any[]) => void): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Helper to create a NEAR signer from browser extension providers
|
|
40
|
+
*/
|
|
41
|
+
export async function createNearSignerFromProvider(provider: NearBrowserWalletProvider, walletName: string) {
|
|
42
|
+
const isConnected = provider.isSignedIn ? provider.isSignedIn() : false;
|
|
43
|
+
if (!isConnected) {
|
|
44
|
+
await provider.connect({ contractId: "swapkit", methodNames: ["transfer"] });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const signer = {
|
|
48
|
+
...provider,
|
|
49
|
+
|
|
50
|
+
async getAddress() {
|
|
51
|
+
if (provider.getAccountId) {
|
|
52
|
+
return provider.getAccountId();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (provider.isSignedIn && !provider.isSignedIn()) {
|
|
56
|
+
// Try connect method for wallets that don't have requestSignIn
|
|
57
|
+
const result = await provider.connect();
|
|
58
|
+
if (Array.isArray(result) && result.length > 0 && result[0]) {
|
|
59
|
+
return typeof result[0] === "string" ? result[0] : result[0].accountId;
|
|
60
|
+
}
|
|
61
|
+
throw new USwapError("wallet_connection_rejected_by_user", { wallet: walletName });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
throw new USwapError("wallet_connection_rejected_by_user", { wallet: walletName });
|
|
65
|
+
},
|
|
66
|
+
async getPublicKey() {
|
|
67
|
+
const { PublicKey } = await import("@near-js/crypto");
|
|
68
|
+
|
|
69
|
+
if (provider.getPublicKey) {
|
|
70
|
+
const pubKey = await provider.getPublicKey();
|
|
71
|
+
return PublicKey.from(pubKey);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
throw new USwapError("wallet_ledger_method_not_supported", { method: "getPublicKey", wallet: walletName });
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
signDelegateAction(_delegateAction: any) {
|
|
78
|
+
// Most browser wallets don't support delegate actions yet
|
|
79
|
+
return Promise.reject(
|
|
80
|
+
new USwapError("wallet_ledger_method_not_supported", { method: "signDelegateAction", wallet: walletName }),
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async signNep413Message(
|
|
85
|
+
message: string,
|
|
86
|
+
_accountId: string,
|
|
87
|
+
recipient: string,
|
|
88
|
+
nonce: Uint8Array,
|
|
89
|
+
callbackUrl?: string,
|
|
90
|
+
) {
|
|
91
|
+
if (!provider.signMessage) {
|
|
92
|
+
throw new USwapError("wallet_ledger_method_not_supported", { method: "signNep413Message", wallet: walletName });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const result = await (provider as Required<Pick<NearBrowserWalletProvider, "signMessage">>).signMessage({
|
|
96
|
+
callbackUrl,
|
|
97
|
+
message,
|
|
98
|
+
nonce: Buffer.from(nonce),
|
|
99
|
+
recipient,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return result;
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
async signTransaction(transaction: Transaction) {
|
|
106
|
+
if (!provider.request) {
|
|
107
|
+
throw new USwapError("wallet_near_method_not_supported", { method: "request", wallet: walletName });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const mappedTransaction = {
|
|
111
|
+
actions: transaction.actions.map((action) => actionToWalletJson(action)),
|
|
112
|
+
receiverId: transaction.receiverId,
|
|
113
|
+
signerId: transaction.signerId,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// @ts-expect-error
|
|
117
|
+
const result: any = await provider.requestSignTransactions({ transactions: [mappedTransaction] });
|
|
118
|
+
|
|
119
|
+
const signedTransaction = SignedTransaction.decode(Uint8Array.fromBase64(result.txs[0].signedTx));
|
|
120
|
+
|
|
121
|
+
return [signedTransaction.signature.ed25519Signature?.data, signedTransaction] as [
|
|
122
|
+
Uint8Array<ArrayBufferLike>,
|
|
123
|
+
SignedTransaction,
|
|
124
|
+
];
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return signer as NearSigner;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Detect if a wallet provider supports NEAR
|
|
133
|
+
*/
|
|
134
|
+
export function detectNearProvider(window: any, providerPath: string): NearBrowserWalletProvider | null {
|
|
135
|
+
const parts = providerPath.split(".");
|
|
136
|
+
let provider = window;
|
|
137
|
+
|
|
138
|
+
for (const part of parts) {
|
|
139
|
+
provider = provider?.[part];
|
|
140
|
+
if (!provider) return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return provider;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Get NEAR chain ID for WalletConnect
|
|
148
|
+
*/
|
|
149
|
+
export function getNearChainId(isTestnet: boolean): string {
|
|
150
|
+
return isTestnet ? "near:testnet" : "near:mainnet";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function actionToWalletJson(action: Transaction["actions"][number]) {
|
|
154
|
+
const kind = action.enum;
|
|
155
|
+
const data = action;
|
|
156
|
+
|
|
157
|
+
switch (kind) {
|
|
158
|
+
case "functionCall":
|
|
159
|
+
case "FunctionCall":
|
|
160
|
+
return {
|
|
161
|
+
params: {
|
|
162
|
+
// args must be base64 string for wallet JSON
|
|
163
|
+
args: typeof Buffer.from(data.functionCall?.args ?? "{}").toString("base64"),
|
|
164
|
+
deposit: (data.functionCall?.deposit ?? 0).toString(),
|
|
165
|
+
gas: (data.functionCall?.gas ?? 0).toString(),
|
|
166
|
+
methodName: data.functionCall?.methodName ?? "",
|
|
167
|
+
},
|
|
168
|
+
type: "FunctionCall",
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
default:
|
|
172
|
+
throw new Error(`Unsupported action kind for wallet JSON: ${kind}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { AssetValue, Chain, ChainId, filterSupportedChains, USwapError, WalletOption } from "@tcswap/helpers";
|
|
6
|
+
import { createWallet, getWalletSupportedChains } from "@tcswap/wallet-core";
|
|
7
|
+
import type { Eip1193Provider } from "ethers";
|
|
8
|
+
import {
|
|
9
|
+
getKEEPKEYAddress,
|
|
10
|
+
getKEEPKEYMethods,
|
|
11
|
+
getKEEPKEYProvider,
|
|
12
|
+
getProviderNameFromChain,
|
|
13
|
+
type WalletTxParams,
|
|
14
|
+
walletTransfer,
|
|
15
|
+
} from "./walletHelpers";
|
|
16
|
+
|
|
17
|
+
export const keepkeyBexWallet = createWallet({
|
|
18
|
+
connect: ({ addChain, supportedChains, walletType }) =>
|
|
19
|
+
async function connectKeepkeyBex(chains: Chain[]) {
|
|
20
|
+
const filteredChains = filterSupportedChains({ chains, supportedChains, walletType });
|
|
21
|
+
|
|
22
|
+
await Promise.all(
|
|
23
|
+
filteredChains.map(async (chain) => {
|
|
24
|
+
const address = await getKEEPKEYAddress(chain);
|
|
25
|
+
const walletMethods = await getWalletMethods(chain);
|
|
26
|
+
|
|
27
|
+
addChain({ ...walletMethods, address, chain, walletType });
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
},
|
|
33
|
+
name: "connectKeepkeyBex",
|
|
34
|
+
supportedChains: [
|
|
35
|
+
Chain.Arbitrum,
|
|
36
|
+
Chain.Avalanche,
|
|
37
|
+
Chain.BinanceSmartChain,
|
|
38
|
+
Chain.Bitcoin,
|
|
39
|
+
Chain.BitcoinCash,
|
|
40
|
+
Chain.Base,
|
|
41
|
+
Chain.Cosmos,
|
|
42
|
+
Chain.Dash,
|
|
43
|
+
Chain.Dogecoin,
|
|
44
|
+
Chain.Ethereum,
|
|
45
|
+
Chain.Kujira,
|
|
46
|
+
Chain.Litecoin,
|
|
47
|
+
Chain.Maya,
|
|
48
|
+
Chain.Optimism,
|
|
49
|
+
Chain.Polygon,
|
|
50
|
+
Chain.Ripple,
|
|
51
|
+
Chain.Solana,
|
|
52
|
+
Chain.THORChain,
|
|
53
|
+
Chain.XLayer,
|
|
54
|
+
],
|
|
55
|
+
walletType: WalletOption.KEEPKEY_BEX,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const KEEPKEY_BEX_SUPPORTED_CHAINS = getWalletSupportedChains(keepkeyBexWallet);
|
|
59
|
+
|
|
60
|
+
async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[number]) {
|
|
61
|
+
switch (chain) {
|
|
62
|
+
case Chain.Maya:
|
|
63
|
+
case Chain.THORChain: {
|
|
64
|
+
const { getCosmosToolbox, THORCHAIN_GAS_VALUE, MAYA_GAS_VALUE } = await import("@tcswap/toolboxes/cosmos");
|
|
65
|
+
|
|
66
|
+
const gasLimit = chain === Chain.Maya ? MAYA_GAS_VALUE : THORCHAIN_GAS_VALUE;
|
|
67
|
+
const toolbox = await getCosmosToolbox(chain);
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
...toolbox,
|
|
71
|
+
deposit: (tx: WalletTxParams) => walletTransfer({ ...tx, recipient: "" }, "deposit"),
|
|
72
|
+
transfer: (tx: WalletTxParams) => walletTransfer({ ...tx, gasLimit }, "transfer"),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
case Chain.Cosmos:
|
|
77
|
+
case Chain.Kujira: {
|
|
78
|
+
const { getCosmosToolbox } = await import("@tcswap/toolboxes/cosmos");
|
|
79
|
+
|
|
80
|
+
// @ts-expect-error assumed available connection
|
|
81
|
+
const signer = window.keepkey?.cosmos?.getOfflineSignerOnlyAmino(ChainId[chain]);
|
|
82
|
+
if (!signer) throw new USwapError("wallet_keepkey_signer_not_found");
|
|
83
|
+
const toolbox = await getCosmosToolbox(chain, { signer });
|
|
84
|
+
|
|
85
|
+
const accounts = await signer.getAccounts();
|
|
86
|
+
if (!accounts?.[0]?.address) throw new USwapError("wallet_keepkey_no_accounts");
|
|
87
|
+
|
|
88
|
+
const [{ address }] = accounts;
|
|
89
|
+
|
|
90
|
+
return { ...toolbox, address };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
case Chain.Dash:
|
|
94
|
+
case Chain.Bitcoin:
|
|
95
|
+
case Chain.BitcoinCash:
|
|
96
|
+
case Chain.Dogecoin:
|
|
97
|
+
case Chain.Litecoin: {
|
|
98
|
+
const { getUtxoToolbox } = await import("@tcswap/toolboxes/utxo");
|
|
99
|
+
const toolbox = await getUtxoToolbox(chain);
|
|
100
|
+
|
|
101
|
+
const getBalance = async () => {
|
|
102
|
+
const providerChain = getProviderNameFromChain(chain);
|
|
103
|
+
// @ts-expect-error We assuming there chains via switch
|
|
104
|
+
const balance = await window?.keepkey?.[providerChain]?.request({ method: "request_balance" });
|
|
105
|
+
const assetValue = AssetValue.from({ chain, value: balance[0].balance });
|
|
106
|
+
return [assetValue];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return { ...toolbox, getBalance, transfer: walletTransfer };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
case Chain.Ethereum:
|
|
113
|
+
case Chain.BinanceSmartChain:
|
|
114
|
+
case Chain.Base:
|
|
115
|
+
case Chain.Arbitrum:
|
|
116
|
+
case Chain.Optimism:
|
|
117
|
+
case Chain.Polygon:
|
|
118
|
+
case Chain.Avalanche: {
|
|
119
|
+
const { prepareNetworkSwitch, switchEVMWalletNetwork } = await import("@tcswap/helpers");
|
|
120
|
+
const { getEvmToolbox } = await import("@tcswap/toolboxes/evm");
|
|
121
|
+
const { BrowserProvider } = await import("ethers");
|
|
122
|
+
const ethereumWindowProvider = getKEEPKEYProvider(chain) as Eip1193Provider;
|
|
123
|
+
|
|
124
|
+
if (!ethereumWindowProvider) {
|
|
125
|
+
throw new USwapError("wallet_keepkey_not_found");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const provider = new BrowserProvider(ethereumWindowProvider, "any");
|
|
129
|
+
const signer = await provider.getSigner();
|
|
130
|
+
const toolbox = await getEvmToolbox(chain, { provider, signer });
|
|
131
|
+
const keepkeyMethods = getKEEPKEYMethods(provider, chain);
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
if (chain !== Chain.Ethereum) {
|
|
135
|
+
const networkParams = toolbox.getNetworkParams();
|
|
136
|
+
await switchEVMWalletNetwork(provider, chain, networkParams);
|
|
137
|
+
}
|
|
138
|
+
} catch {
|
|
139
|
+
throw new USwapError({
|
|
140
|
+
errorKey: "wallet_failed_to_add_or_switch_network",
|
|
141
|
+
info: { chain, wallet: WalletOption.KEEPKEY },
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return prepareNetworkSwitch({ chain, provider, toolbox: { ...toolbox, ...keepkeyMethods } });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
default:
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type AssetValue,
|
|
7
|
+
Chain,
|
|
8
|
+
type EVMChain,
|
|
9
|
+
EVMChains,
|
|
10
|
+
type FeeOption,
|
|
11
|
+
USwapError,
|
|
12
|
+
WalletOption,
|
|
13
|
+
} from "@tcswap/helpers";
|
|
14
|
+
import { erc20ABI } from "@tcswap/helpers/contracts";
|
|
15
|
+
import type { ApproveParams, CallParams, EVMTxParams } from "@tcswap/toolboxes/evm";
|
|
16
|
+
import type { BrowserProvider, Eip1193Provider } from "ethers";
|
|
17
|
+
|
|
18
|
+
interface UTXOProvider {
|
|
19
|
+
request: (
|
|
20
|
+
args: {
|
|
21
|
+
method: string;
|
|
22
|
+
params?: {
|
|
23
|
+
amount: { amount: string; decimals?: number };
|
|
24
|
+
asset: { chain: Chain; symbol: string; ticker: string };
|
|
25
|
+
memo: string | undefined;
|
|
26
|
+
from?: string;
|
|
27
|
+
recipient: string;
|
|
28
|
+
gasLimit?: string | bigint;
|
|
29
|
+
}[];
|
|
30
|
+
},
|
|
31
|
+
callback: (err: string, tx: string) => void,
|
|
32
|
+
) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type TransactionMethod = "transfer" | "deposit";
|
|
36
|
+
|
|
37
|
+
type TransactionParams = {
|
|
38
|
+
asset: string | { chain: string; symbol: string; ticker: string };
|
|
39
|
+
amount: number | string | { amount: string | number; decimals?: number };
|
|
40
|
+
decimal?: number;
|
|
41
|
+
recipient: string;
|
|
42
|
+
memo?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type WalletTxParams = {
|
|
46
|
+
feeOptionKey?: FeeOption;
|
|
47
|
+
from?: string;
|
|
48
|
+
memo?: string;
|
|
49
|
+
recipient: string;
|
|
50
|
+
assetValue: AssetValue;
|
|
51
|
+
gasLimit?: string | bigint | undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const getProviderNameFromChain = (chain: Chain): string => {
|
|
55
|
+
switch (chain) {
|
|
56
|
+
case Chain.Bitcoin:
|
|
57
|
+
return "bitcoin";
|
|
58
|
+
case Chain.BitcoinCash:
|
|
59
|
+
return "bitcoincash";
|
|
60
|
+
case Chain.Dash:
|
|
61
|
+
return "dash";
|
|
62
|
+
case Chain.Dogecoin:
|
|
63
|
+
return "dogecoin";
|
|
64
|
+
case Chain.Litecoin:
|
|
65
|
+
return "litecoin";
|
|
66
|
+
default:
|
|
67
|
+
throw new USwapError("wallet_keepkey_chain_not_supported", { chain });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
declare const window: {
|
|
72
|
+
keepkey?: {
|
|
73
|
+
binance: Eip1193Provider;
|
|
74
|
+
bitcoin: Eip1193Provider;
|
|
75
|
+
bitcoincash: Eip1193Provider;
|
|
76
|
+
dogecoin: Eip1193Provider;
|
|
77
|
+
ethereum: Eip1193Provider;
|
|
78
|
+
cosmos: Eip1193Provider;
|
|
79
|
+
dash: Eip1193Provider;
|
|
80
|
+
litecoin: Eip1193Provider;
|
|
81
|
+
thorchain: Eip1193Provider;
|
|
82
|
+
mayachain: Eip1193Provider;
|
|
83
|
+
};
|
|
84
|
+
} & Window;
|
|
85
|
+
|
|
86
|
+
export function getKEEPKEYProvider<T extends Chain>(chain: T) {
|
|
87
|
+
if (!window.keepkey) throw new USwapError("wallet_keepkey_not_found");
|
|
88
|
+
|
|
89
|
+
switch (chain) {
|
|
90
|
+
case Chain.Ethereum:
|
|
91
|
+
case Chain.Base:
|
|
92
|
+
case Chain.Avalanche:
|
|
93
|
+
case Chain.BinanceSmartChain:
|
|
94
|
+
case Chain.Arbitrum:
|
|
95
|
+
case Chain.Optimism:
|
|
96
|
+
case Chain.Polygon:
|
|
97
|
+
case Chain.XLayer:
|
|
98
|
+
return window.keepkey.ethereum as Eip1193Provider;
|
|
99
|
+
case Chain.Cosmos:
|
|
100
|
+
return window.keepkey.cosmos as Eip1193Provider;
|
|
101
|
+
case Chain.Bitcoin:
|
|
102
|
+
return window.keepkey.bitcoin as UTXOProvider;
|
|
103
|
+
case Chain.BitcoinCash:
|
|
104
|
+
return window.keepkey.bitcoincash as UTXOProvider;
|
|
105
|
+
case Chain.Dogecoin:
|
|
106
|
+
return window.keepkey.dogecoin as UTXOProvider;
|
|
107
|
+
case Chain.Litecoin:
|
|
108
|
+
return window.keepkey.litecoin as UTXOProvider;
|
|
109
|
+
case Chain.Dash:
|
|
110
|
+
return window.keepkey.dash as UTXOProvider;
|
|
111
|
+
case Chain.THORChain:
|
|
112
|
+
return window.keepkey.thorchain as UTXOProvider;
|
|
113
|
+
case Chain.Maya:
|
|
114
|
+
return window.keepkey.mayachain as UTXOProvider;
|
|
115
|
+
|
|
116
|
+
default:
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function transaction({
|
|
122
|
+
method,
|
|
123
|
+
params,
|
|
124
|
+
chain,
|
|
125
|
+
}: {
|
|
126
|
+
method: TransactionMethod;
|
|
127
|
+
params: TransactionParams[];
|
|
128
|
+
chain: Chain;
|
|
129
|
+
}): Promise<string> {
|
|
130
|
+
const client = getKEEPKEYProvider(chain);
|
|
131
|
+
|
|
132
|
+
return new Promise<string>((resolve, reject) => {
|
|
133
|
+
if (client && "request" in client) {
|
|
134
|
+
// @ts-expect-error
|
|
135
|
+
client.request({ method, params }, (err: string, tx: string) => {
|
|
136
|
+
err ? reject(err) : resolve(tx);
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
reject(new USwapError("wallet_provider_not_found"));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function getKEEPKEYAddress(chain: Chain) {
|
|
145
|
+
const eipProvider = getKEEPKEYProvider(chain) as Eip1193Provider;
|
|
146
|
+
if (!eipProvider) {
|
|
147
|
+
throw new USwapError({ errorKey: "wallet_provider_not_found", info: { chain, wallet: WalletOption.KEEPKEY } });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let method = "request_accounts";
|
|
151
|
+
if (EVMChains.includes(chain as EVMChain)) {
|
|
152
|
+
method = "eth_requestAccounts";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const [response] = await eipProvider.request({ method, params: [] });
|
|
156
|
+
return response;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function walletTransfer(
|
|
160
|
+
{ assetValue, recipient, memo, gasLimit }: WalletTxParams & { assetValue: AssetValue },
|
|
161
|
+
method: TransactionMethod = "transfer",
|
|
162
|
+
) {
|
|
163
|
+
if (!assetValue) {
|
|
164
|
+
throw new USwapError("wallet_keepkey_asset_not_defined");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const from = await getKEEPKEYAddress(assetValue.chain);
|
|
168
|
+
const params = [
|
|
169
|
+
{
|
|
170
|
+
amount: { amount: assetValue.getValue("string"), decimals: assetValue.decimal },
|
|
171
|
+
asset: {
|
|
172
|
+
chain: assetValue.chain,
|
|
173
|
+
symbol: assetValue.symbol.toUpperCase(),
|
|
174
|
+
ticker: assetValue.symbol.toUpperCase(),
|
|
175
|
+
},
|
|
176
|
+
from,
|
|
177
|
+
gasLimit,
|
|
178
|
+
memo,
|
|
179
|
+
recipient,
|
|
180
|
+
},
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
return transaction({ chain: assetValue.chain, method, params });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function getKEEPKEYMethods(provider: BrowserProvider, chain: EVMChain) {
|
|
187
|
+
return {
|
|
188
|
+
approve: async ({ assetAddress, spenderAddress, amount, from }: ApproveParams) => {
|
|
189
|
+
const { MAX_APPROVAL, getCreateContractTxObject, toHexString } = await import("@tcswap/toolboxes/evm");
|
|
190
|
+
|
|
191
|
+
const createTx = getCreateContractTxObject({ chain, provider });
|
|
192
|
+
const { value, to, data } = await createTx({
|
|
193
|
+
abi: erc20ABI,
|
|
194
|
+
contractAddress: assetAddress,
|
|
195
|
+
funcName: "approve",
|
|
196
|
+
funcParams: [spenderAddress, BigInt(amount || MAX_APPROVAL)],
|
|
197
|
+
txOverrides: { from },
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return provider.send("eth_sendTransaction", [
|
|
201
|
+
{ data: data || "0x", from, to, value: toHexString(BigInt(value || 0)) },
|
|
202
|
+
]);
|
|
203
|
+
},
|
|
204
|
+
call: async <T>({ contractAddress, abi, funcName, funcParams = [], txOverrides }: CallParams): Promise<T> => {
|
|
205
|
+
if (!contractAddress) {
|
|
206
|
+
throw new USwapError("wallet_keepkey_contract_address_not_provided");
|
|
207
|
+
}
|
|
208
|
+
const { createContract, getCreateContractTxObject, isStateChangingCall, toHexString } = await import(
|
|
209
|
+
"@tcswap/toolboxes/evm"
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const isStateChanging = isStateChangingCall({ abi, funcName });
|
|
213
|
+
|
|
214
|
+
if (isStateChanging) {
|
|
215
|
+
const createTx = getCreateContractTxObject({ chain, provider });
|
|
216
|
+
const { value, from, to, data } = await createTx({ abi, contractAddress, funcName, funcParams, txOverrides });
|
|
217
|
+
|
|
218
|
+
return provider.send("eth_sendTransaction", [
|
|
219
|
+
{ data: data || "0x", from, to, value: toHexString(BigInt(value || 0)) },
|
|
220
|
+
]);
|
|
221
|
+
}
|
|
222
|
+
const contract = createContract(contractAddress, abi, provider);
|
|
223
|
+
|
|
224
|
+
const result = await contract[funcName]?.(...funcParams);
|
|
225
|
+
|
|
226
|
+
return typeof result?.hash === "string" ? result?.hash : result;
|
|
227
|
+
},
|
|
228
|
+
sendTransaction: async (tx: EVMTxParams) => {
|
|
229
|
+
const { from, to, data, value } = tx;
|
|
230
|
+
if (!to) {
|
|
231
|
+
throw new USwapError("wallet_keepkey_send_transaction_no_address");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const { toHexString } = await import("@tcswap/toolboxes/evm");
|
|
235
|
+
|
|
236
|
+
return provider.send("eth_sendTransaction", [
|
|
237
|
+
{ data: data || "0x", from, to, value: toHexString(BigInt(value || 0)) },
|
|
238
|
+
]);
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|