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

Fix Ledger reconnection #176

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
2 changes: 1 addition & 1 deletion apps/demo-react/providers/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const LidoSDKProvider: React.FC<PropsWithChildren> = ({ children }) => {
chainId: chainId as any,
rpcUrls: [getBackendRPCPath(chainId)],
web3Provider: client as any,
logMode: 'debug',
logMode: 'none',
});
// inject lido_sdk for console access
if (typeof window !== 'undefined') (window as any).lido_sdk = sdk;
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

## 5.5.2

### Patch Changes

- 3bfcb7c: Fix modals props passing

## 5.5.1

### Patch Changes
Expand Down
4 changes: 2 additions & 2 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": "5.5.1",
"version": "5.5.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -51,7 +51,7 @@
"@reef-knot/ui-react": "^2.1.1",
"@reef-knot/wallets-helpers": "^2.0.1",
"@reef-knot/web3-react": "^4.0.1",
"@reef-knot/ledger-connector": "^4.1.0",
"@reef-knot/ledger-connector": "^4.1.3",
"eslint-config-custom": "*",
"react": "18.2.0",
"react-dom": "^18.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { useReefKnotModal } from '@reef-knot/core-react';

export function WalletsModal({
children,
...props
metrics,
termsLink,
privacyNoticeLink,
onClickWalletsLess,
onClickWalletsMore,
...passedDownProps
}: React.PropsWithChildren<WalletsModalProps>) {
const { metrics, termsLink, privacyNoticeLink } = props;
const { onClickWalletsLess, onClickWalletsMore, ...passedDownProps } = props;

const { currentModal, closeModal, forceCloseAllModals } = useReefKnotModal();

const termsProps: WalletModalConnectTermsProps = {
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

## 4.1.3

### Patch Changes

- 3bfcb7c: Fix Ledger reconnection: after explicit disconnect and after page reloading

## 4.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion 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": "4.1.2",
"version": "4.1.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
9 changes: 6 additions & 3 deletions packages/connectors/ledger-connector/src/hid/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createConnector,
} from 'wagmi';
import { Chain } from 'wagmi/chains';
import { checkError } from '../hid/helpers';
import { checkError, clearLedgerDerivationPath } from '../hid/helpers';
import type { LedgerHQProvider } from './provider';

export const idLedgerHid = 'ledgerHID';
Expand Down Expand Up @@ -57,9 +57,9 @@ export function ledgerHIDConnector({

async disconnect() {
// Handles programmatic disconnect.
// We don't need to do anything specific for HID Ledger connection in this case.
const provider = await this.getProvider();
provider.removeListener('disconnect', this.onDisconnect);
clearLedgerDerivationPath();
},

async getAccounts() {
Expand All @@ -81,7 +81,10 @@ export function ledgerHIDConnector({
if (!provider) throw new ConnectorNotFoundError();
const [account] = await this.getAccounts();
return !!account;
} catch {
} catch (e) {
// The errors caught here are generally expected in most use cases.
// However, unexpected errors may still occur, so they should at least be logged to the console.
console.error(e);
return false;
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LS_KEY_DERIVATION_PATH = 'reef-knot_ledger-derivation-path';
5 changes: 5 additions & 0 deletions packages/connectors/ledger-connector/src/hid/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
import { resolveProperties } from '@ethersproject/properties';
import { TransactionRequestExtended, UnsignedTransactionStrict } from './types';
import { LS_KEY_DERIVATION_PATH } from './constants';

export const isHIDSupported = () => {
try {
Expand Down Expand Up @@ -82,3 +83,7 @@ export const checkError = (error: any): never => {

throw error;
};

export const clearLedgerDerivationPath = () => {
window?.localStorage.removeItem(LS_KEY_DERIVATION_PATH);
};
2 changes: 1 addition & 1 deletion packages/connectors/ledger-connector/src/hid/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { idLedgerHid, ledgerHIDConnector } from './connector';
export { LS_KEY_DERIVATION_PATH } from './signer';
export { LS_KEY_DERIVATION_PATH } from './constants';
16 changes: 11 additions & 5 deletions packages/connectors/ledger-connector/src/hid/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export class LedgerHQProvider extends JsonRpcBatchProvider {
this.signer = this.getSigner();
}

async loadTransport() {
if (!this.transport) {
const { default: TransportWebHID } = await import(
'@ledgerhq/hw-transport-webhid'
);
this.transport = TransportWebHID;
}
}

getSigner(): LedgerHQSigner {
return new LedgerHQSigner(this);
}
Expand All @@ -30,7 +39,7 @@ export class LedgerHQProvider extends JsonRpcBatchProvider {
}

async getTransport(): Promise<TransportWebHID> {
invariant(this.transport, 'Transport is not defined');
await this.loadTransport();

try {
const transportInstance =
Expand All @@ -45,10 +54,7 @@ export class LedgerHQProvider extends JsonRpcBatchProvider {

async enable(): Promise<string> {
try {
const { default: TransportWebHID } = await import(
'@ledgerhq/hw-transport-webhid'
);
this.transport = TransportWebHID;
await this.loadTransport();

const { hid } = window.navigator;

Expand Down
35 changes: 16 additions & 19 deletions packages/connectors/ledger-connector/src/hid/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ import { toUtf8Bytes } from '@ethersproject/strings';
import { Bytes, hexlify, joinSignature } from '@ethersproject/bytes';
import { _TypedDataEncoder } from '@ethersproject/hash';

import type { LedgerHQProvider } from './provider';
import { checkError, convertToUnsigned, toNumber } from './helpers';
import { UnsignedTransactionStrict } from './types';
import { LS_KEY_DERIVATION_PATH } from './constants';

import type { LedgerHQProvider } from './provider';
import type { UnsignedTransactionStrict } from './types';

export const LS_KEY_DERIVATION_PATH = 'reef-knot_ledger-derivation-path';
const defaultPath = "m/44'/60'/0'/0/0";

export class LedgerHQSigner extends Signer implements TypedDataSigner {
readonly path: string;

readonly provider: LedgerHQProvider;

_index = 0;
get path() {
if (typeof window !== 'undefined') {
return window.localStorage.getItem(LS_KEY_DERIVATION_PATH) || defaultPath;
}
return defaultPath;
}

// required by JsonRpcSigner type
_index = 0;
_address = '';

constructor(provider: LedgerHQProvider, path = '') {
constructor(provider: LedgerHQProvider) {
super();

let pathFromLS;
if (typeof window !== 'undefined') {
pathFromLS = window.localStorage.getItem(LS_KEY_DERIVATION_PATH);
}
this.path = path || pathFromLS || defaultPath;
this.provider = provider;
}

Expand All @@ -63,11 +63,8 @@ export class LedgerHQSigner extends Signer implements TypedDataSigner {
}

async getAddress(): Promise<string> {
if (!this._address) {
const account = await this.withEthApp((eth) => eth.getAddress(this.path));
this._address = this.provider.formatter.address(account.address);
}

const account = await this.withEthApp((eth) => eth.getAddress(this.path));
this._address = this.provider.formatter.address(account.address);
return this._address;
}

Expand Down Expand Up @@ -132,7 +129,7 @@ export class LedgerHQSigner extends Signer implements TypedDataSigner {
}

connect(provider: LedgerHQProvider): JsonRpcSigner {
return new LedgerHQSigner(provider, this.path);
return new LedgerHQSigner(provider);
}

// eslint-disable-next-line class-methods-use-this
Expand Down
8 changes: 8 additions & 0 deletions packages/reef-knot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# reef-knot

## 5.7.4

### Patch Changes

- Updated dependencies [3bfcb7c]
- @reef-knot/[email protected]
- @reef-knot/[email protected]

## 5.7.3

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/reef-knot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reef-knot",
"version": "5.7.3",
"version": "5.7.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,14 +41,14 @@
"lint": "eslint --ext ts,tsx,js,mjs ."
},
"dependencies": {
"@reef-knot/connect-wallet-modal": "5.5.1",
"@reef-knot/connect-wallet-modal": "5.5.2",
"@reef-knot/core-react": "4.3.0",
"@reef-knot/web3-react": "4.0.1",
"@reef-knot/ui-react": "2.1.4",
"@reef-knot/wallets-list": "2.3.0",
"@reef-knot/wallets-helpers": "2.1.0",
"@reef-knot/types": "2.1.0",
"@reef-knot/ledger-connector": "4.1.2"
"@reef-knot/ledger-connector": "4.1.3"
},
"peerDependencies": {
"@lidofinance/lido-ui": "^3.18.0",
Expand Down