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

Tm/network-slice #5

Merged
merged 36 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 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
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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
transform: {
'^.+\\.(ts|js|tsx|jsx)$': ['@swc/jest']
},
setupFilesAfterEnv: ['<rootDir>/src/setupTests.js'],
transformIgnorePatterns: ['node_modules/(^.+\\\\.(ts|js|tsx|jsx)$)'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
Expand Down
27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@multiversx/sdk-dapp-core",
"version": "0.0.0-alpha.1",
"main": "out/index.js",
"module": "out/index.mjs",
"module": "out/index.js",
"types": "out/index.d.ts",
"description": "A library to hold core logic for building TypeScript dApps on the MultiversX blockchain",
"author": "MultiversX",
Expand All @@ -20,32 +20,43 @@
},
"scripts": {
"compile": "tsc && tsc-alias",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json",
"build-esbuild": "rimraf out && node esbuild.js",
"publish-verdaccio": "npm unpublish --registry http://localhost:4873 @multiversx/[email protected] && rimraf out && yarn compile && npm publish --registry http://localhost:4873",
"build": "yarn build-esbuild && yarn compile",
"test": "jest"
"test": "jest",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@multiversx/sdk-web-wallet-cross-window-provider": "0.1.3",
"@multiversx/sdk-native-auth-client": "^1.0.8",
"@multiversx/sdk-web-wallet-cross-window-provider": "0.2.0",
"@types/lodash": "^4.17.4",
"isomorphic-fetch": "^3.0.0",
"lodash": "^4.17.21",
"zustand": "^4.4.7"
},
"peerDependencies": {
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": "^0.0.1",
"@multiversx/sdk-core": ">= 12.18.0",
"axios": ">=1.6.5"
"axios": ">=1.6.5",
"bignumber.js": "9.x"
},
"resolutions": {
"string-width": "4.1.0"
},
"devDependencies": {
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": "^0.0.1",
"@multiversx/sdk-core": ">= 12.18.0",
"@swc/core": "^1.4.17",
"@swc/jest": "^0.2.36",
"@types/node": "20.12.8",
"@typescript-eslint/eslint-plugin": "7.8.0",
"@typescript-eslint/parser": "7.8.0",
"axios": ">=1.6.5",
"axios-mock-adapter": "^1.22.0",
"bignumber.js": "9.x",
"esbuild": "^0.21.1",
"eslint": "9.1.1",
"eslint-config-prettier": "9.1.0",
Expand All @@ -59,7 +70,7 @@
"immer": "^10.1.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"lit": "^3.1.3",
"msw": "1.3.1",
"prettier": "3.2.5",
"protobufjs": "^7.3.0",
"react": "^18.3.1",
Expand Down
11 changes: 11 additions & 0 deletions src/__mocks__/accountConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fallbackNetworkConfigurations } from 'constants/network';
import { EnvironmentsEnum } from 'types';

export const testAddress =
'erd1dm9uxpf5awkn7uhju7zjn9lde0dhahy0qaxqqlu26xcuuw27qqrsqfmej3';

export const testNetwork =
fallbackNetworkConfigurations[EnvironmentsEnum.devnet];

