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

feat: add @trezor/utils package #4666

Merged
merged 8 commits into from
Jan 28, 2022
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
3 changes: 3 additions & 0 deletions ci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- docker login $CI_DEPENDENCY_PROXY_SERVER -u $CI_DEPENDENCY_PROXY_USER -p $CI_DEPENDENCY_PROXY_PASSWORD
script:
- yarn install --pure-lockfile --cache-folder .yarn --prefer-offline
- yarn workspace @trezor/utils build:lib
Nodonisko marked this conversation as resolved.
Show resolved Hide resolved
mroz22 marked this conversation as resolved.
Show resolved Hide resolved
- docker-compose pull
- docker-compose up -d ${CONTAINERS}
- docker-compose run test-run
Expand Down Expand Up @@ -128,6 +129,7 @@ rollout:
COMPOSE_FILE: ./docker/docker-compose.transport-test.yml
script:
- yarn install --pure-lockfile --cache-folder .yarn --prefer-offline
- yarn workspace @trezor/utils build:lib
- yarn workspace @trezor/transport build:lib
- ./docker/docker-transport-test.sh
after_script:
Expand All @@ -148,6 +150,7 @@ transport nightly:
stage: integration testing
script:
- yarn install --frozen-lockfile --cache-folder .yarn --prefer-offline
- yarn workspace @trezor/utils build:lib
- yarn workspace @trezor/blockchain-link build:lib
- yarn workspace @trezor/blockchain-link build:workers
- yarn workspace @trezor/blockchain-link test:integration
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"worker-loader": "^3.0.8"
},
"dependencies": {
"@trezor/utils": "1.0.0",
"bignumber.js": "^9.0.1",
"events": "^3.3.0",
"ripple-lib": "1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain-link/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';
import { createDeferred, Deferred } from '@trezor/utils/lib/createDeferred';
import { CustomError } from './constants/errors';
import { MESSAGES, RESPONSES } from './constants';
import { create as createDeferred, Deferred } from './utils/deferred';
import type { BlockchainSettings } from './types';
import type * as ResponseTypes from './types/responses';
import type * as MessageTypes from './types/messages';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as WebSocket from 'ws';
import { EventEmitter } from 'events';
import { createDeferred, Deferred } from '@trezor/utils/lib/createDeferred';

import { CustomError } from '../../constants/errors';
import { create as createDeferred, Deferred } from '../../utils/deferred';
import type {
BlockNotification,
AddressNotification,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as WebSocket from 'ws';
import { EventEmitter } from 'events';
import { createDeferred, Deferred } from '@trezor/utils/lib/createDeferred';

import { CustomError } from '../../constants/errors';
import { create as createDeferred, Deferred } from '../../utils/deferred';
import type { Send, BlockContent, BlockfrostTransaction } from '../../types/blockfrost';
import type {
AccountInfoParams,
Expand Down
18 changes: 1 addition & 17 deletions packages/integration-tests/websocket-client.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
const WebSocket = require('ws');
const { EventEmitter } = require('events');
const { createDeferred } = require('@trezor/utils');

const NOT_INITIALIZED = new Error('websocket_not_initialized');

const createDeferred = id => {
let localResolve = t => () => {};
let localReject = e => () => {};

const promise = new Promise((resolve, reject) => {
localResolve = resolve;
localReject = reject;
});

return {
id,
resolve: localResolve,
reject: localReject,
promise,
};
};

// Making the timeout high because the controller in trezor-user-env
// must synchronously run actions on emulator and they may take a long time
// (for example in case of Shamir backup)
Expand Down
1 change: 1 addition & 0 deletions packages/suite-desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"afterSign": "scripts/notarize.js"
},
"dependencies": {
"@trezor/utils": "*",
"chalk": "^4.1.2",
"electron-localshortcut": "^3.2.1",
"electron-store": "^8.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/suite-desktop/src-electron/libs/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import si from 'systeminformation';

import { isDev } from '@suite-utils/build';
import { b2t } from '@desktop-electron/libs/utils';
import { toHumanReadable } from '@suite-utils/file';
import { bytesToHumanReadable } from '@trezor/utils';

export const buildInfo = () => {
global.logger.info('build', [
Expand Down Expand Up @@ -41,6 +41,6 @@ export const computerInfo = async () => {
`- Cores: ${cpu.processors}x${cpu.physicalCores}(+${cpu.cores - cpu.physicalCores}) @ ${
cpu.speed
}GHz`,
`- RAM: ${toHumanReadable(mem.total)}`,
`- RAM: ${bytesToHumanReadable(mem.total)}`,
]);
};
6 changes: 3 additions & 3 deletions packages/suite-desktop/src-electron/modules/auto-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { isDev } from '@suite-utils/build';
import { b2t } from '@desktop-electron/libs/utils';
import { verifySignature } from '@desktop-electron/libs/update-checker';
import { toHumanReadable } from '@suite-utils/file';
import { bytesToHumanReadable } from '@trezor/utils';
import { isEnabled } from '@suite-utils/features';

// Runtime flags
Expand Down Expand Up @@ -142,9 +142,9 @@ const init = ({ mainWindow, store }: Dependencies) => {
autoUpdater.on('download-progress', progressObj => {
logger.debug(
'auto-updater',
`Downloading ${progressObj.percent}% (${toHumanReadable(
`Downloading ${progressObj.percent}% (${bytesToHumanReadable(
progressObj.transferred,
)}/${toHumanReadable(progressObj.total)})`,
)}/${bytesToHumanReadable(progressObj.total)})`,
);
mainWindow.webContents.send('update/downloading', { ...progressObj });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { H2, variables } from '@trezor/components';
import { Translation, Modal } from '@suite-components';
import { Row } from './styles';

import { toHumanReadable } from '@suite-utils/file';
import { bytesToHumanReadable } from '@trezor/utils';
import { UpdateProgress } from '@suite-types/desktop';

const ModalHeadingWrapper = styled.div`
Expand Down Expand Up @@ -76,9 +76,9 @@ const Downloading = ({ hideWindow, progress }: Props) => {
</Text>
<DownloadProgress>
<ReceivedData>
{toHumanReadable(progress?.transferred || 0)}
{bytesToHumanReadable(progress?.transferred || 0)}
</ReceivedData>
/<TotalData>{toHumanReadable(progress?.total || 0)}</TotalData>
/<TotalData>{bytesToHumanReadable(progress?.total || 0)}</TotalData>
</DownloadProgress>
</>
)}
Expand Down
1 change: 1 addition & 0 deletions packages/suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@trezor/components": "1.0.0",
"@trezor/suite-data": "1.0.0",
"@trezor/suite-storage": "1.0.0",
"@trezor/utils": "*",
"bignumber.js": "^9.0.2",
"date-fns": "^2.27.0",
"dropbox": "^10.23.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { mergeObj } from '@suite-utils/mergeObj';
import { mergeObject } from '@trezor/utils';
import { init } from '@suite-actions/trezorConnectActions';
import { SUITE } from '@suite-actions/constants';
import { BACKUP } from '@backup-actions/constants';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const getInitialState = (override: any) => {
devices: [],
};
if (override) {
return mergeObj(defaults, override);
return mergeObject(defaults, override);
}
return defaults;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const oldWindowLocation = window.location;

describe('Analytics Actions', () => {
beforeAll(() => {
// eslint-disable-next-line global-require
require('@suite-utils/random');

// @ts-ignore The operand of a 'delete' operator must be optional.
delete window.location;

Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/actions/suite/metadataActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TrezorConnect from 'trezor-connect';
import { createDeferred } from '@trezor/utils';
import { METADATA } from '@suite-actions/constants';
import { createDeferred } from '@suite-utils/deferred';
import { Dispatch, GetState } from '@suite-types';
import {
MetadataProviderType,
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/actions/suite/modalActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import TrezorConnect, { UI } from 'trezor-connect';
import { createDeferred, Deferred, DeferredResponse } from '@trezor/utils';
import { MODAL, SUITE } from '@suite-actions/constants';
import { Route, Dispatch, GetState, TrezorDevice } from '@suite-types';
import { Account, WalletAccountTransaction } from '@wallet-types';
import { createDeferred, Deferred, DeferredResponse } from '@suite-utils/deferred';

export type UserContextPayload =
| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as protocolActions from '@suite-actions/protocolActions';
import { Translation } from '@suite-components';
import { CoinLogo } from '@trezor/components';
import { useActions, useSelector } from '@suite-hooks';
import { capitalizeFirstLetter } from '@suite-utils/string';
import { capitalizeFirstLetter } from '@trezor/utils';
import { PROTOCOL_TO_NETWORK } from '@suite-constants/protocol';
import ConditionalActionRenderer from './ConditionalActionRenderer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useSelector, useActions } from '@suite-hooks';
import * as accountActions from '@wallet-actions/accountActions';
import * as walletSettingsActions from '@settings-actions/walletSettingsActions';
import * as routerActions from '@suite-actions/routerActions';
import { partition } from '@suite-utils/array';
import { arrayPartition } from '@trezor/utils';

import { AccountTypeSelect } from './components/AccountTypeSelect';
import { SelectNetwork } from './components/SelectNetwork';
Expand Down Expand Up @@ -68,12 +68,12 @@ const AddAccountModal = ({ device, onCancel, symbol, noRedirect }: Props) => {
const selectedNetworkEnabled =
!!selectedNetwork && enabledNetworksSymbols.includes(selectedNetwork.symbol);

const [enabledNetworks, disabledNetworks] = partition(internalNetworks, network =>
const [enabledNetworks, disabledNetworks] = arrayPartition(internalNetworks, network =>
enabledNetworksSymbols.includes(network.symbol),
);
const hasDisabledNetworks = !!disabledNetworks?.length;

const [disabledMainnetNetworks, disabledTestnetNetworks] = partition(
const [disabledMainnetNetworks, disabledTestnetNetworks] = arrayPartition(
disabledNetworks,
network => !network?.testnet,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styled, { css } from 'styled-components';
import { Button, useTheme, variables, Input, Tooltip, Checkbox, Icon } from '@trezor/components';
import { Translation } from '@suite-components/Translation';
import { MAX_LENGTH } from '@suite-constants/inputs';
import { countBytesInString } from '@suite-utils/string';
import { countBytesInString } from '@trezor/utils';
import { OpenGuideFromTooltip } from '@guide-views';
import PasswordStrengthIndicator from '@suite-components/PasswordStrengthIndicator';
import { useTranslation } from '@suite-hooks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Icon, Button, useTheme, variables } from '@trezor/components';
import { FormattedCryptoAmount, Sign, Translation, FormattedDate } from '@suite-components';
import { useRbfContext } from '@wallet-hooks/useRbfForm';
import { useLayoutSize } from '@suite-hooks/useLayoutSize';
import { truncateMiddle } from '@suite-utils/string';
import { truncateMiddle } from '@trezor/utils';
import GreyCard from '../GreyCard';
import WarnHeader from '../WarnHeader';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Icon, variables, Checkbox, H3 } from '@trezor/components';
import React, { useState } from 'react';
import { Translation, Modal } from '@suite-components';
import styled, { css } from 'styled-components';
import { Deferred } from '@suite-utils/deferred';
import type { Deferred } from '@trezor/utils';

const Text = styled.div<{ isLast?: boolean; isFirst?: boolean }>`
padding: 20px 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Icon, variables, Checkbox } from '@trezor/components';
import React, { useState } from 'react';
import { Translation, Modal } from '@suite-components';
import styled, { css } from 'styled-components';
import { Deferred } from '@suite-utils/deferred';
import type { Deferred } from '@trezor/utils';

const Text = styled.div<{ isLast?: boolean; isFirst?: boolean }>`
padding: 20px 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Icon, variables, Checkbox, H3 } from '@trezor/components';
import React, { useState } from 'react';
import { Translation, Modal } from '@suite-components';
import styled, { css } from 'styled-components';
import { Deferred } from '@suite-utils/deferred';
import type { Deferred } from '@trezor/utils';

const Text = styled.div<{ isLast?: boolean; isFirst?: boolean }>`
padding: 20px 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Icon, variables, Checkbox, H3 } from '@trezor/components';
import React, { useState } from 'react';
import { Translation, Modal } from '@suite-components';
import styled, { css } from 'styled-components';
import { Deferred } from '@suite-utils/deferred';
import type { Deferred } from '@trezor/utils';

const Text = styled.div<{ isLast?: boolean; isFirst?: boolean }>`
padding: 20px 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { P, Button, variables } from '@trezor/components';
import { Translation, Modal } from '@suite-components';
import { useActions } from '@suite-hooks';
import * as metadataActions from '@suite-actions/metadataActions';
import { Deferred } from '@suite-utils/deferred';
import type { Deferred } from '@trezor/utils';
import { MetadataProviderType } from '@suite-types/metadata';
import { isEnabled } from '@suite-utils/features';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useActions, useSelector, useTranslation } from '@suite-hooks';
import { isUrl } from '@suite-utils/validators';
import { isUrl } from '@trezor/utils';
import { setBackend as setBackendAction } from '@settings-actions/walletSettingsActions';
import type { Network } from '@wallet-types';
import type { BackendType } from '@wallet-reducers/settingsReducer';
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/hooks/suite/useAsyncDebounce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useRef } from 'react';
import { createDeferred } from '@suite-utils/deferred';
import { createDeferred } from '@trezor/utils';

type TimeoutType = ReturnType<typeof setTimeout>; // resolves to Timeout type in react-native, number otherwise

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useForm, useController } from 'react-hook-form';
import { useTranslation, useSelector, useActions } from '@suite-hooks';
import * as protocolActions from '@suite-actions/protocolActions';
import { isHex } from '@wallet-utils/ethUtils';
import { isASCII } from '@suite-utils/validators';
import { isAscii } from '@trezor/utils';
import { isAddressValid } from '@wallet-utils/validation';
import type { Account } from '@wallet-types';
import type { AoppState } from '@suite-reducers/protocolReducer';
Expand Down Expand Up @@ -114,7 +114,7 @@ export const useSignVerifyForm = (page: 'sign' | 'verify', account?: Account) =>
? translationString('DATA_NOT_VALID_HEX')
: undefined,
ascii: (message: string) =>
!formValues.hex && !isASCII(message)
!formValues.hex && !isAscii(message)
? translationString('TR_ASCII_ONLY')
: undefined,
},
Expand Down
12 changes: 6 additions & 6 deletions packages/suite/src/reducers/suite/resizeReducer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import produce from 'immer';
import * as variables from '@trezor/components/lib/config/variables'; // can't import from index cause it would import all UI components
import { RESIZE } from '@suite-actions/constants';
import { getNumberFromPxString } from '@suite-utils/string';
import { getNumberFromPixelString } from '@trezor/utils';
import { Action } from '@suite-types';

const sizes = {
UNAVAILABLE: getNumberFromPxString(variables.SCREEN_SIZE.UNAVAILABLE),
SMALL: getNumberFromPxString(variables.SCREEN_SIZE.SM),
MEDIUM: getNumberFromPxString(variables.SCREEN_SIZE.MD),
LARGE: getNumberFromPxString(variables.SCREEN_SIZE.LG),
XLARGE: getNumberFromPxString(variables.SCREEN_SIZE.XL),
UNAVAILABLE: getNumberFromPixelString(variables.SCREEN_SIZE.UNAVAILABLE),
SMALL: getNumberFromPixelString(variables.SCREEN_SIZE.SM),
MEDIUM: getNumberFromPixelString(variables.SCREEN_SIZE.MD),
LARGE: getNumberFromPixelString(variables.SCREEN_SIZE.LG),
XLARGE: getNumberFromPixelString(variables.SCREEN_SIZE.XL),
};

const getSize = (screenWidth: number | null): State['size'] => {
Expand Down
5 changes: 3 additions & 2 deletions packages/suite/src/reducers/suite/suiteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Action, TrezorDevice, Lock, SuiteThemeColors } from '@suite-types';
import type { Locale } from '@suite-config/languages';
import { isWeb, getWindowWidth } from '@suite-utils/env';
import { ensureLocale } from '@suite-utils/l10n';
import { getNumberFromPxString } from '@suite-utils/string';
import { getNumberFromPixelString } from '@trezor/utils';

export interface DebugModeOptions {
invityAPIUrl?: string;
Expand Down Expand Up @@ -79,7 +79,8 @@ const initialState: SuiteState = {
taprootBannerClosed: false,
securityStepsHidden: false,
dashboardGraphHidden: false,
dashboardAssetsGridMode: getWindowWidth() < getNumberFromPxString(variables.SCREEN_SIZE.SM),
dashboardAssetsGridMode:
getWindowWidth() < getNumberFromPixelString(variables.SCREEN_SIZE.SM),
},
settings: {
theme: {
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/reducers/wallet/discoveryReducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import produce from 'immer';
import { DISCOVERY } from '@wallet-actions/constants';
import { STORAGE } from '@suite-actions/constants';
import { Deferred, createDeferred } from '@suite-utils/deferred';
import { Deferred, createDeferred } from '@trezor/utils';
import { ObjectValues } from '@suite/types/utils';
import { Action as SuiteAction } from '@suite-types';
import { WalletAction, Network } from '@wallet-types';
Expand Down
Loading