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

Web-Wallet cross-window login #7

Merged
merged 45 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
83fa0ae
Turn relative imports into absolute
arhtudormorar May 13, 2024
31ecb72
Work on simplify actions
arhtudormorar May 15, 2024
5296d3c
Actions creator helper
arhtudormorar May 16, 2024
2b89f1e
Refactor account definition
arhtudormorar May 16, 2024
8378c72
Update network slice
arhtudormorar May 16, 2024
58c0cff
Add login shared action
arhtudormorar May 16, 2024
b5130e2
Work on account login
arhtudormorar May 16, 2024
99e8236
Update yarn lock
arhtudormorar May 16, 2024
9c9b888
Update yarn lock
arhtudormorar May 16, 2024
b107d0d
Added nativeAuth services
arhtudormorar May 21, 2024
b95e391
Move services
arhtudormorar May 21, 2024
b9a5360
Add NativeAuth service
arhtudormorar May 21, 2024
39b9bc9
Tests working
arhtudormorar May 21, 2024
ca97881
Compile working
arhtudormorar May 21, 2024
0fe1aa0
0.0.0-alpha.2
arhtudormorar May 21, 2024
58f0d8d
Update yarn.lock
arhtudormorar May 21, 2024
74d1c1b
Update yarn lock
arhtudormorar May 21, 2024
5e0a3db
Added validation
arhtudormorar May 27, 2024
a1d05ae
Add sanitizeCallbackUrl
arhtudormorar May 27, 2024
4da1f4d
Upgrade version
arhtudormorar May 27, 2024
b66d97e
Work on deps
arhtudormorar May 27, 2024
7ccfe89
Upgrade sdk-core
arhtudormorar May 27, 2024
fd4d268
Added buildUrlParams
arhtudormorar May 30, 2024
b6098e9
0.0.0-alpha.4
arhtudormorar May 30, 2024
08b8379
remove lit
andreigiura Jun 11, 2024
559bf54
upgrade cross-window
andreigiura Jun 11, 2024
099e205
Revert version
arhtudormorar Jul 9, 2024
d97424c
Remove unused
arhtudormorar Jul 9, 2024
c987d39
Remove timeout promise
arhtudormorar Jul 9, 2024
a73883b
Account slice (#4)
arhtudormorar Jul 10, 2024
f86e0fa
Merge branch 'tm/feature/account-slice' into tm/network-slice
arhtudormorar Jul 11, 2024
c196b8c
New store structure
arhtudormorar Jul 11, 2024
e83ccdd
Rename useStore
arhtudormorar Jul 11, 2024
58046ea
Expose getState
arhtudormorar Jul 12, 2024
22f69c0
Add indexes
arhtudormorar Jul 12, 2024
1464f15
Fixed import
arhtudormorar Jul 12, 2024
1ea5a84
Work on login slice
arhtudormorar Jul 12, 2024
88a003f
Create login info slice
arhtudormorar Jul 12, 2024
41fdd4b
Work on login
arhtudormorar Jul 12, 2024
16acbd0
Merge branch 'development' into tm/login
arhtudormorar Jul 12, 2024
758bc13
Compiling
arhtudormorar Jul 12, 2024
6f67f14
Add return type to web wallet login
arhtudormorar Jul 15, 2024
af75f16
Add default zero
arhtudormorar Jul 15, 2024
5e32dbd
Resolve address
arhtudormorar Jul 15, 2024
ccb95cd
Move getLoginService to helpers
arhtudormorar Jul 16, 2024
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
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp-core",
"version": "0.0.0-alpha.1",
"version": "0.0.0-alpha.2",
"main": "out/index.js",
"module": "out/index.js",
"types": "out/index.d.ts",
Expand Down Expand Up @@ -30,8 +30,15 @@
"access": "public"
},
"dependencies": {
"@lifeomic/axios-fetch": "3.0.1",
"@multiversx/sdk-extension-provider": "3.0.0",
"@multiversx/sdk-hw-provider": "6.4.0",
"@multiversx/sdk-metamask-provider": "0.0.5",
"@multiversx/sdk-native-auth-client": "^1.0.8",
"@multiversx/sdk-opera-provider": "1.0.0-alpha.1",
"@multiversx/sdk-wallet-connect-provider": "4.1.2",
"@multiversx/sdk-web-wallet-cross-window-provider": "0.2.0",
"@multiversx/sdk-web-wallet-provider": "3.2.1",
"@types/lodash": "^4.17.4",
"isomorphic-fetch": "^3.0.0",
"lodash": "^4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/accountConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fallbackNetworkConfigurations } from 'constants/network';
import { fallbackNetworkConfigurations } from 'constants/network.constants';
import { EnvironmentsEnum } from 'types';

export const testAddress =
Expand Down
25 changes: 25 additions & 0 deletions src/apiCalls/accounts/getAccountFromApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ACCOUNTS_ENDPOINT } from 'apiCalls/endpoints';
import { axiosInstance } from 'apiCalls/utils/axiosInstance';
import { getCleanApiAddress } from 'apiCalls/utils/getCleanApiAddress';

