@injectivelabs/wallet-ledger 1.15.0 → 1.15.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 (47) hide show
  1. package/dist/cjs/index.d.ts +4 -0
  2. package/dist/cjs/index.js +24 -0
  3. package/dist/cjs/package.json +3 -0
  4. package/dist/cjs/strategy/Ledger/Base.d.ts +42 -0
  5. package/dist/cjs/strategy/Ledger/Base.js +245 -0
  6. package/dist/cjs/strategy/Ledger/LedgerLegacy.d.ts +5 -0
  7. package/dist/cjs/strategy/Ledger/LedgerLegacy.js +17 -0
  8. package/dist/cjs/strategy/Ledger/LedgerLive.d.ts +5 -0
  9. package/dist/cjs/strategy/Ledger/LedgerLive.js +17 -0
  10. package/dist/cjs/strategy/Ledger/hw/AccountManager.d.ts +21 -0
  11. package/dist/cjs/strategy/Ledger/hw/AccountManager.js +91 -0
  12. package/dist/cjs/strategy/Ledger/hw/index.d.ts +11 -0
  13. package/dist/cjs/strategy/Ledger/hw/index.js +65 -0
  14. package/dist/cjs/strategy/Ledger/utils.d.ts +2 -0
  15. package/dist/cjs/strategy/Ledger/utils.js +8 -0
  16. package/dist/cjs/strategy/LedgerCosmos/hw/AccountManager.d.ts +20 -0
  17. package/dist/cjs/strategy/LedgerCosmos/hw/AccountManager.js +69 -0
  18. package/dist/cjs/strategy/LedgerCosmos/hw/index.d.ts +11 -0
  19. package/dist/cjs/strategy/LedgerCosmos/hw/index.js +48 -0
  20. package/dist/cjs/strategy/LedgerCosmos/index.d.ts +36 -0
  21. package/dist/cjs/strategy/LedgerCosmos/index.js +159 -0
  22. package/dist/cjs/types.d.ts +17 -0
  23. package/dist/cjs/types.js +14 -0
  24. package/dist/esm/index.d.ts +4 -0
  25. package/dist/esm/index.js +4 -0
  26. package/dist/esm/package.json +3 -0
  27. package/dist/esm/strategy/Ledger/Base.d.ts +42 -0
  28. package/dist/esm/strategy/Ledger/Base.js +239 -0
  29. package/dist/esm/strategy/Ledger/LedgerLegacy.d.ts +5 -0
  30. package/dist/esm/strategy/Ledger/LedgerLegacy.js +10 -0
  31. package/dist/esm/strategy/Ledger/LedgerLive.d.ts +5 -0
  32. package/dist/esm/strategy/Ledger/LedgerLive.js +10 -0
  33. package/dist/esm/strategy/Ledger/hw/AccountManager.d.ts +21 -0
  34. package/dist/esm/strategy/Ledger/hw/AccountManager.js +85 -0
  35. package/dist/esm/strategy/Ledger/hw/index.d.ts +11 -0
  36. package/dist/esm/strategy/Ledger/hw/index.js +59 -0
  37. package/dist/esm/strategy/Ledger/utils.d.ts +2 -0
  38. package/dist/esm/strategy/Ledger/utils.js +3 -0
  39. package/dist/esm/strategy/LedgerCosmos/hw/AccountManager.d.ts +20 -0
  40. package/dist/esm/strategy/LedgerCosmos/hw/AccountManager.js +66 -0
  41. package/dist/esm/strategy/LedgerCosmos/hw/index.d.ts +11 -0
  42. package/dist/esm/strategy/LedgerCosmos/hw/index.js +42 -0
  43. package/dist/esm/strategy/LedgerCosmos/index.d.ts +36 -0
  44. package/dist/esm/strategy/LedgerCosmos/index.js +152 -0
  45. package/dist/esm/types.d.ts +17 -0
  46. package/dist/esm/types.js +11 -0
  47. package/package.json +6 -6
