@swapkit/wallet-hardware 4.2.10 → 4.6.0
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/LICENSE +246 -0
- package/dist/{chunk-7fsaymh4.js → chunk-1jexf7qt.js} +3 -3
- package/dist/{chunk-7fsaymh4.js.map → chunk-1jexf7qt.js.map} +1 -1
- package/dist/chunk-1jpcb30j.js +5 -0
- package/dist/{chunk-fazw0jvt.js.map → chunk-1jpcb30j.js.map} +1 -1
- package/dist/chunk-4fapcrj9.js +5 -0
- package/dist/chunk-4fapcrj9.js.map +10 -0
- package/dist/chunk-d54qkn5p.js +4 -0
- package/dist/chunk-d54qkn5p.js.map +10 -0
- package/dist/chunk-jj2v7mke.js +4 -0
- package/dist/chunk-jj2v7mke.js.map +10 -0
- package/dist/chunk-p94hfx8x.js +4 -0
- package/dist/{chunk-zzfbcc7e.js.map → chunk-p94hfx8x.js.map} +1 -1
- package/dist/chunk-skvfn1dg.js +4 -0
- package/dist/chunk-skvfn1dg.js.map +10 -0
- package/dist/{chunk-37bgpz1y.js → chunk-ybje7f3s.js} +3 -3
- package/dist/{chunk-37bgpz1y.js.map → chunk-ybje7f3s.js.map} +1 -1
- package/dist/keepkey/index.cjs +2 -2
- package/dist/keepkey/index.cjs.map +5 -5
- package/dist/keepkey/index.js +2 -2
- package/dist/keepkey/index.js.map +5 -5
- package/dist/ledger/index.cjs +3 -3
- package/dist/ledger/index.cjs.map +11 -10
- package/dist/ledger/index.js +3 -3
- package/dist/ledger/index.js.map +11 -10
- package/dist/trezor/index.cjs +2 -2
- package/dist/trezor/index.cjs.map +3 -3
- package/dist/trezor/index.js +2 -2
- package/dist/trezor/index.js.map +3 -3
- package/dist/types/core.d.ts +18 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/keepkey/chains/evm.d.ts +2 -2
- package/dist/types/keepkey/chains/evm.d.ts.map +1 -1
- package/dist/types/keepkey/chains/ripple.d.ts +15 -8
- package/dist/types/keepkey/chains/ripple.d.ts.map +1 -1
- package/dist/types/keepkey/chains/utxo.d.ts +331 -5
- package/dist/types/keepkey/chains/utxo.d.ts.map +1 -1
- package/dist/types/keepkey/index.d.ts +3 -3
- package/dist/types/keepkey/index.d.ts.map +1 -1
- package/dist/types/ledger/clients/evm.d.ts +3 -2
- package/dist/types/ledger/clients/evm.d.ts.map +1 -1
- package/dist/types/ledger/clients/sui.d.ts +27 -0
- package/dist/types/ledger/clients/sui.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/lib.d.ts.map +1 -1
- package/dist/types/ledger/clients/utxo.d.ts +43 -7
- package/dist/types/ledger/clients/utxo.d.ts.map +1 -1
- package/dist/types/ledger/clients/xrp.d.ts +2 -2
- package/dist/types/ledger/clients/xrp.d.ts.map +1 -1
- package/dist/types/ledger/helpers/getLedgerAddress.d.ts.map +1 -1
- package/dist/types/ledger/helpers/getLedgerClient.d.ts +4 -1
- package/dist/types/ledger/helpers/getLedgerClient.d.ts.map +1 -1
- package/dist/types/ledger/index.d.ts +3 -3
- package/dist/types/ledger/index.d.ts.map +1 -1
- package/dist/types/ledger/types.d.ts +2 -0
- package/dist/types/ledger/types.d.ts.map +1 -1
- package/dist/types/trezor/evmSigner.d.ts +2 -2
- package/dist/types/trezor/evmSigner.d.ts.map +1 -1
- package/dist/types/trezor/index.d.ts +3 -3
- package/dist/types/trezor/index.d.ts.map +1 -1
- package/package.json +51 -40
- package/dist/chunk-4k7gb7ss.js +0 -4
- package/dist/chunk-4k7gb7ss.js.map +0 -10
- package/dist/chunk-93cj7bky.js +0 -4
- package/dist/chunk-93cj7bky.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-zzfbcc7e.js +0 -5
- package/src/index.ts +0 -1
- package/src/keepkey/chains/cosmos.ts +0 -69
- package/src/keepkey/chains/evm.ts +0 -114
- package/src/keepkey/chains/mayachain.ts +0 -98
- package/src/keepkey/chains/ripple.ts +0 -88
- package/src/keepkey/chains/thorchain.ts +0 -93
- package/src/keepkey/chains/utxo.ts +0 -137
- package/src/keepkey/coins.ts +0 -67
- package/src/keepkey/index.ts +0 -155
- package/src/ledger/clients/cosmos.ts +0 -84
- package/src/ledger/clients/evm.ts +0 -141
- package/src/ledger/clients/near.ts +0 -63
- package/src/ledger/clients/thorchain/common.ts +0 -93
- package/src/ledger/clients/thorchain/helpers.ts +0 -120
- package/src/ledger/clients/thorchain/index.ts +0 -87
- package/src/ledger/clients/thorchain/lib.ts +0 -278
- package/src/ledger/clients/thorchain/utils.ts +0 -69
- package/src/ledger/clients/tron.ts +0 -85
- package/src/ledger/clients/utxo.ts +0 -154
- package/src/ledger/clients/xrp.ts +0 -50
- package/src/ledger/cosmosTypes.ts +0 -98
- package/src/ledger/helpers/getLedgerAddress.ts +0 -69
- package/src/ledger/helpers/getLedgerClient.ts +0 -117
- package/src/ledger/helpers/getLedgerTransport.ts +0 -102
- package/src/ledger/helpers/index.ts +0 -3
- package/src/ledger/index.ts +0 -299
- package/src/ledger/interfaces/CosmosLedgerInterface.ts +0 -54
- package/src/ledger/types.ts +0 -40
- package/src/trezor/evmSigner.ts +0 -177
- package/src/trezor/index.ts +0 -349
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { type DerivationPathArray, LedgerErrorCode, NetworkDerivationPath, SwapKitError } from "@swapkit/helpers";
|
|
2
|
-
|
|
3
|
-
import { THORChainApp } from "../clients/thorchain/lib";
|
|
4
|
-
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
5
|
-
|
|
6
|
-
export abstract class CosmosLedgerInterface {
|
|
7
|
-
ledgerTimeout = 50000;
|
|
8
|
-
derivationPath: DerivationPathArray | string = NetworkDerivationPath.GAIA;
|
|
9
|
-
transport: any;
|
|
10
|
-
ledgerApp: any;
|
|
11
|
-
chain: "thor" | "cosmos" = "thor";
|
|
12
|
-
|
|
13
|
-
checkOrCreateTransportAndLedger = async (forceReconnect = false) => {
|
|
14
|
-
if (!forceReconnect && this.transport && this.ledgerApp) return;
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
this.transport = forceReconnect || !this.transport ? await getLedgerTransport() : this.transport;
|
|
18
|
-
|
|
19
|
-
switch (this.chain) {
|
|
20
|
-
case "thor": {
|
|
21
|
-
this.ledgerApp = forceReconnect || !this.ledgerApp ? new THORChainApp(this.transport) : this.ledgerApp;
|
|
22
|
-
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
case "cosmos": {
|
|
27
|
-
const CosmosApp = (await import("@ledgerhq/hw-app-cosmos")).default;
|
|
28
|
-
this.ledgerApp = forceReconnect || !this.ledgerApp ? new CosmosApp(this.transport) : this.ledgerApp;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return this.ledgerApp;
|
|
33
|
-
} catch (error: unknown) {
|
|
34
|
-
throw new SwapKitError("wallet_ledger_connection_error", error);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
validateResponse = (errorCode: LedgerErrorCode, message?: string) => {
|
|
39
|
-
switch (errorCode) {
|
|
40
|
-
case LedgerErrorCode.NoError:
|
|
41
|
-
return;
|
|
42
|
-
|
|
43
|
-
case LedgerErrorCode.LockedDevice:
|
|
44
|
-
throw new SwapKitError("wallet_ledger_device_locked", { message: `Ledger is locked: ${message}` });
|
|
45
|
-
|
|
46
|
-
case LedgerErrorCode.TC_NotFound:
|
|
47
|
-
throw new SwapKitError("wallet_ledger_device_not_found");
|
|
48
|
-
|
|
49
|
-
default: {
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
package/src/ledger/types.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { CosmosLedger } from "./clients/cosmos";
|
|
2
|
-
import type {
|
|
3
|
-
ArbitrumLedger,
|
|
4
|
-
AuroraLedger,
|
|
5
|
-
AvalancheLedger,
|
|
6
|
-
BaseLedger,
|
|
7
|
-
BinanceSmartChainLedger,
|
|
8
|
-
EthereumLedger,
|
|
9
|
-
GnosisLedger,
|
|
10
|
-
OptimismLedger,
|
|
11
|
-
PolygonLedger,
|
|
12
|
-
} from "./clients/evm";
|
|
13
|
-
import type { THORChainLedger } from "./clients/thorchain";
|
|
14
|
-
import type { TronLedger } from "./clients/tron";
|
|
15
|
-
import type { BitcoinCashLedger, BitcoinLedger, DogecoinLedger, LitecoinLedger } from "./clients/utxo";
|
|
16
|
-
|
|
17
|
-
export type UTXOLedgerClients =
|
|
18
|
-
| ReturnType<typeof BitcoinLedger>
|
|
19
|
-
| ReturnType<typeof BitcoinCashLedger>
|
|
20
|
-
| ReturnType<typeof DogecoinLedger>
|
|
21
|
-
| ReturnType<typeof LitecoinLedger>;
|
|
22
|
-
export type CosmosLedgerClients = CosmosLedger | THORChainLedger;
|
|
23
|
-
export type EVMLedgerClients =
|
|
24
|
-
| ReturnType<typeof ArbitrumLedger>
|
|
25
|
-
| ReturnType<typeof AuroraLedger>
|
|
26
|
-
| ReturnType<typeof AvalancheLedger>
|
|
27
|
-
| ReturnType<typeof BaseLedger>
|
|
28
|
-
| ReturnType<typeof BinanceSmartChainLedger>
|
|
29
|
-
| ReturnType<typeof GnosisLedger>
|
|
30
|
-
| ReturnType<typeof EthereumLedger>
|
|
31
|
-
| ReturnType<typeof OptimismLedger>
|
|
32
|
-
| ReturnType<typeof PolygonLedger>;
|
|
33
|
-
export type TronLedgerClient = ReturnType<typeof TronLedger>;
|
|
34
|
-
|
|
35
|
-
export type GetAddressAndPubKeyResponse = {
|
|
36
|
-
bech32_address: string;
|
|
37
|
-
compressed_pk: any;
|
|
38
|
-
error_message: string;
|
|
39
|
-
return_code: number;
|
|
40
|
-
};
|
package/src/trezor/evmSigner.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
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 } 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(): Promise<string> {
|
|
77
|
-
throw new SwapKitError("wallet_trezor_method_not_supported", { method: "signTypedData" });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
signTransaction = async ({
|
|
81
|
-
to,
|
|
82
|
-
gasLimit,
|
|
83
|
-
value,
|
|
84
|
-
data,
|
|
85
|
-
nonce,
|
|
86
|
-
maxFeePerGas,
|
|
87
|
-
maxPriorityFeePerGas,
|
|
88
|
-
gasPrice,
|
|
89
|
-
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: any: refactor
|
|
90
|
-
}: TransactionRequest) => {
|
|
91
|
-
if (!to) throw new SwapKitError({ errorKey: "wallet_missing_params", info: { to } });
|
|
92
|
-
if (!gasLimit) throw new SwapKitError({ errorKey: "wallet_missing_params", info: { gasLimit } });
|
|
93
|
-
|
|
94
|
-
const isEIP1559 = maxFeePerGas && maxPriorityFeePerGas;
|
|
95
|
-
|
|
96
|
-
if (isEIP1559 && !maxFeePerGas) {
|
|
97
|
-
throw new SwapKitError({ errorKey: "wallet_missing_params", info: { maxFeePerGas } });
|
|
98
|
-
}
|
|
99
|
-
if (isEIP1559 && !maxPriorityFeePerGas) {
|
|
100
|
-
throw new SwapKitError({ errorKey: "wallet_missing_params", info: { maxPriorityFeePerGas } });
|
|
101
|
-
}
|
|
102
|
-
if (!(isEIP1559 || gasPrice)) {
|
|
103
|
-
throw new SwapKitError({ errorKey: "wallet_missing_params", info: { gasPrice } });
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
107
|
-
const { toHexString } = await import("@swapkit/toolboxes/evm");
|
|
108
|
-
const { Transaction } = await import("ethers");
|
|
109
|
-
|
|
110
|
-
const additionalFields = isEIP1559
|
|
111
|
-
? {
|
|
112
|
-
maxFeePerGas: toHexString(BigInt(maxFeePerGas?.toString() || 0)),
|
|
113
|
-
maxPriorityFeePerGas: toHexString(BigInt(maxPriorityFeePerGas?.toString() || 0)),
|
|
114
|
-
}
|
|
115
|
-
: (gasPrice && { gasPrice: toHexString(BigInt(gasPrice?.toString() || 0)) }) || { gasPrice: "0x0" };
|
|
116
|
-
|
|
117
|
-
const hexifiedNonce = toHexString(
|
|
118
|
-
BigInt(nonce || (await this.provider.getTransactionCount(await this.getAddress(), "pending"))),
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
const formattedTx = {
|
|
122
|
-
chainId: Number.parseInt(ChainToChainId[this.chain], 10),
|
|
123
|
-
data: data?.toString() || "0x",
|
|
124
|
-
gasLimit: toHexString(BigInt(gasLimit?.toString() || 0)),
|
|
125
|
-
nonce: hexifiedNonce,
|
|
126
|
-
to: to.toString(),
|
|
127
|
-
value: toHexString(BigInt(value?.toString() || 0)),
|
|
128
|
-
...additionalFields,
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const { success, payload } = await TrezorConnect.ethereumSignTransaction({
|
|
132
|
-
path: derivationPathToString(this.derivationPath),
|
|
133
|
-
transaction: formattedTx,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
if (!success) {
|
|
137
|
-
throw new SwapKitError({
|
|
138
|
-
errorKey: "wallet_trezor_failed_to_sign_transaction",
|
|
139
|
-
info: { ...payload, chain: this.chain, derivationPath: this.derivationPath },
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const { r, s, v } = payload;
|
|
144
|
-
|
|
145
|
-
const signature = Signature.from({ r, s, v: new SwapKitNumber(BigInt(v)).getBaseValue("number") });
|
|
146
|
-
|
|
147
|
-
const serializedTx = Transaction.from({
|
|
148
|
-
...formattedTx,
|
|
149
|
-
nonce: Number.parseInt(formattedTx.nonce, 16),
|
|
150
|
-
signature,
|
|
151
|
-
type: isEIP1559 ? 2 : 0,
|
|
152
|
-
}).serialized;
|
|
153
|
-
|
|
154
|
-
if (!serializedTx) {
|
|
155
|
-
throw new SwapKitError({
|
|
156
|
-
errorKey: "wallet_trezor_failed_to_sign_transaction",
|
|
157
|
-
info: { chain: this.chain, derivationPath: this.derivationPath },
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return serializedTx;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
connect = (provider: Provider | null) => {
|
|
165
|
-
if (!provider) {
|
|
166
|
-
throw new SwapKitError({
|
|
167
|
-
errorKey: "wallet_provider_not_found",
|
|
168
|
-
info: { chain: this.chain, derivationPath: this.derivationPath, wallet: WalletOption.TREZOR },
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return new TrezorSigner({ chain: this.chain, derivationPath: this.derivationPath, provider });
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return new TrezorSigner({ chain, derivationPath, provider });
|
|
177
|
-
}
|
package/src/trezor/index.ts
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
import type { ZcashPsbt } from "@bitgo/utxo-lib/dist/src/bitgo";
|
|
2
|
-
import {
|
|
3
|
-
Chain,
|
|
4
|
-
type DerivationPathArray,
|
|
5
|
-
derivationPathToString,
|
|
6
|
-
FeeOption,
|
|
7
|
-
filterSupportedChains,
|
|
8
|
-
type GenericTransferParams,
|
|
9
|
-
SKConfig,
|
|
10
|
-
SwapKitError,
|
|
11
|
-
WalletOption,
|
|
12
|
-
} from "@swapkit/helpers";
|
|
13
|
-
import type { UTXOToolboxes, UTXOType } from "@swapkit/toolboxes/utxo";
|
|
14
|
-
import { createWallet, getWalletSupportedChains } from "@swapkit/wallet-core";
|
|
15
|
-
import { type Psbt, script } from "bitcoinjs-lib";
|
|
16
|
-
|
|
17
|
-
function getScriptType(derivationPath: DerivationPathArray) {
|
|
18
|
-
switch (derivationPath[0]) {
|
|
19
|
-
case 84:
|
|
20
|
-
return { input: "SPENDWITNESS", output: "PAYTOWITNESS" } as const;
|
|
21
|
-
case 49:
|
|
22
|
-
return { input: "SPENDP2SHWITNESS", output: "PAYTOP2SHWITNESS" } as const;
|
|
23
|
-
case 44:
|
|
24
|
-
return { input: "SPENDADDRESS", output: "PAYTOADDRESS" } as const;
|
|
25
|
-
default:
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async function getTrezorWallet<T extends Chain>({
|
|
31
|
-
chain,
|
|
32
|
-
derivationPath,
|
|
33
|
-
}: {
|
|
34
|
-
chain: T;
|
|
35
|
-
derivationPath: DerivationPathArray;
|
|
36
|
-
}) {
|
|
37
|
-
switch (chain) {
|
|
38
|
-
case Chain.Arbitrum:
|
|
39
|
-
case Chain.Aurora:
|
|
40
|
-
case Chain.Avalanche:
|
|
41
|
-
case Chain.Base:
|
|
42
|
-
case Chain.BinanceSmartChain:
|
|
43
|
-
case Chain.Ethereum:
|
|
44
|
-
case Chain.Gnosis:
|
|
45
|
-
case Chain.Monad:
|
|
46
|
-
case Chain.Optimism:
|
|
47
|
-
case Chain.Polygon:
|
|
48
|
-
case Chain.XLayer: {
|
|
49
|
-
const { getProvider, getEvmToolbox } = await import("@swapkit/toolboxes/evm");
|
|
50
|
-
const { getEVMSigner } = await import("./evmSigner");
|
|
51
|
-
|
|
52
|
-
const provider = await getProvider(chain);
|
|
53
|
-
const signer = await getEVMSigner({ chain, derivationPath, provider });
|
|
54
|
-
const address = await signer.getAddress();
|
|
55
|
-
const toolbox = await getEvmToolbox(chain, { provider, signer });
|
|
56
|
-
|
|
57
|
-
return { ...toolbox, address };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
case Chain.Zcash: {
|
|
61
|
-
const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
62
|
-
|
|
63
|
-
const derivationPathStr = derivationPathToString(derivationPath);
|
|
64
|
-
|
|
65
|
-
const getAddress = async () => {
|
|
66
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
67
|
-
const { success, payload } = await TrezorConnect.getAddress({ coin: "zcash", path: derivationPathStr });
|
|
68
|
-
|
|
69
|
-
if (!success) {
|
|
70
|
-
throw new SwapKitError({
|
|
71
|
-
errorKey: "wallet_trezor_failed_to_get_address",
|
|
72
|
-
info: { chain, error: (payload as { error: string; code?: string }).error || "Unknown error" },
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return payload.address;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const address = await getAddress();
|
|
80
|
-
|
|
81
|
-
const signer = {
|
|
82
|
-
getAddress: async () => address,
|
|
83
|
-
signTransaction: async (zcashPsbt: ZcashPsbt) => {
|
|
84
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
85
|
-
const { address: zcashAddress, networks } = await import("@bitgo/utxo-lib");
|
|
86
|
-
const address_n = derivationPath.map((pathElement, index) =>
|
|
87
|
-
index < 3 ? ((pathElement as number) | 0x80000000) >>> 0 : (pathElement as number),
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
const version = 5;
|
|
91
|
-
const versionGroupId = 0x26a7270a;
|
|
92
|
-
const branchId = 0x4dec4df0;
|
|
93
|
-
|
|
94
|
-
const inputs = zcashPsbt.txInputs.map((input, idx) => ({
|
|
95
|
-
address_n,
|
|
96
|
-
amount: zcashPsbt.data.inputs[idx]?.witnessUtxo?.value?.toString() || "0",
|
|
97
|
-
prev_hash: input.hash.reverse().toString("hex"),
|
|
98
|
-
prev_index: input.index,
|
|
99
|
-
script_type: "SPENDADDRESS" as const,
|
|
100
|
-
}));
|
|
101
|
-
|
|
102
|
-
const outputs = zcashPsbt.txOutputs.map((output) => {
|
|
103
|
-
if (
|
|
104
|
-
Number(output.value) === 0 &&
|
|
105
|
-
output.script.length > 0 &&
|
|
106
|
-
output.script[0] === 0x6a &&
|
|
107
|
-
script.decompile(output.script)
|
|
108
|
-
) {
|
|
109
|
-
return {
|
|
110
|
-
amount: "0",
|
|
111
|
-
op_return_data: (script.decompile(output.script) as any)[1].toString("hex"),
|
|
112
|
-
script_type: "PAYTOOPRETURN",
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const maybeRecipient = zcashAddress.fromOutputScript(output.script, networks.zcash);
|
|
117
|
-
const isChangeAddress = maybeRecipient === address;
|
|
118
|
-
|
|
119
|
-
return isChangeAddress
|
|
120
|
-
? { address_n, amount: output.value.toString(), script_type: "PAYTOADDRESS" }
|
|
121
|
-
: { address: maybeRecipient, amount: output.value.toString(), script_type: "PAYTOADDRESS" };
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
const result = await TrezorConnect.signTransaction({
|
|
125
|
-
branchId,
|
|
126
|
-
coin: "zcash",
|
|
127
|
-
expiry: 0,
|
|
128
|
-
inputs,
|
|
129
|
-
locktime: 0,
|
|
130
|
-
// @ts-expect-error
|
|
131
|
-
outputs,
|
|
132
|
-
overwintered: true,
|
|
133
|
-
version,
|
|
134
|
-
versionGroupId,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
if (result.success) {
|
|
138
|
-
return result.payload.serializedTx;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
throw new SwapKitError({
|
|
142
|
-
errorKey: "wallet_trezor_failed_to_sign_transaction",
|
|
143
|
-
info: { chain, error: (result.payload as { error: string; code?: string }).error },
|
|
144
|
-
});
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const toolbox = await getUtxoToolbox(Chain.Zcash);
|
|
149
|
-
|
|
150
|
-
const transfer = async (params: GenericTransferParams) => {
|
|
151
|
-
if (!(address && params.recipient)) {
|
|
152
|
-
throw new SwapKitError({
|
|
153
|
-
errorKey: "wallet_missing_params",
|
|
154
|
-
info: { address, recipient: params.recipient, wallet: WalletOption.TREZOR },
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const feeRate = params.feeRate || (await toolbox.getFeeRates())[params.feeOptionKey || FeeOption.Fast];
|
|
159
|
-
|
|
160
|
-
const { psbt } = await toolbox.createTransaction({ ...params, feeRate, fetchTxHex: false, sender: address });
|
|
161
|
-
|
|
162
|
-
const txHex = await signer.signTransaction(psbt);
|
|
163
|
-
const tx = await toolbox.broadcastTx(txHex);
|
|
164
|
-
|
|
165
|
-
return tx;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
return { ...toolbox, address, signTransaction: signer.signTransaction, transfer };
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
case Chain.Bitcoin:
|
|
172
|
-
case Chain.BitcoinCash:
|
|
173
|
-
case Chain.Dash:
|
|
174
|
-
case Chain.Dogecoin:
|
|
175
|
-
case Chain.Litecoin: {
|
|
176
|
-
const { toCashAddress, getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
177
|
-
const scriptType = getScriptType(derivationPath);
|
|
178
|
-
|
|
179
|
-
if (!scriptType) {
|
|
180
|
-
throw new SwapKitError({ errorKey: "wallet_trezor_derivation_path_not_supported", info: { derivationPath } });
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const coin = chain.toLowerCase();
|
|
184
|
-
|
|
185
|
-
const getAddress = async (path: DerivationPathArray = derivationPath) => {
|
|
186
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
187
|
-
const { success, payload } = await TrezorConnect.getAddress({ coin, path: derivationPathToString(path) });
|
|
188
|
-
|
|
189
|
-
if (!success) {
|
|
190
|
-
throw new SwapKitError({
|
|
191
|
-
errorKey: "wallet_trezor_failed_to_get_address",
|
|
192
|
-
info: { chain, error: (payload as { error: string; code?: string }).error || "Unknown error" },
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (chain === Chain.BitcoinCash) {
|
|
197
|
-
const toolbox = await getUtxoToolbox(chain as typeof Chain.BitcoinCash);
|
|
198
|
-
return toolbox.stripPrefix(payload.address);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return payload.address;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const address = await getAddress();
|
|
205
|
-
|
|
206
|
-
const signTransaction = async (psbt: Psbt, inputs: UTXOType[], memo = "") => {
|
|
207
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
208
|
-
const address_n = derivationPath.map((pathElement, index) =>
|
|
209
|
-
index < 3 ? ((pathElement as number) | 0x80000000) >>> 0 : (pathElement as number),
|
|
210
|
-
);
|
|
211
|
-
const toolbox = await getUtxoToolbox(chain as typeof Chain.BitcoinCash);
|
|
212
|
-
|
|
213
|
-
const result = await TrezorConnect.signTransaction({
|
|
214
|
-
coin,
|
|
215
|
-
inputs: inputs.map(({ hash, index, value }) => ({
|
|
216
|
-
// Hardens the first 3 elements of the derivation path - required by trezor
|
|
217
|
-
address_n,
|
|
218
|
-
// object needs amount but does not use it for signing
|
|
219
|
-
amount: value,
|
|
220
|
-
prev_hash: hash,
|
|
221
|
-
prev_index: index,
|
|
222
|
-
script_type: scriptType.input,
|
|
223
|
-
})),
|
|
224
|
-
outputs: psbt.txOutputs.map((output) => {
|
|
225
|
-
if (!output.address) {
|
|
226
|
-
return { amount: "0", op_return_data: Buffer.from(memo).toString("hex"), script_type: "PAYTOOPRETURN" };
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const outputAddress =
|
|
230
|
-
chain === Chain.BitcoinCash ? toolbox.stripPrefix(toCashAddress(output.address)) : output.address;
|
|
231
|
-
|
|
232
|
-
const isChangeAddress = outputAddress === address;
|
|
233
|
-
|
|
234
|
-
return isChangeAddress
|
|
235
|
-
? { address_n, amount: output.value, script_type: scriptType.output }
|
|
236
|
-
: { address: outputAddress, amount: output.value, script_type: "PAYTOADDRESS" };
|
|
237
|
-
}),
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
if (result.success) {
|
|
241
|
-
return result.payload.serializedTx;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
throw new SwapKitError({
|
|
245
|
-
errorKey: "wallet_trezor_failed_to_sign_transaction",
|
|
246
|
-
info: { chain, error: (result.payload as { error: string; code?: string }).error },
|
|
247
|
-
});
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
const transfer = async ({
|
|
251
|
-
recipient,
|
|
252
|
-
feeOptionKey,
|
|
253
|
-
feeRate: paramFeeRate,
|
|
254
|
-
memo,
|
|
255
|
-
...rest
|
|
256
|
-
}: GenericTransferParams) => {
|
|
257
|
-
if (!(address && recipient)) {
|
|
258
|
-
throw new SwapKitError({
|
|
259
|
-
errorKey: "wallet_missing_params",
|
|
260
|
-
info: { address, memo, recipient, wallet: WalletOption.TREZOR },
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const toolbox = await getUtxoToolbox(chain);
|
|
265
|
-
|
|
266
|
-
const feeRate = paramFeeRate || (await toolbox.getFeeRates())[feeOptionKey || FeeOption.Fast];
|
|
267
|
-
|
|
268
|
-
const createTxMethod =
|
|
269
|
-
chain === Chain.BitcoinCash
|
|
270
|
-
? (toolbox as UTXOToolboxes["BCH"]).buildTx
|
|
271
|
-
: (toolbox as UTXOToolboxes["BTC"]).createTransaction;
|
|
272
|
-
|
|
273
|
-
const { psbt, inputs } = await createTxMethod({
|
|
274
|
-
...rest,
|
|
275
|
-
feeRate,
|
|
276
|
-
fetchTxHex: true,
|
|
277
|
-
memo,
|
|
278
|
-
recipient,
|
|
279
|
-
sender: address,
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
const txHex = await signTransaction(psbt, inputs, memo);
|
|
283
|
-
const tx = await toolbox.broadcastTx(txHex);
|
|
284
|
-
|
|
285
|
-
return tx;
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
const toolbox = await getUtxoToolbox(chain);
|
|
289
|
-
|
|
290
|
-
return { ...toolbox, address, signTransaction, transfer };
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
default:
|
|
294
|
-
throw new SwapKitError({ errorKey: "wallet_chain_not_supported", info: { chain, wallet: WalletOption.TREZOR } });
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
export const trezorWallet = createWallet({
|
|
299
|
-
connect: ({ addChain, supportedChains, walletType }) =>
|
|
300
|
-
async function connectTrezor(chains: Chain[], derivationPath: DerivationPathArray) {
|
|
301
|
-
const [chain] = filterSupportedChains({ chains, supportedChains, walletType });
|
|
302
|
-
if (!chain) {
|
|
303
|
-
throw new SwapKitError({
|
|
304
|
-
errorKey: "wallet_chain_not_supported",
|
|
305
|
-
info: { chain, wallet: WalletOption.TREZOR },
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
const TrezorConnect = (await import("@trezor/connect-web")).default;
|
|
310
|
-
const { success } = await TrezorConnect.getDeviceState();
|
|
311
|
-
|
|
312
|
-
if (!success) {
|
|
313
|
-
const trezorConfig = SKConfig.get("integrations").trezor;
|
|
314
|
-
const manifest = trezorConfig
|
|
315
|
-
? { ...trezorConfig, appName: (trezorConfig as any).appName || "SwapKit" }
|
|
316
|
-
: { appName: "SwapKit", appUrl: "", email: "" };
|
|
317
|
-
TrezorConnect.init({ lazyLoad: true, manifest });
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const wallet = await getTrezorWallet({ chain, derivationPath });
|
|
321
|
-
|
|
322
|
-
addChain({ ...wallet, chain, walletType });
|
|
323
|
-
|
|
324
|
-
return true;
|
|
325
|
-
},
|
|
326
|
-
name: "connectTrezor",
|
|
327
|
-
supportedChains: [
|
|
328
|
-
Chain.Arbitrum,
|
|
329
|
-
Chain.Aurora,
|
|
330
|
-
Chain.Avalanche,
|
|
331
|
-
Chain.Base,
|
|
332
|
-
Chain.BinanceSmartChain,
|
|
333
|
-
Chain.Bitcoin,
|
|
334
|
-
Chain.BitcoinCash,
|
|
335
|
-
Chain.Dash,
|
|
336
|
-
Chain.Dogecoin,
|
|
337
|
-
Chain.Ethereum,
|
|
338
|
-
Chain.Gnosis,
|
|
339
|
-
Chain.Litecoin,
|
|
340
|
-
Chain.Monad,
|
|
341
|
-
Chain.Optimism,
|
|
342
|
-
Chain.Polygon,
|
|
343
|
-
Chain.XLayer,
|
|
344
|
-
Chain.Zcash,
|
|
345
|
-
],
|
|
346
|
-
walletType: WalletOption.TREZOR,
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
export const TREZOR_SUPPORTED_CHAINS = getWalletSupportedChains(trezorWallet);
|