@swapkit/wallet-hardware 4.2.11 → 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,98 +0,0 @@
|
|
|
1
|
-
import { SwapKitError } from "@swapkit/helpers";
|
|
2
|
-
|
|
3
|
-
export interface Coin {
|
|
4
|
-
readonly denom: string;
|
|
5
|
-
readonly amount: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface EncodeObject {
|
|
9
|
-
readonly typeUrl: string;
|
|
10
|
-
readonly value: any;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface AminoMsg {
|
|
14
|
-
readonly type: string;
|
|
15
|
-
readonly value: any;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface AminoMsgSend extends AminoMsg {
|
|
19
|
-
readonly type: "cosmos-sdk/MsgSend";
|
|
20
|
-
readonly value: {
|
|
21
|
-
/** Bech32 account address */
|
|
22
|
-
readonly from_address: string;
|
|
23
|
-
/** Bech32 account address */
|
|
24
|
-
readonly to_address: string;
|
|
25
|
-
readonly amount: readonly Coin[];
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface AminoConverter {
|
|
30
|
-
readonly aminoType: string;
|
|
31
|
-
readonly toAmino: (value: any) => any;
|
|
32
|
-
readonly fromAmino: (value: any) => any;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** A map from protobuf type URL to the AminoConverter implementation if supported on chain */
|
|
36
|
-
export type AminoConverters = Record<string, AminoConverter | "not_supported_by_chain">;
|
|
37
|
-
|
|
38
|
-
function isAminoConverter(
|
|
39
|
-
converter: [string, AminoConverter | "not_supported_by_chain"],
|
|
40
|
-
): converter is [string, AminoConverter] {
|
|
41
|
-
return typeof converter[1] !== "string";
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A map from Stargate message types as used in the messages's `Any` type
|
|
46
|
-
* to Amino types.
|
|
47
|
-
*/
|
|
48
|
-
export class AminoTypes {
|
|
49
|
-
// The map type here ensures uniqueness of the protobuf type URL in the key.
|
|
50
|
-
// There is no uniqueness guarantee of the Amino type identifier in the type
|
|
51
|
-
// system or constructor. Instead it's the user's responsibility to ensure
|
|
52
|
-
// there is no overlap when fromAmino is called.
|
|
53
|
-
private readonly register: Record<string, AminoConverter | "not_supported_by_chain">;
|
|
54
|
-
|
|
55
|
-
constructor(types: AminoConverters) {
|
|
56
|
-
this.register = types;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
toAmino({ typeUrl, value }: EncodeObject): AminoMsg {
|
|
60
|
-
const converter = this.register[typeUrl];
|
|
61
|
-
if (converter === "not_supported_by_chain") {
|
|
62
|
-
throw new SwapKitError("wallet_ledger_chain_not_supported", {
|
|
63
|
-
reason: `The message type '${typeUrl}' cannot be signed using the Amino JSON sign mode because this is not supported by chain.`,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
if (!converter) {
|
|
67
|
-
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
68
|
-
reason: `Type URL '${typeUrl}' does not exist in the Amino message type register.`,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
return { type: converter.aminoType, value: converter.toAmino(value) };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
fromAmino({ type, value }: AminoMsg): EncodeObject {
|
|
75
|
-
const matches = Object.entries(this.register)
|
|
76
|
-
.filter(isAminoConverter)
|
|
77
|
-
.filter(([_typeUrl, { aminoType }]) => aminoType === type);
|
|
78
|
-
|
|
79
|
-
if (matches.length === 0) {
|
|
80
|
-
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
81
|
-
reason: `Amino type identifier '${type}' does not exist in the Amino message type register.`,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (matches.length > 1) {
|
|
86
|
-
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
87
|
-
reason: `Multiple types are registered with Amino type identifier '${type}': '${matches
|
|
88
|
-
.map(([key, _value]) => key)
|
|
89
|
-
.sort()
|
|
90
|
-
.join("', '")}'. Thus fromAmino cannot be performed.`,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const [typeUrl, converter] = matches[0] as [string, AminoConverter];
|
|
95
|
-
|
|
96
|
-
return { typeUrl, value: converter.fromAmino(value) };
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { Chain, SwapKitError, WalletOption } from "@swapkit/helpers";
|
|
2
|
-
|
|
3
|
-
import type { getNearLedgerClient } from "../clients/near";
|
|
4
|
-
import type { TronLedger } from "../clients/tron";
|
|
5
|
-
import type { XRPLedger } from "../clients/xrp";
|
|
6
|
-
import type { LEDGER_SUPPORTED_CHAINS } from "../index";
|
|
7
|
-
import type { CosmosLedgerClients, EVMLedgerClients, UTXOLedgerClients } from "../types";
|
|
8
|
-
import type { getLedgerClient } from "./getLedgerClient";
|
|
9
|
-
|
|
10
|
-
export const getLedgerAddress = async <
|
|
11
|
-
T extends (typeof LEDGER_SUPPORTED_CHAINS)[number],
|
|
12
|
-
L extends Awaited<ReturnType<typeof getLedgerClient<T>>>,
|
|
13
|
-
>({
|
|
14
|
-
chain,
|
|
15
|
-
ledgerClient,
|
|
16
|
-
}: {
|
|
17
|
-
chain: T;
|
|
18
|
-
ledgerClient: L;
|
|
19
|
-
}) => {
|
|
20
|
-
if (!ledgerClient) return "";
|
|
21
|
-
|
|
22
|
-
switch (chain) {
|
|
23
|
-
case Chain.Cosmos:
|
|
24
|
-
case Chain.THORChain: {
|
|
25
|
-
return (ledgerClient as CosmosLedgerClients).connect();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
case Chain.Ethereum:
|
|
29
|
-
case Chain.BinanceSmartChain:
|
|
30
|
-
case Chain.Avalanche:
|
|
31
|
-
case Chain.Polygon:
|
|
32
|
-
case Chain.Arbitrum:
|
|
33
|
-
case Chain.Optimism:
|
|
34
|
-
case Chain.Base:
|
|
35
|
-
case Chain.Aurora:
|
|
36
|
-
case Chain.Gnosis:
|
|
37
|
-
case Chain.XLayer: {
|
|
38
|
-
return (ledgerClient as EVMLedgerClients).getAddress();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
case Chain.Bitcoin:
|
|
42
|
-
case Chain.BitcoinCash:
|
|
43
|
-
case Chain.Dash:
|
|
44
|
-
case Chain.Dogecoin:
|
|
45
|
-
case Chain.Litecoin:
|
|
46
|
-
case Chain.Zcash: {
|
|
47
|
-
const ledger = ledgerClient as UTXOLedgerClients;
|
|
48
|
-
await ledger.connect();
|
|
49
|
-
const address = await ledger.getAddress();
|
|
50
|
-
|
|
51
|
-
return chain === Chain.BitcoinCash ? address.replace("bitcoincash:", "") : address;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
case Chain.Near: {
|
|
55
|
-
return await (ledgerClient as Awaited<ReturnType<typeof getNearLedgerClient>>).getAddress();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
case Chain.Ripple: {
|
|
59
|
-
return (ledgerClient as Awaited<ReturnType<typeof XRPLedger>>).getAddress();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
case Chain.Tron: {
|
|
63
|
-
return (ledgerClient as Awaited<ReturnType<typeof TronLedger>>).getAddress();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
default:
|
|
67
|
-
throw new SwapKitError("wallet_chain_not_supported", { chain, wallet: WalletOption.LEDGER });
|
|
68
|
-
}
|
|
69
|
-
};
|
|
@@ -1,117 +0,0 @@
|
|
|
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
|
-
BinanceSmartChainLedger,
|
|
10
|
-
EthereumLedger,
|
|
11
|
-
GnosisLedger,
|
|
12
|
-
MonadLedger,
|
|
13
|
-
OptimismLedger,
|
|
14
|
-
PolygonLedger,
|
|
15
|
-
XLayerLedger,
|
|
16
|
-
} from "../clients/evm";
|
|
17
|
-
import { getNearLedgerClient } from "../clients/near";
|
|
18
|
-
import { THORChainLedger } from "../clients/thorchain";
|
|
19
|
-
import { TronLedger } from "../clients/tron";
|
|
20
|
-
import {
|
|
21
|
-
BitcoinCashLedger,
|
|
22
|
-
BitcoinLedger,
|
|
23
|
-
DashLedger,
|
|
24
|
-
DogecoinLedger,
|
|
25
|
-
LitecoinLedger,
|
|
26
|
-
ZcashLedger,
|
|
27
|
-
} from "../clients/utxo";
|
|
28
|
-
import { XRPLedger } from "../clients/xrp";
|
|
29
|
-
|
|
30
|
-
type LedgerSignerMap = {
|
|
31
|
-
[Chain.Arbitrum]: ReturnType<typeof ArbitrumLedger>;
|
|
32
|
-
[Chain.Aurora]: ReturnType<typeof AuroraLedger>;
|
|
33
|
-
[Chain.Avalanche]: ReturnType<typeof AvalancheLedger>;
|
|
34
|
-
[Chain.Base]: ReturnType<typeof BaseLedger>;
|
|
35
|
-
[Chain.BinanceSmartChain]: ReturnType<typeof BinanceSmartChainLedger>;
|
|
36
|
-
[Chain.BitcoinCash]: ReturnType<typeof BitcoinCashLedger>;
|
|
37
|
-
[Chain.Bitcoin]: ReturnType<typeof BitcoinLedger>;
|
|
38
|
-
[Chain.Cosmos]: CosmosLedger;
|
|
39
|
-
[Chain.Dash]: ReturnType<typeof DashLedger>;
|
|
40
|
-
[Chain.Dogecoin]: ReturnType<typeof DogecoinLedger>;
|
|
41
|
-
[Chain.Ethereum]: ReturnType<typeof EthereumLedger>;
|
|
42
|
-
[Chain.Gnosis]: ReturnType<typeof GnosisLedger>;
|
|
43
|
-
[Chain.Litecoin]: ReturnType<typeof LitecoinLedger>;
|
|
44
|
-
[Chain.Monad]: ReturnType<typeof MonadLedger>;
|
|
45
|
-
[Chain.Near]: ReturnType<typeof getNearLedgerClient>;
|
|
46
|
-
[Chain.Optimism]: ReturnType<typeof OptimismLedger>;
|
|
47
|
-
[Chain.Polygon]: ReturnType<typeof PolygonLedger>;
|
|
48
|
-
[Chain.Ripple]: ReturnType<typeof XRPLedger>;
|
|
49
|
-
[Chain.THORChain]: THORChainLedger;
|
|
50
|
-
[Chain.Tron]: ReturnType<typeof TronLedger>;
|
|
51
|
-
[Chain.XLayer]: ReturnType<typeof XLayerLedger>;
|
|
52
|
-
[Chain.Zcash]: ReturnType<typeof ZcashLedger>;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
type LedgerSupportedChain = keyof LedgerSignerMap;
|
|
56
|
-
|
|
57
|
-
export const getLedgerClient = async <T extends LedgerSupportedChain>({
|
|
58
|
-
chain,
|
|
59
|
-
derivationPath,
|
|
60
|
-
}: {
|
|
61
|
-
chain: T;
|
|
62
|
-
derivationPath?: DerivationPathArray;
|
|
63
|
-
}): Promise<LedgerSignerMap[T]> => {
|
|
64
|
-
const { match } = await import("ts-pattern");
|
|
65
|
-
|
|
66
|
-
return (
|
|
67
|
-
match(chain as LedgerSupportedChain)
|
|
68
|
-
.returnType<Promise<LedgerSignerMap[T]>>()
|
|
69
|
-
.with(Chain.THORChain, () => Promise.resolve(new THORChainLedger(derivationPath) as LedgerSignerMap[T]))
|
|
70
|
-
.with(Chain.Cosmos, () => Promise.resolve(new CosmosLedger(derivationPath) as LedgerSignerMap[T]))
|
|
71
|
-
.with(Chain.Bitcoin, () => Promise.resolve(BitcoinLedger(derivationPath) as LedgerSignerMap[T]))
|
|
72
|
-
.with(Chain.BitcoinCash, () => Promise.resolve(BitcoinCashLedger(derivationPath) as LedgerSignerMap[T]))
|
|
73
|
-
.with(Chain.Dash, () => Promise.resolve(DashLedger(derivationPath) as LedgerSignerMap[T]))
|
|
74
|
-
.with(Chain.Dogecoin, () => Promise.resolve(DogecoinLedger(derivationPath) as LedgerSignerMap[T]))
|
|
75
|
-
.with(Chain.Litecoin, () => Promise.resolve(LitecoinLedger(derivationPath) as LedgerSignerMap[T]))
|
|
76
|
-
.with(Chain.Zcash, () => Promise.resolve(ZcashLedger(derivationPath) as LedgerSignerMap[T]))
|
|
77
|
-
.with(Chain.Ripple, () => Promise.resolve(XRPLedger(derivationPath) as LedgerSignerMap[T]))
|
|
78
|
-
.with(Chain.Tron, () => Promise.resolve(TronLedger(derivationPath) as LedgerSignerMap[T]))
|
|
79
|
-
// @ts-expect-error
|
|
80
|
-
.with(Chain.Near, () => {
|
|
81
|
-
return Promise.resolve(getNearLedgerClient(derivationPath));
|
|
82
|
-
})
|
|
83
|
-
.with(
|
|
84
|
-
Chain.Arbitrum,
|
|
85
|
-
Chain.Aurora,
|
|
86
|
-
Chain.Avalanche,
|
|
87
|
-
Chain.BinanceSmartChain,
|
|
88
|
-
Chain.Ethereum,
|
|
89
|
-
Chain.Gnosis,
|
|
90
|
-
Chain.Monad,
|
|
91
|
-
Chain.Optimism,
|
|
92
|
-
Chain.Polygon,
|
|
93
|
-
Chain.Base,
|
|
94
|
-
Chain.XLayer,
|
|
95
|
-
async () => {
|
|
96
|
-
const { getProvider } = await import("@swapkit/toolboxes/evm");
|
|
97
|
-
const params = { derivationPath, provider: await getProvider(chain as EVMChain) };
|
|
98
|
-
|
|
99
|
-
return match(chain as Chain)
|
|
100
|
-
.with(Chain.BinanceSmartChain, () => BinanceSmartChainLedger(params) as LedgerSignerMap[T])
|
|
101
|
-
.with(Chain.Avalanche, () => AvalancheLedger(params) as LedgerSignerMap[T])
|
|
102
|
-
.with(Chain.Arbitrum, () => ArbitrumLedger(params) as LedgerSignerMap[T])
|
|
103
|
-
.with(Chain.Optimism, () => OptimismLedger(params) as LedgerSignerMap[T])
|
|
104
|
-
.with(Chain.Polygon, () => PolygonLedger(params) as LedgerSignerMap[T])
|
|
105
|
-
.with(Chain.Base, () => BaseLedger(params) as LedgerSignerMap[T])
|
|
106
|
-
.with(Chain.Aurora, () => AuroraLedger(params) as LedgerSignerMap[T])
|
|
107
|
-
.with(Chain.Gnosis, () => GnosisLedger(params) as LedgerSignerMap[T])
|
|
108
|
-
.with(Chain.Monad, () => MonadLedger(params) as LedgerSignerMap[T])
|
|
109
|
-
.with(Chain.XLayer, () => XLayerLedger(params) as LedgerSignerMap[T])
|
|
110
|
-
.otherwise(() => EthereumLedger(params) as LedgerSignerMap[T]);
|
|
111
|
-
},
|
|
112
|
-
)
|
|
113
|
-
.otherwise(() => {
|
|
114
|
-
throw new SwapKitError("wallet_chain_not_supported", { chain, wallet: WalletOption.LEDGER });
|
|
115
|
-
})
|
|
116
|
-
);
|
|
117
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
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
|
-
};
|
package/src/ledger/index.ts
DELETED
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Chain,
|
|
3
|
-
type DerivationPathArray,
|
|
4
|
-
FeeOption,
|
|
5
|
-
filterSupportedChains,
|
|
6
|
-
type GenericTransferParams,
|
|
7
|
-
getRPCUrl,
|
|
8
|
-
SwapKitError,
|
|
9
|
-
THORConfig,
|
|
10
|
-
WalletOption,
|
|
11
|
-
} from "@swapkit/helpers";
|
|
12
|
-
import type { ThorchainDepositParams } from "@swapkit/toolboxes/cosmos";
|
|
13
|
-
import type { UTXOBuildTxParams } from "@swapkit/toolboxes/utxo";
|
|
14
|
-
|
|
15
|
-
import { createWallet, getWalletSupportedChains } from "@swapkit/wallet-core";
|
|
16
|
-
import { getLedgerAddress, getLedgerClient } from "./helpers";
|
|
17
|
-
|
|
18
|
-
export const ledgerWallet = createWallet({
|
|
19
|
-
connect: ({ addChain, supportedChains, walletType }) =>
|
|
20
|
-
async function connectLedger(chains: Chain[], derivationPath?: DerivationPathArray) {
|
|
21
|
-
const [chain] = filterSupportedChains({ chains, supportedChains, walletType });
|
|
22
|
-
|
|
23
|
-
if (!chain) return false;
|
|
24
|
-
|
|
25
|
-
const walletMethods = await getWalletMethods({ chain, derivationPath });
|
|
26
|
-
|
|
27
|
-
addChain({ ...walletMethods, chain, walletType: WalletOption.LEDGER });
|
|
28
|
-
|
|
29
|
-
return true;
|
|
30
|
-
},
|
|
31
|
-
name: "connectLedger",
|
|
32
|
-
supportedChains: [
|
|
33
|
-
Chain.Arbitrum,
|
|
34
|
-
Chain.Aurora,
|
|
35
|
-
Chain.Avalanche,
|
|
36
|
-
Chain.Base,
|
|
37
|
-
Chain.BinanceSmartChain,
|
|
38
|
-
Chain.Bitcoin,
|
|
39
|
-
Chain.BitcoinCash,
|
|
40
|
-
Chain.Cosmos,
|
|
41
|
-
Chain.Dash,
|
|
42
|
-
Chain.Dogecoin,
|
|
43
|
-
Chain.Ethereum,
|
|
44
|
-
Chain.Gnosis,
|
|
45
|
-
Chain.Litecoin,
|
|
46
|
-
Chain.Monad,
|
|
47
|
-
Chain.Near,
|
|
48
|
-
Chain.Optimism,
|
|
49
|
-
Chain.Polygon,
|
|
50
|
-
Chain.Ripple,
|
|
51
|
-
Chain.THORChain,
|
|
52
|
-
Chain.XLayer,
|
|
53
|
-
Chain.Tron,
|
|
54
|
-
Chain.Zcash,
|
|
55
|
-
],
|
|
56
|
-
walletType: WalletOption.LEDGER,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export const LEDGER_SUPPORTED_CHAINS = getWalletSupportedChains(ledgerWallet);
|
|
60
|
-
|
|
61
|
-
// reduce memo length by removing trade limit
|
|
62
|
-
function reduceMemo(memo?: string, affiliateAddress = "t") {
|
|
63
|
-
if (!memo?.includes("=:")) return memo;
|
|
64
|
-
|
|
65
|
-
const removedAffiliate = memo.includes(`:${affiliateAddress}:`) ? memo.split(`:${affiliateAddress}:`)[0] : memo;
|
|
66
|
-
|
|
67
|
-
return removedAffiliate?.substring(0, removedAffiliate.lastIndexOf(":"));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function recursivelyOrderKeys(unordered: any) {
|
|
71
|
-
// If it's an array - recursively order any
|
|
72
|
-
// dictionary items within the array
|
|
73
|
-
if (Array.isArray(unordered)) {
|
|
74
|
-
unordered.forEach((item, index) => {
|
|
75
|
-
unordered[index] = recursivelyOrderKeys(item);
|
|
76
|
-
});
|
|
77
|
-
return unordered;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// If it's an object - let's order the keys
|
|
81
|
-
if (typeof unordered !== "object") return unordered;
|
|
82
|
-
const ordered: any = {};
|
|
83
|
-
const sortedKeys = Object.keys(unordered).sort();
|
|
84
|
-
|
|
85
|
-
for (const key of sortedKeys) {
|
|
86
|
-
ordered[key] = recursivelyOrderKeys(unordered[key]);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return ordered;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function stringifyKeysInOrder(data: any) {
|
|
93
|
-
return JSON.stringify(recursivelyOrderKeys(data));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function getWalletMethods({ chain, derivationPath }: { chain: Chain; derivationPath?: DerivationPathArray }) {
|
|
97
|
-
switch (chain) {
|
|
98
|
-
case Chain.BitcoinCash:
|
|
99
|
-
case Chain.Bitcoin:
|
|
100
|
-
case Chain.Dash:
|
|
101
|
-
case Chain.Dogecoin:
|
|
102
|
-
case Chain.Litecoin:
|
|
103
|
-
case Chain.Zcash: {
|
|
104
|
-
const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
105
|
-
const toolbox = await getUtxoToolbox(chain as typeof Chain.Bitcoin);
|
|
106
|
-
|
|
107
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
108
|
-
const address = await getLedgerAddress({ chain, ledgerClient: signer });
|
|
109
|
-
|
|
110
|
-
const transfer = async (params: UTXOBuildTxParams) => {
|
|
111
|
-
const feeRate = params.feeRate || (await toolbox.getFeeRates())[FeeOption.Average];
|
|
112
|
-
const memo = [Chain.Bitcoin].includes(chain as typeof Chain.Bitcoin) ? params.memo : reduceMemo(params.memo);
|
|
113
|
-
|
|
114
|
-
const { psbt, inputs } = await toolbox.createTransaction({
|
|
115
|
-
...params,
|
|
116
|
-
feeRate,
|
|
117
|
-
fetchTxHex: true,
|
|
118
|
-
memo,
|
|
119
|
-
sender: address,
|
|
120
|
-
});
|
|
121
|
-
const txHex = await signer.signTransaction(psbt, inputs);
|
|
122
|
-
const tx = await toolbox.broadcastTx(txHex);
|
|
123
|
-
|
|
124
|
-
return tx;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
return { ...toolbox, address, transfer };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
case Chain.Ethereum:
|
|
131
|
-
case Chain.Avalanche:
|
|
132
|
-
case Chain.Arbitrum:
|
|
133
|
-
case Chain.Optimism:
|
|
134
|
-
case Chain.Polygon:
|
|
135
|
-
case Chain.BinanceSmartChain:
|
|
136
|
-
case Chain.Base:
|
|
137
|
-
case Chain.Aurora:
|
|
138
|
-
case Chain.Gnosis:
|
|
139
|
-
case Chain.Monad:
|
|
140
|
-
case Chain.XLayer: {
|
|
141
|
-
const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
|
|
142
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
143
|
-
const address = await getLedgerAddress({ chain, ledgerClient: signer });
|
|
144
|
-
const toolbox = await getEvmToolbox(chain, { signer });
|
|
145
|
-
|
|
146
|
-
return { ...toolbox, address };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
case Chain.Cosmos: {
|
|
150
|
-
const { createSigningStargateClient, getMsgSendDenom, getCosmosToolbox } = await import(
|
|
151
|
-
"@swapkit/toolboxes/cosmos"
|
|
152
|
-
);
|
|
153
|
-
const toolbox = await getCosmosToolbox(Chain.Cosmos);
|
|
154
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
155
|
-
const address = await getLedgerAddress({ chain, ledgerClient: signer });
|
|
156
|
-
|
|
157
|
-
const transfer = async ({ assetValue, recipient, memo }: GenericTransferParams) => {
|
|
158
|
-
if (!assetValue) throw new SwapKitError("wallet_ledger_invalid_asset");
|
|
159
|
-
|
|
160
|
-
const sendCoinsMessage = {
|
|
161
|
-
amount: [
|
|
162
|
-
{
|
|
163
|
-
amount: assetValue.getBaseValue("string"),
|
|
164
|
-
denom: getMsgSendDenom(`u${assetValue.symbol}`).toLowerCase(),
|
|
165
|
-
},
|
|
166
|
-
],
|
|
167
|
-
fromAddress: address,
|
|
168
|
-
toAddress: recipient,
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
const rpcUrl = await getRPCUrl(chain);
|
|
172
|
-
const signingClient = await createSigningStargateClient(rpcUrl, signer, "0.007uatom");
|
|
173
|
-
|
|
174
|
-
const { transactionHash } = await signingClient.signAndBroadcast(
|
|
175
|
-
address,
|
|
176
|
-
[{ typeUrl: "/cosmos.bank.v1beta1.MsgSend", value: sendCoinsMessage }],
|
|
177
|
-
2,
|
|
178
|
-
memo,
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
return transactionHash;
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
return { ...toolbox, address, transfer };
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
case Chain.THORChain: {
|
|
188
|
-
const { SignMode } = await import("cosmjs-types/cosmos/tx/signing/v1beta1/signing.js");
|
|
189
|
-
const { TxRaw } = await import("cosmjs-types/cosmos/tx/v1beta1/tx.js");
|
|
190
|
-
const importedSigning = await import("@cosmjs/proto-signing");
|
|
191
|
-
const encodePubkey = importedSigning.encodePubkey ?? importedSigning.default?.encodePubkey;
|
|
192
|
-
const makeAuthInfoBytes = importedSigning.makeAuthInfoBytes ?? importedSigning.default?.makeAuthInfoBytes;
|
|
193
|
-
const {
|
|
194
|
-
createStargateClient,
|
|
195
|
-
buildEncodedTxBody,
|
|
196
|
-
getCosmosToolbox,
|
|
197
|
-
buildAminoMsg,
|
|
198
|
-
getDefaultChainFee,
|
|
199
|
-
fromBase64,
|
|
200
|
-
parseAminoMessageForDirectSigning,
|
|
201
|
-
} = await import("@swapkit/toolboxes/cosmos");
|
|
202
|
-
const toolbox = await getCosmosToolbox(chain);
|
|
203
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
204
|
-
const address = await getLedgerAddress({ chain, ledgerClient: signer });
|
|
205
|
-
|
|
206
|
-
const fee = getDefaultChainFee(chain);
|
|
207
|
-
const { pubkey: value, signTransaction, sign: signMessage } = signer;
|
|
208
|
-
|
|
209
|
-
// ANCHOR (@Chillios): Same parts in methods + can extract StargateClient init to toolbox
|
|
210
|
-
const thorchainTransfer = async ({
|
|
211
|
-
memo = "",
|
|
212
|
-
assetValue,
|
|
213
|
-
...rest
|
|
214
|
-
}: GenericTransferParams | ThorchainDepositParams) => {
|
|
215
|
-
const account = await toolbox.getAccount(address);
|
|
216
|
-
if (!account) throw new SwapKitError("wallet_ledger_invalid_account");
|
|
217
|
-
if (!assetValue) throw new SwapKitError("wallet_ledger_invalid_asset");
|
|
218
|
-
if (!value) throw new SwapKitError("wallet_ledger_pubkey_not_found");
|
|
219
|
-
|
|
220
|
-
const { accountNumber, sequence: sequenceNumber } = account;
|
|
221
|
-
const sequence = (sequenceNumber || 0).toString();
|
|
222
|
-
|
|
223
|
-
const orderedMessages = recursivelyOrderKeys([buildAminoMsg({ assetValue, memo, sender: address, ...rest })]);
|
|
224
|
-
|
|
225
|
-
// get tx signing msg
|
|
226
|
-
const rawSendTx = stringifyKeysInOrder({
|
|
227
|
-
account_number: accountNumber?.toString(),
|
|
228
|
-
chain_id: THORConfig.chainId,
|
|
229
|
-
fee,
|
|
230
|
-
memo,
|
|
231
|
-
msgs: orderedMessages,
|
|
232
|
-
sequence,
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
const signatures = await signTransaction(rawSendTx, sequence);
|
|
236
|
-
if (!signatures) throw new SwapKitError("wallet_ledger_signing_error");
|
|
237
|
-
|
|
238
|
-
const pubkey = encodePubkey({ type: "tendermint/PubKeySecp256k1", value });
|
|
239
|
-
const msgs = orderedMessages.map(parseAminoMessageForDirectSigning);
|
|
240
|
-
const bodyBytes = await buildEncodedTxBody({ chain, memo, msgs });
|
|
241
|
-
|
|
242
|
-
const authInfoBytes = makeAuthInfoBytes(
|
|
243
|
-
[{ pubkey, sequence: Number(sequence) }],
|
|
244
|
-
fee.amount,
|
|
245
|
-
Number.parseInt(fee.gas, 10),
|
|
246
|
-
undefined,
|
|
247
|
-
undefined,
|
|
248
|
-
SignMode.SIGN_MODE_LEGACY_AMINO_JSON,
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
const signature = signatures?.[0]?.signature ? fromBase64(signatures[0].signature) : Uint8Array.from([]);
|
|
252
|
-
|
|
253
|
-
const txRaw = TxRaw.fromPartial({ authInfoBytes, bodyBytes, signatures: [signature] });
|
|
254
|
-
const txBytes = TxRaw.encode(txRaw).finish();
|
|
255
|
-
const rpcUrl = await getRPCUrl(Chain.THORChain);
|
|
256
|
-
|
|
257
|
-
const broadcaster = await createStargateClient(rpcUrl);
|
|
258
|
-
const { transactionHash } = await broadcaster.broadcastTx(txBytes);
|
|
259
|
-
|
|
260
|
-
return transactionHash;
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const transfer = (params: GenericTransferParams) => thorchainTransfer(params);
|
|
264
|
-
const deposit = (params: ThorchainDepositParams) => thorchainTransfer(params);
|
|
265
|
-
|
|
266
|
-
return { ...toolbox, address, deposit, signMessage, transfer };
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
case Chain.Near: {
|
|
270
|
-
const { getNearToolbox } = await import("@swapkit/toolboxes/near");
|
|
271
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
272
|
-
const accountId = await signer.getAddress();
|
|
273
|
-
const toolbox = await getNearToolbox({ signer });
|
|
274
|
-
|
|
275
|
-
return { ...toolbox, address: accountId };
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
case Chain.Ripple: {
|
|
279
|
-
const { getRippleToolbox } = await import("@swapkit/toolboxes/ripple");
|
|
280
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
281
|
-
const address = signer.getAddress();
|
|
282
|
-
const toolbox = await getRippleToolbox({ signer });
|
|
283
|
-
|
|
284
|
-
return { ...toolbox, address };
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
case Chain.Tron: {
|
|
288
|
-
const { createTronToolbox } = await import("@swapkit/toolboxes/tron");
|
|
289
|
-
const signer = await getLedgerClient({ chain, derivationPath });
|
|
290
|
-
const address = await getLedgerAddress({ chain, ledgerClient: signer });
|
|
291
|
-
const toolbox = await createTronToolbox({ signer });
|
|
292
|
-
|
|
293
|
-
return { ...toolbox, address };
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
default:
|
|
297
|
-
throw new SwapKitError("wallet_ledger_chain_not_supported", { chain });
|
|
298
|
-
}
|
|
299
|
-
}
|