@@ -0,0 +1,11 @@
1
+ import { Cosmos as CosmosApp } from '@bangjelkoski/ledgerhq-hw-app-cosmos';
2
+ import { Transport } from '@bangjelkoski/ledgerhq-hw-transport';
3
+ import AccountManager from './AccountManager.js';
4
+ export default class LedgerTransport {
5
+ private ledger;
6
+ private accountManager;
7
+ protected static getTransport(): Promise<Transport>;
8
+ getInstance(): Promise<CosmosApp>;
9
+ getAccountManager(): Promise<AccountManager>;
10
+ refresh(): Promise<LedgerTransport>;
11
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ledgerhq_hw_transport_webusb_1 = require("@bangjelkoski/ledgerhq-hw-transport-webusb");
7
+ const ledgerhq_hw_transport_webhid_1 = require("@bangjelkoski/ledgerhq-hw-transport-webhid");
8
+ const ledgerhq_hw_app_cosmos_1 = require("@bangjelkoski/ledgerhq-hw-app-cosmos");
9
+ const exceptions_1 = require("@injectivelabs/exceptions");
10
+ const AccountManager_js_1 = __importDefault(require("./AccountManager.js"));
11
+ class LedgerTransport {
12
+ ledger = null;
13
+ accountManager = null;
14
+ static async getTransport() {
15
+ try {
16
+ if (await ledgerhq_hw_transport_webhid_1.TransportWebHID.isSupported()) {
17
+ return await ledgerhq_hw_transport_webhid_1.TransportWebHID.create();
18
+ }
19
+ if (await ledgerhq_hw_transport_webusb_1.TransportWebUSB.isSupported()) {
20
+ return await ledgerhq_hw_transport_webhid_1.TransportWebHID.create();
21
+ }
22
+ }
23
+ catch (e) {
24
+ throw new exceptions_1.LedgerCosmosException(new Error(e.message));
25
+ }
26
+ return await ledgerhq_hw_transport_webhid_1.TransportWebHID.create();
27
+ }
28
+ async getInstance() {
29
+ if (!this.ledger) {
30
+ this.ledger = new ledgerhq_hw_app_cosmos_1.Cosmos((await LedgerTransport.getTransport()));
31
+ }
32
+ return this.ledger;
33
+ }
34
+ async getAccountManager() {
35
+ if (!this.accountManager) {
36
+ this.accountManager = new AccountManager_js_1.default(await this.getInstance());
37
+ }
38
+ return this.accountManager;
39
+ }
40
+ async refresh() {
41
+ if (!this.ledger) {
42
+ return new LedgerTransport();
43
+ }
44
+ this.ledger.transport.close();
45
+ return new LedgerTransport();
46
+ }
47
+ }
48
+ exports.default = LedgerTransport;
@@ -0,0 +1,36 @@
1
+ import { ChainId, AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
2
+ import { TxRaw, TxResponse, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
3
+ import { StdSignDoc, WalletDeviceType, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions } from '@injectivelabs/wallet-base';
4
+ export declare class LedgerCosmos extends BaseConcreteStrategy implements ConcreteWalletStrategy {
5
+ private baseDerivationPath;
6
+ private ledger;
7
+ constructor(args: {
8
+ chainId: ChainId;
9
+ });
10
+ getWalletDeviceType(): Promise<WalletDeviceType>;
11
+ enable(): Promise<boolean>;
12
+ disconnect(): Promise<void>;
13
+ getAddresses(): Promise<string[]>;
14
+ getSessionOrConfirm(address: AccountAddress): Promise<string>;
15
+ sendEthereumTransaction(_txData: any, _options: {
16
+ address: string;
17
+ ethereumChainId: EthereumChainId;
18
+ }): Promise<string>;
19
+ sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
20
+ signAminoCosmosTransaction(transaction: {
21
+ address: string;
22
+ signDoc: StdSignDoc;
23
+ }): Promise<AminoSignResponse>;
24
+ signCosmosTransaction(_transaction: {
25
+ txRaw: TxRaw;
26
+ accountNumber: number;
27
+ chainId: string;
28
+ address: string;
29
+ }): Promise<DirectSignResponse>;
30
+ signEip712TypedData(_eip712Json: string, _address: AccountAddress): Promise<string>;
31
+ signArbitrary(signer: AccountAddress, data: string | Uint8Array): Promise<string>;
32
+ getEthereumChainId(): Promise<string>;
33
+ getEthereumTransactionReceipt(_txHash: string): Promise<string>;
34
+ getPubKey(address?: string): Promise<string>;
35
+ private getWalletForAddress;
36
+ }
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LedgerCosmos = void 0;
7
+ const exceptions_1 = require("@injectivelabs/exceptions");
8
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
9
+ const wallet_base_1 = require("@injectivelabs/wallet-base");
10
+ const index_js_1 = __importDefault(require("./hw/index.js"));
11
+ const exceptions_2 = require("@injectivelabs/exceptions");
12
+ class LedgerCosmos extends wallet_base_1.BaseConcreteStrategy {
13
+ baseDerivationPath;
14
+ ledger;
15
+ constructor(args) {
16
+ super(args);
17
+ this.baseDerivationPath = wallet_base_1.DEFAULT_BASE_DERIVATION_PATH;
18
+ this.ledger = new index_js_1.default();
19
+ }
20
+ async getWalletDeviceType() {
21
+ return Promise.resolve(wallet_base_1.WalletDeviceType.Hardware);
22
+ }
23
+ async enable() {
24
+ return Promise.resolve(true);
25
+ }
26
+ async disconnect() {
27
+ this.ledger = await this.ledger.refresh();
28
+ }
29
+ async getAddresses() {
30
+ const { baseDerivationPath } = this;
31
+ try {
32
+ const accountManager = await this.ledger.getAccountManager();
33
+ const wallets = await accountManager.getWallets(baseDerivationPath);
34
+ return wallets.map((k) => k.address);
35
+ }
36
+ catch (e) {
37
+ throw new exceptions_1.LedgerCosmosException(new Error(e.message), {
38
+ code: exceptions_1.UnspecifiedErrorCode,
39
+ type: exceptions_1.ErrorType.WalletError,
40
+ contextModule: wallet_base_1.WalletAction.GetAccounts,
41
+ });
42
+ }
43
+ }
44
+ async getSessionOrConfirm(address) {
45
+ return Promise.resolve(`0x${Buffer.from(`Confirmation for ${address} at time: ${Date.now()}`).toString('hex')}`);
46
+ }
47
+ async sendEthereumTransaction(_txData, _options) {
48
+ throw new exceptions_2.CosmosWalletException(new Error('sendEthereumTransaction is not supported. LedgerCosmos only supports sending cosmos transactions'), {
49
+ code: exceptions_1.UnspecifiedErrorCode,
50
+ context: wallet_base_1.WalletAction.SendEthereumTransaction,
51
+ });
52
+ }
53
+ async sendTransaction(transaction, options) {
54
+ const { endpoints, txTimeout } = options;
55
+ if (!endpoints) {
56
+ throw new exceptions_1.WalletException(new Error('You have to pass endpoints.grpc within the options for using LedgerCosmos wallet'));
57
+ }
58
+ const txApi = new sdk_ts_1.TxGrpcApi(endpoints.grpc);
59
+ const response = await txApi.broadcast(transaction, { txTimeout });
60
+ if (response.code !== 0) {
61
+ throw new exceptions_1.TransactionException(new Error(response.rawLog), {
62
+ code: exceptions_1.UnspecifiedErrorCode,
63
+ contextCode: response.code,
64
+ contextModule: response.codespace,
65
+ });
66
+ }
67
+ return response;
68
+ }
69
+ async signAminoCosmosTransaction(transaction) {
70
+ try {
71
+ const { derivationPath } = await this.getWalletForAddress(transaction.address);
72
+ const ledger = await this.ledger.getInstance();
73
+ const result = await ledger.sign(derivationPath, JSON.stringify((0, sdk_ts_1.sortObjectByKeys)(transaction.signDoc)));
74
+ return {
75
+ signed: undefined,
76
+ signature: {
77
+ signature: Buffer.from(result.signature).toString('base64'),
78
+ pub_key: undefined,
79
+ },
80
+ };
81
+ }
82
+ catch (e) {
83
+ throw new exceptions_1.LedgerCosmosException(new Error(e.message), {
84
+ code: exceptions_1.UnspecifiedErrorCode,
85
+ type: exceptions_1.ErrorType.WalletError,
86
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
87
+ });
88
+ }
89
+ }
90
+ async signCosmosTransaction(_transaction) {
91
+ throw new exceptions_2.CosmosWalletException(new Error('This wallet does not support signing using direct sign'), {
92
+ code: exceptions_1.UnspecifiedErrorCode,
93
+ context: wallet_base_1.WalletAction.SendTransaction,
94
+ });
95
+ }
96
+ async signEip712TypedData(_eip712Json, _address) {
97
+ throw new exceptions_2.CosmosWalletException(new Error('This wallet does not support signing Ethereum transactions'), {
98
+ code: exceptions_1.UnspecifiedErrorCode,
99
+ context: wallet_base_1.WalletAction.SendTransaction,
100
+ });
101
+ }
102
+ async signArbitrary(signer, data) {
103
+ try {
104
+ const { derivationPath } = await this.getWalletForAddress(signer);
105
+ const ledger = await this.ledger.getInstance();
106
+ const result = await ledger.sign(derivationPath, (0, sdk_ts_1.toUtf8)(data));
107
+ return Buffer.from(result.signature).toString('base64');
108
+ }
109
+ catch (e) {
110
+ throw new exceptions_1.LedgerCosmosException(new Error(e.message), {
111
+ code: exceptions_1.UnspecifiedErrorCode,
112
+ type: exceptions_1.ErrorType.WalletError,
113
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
114
+ });
115
+ }
116
+ }
117
+ async getEthereumChainId() {
118
+ throw new exceptions_2.CosmosWalletException(new Error('getEthereumChainId is not supported on Keplr'), {
119
+ code: exceptions_1.UnspecifiedErrorCode,
120
+ context: wallet_base_1.WalletAction.GetChainId,
121
+ });
122
+ }
123
+ async getEthereumTransactionReceipt(_txHash) {
124
+ throw new exceptions_2.CosmosWalletException(new Error('getEthereumTransactionReceipt is not supported on Keplr'), {
125
+ code: exceptions_1.UnspecifiedErrorCode,
126
+ context: wallet_base_1.WalletAction.GetEthereumTransactionReceipt,
127
+ });
128
+ }
129
+ async getPubKey(address) {
130
+ if (!address) {
131
+ throw new exceptions_1.WalletException(new Error('You can only fetch PubKey corresponding to an address'));
132
+ }
133
+ const ledgerWalletInfo = await this.getWalletForAddress(address);
134
+ return Buffer.from(ledgerWalletInfo.publicKey || '', 'hex').toString('base64');
135
+ }
136
+ async getWalletForAddress(address) {
137
+ try {
138
+ const { baseDerivationPath } = this;
139
+ const accountManager = await this.ledger.getAccountManager();
140
+ if (!accountManager.hasWalletForAddress(address)) {
141
+ for (let i = 0; i < wallet_base_1.DEFAULT_ADDRESS_SEARCH_LIMIT / wallet_base_1.DEFAULT_NUM_ADDRESSES_TO_FETCH; i += 1) {
142
+ await accountManager.getWallets(baseDerivationPath);
143
+ if (accountManager.hasWalletForAddress(address)) {
144
+ return (await accountManager.getWalletForAddress(address));
145
+ }
146
+ }
147
+ }
148
+ return (await accountManager.getWalletForAddress(address));
149
+ }
150
+ catch (e) {
151
+ throw new exceptions_1.LedgerCosmosException(new Error(e.message), {
152
+ code: exceptions_1.UnspecifiedErrorCode,
153
+ type: exceptions_1.ErrorType.WalletError,
154
+ contextModule: wallet_base_1.WalletAction.GetAccounts,
155
+ });
156
+ }
157
+ }
158
+ }
159
+ exports.LedgerCosmos = LedgerCosmos;
@@ -0,0 +1,17 @@
1
+ import type HDNode from 'hdkey';
2
+ export declare enum LedgerDerivationPathType {
3
+ LedgerLive = "ledger-live",
4
+ LedgerMew = "ledger-mew"
5
+ }
6
+ export interface LedgerWalletInfo {
7
+ address: string;
8
+ baseDerivationPath: string;
9
+ derivationPath: string;
10
+ hdKey?: HDNode;
11
+ publicKey?: string;
12
+ }
13
+ export declare enum WalletLedger {
14
+ Ledger = "ledger",
15
+ LedgerCosmos = "ledger-cosmos",
16
+ LedgerLegacy = "ledger-legacy"
17
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WalletLedger = exports.LedgerDerivationPathType = void 0;
4
+ var LedgerDerivationPathType;
5
+ (function (LedgerDerivationPathType) {
6
+ LedgerDerivationPathType["LedgerLive"] = "ledger-live";
7
+ LedgerDerivationPathType["LedgerMew"] = "ledger-mew";
8
+ })(LedgerDerivationPathType || (exports.LedgerDerivationPathType = LedgerDerivationPathType = {}));
9
+ var WalletLedger;
10
+ (function (WalletLedger) {
11
+ WalletLedger["Ledger"] = "ledger";
12
+ WalletLedger["LedgerCosmos"] = "ledger-cosmos";
13
+ WalletLedger["LedgerLegacy"] = "ledger-legacy";
14
+ })(WalletLedger || (exports.WalletLedger = WalletLedger = {}));
@@ -0,0 +1,4 @@
1
+ export { LedgerLive as LedgerLiveStrategy } from './strategy/Ledger/LedgerLive.js';
2
+ export { LedgerLegacy as LedgerLegacyStrategy } from './strategy/Ledger/LedgerLegacy.js';
3
+ export { LedgerCosmos as LedgerCosmosStrategy } from './strategy/LedgerCosmos/index.js';
4
+ export * from './types.js';
@@ -0,0 +1,4 @@
1
+ export { LedgerLive as LedgerLiveStrategy } from './strategy/Ledger/LedgerLive.js';
2
+ export { LedgerLegacy as LedgerLegacyStrategy } from './strategy/Ledger/LedgerLegacy.js';
3
+ export { LedgerCosmos as LedgerCosmosStrategy } from './strategy/LedgerCosmos/index.js';
4
+ export * from './types.js';
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,42 @@
1
+ import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
2
+ import { TxRaw, TxResponse, DirectSignResponse, AminoSignResponse } from '@injectivelabs/sdk-ts';
3
+ import { StdSignDoc, WalletDeviceType, BaseConcreteStrategy, SendTransactionOptions, ConcreteWalletStrategy, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
4
+ import { LedgerDerivationPathType } from '../../types.js';
5
+ export default class LedgerBase extends BaseConcreteStrategy implements ConcreteWalletStrategy {
6
+ private baseDerivationPath;
7
+ private derivationPathType;
8
+ private ledger;
9
+ private ethereumOptions;
10
+ private alchemy;
11
+ constructor(args: ConcreteEthereumWalletStrategyArgs & {
12
+ derivationPathType: LedgerDerivationPathType;
13
+ });
14
+ getWalletDeviceType(): Promise<WalletDeviceType>;
15
+ enable(): Promise<boolean>;
16
+ disconnect(): Promise<void>;
17
+ getAddresses(): Promise<string[]>;
18
+ getSessionOrConfirm(address: AccountAddress): Promise<string>;
19
+ sendEthereumTransaction(txData: any, options: {
20
+ address: string;
21
+ ethereumChainId: EthereumChainId;
22
+ }): Promise<string>;
23
+ sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
24
+ signEip712TypedData(eip712json: string, address: AccountAddress): Promise<string>;
25
+ signAminoCosmosTransaction(_transaction: {
26
+ address: string;
27
+ signDoc: StdSignDoc;
28
+ }): Promise<AminoSignResponse>;
29
+ signCosmosTransaction(_transaction: {
30
+ txRaw: TxRaw;
31
+ accountNumber: number;
32
+ chainId: string;
33
+ address: string;
34
+ }): Promise<DirectSignResponse>;
35
+ signArbitrary(signer: AccountAddress, data: string | Uint8Array): Promise<string>;
36
+ getEthereumChainId(): Promise<string>;
37
+ getEthereumTransactionReceipt(txHash: string): Promise<string>;
38
+ getPubKey(): Promise<string>;
39
+ private signEthereumTransaction;
40
+ private getWalletForAddress;
41
+ private getAlchemy;
42
+ }
@@ -0,0 +1,239 @@
1
+ /* eslint-disable class-methods-use-this */
2
+ import { EthereumChainId } from '@injectivelabs/ts-types';
3
+ import { bufferToHex, addHexPrefix } from 'ethereumjs-util';
4
+ import { Common, Chain, Hardfork } from '@ethereumjs/common';
5
+ import { FeeMarketEIP1559Transaction } from '@ethereumjs/tx';
6
+ import { ledgerService } from '@bangjelkoski/ledgerhq-hw-app-eth';
7
+ import { ErrorType, GeneralException, LedgerException, TransactionException, UnspecifiedErrorCode, WalletException, } from '@injectivelabs/exceptions';
8
+ import { toUtf8, TxGrpcApi, } from '@injectivelabs/sdk-ts';
9
+ import { TIP_IN_GWEI, WalletAction, WalletDeviceType, getKeyFromRpcUrl, BaseConcreteStrategy, DEFAULT_BASE_DERIVATION_PATH, DEFAULT_ADDRESS_SEARCH_LIMIT, DEFAULT_NUM_ADDRESSES_TO_FETCH, } from '@injectivelabs/wallet-base';
10
+ import LedgerHW from './hw/index.js';
11
+ import { domainHash, messageHash } from './utils.js';
12
+ import { Alchemy, Network as AlchemyNetwork } from 'alchemy-sdk';
13
+ const getNetworkFromChainId = (chainId) => {
14
+ if (chainId === EthereumChainId.Goerli) {
15
+ return Chain.Goerli;
16
+ }
17
+ if (chainId === EthereumChainId.Sepolia) {
18
+ return Chain.Sepolia;
19
+ }
20
+ if (chainId === EthereumChainId.Kovan) {
21
+ return Chain.Goerli;
22
+ }
23
+ return Chain.Mainnet;
24
+ };
25
+ export default class LedgerBase extends BaseConcreteStrategy {
26
+ baseDerivationPath;
27
+ derivationPathType;
28
+ ledger;
29
+ ethereumOptions;
30
+ alchemy;
31
+ constructor(args) {
32
+ super(args);
33
+ this.baseDerivationPath = DEFAULT_BASE_DERIVATION_PATH;
34
+ this.derivationPathType = args.derivationPathType;
35
+ this.ledger = new LedgerHW();
36
+ this.ethereumOptions = args.ethereumOptions;
37
+ }
38
+ async getWalletDeviceType() {
39
+ return Promise.resolve(WalletDeviceType.Hardware);
40
+ }
41
+ async enable() {
42
+ return Promise.resolve(true);
43
+ }
44
+ async disconnect() {
45
+ this.ledger = await this.ledger.refresh();
46
+ }
47
+ async getAddresses() {
48
+ const { baseDerivationPath, derivationPathType } = this;
49
+ try {
50
+ const accountManager = await this.ledger.getAccountManager();
51
+ const wallets = await accountManager.getWallets(baseDerivationPath, derivationPathType);
52
+ return wallets.map((k) => k.address);
53
+ }
54
+ catch (e) {
55
+ throw new LedgerException(new Error(e.message), {
56
+ code: UnspecifiedErrorCode,
57
+ type: ErrorType.WalletError,
58
+ contextModule: WalletAction.GetAccounts,
59
+ });
60
+ }
61
+ }
62
+ async getSessionOrConfirm(address) {
63
+ return Promise.resolve(`0x${Buffer.from(`Confirmation for ${address} at time: ${Date.now()}`).toString('hex')}`);
64
+ }
65
+ async sendEthereumTransaction(txData, options) {
66
+ const signedTransaction = await this.signEthereumTransaction(txData, options);
67
+ try {
68
+ const alchemy = await this.getAlchemy();
69
+ const txReceipt = await alchemy.core.sendTransaction(addHexPrefix(signedTransaction.serialize().toString('hex')));
70
+ return txReceipt.hash;
71
+ }
72
+ catch (e) {
73
+ throw new LedgerException(new Error(e.message), {
74
+ code: UnspecifiedErrorCode,
75
+ type: ErrorType.WalletError,
76
+ contextModule: WalletAction.SendEthereumTransaction,
77
+ });
78
+ }
79
+ }
80
+ async sendTransaction(transaction, options) {
81
+ const { endpoints, txTimeout } = options;
82
+ if (!endpoints) {
83
+ throw new WalletException(new Error('You have to pass endpoints.grpc within the options for using Ethereum native wallets'));
84
+ }
85
+ const txApi = new TxGrpcApi(endpoints.grpc);
86
+ const response = await txApi.broadcast(transaction, { txTimeout });
87
+ if (response.code !== 0) {
88
+ throw new TransactionException(new Error(response.rawLog), {
89
+ code: UnspecifiedErrorCode,
90
+ contextCode: response.code,
91
+ contextModule: response.codespace,
92
+ });
93
+ }
94
+ return response;
95
+ }
96
+ async signEip712TypedData(eip712json, address) {
97
+ const { derivationPath } = await this.getWalletForAddress(address);
98
+ const object = JSON.parse(eip712json);
99
+ try {
100
+ const ledger = await this.ledger.getInstance();
101
+ const result = await ledger.signEIP712HashedMessage(derivationPath, bufferToHex(domainHash(object)), bufferToHex(messageHash(object)));
102
+ const combined = `${result.r}${result.s}${result.v.toString(16)}`;
103
+ return combined.startsWith('0x') ? combined : `0x${combined}`;
104
+ }
105
+ catch (e) {
106
+ throw new LedgerException(new Error(e.message), {
107
+ code: UnspecifiedErrorCode,
108
+ type: ErrorType.WalletError,
109
+ contextModule: WalletAction.SignTransaction,
110
+ });
111
+ }
112
+ }
113
+ async signAminoCosmosTransaction(_transaction) {
114
+ throw new WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
115
+ code: UnspecifiedErrorCode,
116
+ type: ErrorType.WalletError,
117
+ contextModule: WalletAction.SendTransaction,
118
+ });
119
+ }
120
+ // eslint-disable-next-line class-methods-use-this
121
+ async signCosmosTransaction(_transaction) {
122
+ throw new WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
123
+ code: UnspecifiedErrorCode,
124
+ type: ErrorType.WalletError,
125
+ contextModule: WalletAction.SendTransaction,
126
+ });
127
+ }
128
+ async signArbitrary(signer, data) {
129
+ try {
130
+ const { derivationPath } = await this.getWalletForAddress(signer);
131
+ const ledger = await this.ledger.getInstance();
132
+ const result = await ledger.signPersonalMessage(derivationPath, Buffer.from(toUtf8(data), 'utf8').toString('hex'));
133
+ const combined = `${result.r}${result.s}${result.v.toString(16)}`;
134
+ return combined.startsWith('0x') ? combined : `0x${combined}`;
135
+ }
136
+ catch (e) {
137
+ throw new LedgerException(new Error(e.message), {
138
+ code: UnspecifiedErrorCode,
139
+ type: ErrorType.WalletError,
140
+ contextModule: WalletAction.SignTransaction,
141
+ });
142
+ }
143
+ }
144
+ async getEthereumChainId() {
145
+ const alchemy = await this.getAlchemy();
146
+ const alchemyProvider = await alchemy.config.getProvider();
147
+ return alchemyProvider.network.chainId.toString();
148
+ }
149
+ async getEthereumTransactionReceipt(txHash) {
150
+ return Promise.resolve(txHash);
151
+ }
152
+ // eslint-disable-next-line class-methods-use-this
153
+ async getPubKey() {
154
+ throw new WalletException(new Error('You can only fetch PubKey from Cosmos native wallets'));
155
+ }
156
+ async signEthereumTransaction(txData, options) {
157
+ const alchemy = await this.getAlchemy();
158
+ const chainId = parseInt(options.ethereumChainId.toString(), 10);
159
+ const nonce = await alchemy.core.getTransactionCount(options.address);
160
+ const common = new Common({
161
+ chain: getNetworkFromChainId(chainId),
162
+ hardfork: Hardfork.London,
163
+ });
164
+ const eip1559TxData = {
165
+ from: txData.from,
166
+ data: txData.data,
167
+ to: txData.to,
168
+ nonce: addHexPrefix(nonce.toString(16)),
169
+ gas: addHexPrefix(txData.gas),
170
+ gasLimit: addHexPrefix(txData.gas),
171
+ maxFeePerGas: addHexPrefix(txData.gasPrice || txData.maxFeePerGas),
172
+ maxPriorityFeePerGas: addHexPrefix(txData.maxPriorityFeePerGas || TIP_IN_GWEI.toString(16)),
173
+ };
174
+ const tx = FeeMarketEIP1559Transaction.fromTxData(eip1559TxData, { common });
175
+ const msg = tx.getMessageToSign(false);
176
+ // const encodedMessage = msg
177
+ const encodedMessageHex = msg.toString('hex');
178
+ try {
179
+ const ledger = await this.ledger.getInstance();
180
+ const { derivationPath } = await this.getWalletForAddress(options.address);
181
+ const resolution = await ledgerService.resolveTransaction(encodedMessageHex, {}, {});
182
+ const txSig = await ledger.signTransaction(derivationPath, encodedMessageHex, resolution);
183
+ const signedTxData = {
184
+ ...eip1559TxData,
185
+ v: `0x${txSig.v}`,
186
+ r: `0x${txSig.r}`,
187
+ s: `0x${txSig.s}`,
188
+ };
189
+ return FeeMarketEIP1559Transaction.fromTxData(signedTxData, {
190
+ common,
191
+ });
192
+ }
193
+ catch (e) {
194
+ throw new LedgerException(new Error(e.message), {
195
+ code: UnspecifiedErrorCode,
196
+ type: ErrorType.WalletError,
197
+ contextModule: WalletAction.SignEthereumTransaction,
198
+ });
199
+ }
200
+ }
201
+ async getWalletForAddress(address) {
202
+ try {
203
+ const { baseDerivationPath, derivationPathType } = this;
204
+ const accountManager = await this.ledger.getAccountManager();
205
+ if (!accountManager.hasWalletForAddress(address)) {
206
+ for (let i = 0; i < DEFAULT_ADDRESS_SEARCH_LIMIT / DEFAULT_NUM_ADDRESSES_TO_FETCH; i += 1) {
207
+ await accountManager.getWallets(baseDerivationPath, derivationPathType);
208
+ if (accountManager.hasWalletForAddress(address)) {
209
+ return (await accountManager.getWalletForAddress(address));
210
+ }
211
+ }
212
+ }
213
+ return (await accountManager.getWalletForAddress(address));
214
+ }
215
+ catch (e) {
216
+ throw new LedgerException(new Error(e.message), {
217
+ code: UnspecifiedErrorCode,
218
+ type: ErrorType.WalletError,
219
+ contextModule: WalletAction.GetAccounts,
220
+ });
221
+ }
222
+ }
223
+ async getAlchemy() {
224
+ if (this.alchemy) {
225
+ return this.alchemy;
226
+ }
227
+ const { rpcUrl, ethereumChainId } = this.ethereumOptions;
228
+ if (!rpcUrl) {
229
+ throw new GeneralException(new Error('Please pass rpcUrl within the ethereumOptions'));
230
+ }
231
+ this.alchemy = new Alchemy({
232
+ apiKey: getKeyFromRpcUrl(rpcUrl),
233
+ network: ethereumChainId === EthereumChainId.Mainnet
234
+ ? AlchemyNetwork.ETH_MAINNET
235
+ : AlchemyNetwork.ETH_SEPOLIA,
236
+ });
237
+ return this.alchemy;
238
+ }
239
+ }
@@ -0,0 +1,5 @@
1
+ import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import LedgerBase from './Base.js';
3
+ export declare class LedgerLegacy extends LedgerBase {
4
+ constructor(args: ConcreteEthereumWalletStrategyArgs);
5
+ }
@@ -0,0 +1,10 @@
1
+ import { LedgerDerivationPathType } from '../../types.js';
2
+ import LedgerBase from './Base.js';
3
+ export class LedgerLegacy extends LedgerBase {
4
+ constructor(args) {
5
+ super({
6
+ ...args,
7
+ derivationPathType: LedgerDerivationPathType.LedgerMew,
8
+ });
9
+ }
10
+ }
@@ -0,0 +1,5 @@
1
+ import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import LedgerBase from './Base.js';
3
+ export declare class LedgerLive extends LedgerBase {
4
+ constructor(args: ConcreteEthereumWalletStrategyArgs);
5
+ }
@@ -0,0 +1,10 @@
1
+ import { LedgerDerivationPathType } from '../../types.js';
2
+ import LedgerBase from './Base.js';
3
+ export class LedgerLive extends LedgerBase {
4
+ constructor(args) {
5
+ super({
6
+ ...args,
7
+ derivationPathType: LedgerDerivationPathType.LedgerLive,
8
+ });
9
+ }
10
+ }
@@ -0,0 +1,21 @@
1
+ import { AccountAddress } from '@injectivelabs/ts-types';
2
+ import { Eth as EthereumApp } from '@bangjelkoski/ledgerhq-hw-app-eth';
3
+ import { LedgerDerivationPathType, LedgerWalletInfo } from '../../../types.js';
4
+ export default class AccountManager {
5
+ private wallets;
6
+ private ledger;
7
+ constructor(ledger: EthereumApp);
8
+ getWallets(baseDerivationPath: string, derivationPathType: LedgerDerivationPathType): Promise<LedgerWalletInfo[]>;
9
+ getLedgerDerivationPathBasedOnType: ({ fullBaseDerivationPath, derivationPathType, index, }: {
10
+ fullBaseDerivationPath: string;
11
+ derivationPathType: LedgerDerivationPathType;
12
+ index: number;
13
+ }) => string;
14
+ private getWalletsBasedOnIndex;
15
+ private hasWallets;
16
+ private hasWalletsInOffset;
17
+ private getOffset;
18
+ hasWalletForAddress(address: AccountAddress): boolean;
19
+ getWalletForAddress(address: AccountAddress): Promise<LedgerWalletInfo | undefined>;
20
+ reset(): void;
21
+ }