@swapkit/wallet-hardware 4.8.1 → 4.8.3

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 +3 -3
  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,210 @@
1
+ import {
2
+ type Chain,
3
+ ChainToChainId,
4
+ type DerivationPathArray,
5
+ derivationPathToString,
6
+ SwapKitError,
7
+ SwapKitNumber,
8
+ WalletOption,
9
+ } from "@swapkit/helpers";
10
+ import type { JsonRpcProvider, Provider, TransactionRequest, TypedDataDomain, TypedDataField } from "ethers";
11
+
12
+ type TrezorEVMSignerParams = {
13
+ chain: Chain;
14
+ derivationPath: DerivationPathArray;
15
+ provider: Provider | JsonRpcProvider;
16
+ };
17
+
18
+ export async function getEVMSigner({ chain, derivationPath, provider }: TrezorEVMSignerParams) {
19
+ const { AbstractSigner, Signature } = await import("ethers");
20
+
21
+ class TrezorSigner extends AbstractSigner {
22
+ address: string;
23
+ chain: Chain;
24
+ derivationPath: DerivationPathArray;
25
+ readonly provider: Provider | JsonRpcProvider;
26
+
27
+ constructor({ chain, derivationPath, provider }: TrezorEVMSignerParams) {
28
+ super(provider);
29
+
30
+ this.address = "";
31
+ this.chain = chain;
32
+ this.derivationPath = derivationPath;
33
+ this.provider = provider;
34
+ }
35
+
36
+ getAddress = async () => {
37
+ if (!this.address) {
38
+ const TrezorConnect = (await import("@trezor/connect-web")).default;
39
+
40
+ const result = await TrezorConnect.ethereumGetAddress({
41
+ path: derivationPathToString(this.derivationPath),
42
+ showOnTrezor: true,
43
+ });
44
+
45
+ if (!result.success) {
46
+ throw new SwapKitError({
47
+ errorKey: "wallet_trezor_failed_to_get_address",
48
+ info: { ...result, chain: this.chain, derivationPath: this.derivationPath },
49
+ });
50
+ }
51
+
52
+ this.address = result.payload.address;
53
+ }
54
+
55
+ return this.address;
56
+ };
57
+
58
+ signMessage = async (message: string) => {
59
+ const TrezorConnect = (await import("@trezor/connect-web")).default;
60
+
61
+ const result = await TrezorConnect.ethereumSignMessage({
62
+ message,
63
+ path: derivationPathToString(this.derivationPath),
64
+ });
65
+
66
+ if (!result.success) {
67
+ throw new SwapKitError({
68
+ errorKey: "wallet_trezor_failed_to_sign_transaction",
69
+ info: { ...result, chain: this.chain, derivationPath: this.derivationPath, message },
70
+ });
71
+ }
72
+
73
+ return result.payload.signature;
74
+ };
75
+
76
+ signTypedData = async (
77
+ domain: TypedDataDomain,
78
+ types: Record<string, TypedDataField[]>,
79
+ value: Record<string, unknown>,
80
+ explicitPrimaryType?: string,
81
+ ) => {
82
+ const TrezorConnect = (await import("@trezor/connect-web")).default;
83
+ const { buildEIP712DomainType } = await import("@swapkit/toolboxes/evm");
84
+ const { TypedDataEncoder } = await import("ethers");
85
+
86
+ const { EIP712Domain: _, ...filteredTypes } = types;
87
+ const primaryType = explicitPrimaryType ?? TypedDataEncoder.from(filteredTypes).primaryType;
88
+ const domainTypes = buildEIP712DomainType(domain);
89
+
90
+ const data = {
91
+ domain: domain as Record<string, unknown>,
92
+ message: value,
93
+ primaryType,
94
+ types: { EIP712Domain: domainTypes, ...filteredTypes },
95
+ };
96
+
97
+ const result = await TrezorConnect.ethereumSignTypedData({
98
+ data: data as Parameters<typeof TrezorConnect.ethereumSignTypedData>[0]["data"],
99
+ metamask_v4_compat: true,
100
+ path: derivationPathToString(this.derivationPath),
101
+ });
102
+
103
+ if (!result.success) {
104
+ throw new SwapKitError({
105
+ errorKey: "wallet_trezor_failed_to_sign_transaction",
106
+ info: { chain: this.chain, error: result.payload.error },
107
+ });
108
+ }
109
+
110
+ return result.payload.signature.startsWith("0x") ? result.payload.signature : `0x${result.payload.signature}`;
111
+ };
112
+
113
+ signTransaction = async ({
114
+ to,
115
+ gasLimit,
116
+ value,
117
+ data,
118
+ nonce,
119
+ maxFeePerGas,
120
+ maxPriorityFeePerGas,
121
+ gasPrice,
122
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: any: refactor
123
+ }: TransactionRequest) => {
124
+ if (!to) throw new SwapKitError({ errorKey: "wallet_missing_params", info: { to } });
125
+ if (!gasLimit) throw new SwapKitError({ errorKey: "wallet_missing_params", info: { gasLimit } });
126
+
127
+ const isEIP1559 = maxFeePerGas && maxPriorityFeePerGas;
128
+
129
+ if (isEIP1559 && !maxFeePerGas) {
130
+ throw new SwapKitError({ errorKey: "wallet_missing_params", info: { maxFeePerGas } });
131
+ }
132
+ if (isEIP1559 && !maxPriorityFeePerGas) {
133
+ throw new SwapKitError({ errorKey: "wallet_missing_params", info: { maxPriorityFeePerGas } });
134
+ }
135
+ if (!(isEIP1559 || gasPrice)) {
136
+ throw new SwapKitError({ errorKey: "wallet_missing_params", info: { gasPrice } });
137
+ }
138
+
139
+ const TrezorConnect = (await import("@trezor/connect-web")).default;
140
+ const { toHexString } = await import("@swapkit/toolboxes/evm");
141
+ const { Transaction } = await import("ethers");
142
+
143
+ const additionalFields = isEIP1559
144
+ ? {
145
+ maxFeePerGas: toHexString(BigInt(maxFeePerGas?.toString() || 0)),
146
+ maxPriorityFeePerGas: toHexString(BigInt(maxPriorityFeePerGas?.toString() || 0)),
147
+ }
148
+ : (gasPrice && { gasPrice: toHexString(BigInt(gasPrice?.toString() || 0)) }) || { gasPrice: "0x0" };
149
+
150
+ const hexifiedNonce = toHexString(
151
+ BigInt(nonce || (await this.provider.getTransactionCount(await this.getAddress(), "pending"))),
152
+ );
153
+
154
+ const formattedTx = {
155
+ chainId: Number.parseInt(ChainToChainId[this.chain], 10),
156
+ data: data?.toString() || "0x",
157
+ gasLimit: toHexString(BigInt(gasLimit?.toString() || 0)),
158
+ nonce: hexifiedNonce,
159
+ to: to.toString(),
160
+ value: toHexString(BigInt(value?.toString() || 0)),
161
+ ...additionalFields,
162
+ };
163
+
164
+ const { success, payload } = await TrezorConnect.ethereumSignTransaction({
165
+ path: derivationPathToString(this.derivationPath),
166
+ transaction: formattedTx,
167
+ });
168
+
169
+ if (!success) {
170
+ throw new SwapKitError({
171
+ errorKey: "wallet_trezor_failed_to_sign_transaction",
172
+ info: { ...payload, chain: this.chain, derivationPath: this.derivationPath },
173
+ });
174
+ }
175
+
176
+ const { r, s, v } = payload;
177
+
178
+ const signature = Signature.from({ r, s, v: new SwapKitNumber(BigInt(v)).getBaseValue("number") });
179
+
180
+ const serializedTx = Transaction.from({
181
+ ...formattedTx,
182
+ nonce: Number.parseInt(formattedTx.nonce, 16),
183
+ signature,
184
+ type: isEIP1559 ? 2 : 0,
185
+ }).serialized;
186
+
187
+ if (!serializedTx) {
188
+ throw new SwapKitError({
189
+ errorKey: "wallet_trezor_failed_to_sign_transaction",
190
+ info: { chain: this.chain, derivationPath: this.derivationPath },
191
+ });
192
+ }
193
+
194
+ return serializedTx;
195
+ };
196
+
197
+ connect = (provider: Provider | null) => {
198
+ if (!provider) {
199
+ throw new SwapKitError({
200
+ errorKey: "wallet_provider_not_found",
201
+ info: { chain: this.chain, derivationPath: this.derivationPath, wallet: WalletOption.TREZOR },
202
+ });
203
+ }
204
+
205
+ return new TrezorSigner({ chain: this.chain, derivationPath: this.derivationPath, provider });
206
+ };
207
+ }
208
+
209
+ return new TrezorSigner({ chain, derivationPath, provider });
210
+ }