Skip to content

Commit

Permalink
Merge pull request #394 from nxqbao/feat/integrate-trezor
Browse files Browse the repository at this point in the history
Integrate with trezor wallet
  • Loading branch information
wighawag authored May 13, 2023
2 parents 1d55c10 + 43f8f8a commit 972ccea
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/hdpath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import chalk from 'chalk';

function logError(...args: any[]) {
console.log(chalk.red(...args));
}

export function getDerivationPath(chainId: number): string | undefined {
let coinType;

switch (chainId) {
case 1:
case 2020 /* Ronin Mainnet */:
case 2021 /* Ronin Testnet */:
coinType = '60';
break;
case 3:
case 4:
case 5:
coinType = '1';
break;
default:
logError(`Network with chainId: ${chainId} not supported.`);
return undefined;
}

const derivationPath = `m/44'/${coinType}'/0'/0`;
return derivationPath;
}
46 changes: 45 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ import {
parse as parseTransaction,
Transaction,
} from '@ethersproject/transactions';
import {getDerivationPath} from './hdpath';

let LedgerSigner: any; // TODO type
let TrezorSigner: any; // TODO type
let hardwareSigner: any; // TODO type

async function handleSpecificErrors<T>(p: Promise<T>): Promise<T> {
let result: T;
Expand Down Expand Up @@ -620,6 +623,8 @@ export function addHelpers(
// TODO: Remove me when LedgerSigner adds proper support for 1559 txns
if (hardwareWallet === 'ledger') {
unsignedTx.type = 1;
} else if (hardwareWallet === 'trezor') {
unsignedTx.type = 1;
}

if (unknown) {
Expand Down Expand Up @@ -1728,6 +1733,7 @@ Note that in this case, the contract deployment will not behave the same if depl
let wallet: Wallet | zk.Wallet | undefined;
let hardwareWallet: string | undefined = undefined;
let unknown = false;
let derivationPath: string | undefined = undefined;

if (from.length >= 64) {
if (from.length === 64) {
Expand Down Expand Up @@ -1805,8 +1811,46 @@ Note that in this case, the contract deployment will not behave the same if depl

ethersSigner = new LedgerSigner(provider);
hardwareWallet = 'ledger';

ledgerSigner = ethersSigner;
} else if (registeredProtocol === 'trezor') {
if (!TrezorSigner) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let error: any | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const hardwareWalletModule = require('@nxqbao/eth-signer-trezor');
derivationPath = getDerivationPath(network.config.chainId);

if (!derivationPath) {
throw new Error(
`network is currently unsupported with trezor`
);
}

TrezorSigner = hardwareWalletModule.TrezorSigner;
} catch (e) {
error = e;
}

if (error) {
console.error(
`failed to loader hardware wallet module for trezor`
);
throw error;
}
}

if (!hardwareSigner) {
hardwareSigner = new TrezorSigner(
provider,
derivationPath,
undefined, // TODO: support fetch by index
from,
'hardhat-deploy-trezor'
);
}
ethersSigner = hardwareSigner;
hardwareWallet = 'trezor';
} else if (registeredProtocol.startsWith('privatekey')) {
ethersSigner = new Wallet(registeredProtocol.substr(13), provider);
} else if (registeredProtocol.startsWith('gnosis')) {
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ function transformNamedAccounts(
addressesToProtocol[address.toLowerCase()] =
protocolSplit[0].toLowerCase();
// knownAccountsDict[address.toLowerCase()] = true; // TODO ? this would prevent auto impersonation in fork/test
} else if (
protocolSplit[0].toLowerCase() === 'trezor'
) {
address = protocolSplit[1];
addressesToProtocol[address.toLowerCase()] =
protocolSplit[0].toLowerCase();
} else if (protocolSplit[0].toLowerCase() === 'ledger') {
const addressSplit = protocolSplit[1].split(':');
if (addressSplit.length > 1) {
Expand Down

0 comments on commit 972ccea

Please sign in to comment.