-
Notifications
You must be signed in to change notification settings - Fork 237
Devop/trezor conditional #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a375ae9
274f157
ab81f6c
081461f
6b5a503
fcce244
e1dab4c
c17d693
5892e97
eb31eaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import TrezorConnect from "@trezor/connect-webextension"; | ||
| import { HWwalletCapabilities, NetworkNames } from "@enkryptcom/types"; | ||
| import HDKey from "hdkey"; | ||
| import { bigIntToHex, bufferToHex, hexToBuffer } from "@enkryptcom/utils"; | ||
| import { publicToAddress, toRpcSig } from "@ethereumjs/util"; | ||
| import { FeeMarketEIP1559Transaction, LegacyTransaction } from "@ethereumjs/tx"; | ||
| import type { TrezorConnect } from "@trezor/connect-web"; | ||
| import { | ||
| AddressResponse, | ||
| getAddressRequest, | ||
|
|
@@ -13,10 +13,11 @@ import { | |
| SignTransactionRequest, | ||
| } from "../../types"; | ||
| import { supportedPaths } from "./configs"; | ||
| import getTrezorConnect from "../trezorConnect"; | ||
|
|
||
| class TrezorEthereum implements HWWalletProvider { | ||
| network: NetworkNames; | ||
|
|
||
| TrezorConnect: TrezorConnect; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add initialization checks for TrezorConnect Same issue as in the bitcoin implementation - the Add null checks consistently across all methods that use async getAddress(options: getAddressRequest): Promise<AddressResponse> {
+ if (!this.TrezorConnect) {
+ throw new Error("TrezorConnect not initialized. Call init() first.");
+ }
if (!supportedPaths[this.network])
return Promise.reject(new Error("trezor-ethereum: Invalid network name"));Also applies to: 29-30 🤖 Prompt for AI Agents |
||
| HDNodes: Record<string, HDKey>; | ||
|
|
||
| constructor(network: NetworkNames) { | ||
|
|
@@ -25,15 +26,7 @@ class TrezorEthereum implements HWWalletProvider { | |
| } | ||
|
|
||
| async init(): Promise<boolean> { | ||
| TrezorConnect.init({ | ||
| manifest: { | ||
| email: "info@enkrypt.com", | ||
| appUrl: "https://www.enkrypt.com", | ||
| }, | ||
| transports: ["BridgeTransport", "WebUsbTransport"], | ||
| connectSrc: "https://connect.trezor.io/9/", | ||
| _extendWebextensionLifetime: true, | ||
| }); | ||
| this.TrezorConnect = await getTrezorConnect(); | ||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -42,14 +35,14 @@ class TrezorEthereum implements HWWalletProvider { | |
| return Promise.reject(new Error("trezor-ethereum: Invalid network name")); | ||
|
|
||
| if (!this.HDNodes[options.pathType.basePath]) { | ||
| const rootPub = await TrezorConnect.ethereumGetPublicKey({ | ||
| const rootPub = await this.TrezorConnect.ethereumGetPublicKey({ | ||
| path: options.pathType.basePath, | ||
| showOnTrezor: options.confirmAddress, | ||
| }); | ||
| if (!rootPub.payload) { | ||
| throw new Error("popup failed to open"); | ||
| } | ||
| if (!rootPub.success) throw new Error(rootPub.payload.error as string); | ||
| if (!rootPub.success) throw new Error((rootPub.payload as any).error); | ||
|
|
||
| const hdKey = new HDKey(); | ||
| hdKey.publicKey = Buffer.from(rootPub.payload.publicKey, "hex"); | ||
|
|
@@ -78,12 +71,12 @@ class TrezorEthereum implements HWWalletProvider { | |
| } | ||
|
|
||
| async signPersonalMessage(options: SignMessageRequest): Promise<string> { | ||
| const result = await TrezorConnect.ethereumSignMessage({ | ||
| const result = await this.TrezorConnect.ethereumSignMessage({ | ||
| path: options.pathType.path.replace(`{index}`, options.pathIndex), | ||
| message: options.message.toString("hex"), | ||
| hex: true, | ||
| }); | ||
| if (!result.success) throw new Error(result.payload.error as string); | ||
| if (!result.success) throw new Error((result.payload as any).error); | ||
| return bufferToHex(hexToBuffer(result.payload.signature)); | ||
| } | ||
|
|
||
|
|
@@ -100,14 +93,14 @@ class TrezorEthereum implements HWWalletProvider { | |
| data: bufferToHex(tx.data), | ||
| }; | ||
| if ((options.transaction as LegacyTransaction).gasPrice) { | ||
| return TrezorConnect.ethereumSignTransaction({ | ||
| return this.TrezorConnect.ethereumSignTransaction({ | ||
| path: options.pathType.path.replace(`{index}`, options.pathIndex), | ||
| transaction: { | ||
| ...txObject, | ||
| gasPrice: bigIntToHex(tx.gasPrice), | ||
| }, | ||
| }).then((result) => { | ||
| if (!result.success) throw new Error(result.payload.error as string); | ||
| if (!result.success) throw new Error((result.payload as any).error); | ||
| const rv = BigInt(parseInt(result.payload.v, 16)); | ||
| const cv = tx.common.chainId() * 2n + 35n; | ||
| return toRpcSig( | ||
|
|
@@ -119,15 +112,15 @@ class TrezorEthereum implements HWWalletProvider { | |
| } | ||
|
|
||
| tx = options.transaction as FeeMarketEIP1559Transaction; | ||
| return TrezorConnect.ethereumSignTransaction({ | ||
| return this.TrezorConnect.ethereumSignTransaction({ | ||
| path: options.pathType.path.replace(`{index}`, options.pathIndex), | ||
| transaction: { | ||
| ...txObject, | ||
| maxFeePerGas: bigIntToHex(tx.maxFeePerGas), | ||
| maxPriorityFeePerGas: bigIntToHex(tx.maxPriorityFeePerGas), | ||
| }, | ||
| }).then((result) => { | ||
| if (!result.success) throw new Error(result.payload.error as string); | ||
| if (!result.success) throw new Error((result.payload as any).error); | ||
| return toRpcSig( | ||
| BigInt(result.payload.v), | ||
| hexToBuffer(result.payload.r), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { TrezorConnect as TrezorConnectType } from "@trezor/connect-web"; | ||
|
|
||
| const getTrezorConnect = async () => { | ||
| if (chrome && chrome.runtime && chrome.runtime.getPlatformInfo) { | ||
| const TrezorConnect = await import("@trezor/connect-webextension"); | ||
| await TrezorConnect.default.init({ | ||
| manifest: { | ||
| email: "info@enkrypt.com", | ||
| appUrl: "https://www.enkrypt.com", | ||
kvhnuke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| transports: ["BridgeTransport", "WebUsbTransport"], | ||
| connectSrc: "https://connect.trezor.io/9/", | ||
| _extendWebextensionLifetime: true, | ||
| }); | ||
| return TrezorConnect.default as TrezorConnectType; | ||
| } else { | ||
| const TrezorConnect = await import("@trezor/connect-web"); | ||
| await TrezorConnect.default.init({ | ||
| lazyLoad: true, | ||
| manifest: { | ||
| email: "info@enkrypt.com", | ||
| appUrl: "http://www.myetherwallet.com", | ||
| }, | ||
| }); | ||
| return TrezorConnect.default as TrezorConnectType; | ||
| } | ||
| }; | ||
kvhnuke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default getTrezorConnect; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add null checking for TrezorConnect instance
The
TrezorConnectproperty is not initialized in the constructor and could cause runtime errors if other methods are called beforeinit().Consider adding null checks in methods that use
this.TrezorConnect:async getAddress(options: getAddressRequest): Promise<AddressResponse> { + if (!this.TrezorConnect) { + throw new Error("TrezorConnect not initialized. Call init() first."); + } if (!supportedPaths[this.network]) return Promise.reject(new Error("trezor-bitcoin: Invalid network name"));Apply similar checks to
signPersonalMessageandsignTransactionmethods.Also applies to: 29-30
🤖 Prompt for AI Agents