Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/koni/dev/issue-3056…
Browse files Browse the repository at this point in the history
…' into subwallet-dev

# Conflicts:
#	packages/extension-koni-ui/src/types/index.ts
  • Loading branch information
saltict committed May 29, 2024
2 parents 7672394 + d03d7e5 commit 67f2dde
Show file tree
Hide file tree
Showing 34 changed files with 181 additions and 74 deletions.
17 changes: 13 additions & 4 deletions packages/extension-koni-ui/src/Popup/Confirmations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { AccountJson, AuthorizeRequest, MetadataRequest, SigningRequest } from '
import { WalletConnectNotSupportRequest, WalletConnectSessionRequest } from '@subwallet/extension-base/services/wallet-connect-service/types';
import { detectTranslate } from '@subwallet/extension-base/utils';
import { AlertModal } from '@subwallet/extension-koni-ui/components';
import { NEED_SIGN_CONFIRMATION } from '@subwallet/extension-koni-ui/constants';
import { isProductionMode, NEED_SIGN_CONFIRMATION } from '@subwallet/extension-koni-ui/constants';
import { useAlert, useConfirmationsInfo, useSelector } from '@subwallet/extension-koni-ui/hooks';
import { ConfirmationType } from '@subwallet/extension-koni-ui/stores/base/RequestState';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import { isRawPayload } from '@subwallet/extension-koni-ui/utils';
import { AccountSignMode, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { getSignMode, isRawPayload } from '@subwallet/extension-koni-ui/utils';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import styled from 'styled-components';

import { SignerPayloadJSON } from '@polkadot/types/types';
import { isEthereumAddress } from '@polkadot/util-crypto';

import { ConfirmationHeader } from './parts';
import { AddNetworkConfirmation, AddTokenConfirmation, AuthorizeConfirmation, ConnectWalletConnectConfirmation, EvmSignatureConfirmation, EvmTransactionConfirmation, MetadataConfirmation, NotSupportConfirmation, NotSupportWCConfirmation, SignConfirmation, TransactionConfirmation } from './variants';
Expand Down Expand Up @@ -91,7 +92,15 @@ const Component = function ({ className }: Props) {
isMessage = confirmation.type === 'evmSignatureRequest';
}

if (account?.isReadOnly || !canSign) {
const signMode = getSignMode(account);
const isEvm = isEthereumAddress(account?.address);

const notSupport = signMode === AccountSignMode.READ_ONLY ||
signMode === AccountSignMode.UNKNOWN ||
(signMode === AccountSignMode.QR && isEvm && isProductionMode) ||
!canSign;

if (notSupport) {
return (
<NotSupportConfirmation
account={account}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import QrScannerErrorNotice from '@subwallet/extension-koni-ui/components/Qr/Scanner/ErrorNotice';
import { CONFIRMATION_SCAN_MODAL } from '@subwallet/extension-koni-ui/constants/modal';
import { SigData, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { ScannerResult } from '@subwallet/extension-koni-ui/types/scanner';
import { ScannerResult, SigData, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { ModalContext, SwQrScanner } from '@subwallet/react-ui';
import React, { useCallback, useContext, useState } from 'react';
import { useTranslation } from 'react-i18next';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { ConfirmationResult } from '@subwallet/extension-base/background/KoniTypes';
import { AccountJson, ConfirmationRequestBase } from '@subwallet/extension-base/background/types';
import { detectTranslate } from '@subwallet/extension-base/utils';
import { AccountItemWithName, ConfirmationGeneralInfo } from '@subwallet/extension-koni-ui/components';
import { NEED_SIGN_CONFIRMATION } from '@subwallet/extension-koni-ui/constants';
import { useGetAccountTitleByAddress } from '@subwallet/extension-koni-ui/hooks';
Expand All @@ -11,7 +12,7 @@ import { EvmSignatureSupportType, ThemeProps } from '@subwallet/extension-koni-u
import { Button } from '@subwallet/react-ui';
import CN from 'classnames';
import React, { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Trans, useTranslation } from 'react-i18next';
import styled from 'styled-components';

interface Props extends ThemeProps {
Expand Down Expand Up @@ -73,9 +74,15 @@ const Component: React.FC<Props> = (props: Props) => {
{ isMessage ? t('Signature required') : t('Transaction request')}
</div>
<div className='description'>
<span>{t('This feature is not available for')}</span>
<span className='highlight'>&nbsp;{accountTitle}</span>
<span>.&nbsp;{t('Please change to another account type.')}</span>
<Trans
components={{
highlight: (
<span className='highlight' />
)
}}
i18nKey={detectTranslate('Feature not available for <highlight>{{accountTitle}}</highlight>. Change to another account type and try again.')}
values={{ accountTitle }}
/>
</div>
<AccountItemWithName
accountName={account?.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { _ChainAsset } from '@subwallet/chain-list/types';
import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
import { NotificationType } from '@subwallet/extension-base/background/KoniTypes';
import { ExtrinsicType, NotificationType } from '@subwallet/extension-base/background/KoniTypes';
import { _getAssetDecimals, _getAssetOriginChain, _getAssetSymbol, _getChainNativeTokenSlug, _getOriginChainOfAsset, _isChainEvmCompatible, _parseAssetRefKey } from '@subwallet/extension-base/services/chain-service/utils';
import { getSwapAlternativeAsset } from '@subwallet/extension-base/services/swap-service/utils';
import { SWTransactionResponse } from '@subwallet/extension-base/services/transaction-service/types';
Expand All @@ -15,7 +15,7 @@ import { AddMoreBalanceModal, ChooseFeeTokenModal, SlippageModal, SwapIdleWarnin
import { QuoteResetTime, SwapRoute } from '@subwallet/extension-koni-ui/components/Swap';
import { BN_TEN, BN_ZERO, CONFIRM_SWAP_TERM, DEFAULT_SWAP_PARAMS, SWAP_ALL_QUOTES_MODAL, SWAP_CHOOSE_FEE_TOKEN_MODAL, SWAP_IDLE_WARNING_MODAL, SWAP_MORE_BALANCE_MODAL, SWAP_SLIPPAGE_MODAL, SWAP_TERMS_OF_SERVICE_MODAL } from '@subwallet/extension-koni-ui/constants';
import { DataContext } from '@subwallet/extension-koni-ui/contexts/DataContext';
import { useChainConnection, useGetChainPrefixBySlug, useNotification, useSelector, useSetCurrentPage, useTransactionContext, useWatchTransaction } from '@subwallet/extension-koni-ui/hooks';
import { useChainConnection, useGetChainPrefixBySlug, useNotification, usePreCheckAction, useSelector, useSetCurrentPage, useTransactionContext, useWatchTransaction } from '@subwallet/extension-koni-ui/hooks';
import { getLatestSwapQuote, handleSwapRequest, handleSwapStep, validateSwapProcess } from '@subwallet/extension-koni-ui/messaging/transaction/swap';
import { FreeBalance, FreeBalanceToEarn, TransactionContent, TransactionFooter } from '@subwallet/extension-koni-ui/Popup/Transaction/parts';
import { DEFAULT_SWAP_PROCESS, SwapActionType, swapReducer } from '@subwallet/extension-koni-ui/reducer';
Expand Down Expand Up @@ -144,6 +144,7 @@ const Component = () => {
const chainValue = useWatchTransaction('chain', form, defaultData);
const recipientValue = useWatchTransaction('recipient', form, defaultData);
const { checkChainConnected, turnOnChain } = useChainConnection();
const onPreCheck = usePreCheckAction(fromValue);

const [processState, dispatchProcessState] = useReducer(swapReducer, DEFAULT_SWAP_PROCESS);

Expand Down Expand Up @@ -1375,7 +1376,7 @@ const Component = () => {
className={'__swap-submit-button'}
disabled={submitLoading || handleRequestLoading || isNotConnectedAltChain}
loading={submitLoading}
onClick={form.submit}
onClick={onPreCheck(form.submit, ExtrinsicType.SWAP)}
>
{t('Swap')}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { CloseIcon, Layout, QrScannerErrorNotice, WalletConnect } from '@subwall
import { TIME_OUT_RECORD } from '@subwallet/extension-koni-ui/constants';
import { useDefaultNavigate, useOpenQrScanner } from '@subwallet/extension-koni-ui/hooks';
import { addConnection } from '@subwallet/extension-koni-ui/messaging';
import { FormCallbacks, Theme, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { ScannerResult } from '@subwallet/extension-koni-ui/types/scanner';
import { FormCallbacks, ScannerResult, Theme, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { noop, validWalletConnectUri } from '@subwallet/extension-koni-ui/utils';
import { Button, Form, Icon, Input, ModalContext, PageIcon, SwModal, SwQrScanner } from '@subwallet/react-ui';
import CN from 'classnames';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import { AbstractAddressJson } from '@subwallet/extension-base/background/types';
import { CHAINS_SUPPORTED_DOMAIN, isAzeroDomain } from '@subwallet/extension-base/koni/api/dotsama/domain';
import { reformatAddress } from '@subwallet/extension-base/utils';
import { AddressBookModal } from '@subwallet/extension-koni-ui/components';
import { useForwardInputRef, useOpenQrScanner, useSelector, useTranslation } from '@subwallet/extension-koni-ui/hooks';
import { resolveAddressToDomain, resolveDomainToAddress, saveRecentAccount } from '@subwallet/extension-koni-ui/messaging';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import { ScannerResult } from '@subwallet/extension-koni-ui/types/scanner';
import { ScannerResult, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { findContactByAddress, toShort } from '@subwallet/extension-koni-ui/utils';
import { Button, Icon, Input, InputRef, ModalContext, SwQrScanner } from '@subwallet/react-ui';
import CN from 'classnames';
Expand All @@ -19,6 +17,7 @@ import styled from 'styled-components';
import { decodeAddress, isAddress, isEthereumAddress } from '@polkadot/util-crypto';

import { Avatar } from '../Avatar';
import { AddressBookModal } from '../Modal';
import { QrScannerErrorNotice } from '../Qr';
import { BasicInputWrapper } from './Base';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { CMD, CRYPTO_SR25519, ETHEREUM_ID, SUBSTRATE_ID } from '@subwallet/extension-koni-ui/constants';
import { CMD, CRYPTO_ETHEREUM, CRYPTO_SR25519, ETHEREUM_ID, SUBSTRATE_ID } from '@subwallet/extension-koni-ui/constants';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import React, { useMemo } from 'react';
import styled from 'styled-components';

import { numberToU8a, u8aConcat, u8aToU8a } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import { decodeAddress, isEthereumAddress } from '@polkadot/util-crypto';

import BytesQr from './BytesQr';

Expand Down Expand Up @@ -45,10 +45,10 @@ const Component = (props: Props) => {
if (isEthereum) {
return u8aConcat(ETHEREUM_ID, numberToU8a(cmd), decodeAddress(address), u8aToU8a(payload));
} else {
// EVM genesisHash have _evm or _anyString at end
const genesis = genesisHash.split('_')[0];
const isEvm = isEthereumAddress(address);
const crypto = isEvm ? CRYPTO_ETHEREUM : CRYPTO_SR25519;

return u8aConcat(SUBSTRATE_ID, CRYPTO_SR25519, new Uint8Array([cmd]), decodeAddress(address), u8aToU8a(payload), u8aToU8a(genesis));
return u8aConcat(SUBSTRATE_ID, crypto, new Uint8Array([cmd]), decodeAddress(address), u8aToU8a(payload), u8aToU8a(genesisHash));
}
}, [address, cmd, payload, genesisHash, isEthereum]);

Expand Down
7 changes: 7 additions & 0 deletions packages/extension-koni-ui/src/constants/enviroment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

const PRODUCTION_BRANCHES = ['master', 'webapp', 'webapp-dev'];
const branchName = process.env.BRANCH_NAME || 'subwallet-dev';

export const isProductionMode = PRODUCTION_BRANCHES.indexOf(branchName) > -1;
2 changes: 2 additions & 0 deletions packages/extension-koni-ui/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
export * from './account';
export * from './buy';
export * from './common';
export * from './enviroment';
export * from './error';
export * from './history';
export * from './ledger';
export * from './localStorage';
Expand Down
1 change: 1 addition & 0 deletions packages/extension-koni-ui/src/constants/qr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const STANDARD_FRAME_SIZE = 2 ** 8;
export const ETHEREUM_ID = new Uint8Array([0x45]);
export const SUBSTRATE_ID = new Uint8Array([0x53]);
export const CRYPTO_SR25519 = new Uint8Array([0x01]);
export const CRYPTO_ETHEREUM = new Uint8Array([0x03]);
export const CMD = {
ETHEREUM: {
SIGN_HASH: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { AccountSignMode } from '@subwallet/extension-koni-ui/types';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { isEthereumAddress } from '@polkadot/util-crypto';

const useGetAccountTitleByAddress = (address?: string): string => {
const { t } = useTranslation();

const signMode = useGetAccountSignModeByAddress(address);
const isEvm = useMemo(() => isEthereumAddress(address || ''), [address]);

return useMemo((): string => {
switch (signMode) {
Expand All @@ -19,15 +22,21 @@ const useGetAccountTitleByAddress = (address?: string): string => {
return t('All account');
case AccountSignMode.PASSWORD:
return t('Normal account');

case AccountSignMode.QR:
return t('QR signer account');
if (isEvm) {
return t('EVM QR signer account');
} else {
return t('Substrate QR signer account');
}

case AccountSignMode.READ_ONLY:
return t('Watch-only account');
case AccountSignMode.UNKNOWN:
default:
return t('Unknown account');
}
}, [signMode, t]);
}, [signMode, t, isEvm]);
};

export default useGetAccountTitleByAddress;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
import { AccountJson } from '@subwallet/extension-base/background/types';
import { detectTranslate } from '@subwallet/extension-base/utils';
import { ALL_STAKING_ACTIONS, isLedgerCapable, ledgerIncompatible } from '@subwallet/extension-koni-ui/constants';
import { ALL_STAKING_ACTIONS, isLedgerCapable, isProductionMode, ledgerIncompatible } from '@subwallet/extension-koni-ui/constants';
import { BLOCK_ACTION_LEDGER_NETWORKS, PredefinedLedgerNetwork } from '@subwallet/extension-koni-ui/constants/ledger';
import { AccountSignMode } from '@subwallet/extension-koni-ui/types';
import { getSignMode } from '@subwallet/extension-koni-ui/utils';
Expand Down Expand Up @@ -54,6 +54,7 @@ const usePreCheckAction = (address?: string, blockAllAccount = true, message?: s
let block = false;
let accountTitle = getAccountTypeTitle(account);
let defaultMessage = detectTranslate('The account you are using is {{accountTitle}}, you cannot use this feature with it');
const isEthereumAccount = isEthereumAddress(account.address);

switch (mode) {
case AccountSignMode.READ_ONLY:
Expand All @@ -72,6 +73,13 @@ const usePreCheckAction = (address?: string, blockAllAccount = true, message?: s
defaultMessage = detectTranslate('You are using a {{accountTitle}}. Earning is not supported with this account type');
}

if (mode === AccountSignMode.QR) {
if (isEthereumAccount && isProductionMode) {
accountTitle = t('EVM QR signer account');
block = true;
}
}

if (mode === AccountSignMode.LEDGER) {
if (!isLedgerCapable) {
notify({
Expand All @@ -84,7 +92,6 @@ const usePreCheckAction = (address?: string, blockAllAccount = true, message?: s
}

const networkBlock: string[] = BLOCK_ACTION_LEDGER_NETWORKS[action] || [];
const isEthereumAccount = isEthereumAddress(account.address);

if (networkBlock.includes('*')) { // Block all network
block = true;
Expand Down
17 changes: 13 additions & 4 deletions packages/extension-koni-ui/src/hooks/qr/useScanAccountQr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import useOpenQrScanner from '@subwallet/extension-koni-ui/hooks/qr/useOpenQrScanner';
import { ValidateState } from '@subwallet/extension-koni-ui/types';
import { QrAccount, ScannerResult } from '@subwallet/extension-koni-ui/types/scanner';
import { isProductionMode } from '@subwallet/extension-koni-ui/constants';
import { QrAccount, ScannerResult, ValidateState } from '@subwallet/extension-koni-ui/types';
import { useCallback, useMemo } from 'react';

import { useTranslation } from '../common';
import useOpenQrScanner from './useOpenQrScanner';

const useScanAccountQr = (
modalId: string,
Expand All @@ -21,7 +21,16 @@ const useScanAccountQr = (
const result = convertResult(val);

if (result) {
return result;
if (isProductionMode && result.isEthereum) {
setValidateState({
message: t('Invalid QR code. EVM networks are not supported'),
status: 'error'
});

return null;
} else {
return result;
}
} else {
setValidateState({
message: t('Invalid QR code'),
Expand Down
3 changes: 3 additions & 0 deletions packages/extension-koni-ui/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ export * from './buy';
export * from './chain';
export * from './confirmation';
export * from './crowdloan';
export * from './earning';
export * from './form';
export * from './history';
export * from './hook';
export * from './ledger';
export * from './missionPool';
export * from './navigation';
export * from './scanner';
export * from './staking';
export * from './transaction';
export * from './walletConnect';
Expand Down
4 changes: 3 additions & 1 deletion packages/extension-koni-ui/src/types/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

import { Transaction } from '@subwallet/extension-base/utils/eth';
import { ScannerResult as _ScannerResult } from '@subwallet/react-ui/es/sw-qr-scanner';

import { GenericExtrinsicPayload } from '@polkadot/types';

export { ScannerResult } from '@subwallet/react-ui/es/sw-qr-scanner';
export type ScannerResult = _ScannerResult;

export interface Frames {
completedFramesCount: number;
isMultipart: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-koni-ui/src/utils/scanner/attach.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { ETHEREUM_PREFIX, SCAN_TYPE, SECRET_PREFIX, SUBSTRATE_PREFIX } from '@subwallet/extension-koni-ui/constants/qr';
import { QrAccount } from '@subwallet/extension-koni-ui/types/scanner';
import { ETHEREUM_PREFIX, SCAN_TYPE, SECRET_PREFIX, SUBSTRATE_PREFIX } from '@subwallet/extension-koni-ui/constants';
import { QrAccount } from '@subwallet/extension-koni-ui/types';

import { isHex } from '@polkadot/util';
import { decodeAddress, encodeAddress, isEthereumAddress } from '@polkadot/util-crypto';
Expand Down
Loading

1 comment on commit 67f2dde

@saltict
Copy link
Author

Choose a reason for hiding this comment

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

Please sign in to comment.