Skip to content

Commit

Permalink
Merge pull request #112 from lidofinance/feature/ledger-connector-rework
Browse files Browse the repository at this point in the history
Ledger HID connector based on wagmi + adapter
  • Loading branch information
alx-khramov authored Dec 22, 2023
2 parents f49b641 + 572b42e commit a197cb4
Show file tree
Hide file tree
Showing 52 changed files with 648 additions and 292 deletions.
2 changes: 2 additions & 0 deletions apps/demo-react/components/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useWeb3 } from 'reef-knot/web3-react';
import { utils } from 'ethers';
import { CHAINS } from '../config/chains';

// not working properly, or not working at all
// TODO: rework NetworkSelector or remove completely
const NetworkSelector = () => {
const { chainId = CHAINS.Goerli, library } = useWeb3();

Expand Down
4 changes: 3 additions & 1 deletion apps/demo-react/components/ProviderWeb3WithProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const connectors = getConnectors({
rpc: rpcUrlsString,
walletconnectProjectId: WC_PROJECT_ID,
chains,
defaultChain: goerli,
defaultChain:
supportedChains.find((chain) => chain.id === defaultChainId) ||
supportedChains[0],
});

const client = createClient({
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-react/components/contractTesting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ContractTesting = () => {
const stethBalance = useSTETHBalance();
const stethContractWeb3 = useSTETHContractWeb3();

const { chainId = 5, account } = useWeb3();
const { chainId = 17000, account } = useWeb3();
const { providerWeb3 } = useSDK();
const ethBalance = useEthereumBalance();
const wstethBalance = useWSTETHBalance();
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-react/hooks/useWithdrawTxPrice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react';
import { useLidoSWR, useSDK } from '@lido-sdk/react';

import { useWeb3 } from '@reef-knot/web3-react';
import { useWeb3 } from 'reef-knot/web3-react';
import { TOKENS } from '@lido-sdk/constants';

import { BigNumber } from 'ethers';
Expand Down
1 change: 0 additions & 1 deletion apps/demo-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@lido-sdk/providers": "^1.4.13",
"@lido-sdk/react": "^2.0.2",
"@lidofinance/lido-ui": "^3.18.0",
"@reef-knot/web3-react": "^1.4.1",
"@typechain/ethers-v5": "^11.1.0",
"ethers": "^5.7.2",
"flatted": "^3.2.7",
Expand Down
2 changes: 0 additions & 2 deletions apps/demo-react/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
WalletsModal,
MainSection,
ContractTesting,
NetworkSelector,
} from '../components';

import { GlobalStyle } from '../styles/global';
Expand All @@ -42,7 +41,6 @@ export function Web() {
<MainContainer>
<ConnectDisconnect handleOpen={handleOpen} />
<div style={{ display: 'flex', gap: '40px' }}>
<NetworkSelector />
<ThemeSelect
selectedTheme={selectedTheme}
handleSelect={setSelectedTheme as (e: OptionValue) => void}
Expand Down
6 changes: 6 additions & 0 deletions packages/connect-wallet-modal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/connect-wallet-modal

## 1.16.0

### Minor Changes

- Use Ledger hid wallet adapter, based on wagmi

## 1.15.0

### Minor Changes
Expand Down
14 changes: 7 additions & 7 deletions packages/connect-wallet-modal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/connect-wallet-modal",
"version": "1.15.0",
"version": "1.16.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -45,13 +45,13 @@
"@types/react-dom": "18.2.17"
},
"devDependencies": {
"@reef-knot/core-react": "^1.7.0",
"@reef-knot/types": "^1.3.0",
"@reef-knot/core-react": "^1.8.0",
"@reef-knot/types": "^1.4.0",
"@reef-knot/ui-react": "^1.0.7",
"@reef-knot/wallets-helpers": "^1.1.5",
"@reef-knot/wallets-icons": "^1.5.0",
"@reef-knot/web3-react": "^1.11.0",
"@reef-knot/ledger-connector": "^1.1.0",
"@reef-knot/web3-react": "^1.12.0",
"@reef-knot/ledger-connector": "^2.0.0",
"@types/ua-parser-js": "^0.7.36",
"eslint-config-custom": "*",
"react": "18.2.0",
Expand All @@ -65,8 +65,8 @@
"@reef-knot/ui-react": "^1.0.4",
"@reef-knot/wallets-helpers": "^1.1.2",
"@reef-knot/wallets-icons": "^1.0.0",
"@reef-knot/web3-react": "^1.3.0",
"@reef-knot/ledger-connector": "^1.0.1",
"@reef-knot/web3-react": "^1.12.0",
"@reef-knot/ledger-connector": "^2.0.0",
"react": ">=18",
"@lidofinance/lido-ui": "^3.18.0",
"ua-parser-js": "^1.0.33",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ButtonStyled = styled.div`
padding: 16px 20px;
justify-content: space-between;
align-items: center;
box-sizing: border-box;
margin-top: 10px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { FC, useCallback, useEffect, useState } from 'react';
import styled from '@reef-knot/ui-react/styled-wrapper';
import { helpers } from '@reef-knot/web3-react';
import { useConnect } from 'wagmi';
import { Pagination, Stack, StackItem } from '@lidofinance/lido-ui';
import { AccountButton, AccountButtonSkeleton } from './LedgerAccountButton';
import { useLedgerAccounts, useLedgerContext } from './hooks';
import { getFirstIndexOnPage, saveLedgerDerivationPath } from './helpers';
import { Metrics } from '../WalletsModal';
import { useConnectorLedger, helpers } from '@reef-knot/web3-react';
import { LedgerDerivationPathSelect } from './LedgerDerivationPathSelect';
import { AccountsStorage } from './types';
import { DERIVATION_PATHS } from './constants';
Expand Down Expand Up @@ -57,23 +58,29 @@ export const LedgerAccountScreen: FC<Props> = ({ metrics, closeScreen }) => {
}
}, [accountsForPage, accountsStorage, derivationPathTemplate]);

const onConnectLedger = metrics?.events?.connect?.handlers.onConnectLedger;
const { connect } = useConnectorLedger({
onConnect: onConnectLedger,
const metricsOnConnect = metrics?.events?.connect?.handlers.onConnectLedger;

const { connect, connectors } = useConnect({
onSuccess() {
metricsOnConnect?.();
},
});
const ledgerConnector = connectors.find(
(connector) => connector.id === 'ledgerHID',
);

const handleAccountButtonClick = useCallback(
async (account) => {
saveLedgerDerivationPath(account.path);
await disconnectTransport(true);
try {
await connect();
connect({ connector: ledgerConnector });
closeScreen?.();
} catch (e) {
setError(helpers.interceptLedgerError(e as Error));
}
},
[closeScreen, connect, disconnectTransport, setError],
[closeScreen, connect, disconnectTransport, ledgerConnector, setError],
);

const handleDerivationPathSelect = useCallback((value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import React, {
} from 'react';
import type Transport from '@ledgerhq/hw-transport';
import Eth from '@ledgerhq/hw-app-eth';
import { useConnectorLedger, helpers } from '@reef-knot/web3-react';
import { getTransport } from './helpers';
import { helpers } from '@reef-knot/web3-react';
import { getTransport, isHIDSupported } from './helpers';

export interface LedgerContextProps {
isActive: boolean;
Expand Down Expand Up @@ -40,7 +40,6 @@ export const LedgerContextProvider: FC<LedgerContextProps> = ({
const [isTransportConnected, setIsTransportConnected] = useState(false);
const [activeAccountsRequestsCounter, setActiveAccountsRequestsCounter] =
useState(0);
const { connector } = useConnectorLedger();

const disconnectTransport = useCallback(async (goingToReconnect = false) => {
try {
Expand All @@ -59,7 +58,7 @@ export const LedgerContextProvider: FC<LedgerContextProps> = ({
if (transport.current) return;
setError(null);

if (!connector.isSupported()) {
if (!isHIDSupported()) {
setError(
new Error(
"Your browser doesn't support direct connection with Ledger. Please, try another browser.",
Expand All @@ -77,7 +76,7 @@ export const LedgerContextProvider: FC<LedgerContextProps> = ({
await disconnectTransport(true);
setError(helpers.interceptLedgerError(e));
}
}, [connector, disconnectTransport]);
}, [disconnectTransport]);

const reconnectTransport = useCallback(async () => {
await disconnectTransport(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ export const isDeviceBusy = (transport: LedgerContextValue['transport']) => {

export const getFirstIndexOnPage = (page: number, accountsPerPage: number) =>
accountsPerPage * (page - 1);

export const isHIDSupported = () => {
try {
return 'hid' in window.navigator;
} catch (error) {
return false;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const walletsButtons: { [K in WalletId | string]: React.ComponentType } = {
injected: ConnectInjected,
walletConnect: ConnectWC,
coinbaseWallet: ConnectCoinbase,
ledgerHID: ConnectLedger,
[WALLET_IDS.METAMASK]: ConnectMetamask,
[WALLET_IDS.LEDGER]: ConnectLedger,
};

function getWalletButton(
Expand Down Expand Up @@ -59,7 +59,7 @@ function getWalletsButtons(
addWalletTo(wallets, walletId, !!detector?.());
});

wallets = [...wallets, WALLET_IDS.LEDGER].filter(
wallets = [...wallets].filter(
// Filtering wallets marked as hidden
(wallet) => !hiddenWallets.includes(wallet),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import React, { FC, useCallback } from 'react';
import { Ledger, LedgerInversion } from '@reef-knot/wallets-icons/react';
import { ConnectWalletProps } from './types';
import { ConnectLedgerProps } from './types';
import { ConnectButton } from '../components/ConnectButton';

const ConnectLedger: FC<ConnectWalletProps> = (props) => {
export const ConnectLedger: FC<ConnectLedgerProps> = (props) => {
const {
onConnect,
onBeforeConnect,
setRequirements,
setLedgerScreenVisible,
shouldInvertWalletIcon,
icon: WalletIcon,
metrics,
...rest
} = props;
const onClickLedger = metrics?.events?.click?.handlers.onClickLedger;
const WalletIcon = shouldInvertWalletIcon ? LedgerInversion : Ledger;

const handleConnect = useCallback(() => {
onBeforeConnect?.();
Expand All @@ -24,10 +23,13 @@ const ConnectLedger: FC<ConnectWalletProps> = (props) => {
}, [onBeforeConnect, onClickLedger, setLedgerScreenVisible]);

return (
<ConnectButton {...rest} icon={WalletIcon} onClick={handleConnect}>
<ConnectButton
{...rest}
icon={WalletIcon}
shouldInvertWalletIcon={shouldInvertWalletIcon}
onClick={handleConnect}
>
Ledger
</ConnectButton>
);
};

export default ConnectLedger;
2 changes: 1 addition & 1 deletion packages/connect-wallet-modal/src/connectButtons/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as ConnectLedger } from './connectLedger';
export * from './ConnectLedger';
export { default as ConnectMetamask } from './connectMetamask';
export * from './ConnectInjected';
export * from './ConnectWC';
Expand Down
1 change: 1 addition & 0 deletions packages/connect-wallet-modal/src/connectButtons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export type ConnectWalletProps = ButtonsCommonProps & ButtonProps;

export type ConnectInjectedProps = WalletAdapterData & ConnectWalletProps;
export type ConnectWCProps = WalletAdapterData & ConnectWalletProps;
export type ConnectLedgerProps = WalletAdapterData & ConnectWalletProps;
1 change: 0 additions & 1 deletion packages/connect-wallet-modal/src/constants/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const WALLET_IDS = {
METAMASK: 'Metamask',
LEDGER: 'Ledger',
COINBASE: 'Coinbase',
} as const;

Expand Down
6 changes: 6 additions & 0 deletions packages/connectors/ledger-connector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/ledger-connector

## 2.0.0

### Major Changes

- Rework: use wagmi for hid connector instead of web3-react

## 1.1.1

### Patch Changes
Expand Down
14 changes: 9 additions & 5 deletions packages/connectors/ledger-connector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/ledger-connector",
"version": "1.1.1",
"version": "2.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"@types/w3c-web-hid": "^1.0.2",
"wagmi": "^0.12.19",
"eslint-config-custom": "*"
},
"dependencies": {
Expand All @@ -44,12 +45,15 @@
"@ethersproject/strings": "^5.5.0",
"@ethersproject/transactions": "^5.5.0",
"@ethersproject/hash": "^5.5.0",
"@ledgerhq/hw-app-eth": "^6.34.8",
"@ledgerhq/hw-transport": "^6.28.8",
"@ledgerhq/hw-transport-webhid": "^6.27.19",
"@ledgerhq/iframe-provider": "0",
"@ledgerhq/hw-app-eth": "^6.35.0",
"@ledgerhq/hw-transport": "^6.30.0",
"@ledgerhq/hw-transport-webhid": "^6.28.0",
"@ledgerhq/iframe-provider": "^0.4.3",
"@web3-react/abstract-connector": "^6.0.7",
"@web3-react/types": "^6.0.7",
"tiny-invariant": "^1.2.0"
},
"peerDependencies": {
"wagmi": "^0.12.19"
}
}
Loading

0 comments on commit a197cb4

Please sign in to comment.