export const accountFetcher = (address: string | null) => {
const apiAddress = getCleanApiAddress();
const url = `${apiAddress}/${ACCOUNTS_ENDPOINT}/${address}?withGuardianInfo=true`;
// we need to get it with an axios instance because of cross-window user interaction issues
return axiosInstance.get(url);
};

export const getAccountFromApi = async (address?: string) => {
if (!address) {
return null;
}

try {
const { data } = await accountFetcher(address);
return data;
} catch (err) {
console.error('error fetching configuration for ', address);
}

return null;
};
1 change: 1 addition & 0 deletions src/apiCalls/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getAccountFromApi';
2 changes: 1 addition & 1 deletion src/apiCalls/configuration/getNetworkConfigFromApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { ApiNetworkConfigType } from 'types/network.types';
import { NETWORK_CONFIG_ENDPOINT } from '../endpoints';
import { getCleanApiAddress } from './getCleanApiAddress';
import { getCleanApiAddress } from '../utils/getCleanApiAddress';

const urlIsValid = (url: string) => {
try {
Expand Down
4 changes: 4 additions & 0 deletions src/apiCalls/utils/axiosFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { axiosInstance } from 'apiCalls/utils/axiosInstance';

export const axiosFetcher = (url: string) =>
axiosInstance.get(url).then((response) => response.data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would catch and log the error here

99 changes: 99 additions & 0 deletions src/apiCalls/utils/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { buildAxiosFetch } from '@lifeomic/axios-fetch';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';

// Needs to be used beacause an async call made after cross-window user interaction makes the dapp unresponsive

const fetch = buildAxiosFetch(axios);

const getFormattedAxiosResponse = async <T>(response: Response, config?: T) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Clone the response to be able to read it twice (for status and data)
const clonedResponse = response.clone();

// Parse the JSON body asynchronously
const jsonPromise = clonedResponse.json();

// Return the standardized response object
const [responseData] = await Promise.all([jsonPromise]);
return {
data: responseData,
status: response.status,
statusText: response.statusText,
headers: response.headers,
config
};
};

async function customPost<T = any, R = AxiosResponse<T, any>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D> | undefined
): Promise<R> {
try {
const response = await fetch(url, {
method: 'POST',
body: data ? JSON.stringify(data) : undefined,
headers: {
'Content-Type': 'application/json',
...(config?.headers || {})
},
...config
} as RequestInit);

return getFormattedAxiosResponse(response, config) as unknown as Promise<R>;
} catch (error) {
console.error('Fetch Error:', error);
throw error;
}
}

async function customGet<T = any, R = AxiosResponse<T, any>, D = any>(
url: string,
config?: AxiosRequestConfig<D> | undefined
): Promise<R> {
try {
const response = await fetch(url, config as RequestInit);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

return getFormattedAxiosResponse(response, config) as unknown as Promise<R>;
} catch (error) {
console.error('Fetch Error:', error);
throw error;
}
}

async function customPatch<T = any, R = AxiosResponse<T, any>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D> | undefined
): Promise<R> {
try {
const response = await fetch(url, {
method: 'PATCH',
body: data ? JSON.stringify(data) : undefined,
headers: config?.headers || {},
...config
} as RequestInit);

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

return getFormattedAxiosResponse(response, config) as unknown as Promise<R>;
} catch (error) {
console.error('Fetch Error:', error);
throw error;
}
}

