-
Notifications
You must be signed in to change notification settings - Fork 237
Devop/trezor conditional #699
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
cd4677a
fe4ed5b
6cab721
a375ae9
274f157
ab81f6c
081461f
6b5a503
fcce244
e1dab4c
5892e97
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; | ||||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
28
to
30
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 Updated init method for dynamic loading The init method now properly uses the getTrezorConnect helper to dynamically load and initialize TrezorConnect. Consider adding error handling to gracefully handle initialization failures: async init(): Promise<boolean> {
- this.TrezorConnect = await getTrezorConnect();
- return true;
+ try {
+ this.TrezorConnect = await getTrezorConnect();
+ return true;
+ } catch (error) {
+ console.error("Failed to initialize Trezor Connect:", error);
+ return false;
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||||||||
| import TrezorConnect from "@trezor/connect-webextension"; | ||||||||||||||||||||||||||
| import type { TrezorConnect } from "@trezor/connect-web"; | ||||||||||||||||||||||||||
| import { HWwalletCapabilities, NetworkNames } from "@enkryptcom/types"; | ||||||||||||||||||||||||||
| import HDKey from "hdkey"; | ||||||||||||||||||||||||||
| import base58 from "bs58"; | ||||||||||||||||||||||||||
|
|
@@ -12,10 +12,11 @@ import { | |||||||||||||||||||||||||
| SolSignTransaction, | ||||||||||||||||||||||||||
| } from "../../types"; | ||||||||||||||||||||||||||
| import { supportedPaths } from "./configs"; | ||||||||||||||||||||||||||
| import getTrezorConnect from "../trezorConnect"; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class TrezorSolana implements HWWalletProvider { | ||||||||||||||||||||||||||
| network: NetworkNames; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| TrezorConnect: TrezorConnect; | ||||||||||||||||||||||||||
| HDNodes: Record<string, HDKey>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| constructor(network: NetworkNames) { | ||||||||||||||||||||||||||
|
|
@@ -24,28 +25,20 @@ class TrezorSolana 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; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+28
to
30
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 error handling to init() method The async init(): Promise<boolean> {
- this.TrezorConnect = await getTrezorConnect();
- return true;
+ try {
+ this.TrezorConnect = await getTrezorConnect();
+ return true;
+ } catch (error) {
+ console.error('Failed to initialize TrezorConnect:', error);
+ return false;
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async getAddress(options: getAddressRequest): Promise<AddressResponse> { | ||||||||||||||||||||||||||
| if (!supportedPaths[this.network]) | ||||||||||||||||||||||||||
| return Promise.reject(new Error("trezor-bitcoin: Invalid network name")); | ||||||||||||||||||||||||||
| const res = await TrezorConnect.solanaGetAddress({ | ||||||||||||||||||||||||||
| const res = await this.TrezorConnect.solanaGetAddress({ | ||||||||||||||||||||||||||
| path: options.pathType.path.replace(`{index}`, options.pathIndex), | ||||||||||||||||||||||||||
| showOnTrezor: options.confirmAddress, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| address: bufferToHex(base58.decode(res.payload.address)), | ||||||||||||||||||||||||||
| publicKey: bufferToHex(base58.decode(res.payload.address)), | ||||||||||||||||||||||||||
| address: bufferToHex(base58.decode((res.payload as any).address)), | ||||||||||||||||||||||||||
| publicKey: bufferToHex(base58.decode((res.payload as any).address)), | ||||||||||||||||||||||||||
|
Comment on lines
+40
to
+41
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 Avoid type casting to Type casting to - address: bufferToHex(base58.decode((res.payload as any).address)),
- publicKey: bufferToHex(base58.decode((res.payload as any).address)),
+ address: bufferToHex(base58.decode((res.payload as SolanaGetAddressResponse).address)),
+ publicKey: bufferToHex(base58.decode((res.payload as SolanaGetAddressResponse).address)),And similarly for the transaction signature: - }).then((result) => (result.payload as any).signature);
+ }).then((result) => (result.payload as SolanaSignTransactionResponse).signature);You'll need to define these interfaces (if they don't already exist in the TrezorConnect types): interface SolanaGetAddressResponse {
address: string;
// other properties...
}
interface SolanaSignTransactionResponse {
signature: string;
// other properties...
}Also applies to: 67-67 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -66,12 +59,12 @@ class TrezorSolana implements HWWalletProvider { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async signTransaction(options: SignTransactionRequest): Promise<string> { | ||||||||||||||||||||||||||
| return TrezorConnect.solanaSignTransaction({ | ||||||||||||||||||||||||||
| return this.TrezorConnect.solanaSignTransaction({ | ||||||||||||||||||||||||||
| path: options.pathType.path.replace(`{index}`, options.pathIndex), | ||||||||||||||||||||||||||
| serializedTx: (options.transaction as SolSignTransaction).solTx.toString( | ||||||||||||||||||||||||||
| "hex", | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| }).then((result) => result.payload.signature); | ||||||||||||||||||||||||||
| }).then((result) => (result.payload as any).signature); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| static getSupportedNetworks(): NetworkNames[] { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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", | ||
| }, | ||
| 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; | ||
| } | ||
| }; | ||
|
|
||
| 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
Updated init method for dynamic loading
The init method now properly uses the getTrezorConnect helper to dynamically load and initialize TrezorConnect.
Consider adding error handling to gracefully handle initialization failures:
async init(): Promise<boolean> { - this.TrezorConnect = await getTrezorConnect(); - return true; + try { + this.TrezorConnect = await getTrezorConnect(); + return true; + } catch (error) { + console.error("Failed to initialize Trezor Connect:", error); + return false; + } }📝 Committable suggestion
🤖 Prompt for AI Agents