Skip to content

Commit

Permalink
Omit HID libraries for hardware-wallets package on unsupported enviro…
Browse files Browse the repository at this point in the history
…nments (#798).
  • Loading branch information
ricmoo committed Apr 24, 2020
1 parent 017ea0d commit 2e24920
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/hardware-wallets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"dependencies": {
"@ledgerhq/hw-app-eth": "5.3.0",
"@ledgerhq/hw-transport": "5.3.0",
"@ledgerhq/hw-transport-node-hid": "5.3.0",
"@ledgerhq/hw-transport-u2f": "5.3.0",
"ethers": ">=5.0.0-beta.166"
},
Expand All @@ -33,6 +32,9 @@
"main": "./lib/index.js",
"module": "./lib.esm/index.js",
"name": "@ethersproject/hardware-wallets",
"optionalDependencies": {
"@ledgerhq/hw-transport-node-hid": "5.3.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
29 changes: 23 additions & 6 deletions packages/hardware-wallets/src.ts/ledger-transport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
"use strict";

import hid from "@ledgerhq/hw-transport-node-hid";

export type TransportCreator = {
create: () => Promise<Transport>;
};

export const transports: { [ name: string ]: TransportCreator } = {
"hid": hid,
"default": hid
};
let hidCache: Promise<typeof import("@ledgerhq/hw-transport-node-hid")> = null;

const hidWrapper = Object.freeze({
create: function(): Promise<Transport> {
// Load the library if not loaded
if (hidCache == null) {
hidCache = import("@ledgerhq/hw-transport-node-hid").then((hid) => {
if (hid.create == null) { return hid["default"]; }
return hid;
});
}

return hidCache.then((hid) => {
console.log(hid, hid.create);
return hid.create()
});
}
});

export const transports: { [ name: string ]: TransportCreator } = Object.freeze({
"hid": hidWrapper,
"default": hidWrapper
});

0 comments on commit 2e24920

Please sign in to comment.