const axiosInstance = axios.create();
axiosInstance.get = customGet;
axiosInstance.post = customPost;
axiosInstance.patch = customPatch;

export { axiosInstance };
8 changes: 8 additions & 0 deletions src/apiCalls/utils/getCleanApiAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';

export const getCleanApiAddress = (customApiAddress?: string) => {
const network = networkSelector(getState());
const apiAddress = customApiAddress ?? network.apiAddress;
return apiAddress.endsWith('/') ? apiAddress.slice(0, -1) : apiAddress;
};
18 changes: 18 additions & 0 deletions src/apiCalls/utils/getScamAddressData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios';
import { ScamInfoType } from 'types/account.types';
import { ACCOUNTS_ENDPOINT } from '../endpoints';
import { networkSelector } from 'store/selectors';
import { getState } from 'store/store';

export async function getScamAddressData(addressToVerify: string) {
const { apiAddress, apiTimeout } = networkSelector(getState());

const { data } = await axios.get<{
scamInfo?: ScamInfoType;
code?: string;
}>(`${apiAddress}/${ACCOUNTS_ENDPOINT}/${addressToVerify}`, {
timeout: Number(apiTimeout)
});

return data;
}
4 changes: 4 additions & 0 deletions src/apiCalls/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './axiosFetcher';
export * from './axiosInstance';
export * from './getCleanApiAddress';
export * from './getScamAddressData';
10 changes: 10 additions & 0 deletions src/constants/browser.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { safeWindow } from './window.constants';

const userAgent = String(safeWindow?.navigator?.userAgent);

export const isSafari = /^((?!chrome|android).)*safari/i.test(userAgent);

const isFirefoxOnWindows =
/firefox/i.test(userAgent) && /windows/i.test(userAgent);

export const isBrowserWithPopupConfirmation = isSafari || isFirefoxOnWindows;
13 changes: 13 additions & 0 deletions src/constants/errorMessages.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const ERROR_SIGNING = 'error when signing';
export const CANCELLED = 'cancelled';
export const TRANSACTION_CANCELLED = 'Transaction canceled';
export const ERROR_SIGNING_TX = 'error signing transaction';
export const PROVIDER_NOT_INITIALIZED = 'provider not initialized';
export const MISSING_PROVIDER_MESSAGE =
'You need a signer/valid signer to send a transaction,use either WalletProvider, LedgerProvider or WalletConnect';
export const DEFAULT_TRANSACTION_STATUS_MESSAGE =
'Undefined transaction status';
export const SECOND_LOGIN_ATTEMPT_ERROR =
'Action not allowed. User is logged in. Call logout() first';
export const SENDER_DIFFERENT_THAN_LOGGED_IN_ADDRESS =
'You cannot sign transactions from a different account';
11 changes: 7 additions & 4 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from './network';
export * from './placeholders';
export * from './storage';
export * from './window';
export * from './network.constants';
export * from './placeholders.constants';
export * from './storage.constants';
export * from './window.constants';
export * from './browser.constants';
export * from './errorMessages.constants';
export * from './ledger.constants';
1 change: 1 addition & 0 deletions src/constants/ledger.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LEDGER_CONTRACT_DATA_ENABLED_VALUE = 1;
File renamed without changes.
8 changes: 8 additions & 0 deletions src/constants/placeholders.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Not Applicable
* @value N/A
*/
export const N_A = 'N/A';

export const ZERO = '0';
export const ELLIPSIS = '...';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createJSONStorage } from 'zustand/middleware';
import { safeWindow } from './window';
import { safeWindow } from './window.constants';

export const persistConfig: {
persistReducersStorageType: 'localStorage' | 'sessionStorage';
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/core/methods/account/getAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { accountSelector } from 'store/selectors';
import { getState } from 'store/store';

export function getAccount() {
return accountSelector(getState());
}
6 changes: 6 additions & 0 deletions src/core/methods/account/getAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { addressSelector } from 'store/selectors';
import { getState } from 'store/store';

export function getAddress() {
return addressSelector(getState());
}
5 changes: 5 additions & 0 deletions src/core/methods/account/getIsLoggedIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getAddress } from './getAddress';

export function getIsLoggedIn() {
return Boolean(getAddress());
}
Loading
Loading