@injectivelabs/wallet-trezor 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 +3 -0
  2. package/dist/cjs/index.js +22 -0
  3. package/dist/cjs/package.json +3 -0
  4. package/dist/cjs/strategy/Base.d.ts +42 -0
  5. package/dist/cjs/strategy/Base.js +280 -0
  6. package/dist/cjs/strategy/TrezorBip32.d.ts +5 -0
  7. package/dist/cjs/strategy/TrezorBip32.js +17 -0
  8. package/dist/cjs/strategy/TrezorBip44.d.ts +5 -0
  9. package/dist/cjs/strategy/TrezorBip44.js +17 -0
  10. package/dist/cjs/strategy/hw/AccountManager.d.ts +19 -0
  11. package/dist/cjs/strategy/hw/AccountManager.js +106 -0
  12. package/dist/cjs/strategy/hw/index.d.ts +4 -0
  13. package/dist/cjs/strategy/hw/index.js +12 -0
  14. package/dist/cjs/strategy/hw/transport/base.d.ts +6 -0
  15. package/dist/cjs/strategy/hw/transport/base.js +19 -0
  16. package/dist/cjs/strategy/hw/transport/transport-init.d.ts +4 -0
  17. package/dist/cjs/strategy/hw/transport/transport-init.js +27 -0
  18. package/dist/cjs/strategy/hw/transport/transport-no-init.d.ts +4 -0
  19. package/dist/cjs/strategy/hw/transport/transport-no-init.js +12 -0
  20. package/dist/cjs/types.d.ts +11 -0
  21. package/dist/cjs/types.js +9 -0
  22. package/dist/cjs/utils.d.ts +16 -0
  23. package/dist/cjs/utils.js +34 -0
  24. package/dist/esm/index.d.ts +3 -0
  25. package/dist/esm/index.js +3 -0
  26. package/dist/esm/package.json +3 -0
  27. package/dist/esm/strategy/Base.d.ts +42 -0
  28. package/dist/esm/strategy/Base.js +277 -0
  29. package/dist/esm/strategy/TrezorBip32.d.ts +5 -0
  30. package/dist/esm/strategy/TrezorBip32.js +10 -0
  31. package/dist/esm/strategy/TrezorBip44.d.ts +5 -0
  32. package/dist/esm/strategy/TrezorBip44.js +10 -0
  33. package/dist/esm/strategy/hw/AccountManager.d.ts +19 -0
  34. package/dist/esm/strategy/hw/AccountManager.js +100 -0
  35. package/dist/esm/strategy/hw/index.d.ts +4 -0
  36. package/dist/esm/strategy/hw/index.js +4 -0
  37. package/dist/esm/strategy/hw/transport/base.d.ts +6 -0
  38. package/dist/esm/strategy/hw/transport/base.js +13 -0
  39. package/dist/esm/strategy/hw/transport/transport-init.d.ts +4 -0
  40. package/dist/esm/strategy/hw/transport/transport-init.js +21 -0
  41. package/dist/esm/strategy/hw/transport/transport-no-init.d.ts +4 -0
  42. package/dist/esm/strategy/hw/transport/transport-no-init.js +6 -0
  43. package/dist/esm/types.d.ts +11 -0
  44. package/dist/esm/types.js +6 -0
  45. package/dist/esm/utils.d.ts +16 -0
  46. package/dist/esm/utils.js +30 -0
  47. package/package.json +6 -6