export const testReceiver =
'erd1qqqqqqqqqqqqqpgqp699jngundfqw07d8jzkepucvpzush6k3wvqyc44rx';
10 changes: 10 additions & 0 deletions src/__mocks__/data/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const account = {
address: 'erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx',
balance: '116893786890813785912',
nonce: 12320,
shard: 0,
rootHash: 'wICKVeNpCg/TsBRyRyZMMMhcW1KENpAbopfinRVyENQ=',
txCount: 12655,
scrCount: 14084,
developerReward: '0'
};
6 changes: 6 additions & 0 deletions src/__mocks__/data/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const blocks = [
{
hash: 'fff67d31476ad920d53093a3a4c2178e198179b35656eeefa419107fa718b780',
timestamp: 1671204768
}
];
16 changes: 16 additions & 0 deletions src/__mocks__/data/dappConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const dappConfig = {
id: 'devnet',
name: 'Devnet',
egldLabel: 'xEGLD',
decimals: '4',
egldDenomination: '18',
gasPerDataByte: '1500',
apiTimeout: '4000',
walletConnectDeepLink:
'https://maiar.page.link/?apn=com.elrond.maiar.wallet&isi=1519405832&ibi=com.elrond.maiar.wallet&link=https://xportal.com/',
walletConnectBridgeAddresses: ['https://bridge.walletconnect.org'],
walletAddress: 'https://devnet-wallet.multiversx.com',
apiAddress: 'https://devnet-api.multiversx.com',
explorerAddress: 'http://devnet-explorer.multiversx.com',
chainId: 'D'
};
7 changes: 7 additions & 0 deletions src/__mocks__/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './account';
export * from './blocks';
export * from './networkConfig';
export * from './dappConfig';
export * from './socketResponse';
export * from './websocketConfig';
export * from './blocks';
28 changes: 28 additions & 0 deletions src/__mocks__/data/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const networkConfig = {
data: {
config: {
erd_adaptivity: 'false',
erd_chain_id: 'D',
erd_denomination: 18,
erd_gas_per_data_byte: 1500,
erd_gas_price_modifier: '0.01',
erd_hysteresis: '0.200000',
erd_latest_tag_software_version: 'D1.3.50.0-hf01',
erd_max_gas_per_transaction: 600000000,
erd_meta_consensus_group_size: 58,
erd_min_gas_limit: 50000,
erd_min_gas_price: 1000000000,
erd_min_transaction_version: 1,
erd_num_metachain_nodes: 58,
erd_num_nodes_in_shard: 58,
erd_num_shards_without_meta: 3,
erd_rewards_top_up_gradient_point: '2000000000000000000000000',
erd_round_duration: 6000,
erd_rounds_per_epoch: 1200,
erd_shard_consensus_group_size: 21,
erd_start_time: 1648551600,
erd_top_up_factor: '0.500000'
}
},
code: 'successful'
};
7 changes: 7 additions & 0 deletions src/__mocks__/data/socketResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const socketResponse = {
sid: 'RlV7upxKjiJRyIhRAKb5',
upgrades: ['websocket'],
pingInterval: 25000,
pingTimeout: 20000,
maxPayload: 1000000
};
1 change: 1 addition & 0 deletions src/__mocks__/data/websocketConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const websocketConfig = { url: 'devnet-socket-api.multiversx.com' };
3 changes: 3 additions & 0 deletions src/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './accountConfig';
export * from './server';
export * from './utils';
55 changes: 55 additions & 0 deletions src/__mocks__/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
DefaultBodyType,
PathParams,
ResponseComposition,
rest,
RestContext,
RestRequest
} from 'msw';
import { setupServer } from 'msw/node';
import { testAddress, testNetwork } from './accountConfig';
import {
account,
blocks,
dappConfig,
networkConfig,
socketResponse,
websocketConfig
} from './data';

export const mockResponse =
<T extends DefaultBodyType>(body: T) =>
(
_req: RestRequest<never, PathParams<string>>,
res: ResponseComposition<DefaultBodyType>,
ctx: RestContext
) => {
return res(ctx.status(200), ctx.json(body));
};

const handlers = [
rest.get(`${testNetwork.apiAddress}/dapp/config`, mockResponse(dappConfig)),

rest.get(
`${testNetwork.apiAddress}/network/config`,
mockResponse(networkConfig)
),
rest.get(
`https://${websocketConfig.url}/socket.io/`,
mockResponse(socketResponse)
),
rest.get(
`${testNetwork.apiAddress}/websocket/config`,
mockResponse(websocketConfig)
),
rest.get(
`${testNetwork.apiAddress}/accounts/${testAddress}`,
mockResponse(account)
),
rest.get(`${testNetwork.apiAddress}/blocks`, mockResponse(blocks))
];

