Skip to content
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

Ledger path support #420

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,29 @@ import {
Transaction,
} from '@ethersproject/transactions';

let LedgerSigner: any; // TODO type
let ledgerSigner: Signer;

function ledgerSignerFactory(
provider: Web3Provider | zk.Web3Provider,
path: string
): Signer {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const hardwareWalletModule = require('@ethersproject/hardware-wallets');
return new hardwareWalletModule.LedgerSigner(provider, 'default', path);
} catch (_) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const hardwareWalletModule = require('@anders-t/ethers-ledger');
return new hardwareWalletModule.LedgerSigner(provider, path);
} catch (e) {
console.error(
`failed to loader hardhware wallet module for ledger, you can either use "@ethersproject/hardware-wallets" which as time of writing does not work or use "@anders-t/ethers-ledger."`
);
throw e;
}
}
}

async function handleSpecificErrors<T>(p: Promise<T>): Promise<T> {
let result: T;
Expand Down Expand Up @@ -1763,33 +1785,16 @@ Note that in this case, the contract deployment will not behave the same if depl
return provider.getTransaction(response.hash);
};
hardwareWallet = 'external';
} else if (registeredProtocol === 'ledger') {
if (!LedgerSigner) {
// 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('@ethersproject/hardware-wallets');
LedgerSigner = hardwareWalletModule.LedgerSigner;
} catch (e) {
error = e;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const hardwareWalletModule = require('@anders-t/ethers-ledger');
LedgerSigner = hardwareWalletModule.LedgerSigner;
error = undefined;
} catch (e) {}
}

if (error) {
console.error(
`failed to loader hardhware wallet module for ledger, you can either use "@ethersproject/hardware-wallets" which as time of writing does not work or use "@anders-t/ethers-ledger."`
);
throw error;
}
} else if (registeredProtocol.startsWith('ledger')) {
if (!ledgerSigner) {
ledgerSigner = ledgerSignerFactory(
provider,
registeredProtocol.substr(9)
);
}
ethersSigner = new LedgerSigner(provider);

hardwareWallet = 'ledger';
ethersSigner = ledgerSigner;
} else if (registeredProtocol.startsWith('privatekey')) {
ethersSigner = new Wallet(registeredProtocol.substr(13), provider);
} else if (registeredProtocol.startsWith('gnosis')) {
Expand Down
14 changes: 11 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,17 @@ function transformNamedAccounts(
protocolSplit[0].toLowerCase();
// knownAccountsDict[address.toLowerCase()] = true; // TODO ? this would prevent auto impersonation in fork/test
} else if (protocolSplit[0].toLowerCase() === 'ledger') {
address = protocolSplit[1];
addressesToProtocol[address.toLowerCase()] =
protocolSplit[0].toLowerCase();
const addressSplit = protocolSplit[1].split(':');
if (addressSplit.length > 1) {
address = addressSplit[1];
addressesToProtocol[
address.toLowerCase()
] = `ledger://${addressSplit[0]}`;
} else {
address = protocolSplit[1];
addressesToProtocol[address.toLowerCase()] =
"ledger://m/44'/60'/0'/0/0";
}
// knownAccountsDict[address.toLowerCase()] = true; // TODO ? this would prevent auto impersonation in fork/test
} else if (protocolSplit[0].toLowerCase() === 'privatekey') {
address = new Wallet(protocolSplit[1]).address;
Expand Down