@@ -0,0 +1,3 @@
1
+ export { TrezorBip32 as TrezorBip32Strategy } from './strategy/TrezorBip32.js';
2
+ export { TrezorBip44 as TrezorBip44Strategy } from './strategy/TrezorBip44.js';
3
+ export * from './types.js';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.TrezorBip44Strategy = exports.TrezorBip32Strategy = void 0;
18
+ var TrezorBip32_js_1 = require("./strategy/TrezorBip32.js");
19
+ Object.defineProperty(exports, "TrezorBip32Strategy", { enumerable: true, get: function () { return TrezorBip32_js_1.TrezorBip32; } });
20
+ var TrezorBip44_js_1 = require("./strategy/TrezorBip44.js");
21
+ Object.defineProperty(exports, "TrezorBip44Strategy", { enumerable: true, get: function () { return TrezorBip44_js_1.TrezorBip44; } });
22
+ __exportStar(require("./types.js"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
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, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
4
+ import { TrezorDerivationPathType } from '../types.js';
5
+ export default class TrezorBase extends BaseConcreteStrategy implements ConcreteWalletStrategy {
6
+ private baseDerivationPath;
7
+ private trezor;
8
+ private ethereumOptions;
9
+ private alchemy;
10
+ private derivationPathType;
11
+ constructor(args: ConcreteEthereumWalletStrategyArgs & {
12
+ derivationPathType: TrezorDerivationPathType;
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,280 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable camelcase */
4
+ /* eslint-disable class-methods-use-this */
5
+ const ts_types_1 = require("@injectivelabs/ts-types");
6
+ const alchemy_sdk_1 = require("alchemy-sdk");
7
+ const ethereumjs_util_1 = require("ethereumjs-util");
8
+ const tx_1 = require("@ethereumjs/tx");
9
+ const common_1 = require("@ethereumjs/common");
10
+ const trezor_connect_web_1 = require("@bangjelkoski/trezor-connect-web");
11
+ const exceptions_1 = require("@injectivelabs/exceptions");
12
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
13
+ const wallet_base_1 = require("@injectivelabs/wallet-base");
14
+ const index_js_1 = require("./hw/index.js");
15
+ const utils_js_1 = require("../utils.js");
16
+ const getNetworkFromChainId = (chainId) => {
17
+ if (chainId === ts_types_1.EthereumChainId.Goerli) {
18
+ return common_1.Chain.Goerli;
19
+ }
20
+ if (chainId === ts_types_1.EthereumChainId.Sepolia) {
21
+ return common_1.Chain.Sepolia;
22
+ }
23
+ if (chainId === ts_types_1.EthereumChainId.Kovan) {
24
+ return common_1.Chain.Goerli;
25
+ }
26
+ return common_1.Chain.Mainnet;
27
+ };
28
+ class TrezorBase extends wallet_base_1.BaseConcreteStrategy {
29
+ baseDerivationPath;
30
+ trezor;
31
+ ethereumOptions;
32
+ alchemy;
33
+ derivationPathType;
34
+ constructor(args) {
35
+ super(args);
36
+ this.baseDerivationPath = wallet_base_1.DEFAULT_BASE_DERIVATION_PATH;
37
+ this.derivationPathType = args.derivationPathType;
38
+ this.trezor = new index_js_1.TrezorTransportInit();
39
+ this.ethereumOptions = args.ethereumOptions;
40
+ }
41
+ async getWalletDeviceType() {
42
+ return Promise.resolve(wallet_base_1.WalletDeviceType.Hardware);
43
+ }
44
+ async enable() {
45
+ return Promise.resolve(true);
46
+ }
47
+ async disconnect() {
48
+ return Promise.resolve();
49
+ }
50
+ async getAddresses() {
51
+ const { baseDerivationPath, derivationPathType } = this;
52
+ try {
53
+ await this.trezor.connect();
54
+ const accountManager = await this.trezor.getAccountManager();
55
+ const wallets = await accountManager.getWallets(baseDerivationPath, derivationPathType);
56
+ return wallets.map((k) => k.address);
57
+ }
58
+ catch (e) {
59
+ throw new exceptions_1.TrezorException(new Error(e.message), {
60
+ code: exceptions_1.UnspecifiedErrorCode,
61
+ type: exceptions_1.ErrorType.WalletError,
62
+ contextModule: wallet_base_1.WalletAction.GetAccounts,
63
+ });
64
+ }
65
+ }
66
+ async getSessionOrConfirm(address) {
67
+ return Promise.resolve(`0x${Buffer.from(`Confirmation for ${address} at time: ${Date.now()}`).toString('hex')}`);
68
+ }
69
+ async sendEthereumTransaction(txData, options) {
70
+ const signedTransaction = await this.signEthereumTransaction(txData, options);
71
+ try {
72
+ const alchemy = await this.getAlchemy();
73
+ const txReceipt = await alchemy.core.sendTransaction((0, ethereumjs_util_1.addHexPrefix)(signedTransaction.serialize().toString('hex')));
74
+ return txReceipt.hash;
75
+ }
76
+ catch (e) {
77
+ throw new exceptions_1.TrezorException(new Error(e.message), {
78
+ code: exceptions_1.UnspecifiedErrorCode,
79
+ type: exceptions_1.ErrorType.WalletError,
80
+ contextModule: wallet_base_1.WalletAction.SendEthereumTransaction,
81
+ });
82
+ }
83
+ }
84
+ async sendTransaction(transaction, options) {
85
+ const { endpoints, txTimeout } = options;
86
+ if (!endpoints) {
87
+ throw new exceptions_1.WalletException(new Error('You have to pass endpoints.grpc within the options for using Ethereum native wallets'));
88
+ }
89
+ const txApi = new sdk_ts_1.TxGrpcApi(endpoints.grpc);
90
+ const response = await txApi.broadcast(transaction, { txTimeout });
91
+ if (response.code !== 0) {
92
+ throw new exceptions_1.TransactionException(new Error(response.rawLog), {
93
+ code: exceptions_1.UnspecifiedErrorCode,
94
+ contextCode: response.code,
95
+ contextModule: response.codespace,
96
+ });
97
+ }
98
+ return response;
99
+ }
100
+ async signEip712TypedData(eip712json, address) {
101
+ const object = JSON.parse(eip712json);
102
+ const compatibleObject = {
103
+ ...object,
104
+ domain: {
105
+ ...object.domain,
106
+ chainId: object.domain.chainId,
107
+ salt: '0',
108
+ },
109
+ };
110
+ const dataWithHashes = (0, utils_js_1.transformTypedData)(compatibleObject);
111
+ const { types: { EIP712Domain = [], ...otherTypes } = {}, message = {}, domain = {}, primaryType, domain_separator_hash, message_hash, } = dataWithHashes;
112
+ try {
113
+ await this.trezor.connect();
114
+ const { derivationPath } = await this.getWalletForAddress(address);
115
+ const response = await trezor_connect_web_1.TrezorConnect.ethereumSignTypedData({
116
+ path: derivationPath,
117
+ data: {
118
+ types: { EIP712Domain, ...otherTypes },
119
+ message,
120
+ domain,
121
+ primaryType,
122
+ },
123
+ message_hash,
124
+ domain_separator_hash,
125
+ metamask_v4_compat: true,
126
+ });
127
+ if (!response.success) {
128
+ // noinspection ExceptionCaughtLocallyJS
129
+ throw new Error((response.payload && response.payload.error) || 'Unknown error');
130
+ }
131
+ return response.payload.signature;
132
+ }
133
+ catch (e) {
134
+ throw new exceptions_1.TrezorException(new Error(e.message), {
135
+ code: exceptions_1.UnspecifiedErrorCode,
136
+ type: exceptions_1.ErrorType.WalletError,
137
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
138
+ });
139
+ }
140
+ }
141
+ async signAminoCosmosTransaction(_transaction) {
142
+ throw new exceptions_1.WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
143
+ code: exceptions_1.UnspecifiedErrorCode,
144
+ type: exceptions_1.ErrorType.WalletError,
145
+ contextModule: wallet_base_1.WalletAction.SendTransaction,
146
+ });
147
+ }
148
+ // eslint-disable-next-line class-methods-use-this
149
+ async signCosmosTransaction(_transaction) {
150
+ throw new exceptions_1.WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
151
+ code: exceptions_1.UnspecifiedErrorCode,
152
+ type: exceptions_1.ErrorType.WalletError,
153
+ contextModule: wallet_base_1.WalletAction.SendTransaction,
154
+ });
155
+ }
156
+ async signArbitrary(signer, data) {
157
+ try {
158
+ await this.trezor.connect();
159
+ const { derivationPath } = await this.getWalletForAddress(signer);
160
+ const response = await trezor_connect_web_1.TrezorConnect.ethereumSignMessage({
161
+ path: derivationPath,
162
+ message: (0, sdk_ts_1.toUtf8)(data),
163
+ });
164
+ if (!response.success) {
165
+ throw new Error((response.payload && response.payload.error) || 'Unknown error');
166
+ }
167
+ return response.payload.signature;
168
+ }
169
+ catch (e) {
170
+ throw new exceptions_1.TrezorException(new Error(e.message), {
171
+ code: exceptions_1.UnspecifiedErrorCode,
172
+ type: exceptions_1.ErrorType.WalletError,
173
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
174
+ });
175
+ }
176
+ }
177
+ async getEthereumChainId() {
178
+ const alchemy = await this.getAlchemy();
179
+ const alchemyProvider = await alchemy.config.getProvider();
180
+ return alchemyProvider.network.chainId.toString();
181
+ }
182
+ async getEthereumTransactionReceipt(txHash) {
183
+ return Promise.resolve(txHash);
184
+ }
185
+ // eslint-disable-next-line class-methods-use-this
186
+ async getPubKey() {
187
+ throw new exceptions_1.WalletException(new Error('You can only fetch PubKey from Cosmos native wallets'));
188
+ }
189
+ async signEthereumTransaction(txData, options) {
190
+ const chainId = parseInt(options.ethereumChainId.toString(), 10);
191
+ const alchemy = await this.getAlchemy();
192
+ const nonce = await alchemy.core.getTransactionCount(options.address);
193
+ const common = new common_1.Common({
194
+ chain: getNetworkFromChainId(chainId),
195
+ hardfork: common_1.Hardfork.London,
196
+ });
197
+ const eip1559TxData = {
198
+ from: txData.from,
199
+ data: txData.data,
200
+ to: txData.to,
201
+ nonce: (0, ethereumjs_util_1.addHexPrefix)(nonce.toString(16)),
202
+ gas: (0, ethereumjs_util_1.addHexPrefix)(txData.gas),
203
+ gasLimit: (0, ethereumjs_util_1.addHexPrefix)(txData.gas),
204
+ maxFeePerGas: (0, ethereumjs_util_1.addHexPrefix)(txData.gasPrice || txData.maxFeePerGas),
205
+ maxPriorityFeePerGas: (0, ethereumjs_util_1.addHexPrefix)(txData.maxPriorityFeePerGas || wallet_base_1.TIP_IN_GWEI.toString(16)),
206
+ };
207
+ const tx = tx_1.FeeMarketEIP1559Transaction.fromTxData(eip1559TxData, {
208
+ common,
209
+ });
210
+ const transaction = {
211
+ ...tx.toJSON(),
212
+ chainId,
213
+ };
214
+ try {
215
+ await this.trezor.connect();
216
+ const { derivationPath } = await this.getWalletForAddress(options.address);
217
+ const response = await trezor_connect_web_1.TrezorConnect.ethereumSignTransaction({
218
+ path: derivationPath,
219
+ transaction,
220
+ });
221
+ if (!response.success) {
222
+ throw new exceptions_1.TrezorException(new Error((response.payload && response.payload.error) ||
223
+ 'Something happened while signing with Trezor'), {
224
+ code: exceptions_1.UnspecifiedErrorCode,
225
+ type: exceptions_1.ErrorType.WalletError,
226
+ contextModule: wallet_base_1.WalletAction.SignEthereumTransaction,
227
+ });
228
+ }
229
+ const signedTxData = {
230
+ ...eip1559TxData,
231
+ v: `${response.payload.v}`,
232
+ r: `${response.payload.r}`,
233
+ s: `${response.payload.s}`,
234
+ };
235
+ return tx_1.FeeMarketEIP1559Transaction.fromTxData(signedTxData, {
236
+ common,
237
+ });
238
+ }
239
+ catch (e) {
240
+ if (e instanceof exceptions_1.TrezorException) {
241
+ throw e;
242
+ }
243
+ throw new exceptions_1.TrezorException(new Error(e.message), {
244
+ code: exceptions_1.UnspecifiedErrorCode,
245
+ type: exceptions_1.ErrorType.WalletError,
246
+ contextModule: wallet_base_1.WalletAction.SignEthereumTransaction,
247
+ });
248
+ }
249
+ }
250
+ async getWalletForAddress(address) {
251
+ const { baseDerivationPath, derivationPathType } = this;
252
+ const accountManager = await this.trezor.getAccountManager();
253
+ if (!accountManager.hasWalletForAddress(address)) {
254
+ for (let i = 0; i < wallet_base_1.DEFAULT_ADDRESS_SEARCH_LIMIT / wallet_base_1.DEFAULT_NUM_ADDRESSES_TO_FETCH; i += 1) {
255
+ await accountManager.getWallets(baseDerivationPath, derivationPathType);
256
+ if (accountManager.hasWalletForAddress(address)) {
257
+ return (await accountManager.getWalletForAddress(address));
258
+ }
259
+ }
260
+ }
261
+ return (await accountManager.getWalletForAddress(address));
262
+ }
263
+ async getAlchemy() {
264
+ if (this.alchemy) {
265
+ return this.alchemy;
266
+ }
267
+ const { rpcUrl, ethereumChainId } = this.ethereumOptions;
268
+ if (!rpcUrl) {
269
+ throw new exceptions_1.GeneralException(new Error('Please pass rpcUrl within the ethereumOptions'));
270
+ }
271
+ this.alchemy = new alchemy_sdk_1.Alchemy({
272
+ apiKey: (0, wallet_base_1.getKeyFromRpcUrl)(rpcUrl),
273
+ network: ethereumChainId === ts_types_1.EthereumChainId.Mainnet
274
+ ? alchemy_sdk_1.Network.ETH_MAINNET
275
+ : alchemy_sdk_1.Network.ETH_SEPOLIA,
276
+ });
277
+ return this.alchemy;
278
+ }
279
+ }
280
+ exports.default = TrezorBase;
@@ -0,0 +1,5 @@
1
+ import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import TrezorBase from './Base.js';
3
+ export declare class TrezorBip32 extends TrezorBase {
4
+ constructor(args: ConcreteEthereumWalletStrategyArgs);
5
+ }
@@ -0,0 +1,17 @@
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.TrezorBip32 = void 0;
7
+ const types_js_1 = require("../types.js");
8
+ const Base_js_1 = __importDefault(require("./Base.js"));
9
+ class TrezorBip32 extends Base_js_1.default {
10
+ constructor(args) {
11
+ super({
12
+ ...args,
13
+ derivationPathType: types_js_1.TrezorDerivationPathType.Bip32,
14
+ });
15
+ }
16
+ }
17
+ exports.TrezorBip32 = TrezorBip32;
@@ -0,0 +1,5 @@
1
+ import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import TrezorBase from './Base.js';
3
+ export declare class TrezorBip44 extends TrezorBase {
4
+ constructor(args: ConcreteEthereumWalletStrategyArgs);
5
+ }
@@ -0,0 +1,17 @@
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.TrezorBip44 = void 0;
7
+ const types_js_1 = require("../types.js");
8
+ const Base_js_1 = __importDefault(require("./Base.js"));
9
+ class TrezorBip44 extends Base_js_1.default {
10
+ constructor(args) {
11
+ super({
12
+ ...args,
13
+ derivationPathType: types_js_1.TrezorDerivationPathType.Bip44,
14
+ });
15
+ }
16
+ }
17
+ exports.TrezorBip44 = TrezorBip44;
@@ -0,0 +1,19 @@
1
+ import { AccountAddress } from '@injectivelabs/ts-types';
2
+ import { TrezorWalletInfo, TrezorDerivationPathType } from '../../types.js';
3
+ export default class AccountManager {
4
+ private wallets;
5
+ constructor();
6
+ getWallets(baseDerivationPath: string, derivationPathType: TrezorDerivationPathType): Promise<TrezorWalletInfo[]>;
7
+ getTrezorDerivationPathBasedOnType: ({ fullBaseDerivationPath, derivationPathType, index, }: {
8
+ fullBaseDerivationPath: string;
9
+ derivationPathType: TrezorDerivationPathType;
10
+ index: number;
11
+ }) => string;
12
+ private getWalletsBasedOnIndex;
13
+ private hasWallets;
14
+ private hasWalletsInOffset;
15
+ private getOffset;
16
+ hasWalletForAddress(address: AccountAddress): boolean;
17
+ getWalletForAddress(address: AccountAddress): Promise<TrezorWalletInfo | undefined>;
18
+ reset(): void;
19
+ }
@@ -0,0 +1,106 @@
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 hdkey_1 = __importDefault(require("hdkey"));
7
+ const exceptions_1 = require("@injectivelabs/exceptions");
8
+ const types_js_1 = require("../../types.js");
9
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
10
+ const wallet_base_1 = require("@injectivelabs/wallet-base");
11
+ const trezor_connect_web_1 = require("@bangjelkoski/trezor-connect-web");
12
+ const addressOfHDKey = (hdKey) => {
13
+ const shouldSanitizePublicKey = true;
14
+ const derivedPublicKey = hdKey.publicKey;
15
+ const ethereumAddressWithoutPrefix = Buffer.from((0, sdk_ts_1.publicKeyToAddress)(derivedPublicKey, shouldSanitizePublicKey)).toString('hex');
16
+ const address = (0, sdk_ts_1.addHexPrefix)(ethereumAddressWithoutPrefix);
17
+ return address;
18
+ };
19
+ class AccountManager {
20
+ wallets = [];
21
+ constructor() {
22
+ this.wallets = [];
23
+ }
24
+ async getWallets(baseDerivationPath, derivationPathType) {
25
+ const { start, end } = this.getOffset();
26
+ /**
27
+ * 1. Wallets are not yet fetched at all,
28
+ * 2. Wallets are not yet fetched for that offset
29
+ */
30
+ if (!this.hasWallets() || !this.hasWalletsInOffset(start)) {
31
+ await this.getWalletsBasedOnIndex({
32
+ start,
33
+ end,
34
+ baseDerivationPath,
35
+ derivationPathType,
36
+ });
37
+ }
38
+ return this.wallets.slice(start, end);
39
+ }
40
+ getTrezorDerivationPathBasedOnType = ({ fullBaseDerivationPath, derivationPathType, index, }) => {
41
+ if (derivationPathType === types_js_1.TrezorDerivationPathType.Bip44) {
42
+ return `${fullBaseDerivationPath}/${index}'/0/0`;
43
+ }
44
+ if (derivationPathType === types_js_1.TrezorDerivationPathType.Legacy) {
45
+ return `m/${index}`;
46
+ }
47
+ return `${fullBaseDerivationPath}/0'/0/${index}`;
48
+ };
49
+ async getWalletsBasedOnIndex({ start, end, baseDerivationPath, derivationPathType, }) {
50
+ const pathsToFetch = [];
51
+ for (let index = start; index < end; index += 1) {
52
+ const path = this.getTrezorDerivationPathBasedOnType({
53
+ fullBaseDerivationPath: baseDerivationPath,
54
+ derivationPathType,
55
+ index,
56
+ });
57
+ pathsToFetch.push({
58
+ path,
59
+ showOnTrezor: false,
60
+ });
61
+ }
62
+ const result = await trezor_connect_web_1.TrezorConnect.ethereumGetPublicKey({
63
+ bundle: pathsToFetch,
64
+ });
65
+ if (!result.success) {
66
+ throw new exceptions_1.TrezorException(new Error((result.payload && result.payload.error) ||
67
+ 'Please make sure your Trezor is connected and unlocked'));
68
+ }
69
+ for (const item of result.payload) {
70
+ const hdKey = new hdkey_1.default();
71
+ hdKey.publicKey = Buffer.from(item.publicKey, 'hex');
72
+ hdKey.chainCode = Buffer.from(item.chainCode, 'hex');
73
+ const address = addressOfHDKey(hdKey);
74
+ this.wallets.push({
75
+ hdKey,
76
+ derivationPath: item.serializedPath,
77
+ address: address.toLowerCase(),
78
+ });
79
+ }
80
+ }
81
+ hasWallets() {
82
+ return this.wallets.length > 0;
83
+ }
84
+ hasWalletsInOffset(offset) {
85
+ return this.wallets.length > offset;
86
+ }
87
+ getOffset() {
88
+ const totalWallets = this.wallets.length;
89
+ const nextBatchStart = totalWallets;
90
+ const nextBatchEnd = totalWallets + wallet_base_1.DEFAULT_NUM_ADDRESSES_TO_FETCH;
91
+ return {
92
+ start: nextBatchStart,
93
+ end: nextBatchEnd,
94
+ };
95
+ }
96
+ hasWalletForAddress(address) {
97
+ return (this.wallets.find((wallet) => wallet.address.toLowerCase() === address.toLowerCase()) !== undefined);
98
+ }
99
+ async getWalletForAddress(address) {
100
+ return this.wallets.find((wallet) => wallet.address.toLowerCase() === address.toLowerCase());
101
+ }
102
+ reset() {
103
+ this.wallets = [];
104
+ }
105
+ }
106
+ exports.default = AccountManager;
@@ -0,0 +1,4 @@
1
+ import TrezorTransportInit from './transport/transport-init.js';
2
+ import TrezorTransportNoInit from './transport/transport-no-init.js';
3
+ import BaseTrezorTransport from './transport/base.js';
4
+ export { TrezorTransportInit, TrezorTransportNoInit, BaseTrezorTransport };
@@ -0,0 +1,12 @@
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.BaseTrezorTransport = exports.TrezorTransportNoInit = exports.TrezorTransportInit = void 0;
7
+ const transport_init_js_1 = __importDefault(require("./transport/transport-init.js"));
8
+ exports.TrezorTransportInit = transport_init_js_1.default;
9
+ const transport_no_init_js_1 = __importDefault(require("./transport/transport-no-init.js"));
10
+ exports.TrezorTransportNoInit = transport_no_init_js_1.default;
11
+ const base_js_1 = __importDefault(require("./transport/base.js"));
12
+ exports.BaseTrezorTransport = base_js_1.default;
@@ -0,0 +1,6 @@
1
+ import AccountManager from './../AccountManager.js';
2
+ export default class BaseTrezorTransport {
3
+ private accountManager;
4
+ connect(): Promise<void>;
5
+ getAccountManager(): Promise<AccountManager>;
6
+ }
@@ -0,0 +1,19 @@
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 AccountManager_js_1 = __importDefault(require("./../AccountManager.js"));
7
+ class BaseTrezorTransport {
8
+ accountManager = null;
9
+ async connect() {
10
+ return Promise.resolve();
11
+ }
12
+ async getAccountManager() {
13
+ if (!this.accountManager) {
14
+ this.accountManager = new AccountManager_js_1.default();
15
+ }
16
+ return this.accountManager;
17
+ }
18
+ }
19
+ exports.default = BaseTrezorTransport;
@@ -0,0 +1,4 @@
1
+ import BaseTrezorTransport from './base.js';
2
+ export default class TrezorTransportInit extends BaseTrezorTransport {
3
+ constructor();
4
+ }
@@ -0,0 +1,27 @@
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 trezor_connect_web_1 = require("@bangjelkoski/trezor-connect-web");
7
+ const exceptions_1 = require("@injectivelabs/exceptions");
8
+ const base_js_1 = __importDefault(require("./base.js"));
9
+ const TREZOR_CONNECT_MANIFEST = {
10
+ email: 'contact@injectivelabs.org',
11
+ appUrl: 'https://injectivelabs.org',
12
+ };
13
+ class TrezorTransportInit extends base_js_1.default {
14
+ constructor() {
15
+ super();
16
+ try {
17
+ trezor_connect_web_1.TrezorConnect.init({
18
+ lazyLoad: true,
19
+ manifest: TREZOR_CONNECT_MANIFEST,
20
+ });
21
+ }
22
+ catch (e) {
23
+ throw new exceptions_1.WalletException(e);
24
+ }
25
+ }
26
+ }
27
+ exports.default = TrezorTransportInit;
@@ -0,0 +1,4 @@
1
+ import BaseTrezorTransport from './base.js';
2
+ export default class TrezorTransportNoInit extends BaseTrezorTransport {
3
+ constructor();
4
+ }
@@ -0,0 +1,12 @@
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 base_js_1 = __importDefault(require("./base.js"));
7
+ class TrezorTransportNoInit extends base_js_1.default {
8
+ constructor() {
9
+ super();
10
+ }
11
+ }
12
+ exports.default = TrezorTransportNoInit;
@@ -0,0 +1,11 @@
1
+ import type HDNode from 'hdkey';
2
+ export declare enum TrezorDerivationPathType {
3
+ Bip32 = "bip32",
4
+ Bip44 = "bip44",
5
+ Legacy = "legacy"
6
+ }
7
+ export interface TrezorWalletInfo {
8
+ address: string;
9
+ hdKey: HDNode;
10
+ derivationPath: string;
11
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrezorDerivationPathType = void 0;
4
+ var TrezorDerivationPathType;
5
+ (function (TrezorDerivationPathType) {
6
+ TrezorDerivationPathType["Bip32"] = "bip32";
7
+ TrezorDerivationPathType["Bip44"] = "bip44";
8
+ TrezorDerivationPathType["Legacy"] = "legacy";
9
+ })(TrezorDerivationPathType || (exports.TrezorDerivationPathType = TrezorDerivationPathType = {}));