// This configures a request mocking server with the given request handlers.
const server = setupServer(...handlers);

export { server, rest };
2 changes: 2 additions & 0 deletions src/__mocks__/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mockWindowLocation';
export * from './mockWindowHistory';
24 changes: 24 additions & 0 deletions src/__mocks__/utils/mockWindowHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const mockWindowHistory = () => {
if (!window) {
return;
}

const history = window.history;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.history;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.history = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(history),
pushState: {
configurable: true,
value: jest.fn()
}
}
);
};
24 changes: 24 additions & 0 deletions src/__mocks__/utils/mockWindowLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const mockWindowLocation = () => {
if (!window) {
return;
}

const location = window.location;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.location;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.location = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(location),
assign: {
configurable: true,
value: jest.fn()
}
}
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { networkStore } from '../network';
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';

export const getCleanApiAddress = (customApiAddress?: string) => {
const { network } = networkStore.getState();
const network = networkSelector(getState());
const apiAddress = customApiAddress ?? network.apiAddress;
return apiAddress.endsWith('/') ? apiAddress.slice(0, -1) : apiAddress;
};
4 changes: 2 additions & 2 deletions src/apiCalls/configuration/getNetworkConfigFromApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { getCleanApiAddress } from 'store/slices/network/actions/getCleanApiAddress';
import { ApiNetworkConfigType } from '../../types/network.types';
import { ApiNetworkConfigType } from 'types/network.types';
import { NETWORK_CONFIG_ENDPOINT } from '../endpoints';
import { getCleanApiAddress } from './getCleanApiAddress';

const urlIsValid = (url: string) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/apiCalls/configuration/getServerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { NetworkType } from '../../types/network.types';
import { NetworkType } from 'types/network.types';
import { CONFIG_ENDPOINT } from '../endpoints';

export async function getServerConfiguration(apiAddress: string) {
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './network';
export * from './placeholders';
export * from './storage';
export * from './window';
4 changes: 2 additions & 2 deletions src/constants/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EnvironmentsEnum } from '../types/enums.types';
import { NetworkType } from '../types/network.types';
import { EnvironmentsEnum } from 'types/enums.types';
import { NetworkType } from 'types/network.types';

export const fallbackNetworkConfigurations: Record<
keyof typeof EnvironmentsEnum,
Expand Down
8 changes: 8 additions & 0 deletions src/constants/placeholders.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 = '...';
2 changes: 1 addition & 1 deletion src/constants/window.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const safeWindow =
export const safeWindow: Window =
typeof window !== 'undefined' ? window : ({} as Window);
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './core';
export * from './constants';
export * from './apiCalls';
export * from './store';
export * from './types';
export * from './services';
export * from './utils';
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './nativeAuth';
40 changes: 40 additions & 0 deletions src/services/nativeAuth/helpers/decodeLoginToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import isString from 'lodash/isString';
import { decodeBase64 } from 'utils/decoders/base64Utils';

export interface DecodedLoginTokenType {
blockHash: string;
extraInfo?: { timestamp: number };
origin: string;
ttl: number;
}

export const decodeLoginToken = (
loginToken: string
): DecodedLoginTokenType | null => {
if (!loginToken || !isString(loginToken)) {
return null;
}

const parts = loginToken.split('.');

if (parts.length !== 4) {
return null;
}

try {
const [origin, blockHash, ttl, extraInfo] = parts;
const parsedExtraInfo = JSON.parse(decodeBase64(extraInfo));
const parsedOrigin = decodeBase64(origin);

return {
ttl: Number(ttl),
extraInfo: parsedExtraInfo,
origin: parsedOrigin,
blockHash
};
} catch (e) {
console.error(`Error trying to decode ${loginToken}:`, e);

return null;
}
};
Loading
Loading