@swapkit/wallet-hardware 4.8.1 → 4.8.2

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.
Files changed (34) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +1 -0
  3. package/src/keepkey/chains/cosmos.ts +69 -0
  4. package/src/keepkey/chains/evm.ts +141 -0
  5. package/src/keepkey/chains/mayachain.ts +98 -0
  6. package/src/keepkey/chains/ripple.ts +88 -0
  7. package/src/keepkey/chains/thorchain.ts +93 -0
  8. package/src/keepkey/chains/utxo.ts +364 -0
  9. package/src/keepkey/coins.ts +67 -0
  10. package/src/keepkey/index.ts +174 -0
  11. package/src/ledger/clients/cosmos.ts +84 -0
  12. package/src/ledger/clients/evm.ts +186 -0
  13. package/src/ledger/clients/near.ts +63 -0
  14. package/src/ledger/clients/sui.ts +130 -0
  15. package/src/ledger/clients/thorchain/common.ts +93 -0
  16. package/src/ledger/clients/thorchain/helpers.ts +120 -0
  17. package/src/ledger/clients/thorchain/index.ts +87 -0
  18. package/src/ledger/clients/thorchain/lib.ts +258 -0
  19. package/src/ledger/clients/thorchain/utils.ts +69 -0
  20. package/src/ledger/clients/tron.ts +85 -0
  21. package/src/ledger/clients/utxo-legacy-adapter.ts +71 -0
  22. package/src/ledger/clients/utxo-psbt.ts +145 -0
  23. package/src/ledger/clients/utxo.ts +359 -0
  24. package/src/ledger/clients/xrp.ts +50 -0
  25. package/src/ledger/cosmosTypes.ts +98 -0
  26. package/src/ledger/helpers/getLedgerAddress.ts +76 -0
  27. package/src/ledger/helpers/getLedgerClient.ts +124 -0
  28. package/src/ledger/helpers/getLedgerTransport.ts +102 -0
  29. package/src/ledger/helpers/index.ts +3 -0
  30. package/src/ledger/index.ts +546 -0
  31. package/src/ledger/interfaces/CosmosLedgerInterface.ts +54 -0
  32. package/src/ledger/types.ts +42 -0
  33. package/src/trezor/evmSigner.ts +210 -0
  34. package/src/trezor/index.ts +847 -0
