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

Fixed Ledger login #1321

Merged
merged 14 commits into from
Nov 27, 2024
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: yarn install

- name: Run tests
run: yarn test
run: yarn test --silent

- name: Build project
run: yarn build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-merge-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
- name: Build
run: yarn build
- name: Run unit tests
run: yarn test
run: yarn test --silent
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ All notable changes will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- [Fixed Ledger login](https://github.com/multiversx/mx-sdk-dapp/pull/1322)
- [Removed refreshAccount on websocket event](https://github.com/multiversx/mx-sdk-dapp/pull/1321)

## [[v3.0.17](https://github.com/multiversx/mx-sdk-dapp/pull/1320)] - 2024-11-26

- [Updated get network config from API logic](https://github.com/multiversx/mx-sdk-dapp/pull/1319)

## [[v3.0.16](https://github.com/multiversx/mx-sdk-dapp/pull/1318)] - 2024-11-26

- [Updated provider initializer to use useGetAccountFromApi](https://github.com/multiversx/mx-sdk-dapp/pull/1317)

## [Unreleased]

## [[v3.0.15](https://github.com/multiversx/mx-sdk-dapp/pull/1316)] - 2024-11-25

- [Added transaction data decoder](https://github.com/multiversx/mx-sdk-dapp/pull/1311)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp",
"version": "3.0.16",
"version": "3.0.17",
"description": "A library to hold the main logic for a dapp on the MultiversX blockchain",
"author": "MultiversX",
"license": "GPL-3.0-or-later",
Expand Down
15 changes: 15 additions & 0 deletions src/components/ProviderInitializer/ProviderInitializer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { useEffect, useRef } from 'react';
import { getNetworkConfigFromApi, useGetAccountFromApi } from 'apiCalls';
import {
DEVNET_CHAIN_ID,
MAINNET_CHAIN_ID,
TESTNET_CHAIN_ID
} from 'constants/index';
import { useLoginService } from 'hooks/login/useLoginService';
import { useWalletConnectV2Login } from 'hooks/login/useWalletConnectV2Login';
import { useWebViewLogin } from 'hooks/login/useWebViewLogin';
Expand Down Expand Up @@ -120,6 +125,16 @@ export function ProviderInitializer() {
}, [ledgerAccount, isLoggedIn, ledgerData]);

async function refreshNetworkConfig() {
const shouldGetConfig =
!network.chainId ||
![DEVNET_CHAIN_ID, TESTNET_CHAIN_ID, MAINNET_CHAIN_ID].includes(
network.chainId
);

if (!shouldGetConfig) {
return;
}

try {
const networkConfig = await getNetworkConfigFromApi();
const hasDifferentNetworkConfig =
Expand Down
51 changes: 29 additions & 22 deletions src/hooks/login/useLedgerLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,16 @@ export const useLedgerLogin = ({
};

const onLoginFailed = (err: any, customMessage = '') => {
if (!err) {
return;
}
if (isLoading) {
const { errorMessage, defaultErrorMessage } = getLedgerErrorCodes(err);

const { errorMessage, defaultErrorMessage } = getLedgerErrorCodes(err);
const message =
errorMessage ?? defaultErrorMessage ?? failInitializeErrorText;

const message =
errorMessage ?? defaultErrorMessage ?? failInitializeErrorText;

setError(`${message}.${customMessage}`);
setIsLoading(false);
dispatch(setLedgerAccount(null));
setError(`${message}.${customMessage}`);
setIsLoading(false);
dispatch(setLedgerAccount(null));
}
};

const isHWProviderInitialized = async () => {
Expand Down Expand Up @@ -239,12 +237,10 @@ export const useLedgerLogin = ({
);
}

setIsLoading(false);
await loginUser();
setIsLoading(false);
} catch (err) {
onLoginFailed(err);
} finally {
setIsLoading(false);
}

setShowAddressList(false);
Expand Down Expand Up @@ -283,12 +279,10 @@ export const useLedgerLogin = ({
}

clearInitiatedLogins();

setError('');

try {
setIsLoading(true);
await initHWProvider();
const isInitialized = await isHWProviderInitialized();

if (!isInitialized) {
Expand Down Expand Up @@ -323,19 +317,32 @@ export const useLedgerLogin = ({

setShowAddressList(true);
}
} catch (error) {
onLoginFailed(error);
} finally {

setIsLoading(false);
} catch (err) {
onLoginFailed(err);
}
};

useEffect(() => {
initHWProvider();
}, [hwProvider]);
const initProviderAndAccounts = async () => {
try {
await initHWProvider();
const isInitialized = await isHWProviderInitialized();

if (!isInitialized) {
return onLoginFailed(failInitializeErrorText);
}

if (accounts.length === 0 || startIndex > 0) {
await fetchAccounts();
}
} catch (err) {
onLoginFailed(err, failInitializeErrorText);
}
};

useEffect(() => {
fetchAccounts();
initProviderAndAccounts();
}, [startIndex, showAddressList, hwProvider]);

useEffect(() => {
Expand Down
1 change: 0 additions & 1 deletion src/hooks/transactions/useTransactionsTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function useTransactionsTracker(props?: TransactionsTrackerType) {

const onMessage = () => {
checkTransactionStatus({
shouldRefreshBalance: isWebsocketCompleted,
getTransactionsByHash,
...props
});
Expand Down