Skip to content

Commit

Permalink
feat: Override Ledger transport layer (#125)
Browse files Browse the repository at this point in the history
* feat: Override Ledger trasnport layer

This feature mainly aims to target fullstack frameworks (ex: Next.js) allowing developers to instanciate the Account server-side or client-side.
  • Loading branch information
BjrInt authored Mar 13, 2023
1 parent 9db8e85 commit bcf0c14
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/accounts/providers/Ledger/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Account } from "../../account";
import { Chain, BaseMessage } from "../../../messages/message";
import { GetVerificationBuffer } from "../../../messages";
import { getTransport } from "./transport";
import { JSExecutionEnvironment } from "../../../utils/env";

const DERIVATION_PATH = "44'/60'/0'/0/0";

Expand Down Expand Up @@ -47,8 +48,8 @@ export class ETHLedgerAccount extends Account {
/**
* This method retrieves an Ethereum account from a connected Ledger device
*/
export async function GetAccountFromLedger(): Promise<ETHLedgerAccount> {
const transport = await getTransport();
export async function GetAccountFromLedger(overrideEnvironment?: JSExecutionEnvironment): Promise<ETHLedgerAccount> {
const transport = await getTransport(overrideEnvironment);
const signer = new EthApp(transport);

const { version } = await signer.getAppConfiguration();
Expand Down
9 changes: 6 additions & 3 deletions src/accounts/providers/Ledger/transport.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Transport from "@ledgerhq/hw-transport";
import { isNode } from "../../../utils/env";
import { isNode, JSExecutionEnvironment } from "../../../utils/env";

export async function getTransport(): Promise<Transport> {
export async function getTransport(overrideEnvironment?: JSExecutionEnvironment): Promise<Transport> {
let transport;

if (isNode()) {
if (
overrideEnvironment === "node" ||
(isNode() && overrideEnvironment !== "browser" && overrideEnvironment !== undefined)
) {
transport = await import("@ledgerhq/hw-transport-node-hid");
} else {
transport = await import("@ledgerhq/hw-transport-webusb");
Expand Down
2 changes: 2 additions & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export function isNode(): boolean {

return hasNodeVersion && hasNoWindow;
}

export type JSExecutionEnvironment = "node" | "browser";

0 comments on commit bcf0c14

Please sign in to comment.