@@ -0,0 +1,124 @@
1
+ import { Chain, type DerivationPathArray, type EVMChain, SwapKitError, WalletOption } from "@swapkit/helpers";
2
+
3
+ import { CosmosLedger } from "../clients/cosmos";
4
+ import {
5
+ ArbitrumLedger,
6
+ AuroraLedger,
7
+ AvalancheLedger,
8
+ BaseLedger,
9
+ BerachainLedger,
10
+ BinanceSmartChainLedger,
11
+ EthereumLedger,
12
+ GnosisLedger,
13
+ MonadLedger,
14
+ OptimismLedger,
15
+ PolygonLedger,
16
+ XLayerLedger,
17
+ } from "../clients/evm";
18
+ import { getNearLedgerClient } from "../clients/near";
19
+ import { SuiLedger } from "../clients/sui";
20
+ import { THORChainLedger } from "../clients/thorchain";
21
+ import { TronLedger } from "../clients/tron";
22
+ import {
23
+ BitcoinCashLedger,
24
+ BitcoinLedger,
25
+ DashLedger,
26
+ DogecoinLedger,
27
+ LitecoinLedger,
28
+ ZcashLedger,
29
+ } from "../clients/utxo";
30
+ import { XRPLedger } from "../clients/xrp";
31
+
32
+ type LedgerSignerMap = {
33
+ [Chain.Arbitrum]: ReturnType<typeof ArbitrumLedger>;
34
+ [Chain.Aurora]: ReturnType<typeof AuroraLedger>;
35
+ [Chain.Avalanche]: ReturnType<typeof AvalancheLedger>;
36
+ [Chain.Base]: ReturnType<typeof BaseLedger>;
37
+ [Chain.Berachain]: ReturnType<typeof BerachainLedger>;
38
+ [Chain.BinanceSmartChain]: ReturnType<typeof BinanceSmartChainLedger>;
39
+ [Chain.BitcoinCash]: ReturnType<typeof BitcoinCashLedger>;
40
+ [Chain.Bitcoin]: ReturnType<typeof BitcoinLedger>;
41
+ [Chain.Cosmos]: CosmosLedger;
42
+ [Chain.Dash]: ReturnType<typeof DashLedger>;
43
+ [Chain.Dogecoin]: ReturnType<typeof DogecoinLedger>;
44
+ [Chain.Ethereum]: ReturnType<typeof EthereumLedger>;
45
+ [Chain.Gnosis]: ReturnType<typeof GnosisLedger>;
46
+ [Chain.Litecoin]: ReturnType<typeof LitecoinLedger>;
47
+ [Chain.Monad]: ReturnType<typeof MonadLedger>;
48
+ [Chain.Near]: ReturnType<typeof getNearLedgerClient>;
49
+ [Chain.Optimism]: ReturnType<typeof OptimismLedger>;
50
+ [Chain.Polygon]: ReturnType<typeof PolygonLedger>;
51
+ [Chain.Ripple]: ReturnType<typeof XRPLedger>;
52
+ [Chain.Sui]: ReturnType<typeof SuiLedger>;
53
+ [Chain.THORChain]: THORChainLedger;
54
+ [Chain.Tron]: ReturnType<typeof TronLedger>;
55
+ [Chain.XLayer]: ReturnType<typeof XLayerLedger>;
56
+ [Chain.Zcash]: ReturnType<typeof ZcashLedger>;
57
+ };
58
+
59
+ type LedgerSupportedChain = keyof LedgerSignerMap;
60
+
61
+ export const getLedgerClient = async <T extends LedgerSupportedChain>({
62
+ chain,
63
+ derivationPath,
64
+ }: {
65
+ chain: T;
66
+ derivationPath?: DerivationPathArray;
67
+ }): Promise<LedgerSignerMap[T]> => {
68
+ const { match } = await import("ts-pattern");
69
+
70
+ return (
71
+ match(chain as LedgerSupportedChain)
72
+ .returnType<Promise<LedgerSignerMap[T]>>()
73
+ .with(Chain.THORChain, () => Promise.resolve(new THORChainLedger(derivationPath) as LedgerSignerMap[T]))
74
+ .with(Chain.Cosmos, () => Promise.resolve(new CosmosLedger(derivationPath) as LedgerSignerMap[T]))
75
+ .with(Chain.Bitcoin, () => Promise.resolve(BitcoinLedger(derivationPath) as LedgerSignerMap[T]))
76
+ .with(Chain.BitcoinCash, () => Promise.resolve(BitcoinCashLedger(derivationPath) as LedgerSignerMap[T]))
77
+ .with(Chain.Dash, () => Promise.resolve(DashLedger(derivationPath) as LedgerSignerMap[T]))
78
+ .with(Chain.Dogecoin, () => Promise.resolve(DogecoinLedger(derivationPath) as LedgerSignerMap[T]))
79
+ .with(Chain.Litecoin, () => Promise.resolve(LitecoinLedger(derivationPath) as LedgerSignerMap[T]))
80
+ .with(Chain.Zcash, () => Promise.resolve(ZcashLedger(derivationPath) as LedgerSignerMap[T]))
81
+ .with(Chain.Ripple, () => Promise.resolve(XRPLedger(derivationPath) as LedgerSignerMap[T]))
82
+ .with(Chain.Tron, () => Promise.resolve(TronLedger(derivationPath) as LedgerSignerMap[T]))
83
+ .with(Chain.Sui, () => Promise.resolve(SuiLedger(derivationPath) as LedgerSignerMap[T]))
84
+ // @ts-expect-error
85
+ .with(Chain.Near, () => {
86
+ return Promise.resolve(getNearLedgerClient(derivationPath));
87
+ })
88
+ .with(
89
+ Chain.Arbitrum,
90
+ Chain.Aurora,
91
+ Chain.Avalanche,
92
+ Chain.Berachain,
93
+ Chain.BinanceSmartChain,
94
+ Chain.Ethereum,
95
+ Chain.Gnosis,
96
+ Chain.Monad,
97
+ Chain.Optimism,
98
+ Chain.Polygon,
99
+ Chain.Base,
100
+ Chain.XLayer,
101
+ async () => {
102
+ const { getProvider } = await import("@swapkit/toolboxes/evm");
103
+ const params = { derivationPath, provider: await getProvider(chain as EVMChain) };
104
+
105
+ return match(chain as Chain)
106
+ .with(Chain.BinanceSmartChain, () => BinanceSmartChainLedger(params) as LedgerSignerMap[T])
107
+ .with(Chain.Avalanche, () => AvalancheLedger(params) as LedgerSignerMap[T])
108
+ .with(Chain.Arbitrum, () => ArbitrumLedger(params) as LedgerSignerMap[T])
109
+ .with(Chain.Berachain, () => BerachainLedger(params) as LedgerSignerMap[T])
110
+ .with(Chain.Optimism, () => OptimismLedger(params) as LedgerSignerMap[T])
111
+ .with(Chain.Polygon, () => PolygonLedger(params) as LedgerSignerMap[T])
112
+ .with(Chain.Base, () => BaseLedger(params) as LedgerSignerMap[T])
113
+ .with(Chain.Aurora, () => AuroraLedger(params) as LedgerSignerMap[T])
114
+ .with(Chain.Gnosis, () => GnosisLedger(params) as LedgerSignerMap[T])
115
+ .with(Chain.Monad, () => MonadLedger(params) as LedgerSignerMap[T])
116
+ .with(Chain.XLayer, () => XLayerLedger(params) as LedgerSignerMap[T])
117
+ .otherwise(() => EthereumLedger(params) as LedgerSignerMap[T]);
118
+ },
119
+ )
120
+ .otherwise(() => {
121
+ throw new SwapKitError("wallet_chain_not_supported", { chain, wallet: WalletOption.LEDGER });
122
+ })
123
+ );
124
+ };
@@ -0,0 +1,102 @@
1
+ import type Transport from "@ledgerhq/hw-transport";
2
+ import { SwapKitError } from "@swapkit/helpers";
3
+
4
+ const getNavigatorUsb = () =>
5
+ navigator?.usb as unknown as {
6
+ getDevices: () => Promise<any[]>;
7
+ requestDevice: (requestObject: any) => Promise<any>;
8
+ removeEventListener: (event: string, callback: (e: any) => void) => void;
9
+ addEventListener: (event: string, callback: (e: any) => void) => void;
10
+ };
11
+
12
+ const getLedgerDevices = async (): Promise<USBDevice> => {
13
+ const navigatorUsb = getNavigatorUsb();
14
+
15
+ if (typeof navigatorUsb?.getDevices !== "function") return {} as USBDevice;
16
+ const { ledgerUSBVendorId } = await import("@ledgerhq/devices");
17
+
18
+ const devices = await navigatorUsb?.getDevices();
19
+ const existingDevices = devices.filter((d) => d.vendorId === ledgerUSBVendorId);
20
+ if (existingDevices.length > 0) return existingDevices[0];
21
+
22
+ return navigatorUsb?.requestDevice({ filters: [{ vendorId: ledgerUSBVendorId }] });
23
+ };
24
+
25
+ export const getLedgerTransport = async (): Promise<Transport> => {
26
+ const device = await getLedgerDevices();
27
+
28
+ if (!device) {
29
+ throw new SwapKitError("wallet_ledger_device_not_found");
30
+ }
31
+
32
+ device.opened || (await device.open());
33
+ if (device.configuration === null) await device.selectConfiguration(1);
34
+
35
+ try {
36
+ await device.reset();
37
+ } catch {
38
+ // reset fails on devices that are already open
39
+ }
40
+
41
+ const configuration = device.configuration ?? device.configurations?.[0];
42
+
43
+ const iface =
44
+ configuration?.interfaces.find(({ alternates }: { alternates: { interfaceClass: number }[] }) =>
45
+ alternates.some(({ interfaceClass }) => interfaceClass === 0xff),
46
+ ) ||
47
+ configuration?.interfaces.find(({ alternates }: { alternates: { interfaceClass: number }[] }) =>
48
+ alternates.some(({ interfaceClass }) => interfaceClass === 0x03),
49
+ );
50
+
51
+ if (!iface) {
52
+ await device.close();
53
+ throw new SwapKitError("wallet_ledger_connection_error");
54
+ }
55
+
56
+ const klass0x03 = (iface.alternates as any[])?.find(
57
+ ({ interfaceClass }: { interfaceClass: number }) => interfaceClass === 0x03,
58
+ )?.interfaceClass as number;
59
+
60
+ const klass0xff = (iface.alternates as any[])?.find(
61
+ ({ interfaceClass }: { interfaceClass: number }) => interfaceClass === 0xff,
62
+ )?.interfaceClass as number;
63
+
64
+ if (klass0x03 && !klass0xff) {
65
+ // -------- HID class (NEAR, ETH, SOL, etc.) -> WebHID transport --------
66
+ const TransportWebHID = (await import("@ledgerhq/hw-transport-webhid")).default;
67
+ const supported = await TransportWebHID.isSupported();
68
+ if (!supported) {
69
+ await device.close();
70
+ throw new SwapKitError("wallet_ledger_webhid_not_supported");
71
+ }
72
+ const transport = await TransportWebHID.create();
73
+ return transport;
74
+ }
75
+
76
+ try {
77
+ await device.claimInterface(iface.interfaceNumber);
78
+ } catch (error: unknown) {
79
+ await device.close();
80
+
81
+ throw new SwapKitError("wallet_ledger_connection_claimed", error);
82
+ }
83
+
84
+ const Transport = (await import("@ledgerhq/hw-transport-webusb")).default;
85
+ const isSupported = await Transport.isSupported();
86
+ if (!isSupported) throw new SwapKitError("wallet_ledger_webusb_not_supported");
87
+
88
+ const { DisconnectedDevice } = await import("@ledgerhq/errors");
89
+
90
+ const transport = new Transport(device, iface.interfaceNumber);
91
+
92
+ const onDisconnect = (e: any) => {
93
+ if (device === e.device) {
94
+ getNavigatorUsb()?.removeEventListener("disconnect", onDisconnect);
95
+
96
+ transport._emitDisconnect(new DisconnectedDevice());
97
+ }
98
+ };
99
+ getNavigatorUsb()?.addEventListener("disconnect", onDisconnect);
100
+
101
+ return transport as Transport;
102
+ };
@@ -0,0 +1,3 @@
1
+ export { getLedgerAddress } from "./getLedgerAddress";
2
+ export { getLedgerClient } from "./getLedgerClient";
3
+ export { getLedgerTransport } from "./getLedgerTransport";