Skip to content

Commit

Permalink
Merge branch 'subwallet-dev' into koni/dev/issue-2982_new
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed Jul 30, 2024
2 parents 57fda94 + 5c51ff1 commit 0f5a3c3
Show file tree
Hide file tree
Showing 29 changed files with 442 additions and 102 deletions.
8 changes: 8 additions & 0 deletions packages/extension-base/src/background/KoniTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ export interface RequestAccountCreateHardwareV2 {
address: string;
addressOffset: number;
genesisHash: string;
originGenesisHash: string;
hardwareType: string;
name: string;
isAllowed?: boolean;
Expand All @@ -977,6 +978,7 @@ export interface CreateHardwareAccountItem {
address: string;
addressOffset: number;
genesisHash: string;
originGenesisHash: string;
hardwareType: string;
name: string;
isEthereum: boolean;
Expand Down Expand Up @@ -1415,10 +1417,16 @@ export interface LedgerNetwork {
isGeneric: boolean;
/** Use for evm account */
isEthereum: boolean;
/** Hide networks that are supported by the dot migration app */
isHide?: boolean;
/** Slip44 in the derivation path */
slip44: number;
}

export interface MigrationLedgerNetwork extends Omit<LedgerNetwork, 'isGeneric' | 'isEthereum' | 'isDevMode' | 'icon' > {
ss58_addr_type: number
}

/// Qr Sign

// Parse Substrate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2266,13 +2266,14 @@ export default class KoniExtension {
genesisHash,
hardwareType,
isAllowed,
name }: RequestAccountCreateHardwareV2): Promise<boolean> {
name,
originGenesisHash }: RequestAccountCreateHardwareV2): Promise<boolean> {
const key = keyring.addHardware(address, hardwareType, {
accountIndex,
addressOffset,
genesisHash,
name,
originGenesisHash: genesisHash
originGenesisHash
});

const result = key.pair;
Expand Down Expand Up @@ -2305,7 +2306,7 @@ export default class KoniExtension {
const slugMap: Record<string, string> = {};

for (const account of accounts) {
const { accountIndex, address, addressOffset, genesisHash, hardwareType, isEthereum, isGeneric, name } = account;
const { accountIndex, address, addressOffset, genesisHash, hardwareType, isEthereum, isGeneric, name, originGenesisHash } = account;

let result: KeyringPair;

Expand All @@ -2315,7 +2316,7 @@ export default class KoniExtension {
accountIndex,
addressOffset,
genesisHash,
originGenesisHash: genesisHash,
originGenesisHash,
isGeneric
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,9 @@ export default class BifrostLiquidStakingPoolHandler extends BaseLiquidStakingPo
}
}

const unstakingList: UnstakingInfo[] = [];

useAddresses.forEach((address) => {
const formattedAddress = reformatAddress(address);
const unstakingList: UnstakingInfo[] = [];

const bnActiveBalance = activeBalanceMap[formattedAddress];
const unlockings = unlockingMap[formattedAddress];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class MigrateLedgerAccountV2 extends BaseMigrationJob {
const store = new AccountsStore();

const update = (key: string, value: KeyringJson) => {
if (key.startsWith('account:') && value.meta && isString(value.meta?.originGenesisHash)) {
if (key.startsWith('account:') && value.meta && isString(value.meta?.genesisHash)) {
const newValue = { ...value };

if (value.meta.isHardware) {
Expand All @@ -25,11 +25,11 @@ export default class MigrateLedgerAccountV2 extends BaseMigrationJob {
if (isEther) {
newValue.meta.isGeneric = true;
} else {
newValue.meta.isGeneric = !newValue.meta.originGenesisHash;
newValue.meta.isGeneric = !newValue.meta.genesisHash;
}
}

newValue.meta.availableGenesisHashes = [value.meta.originGenesisHash];
newValue.meta.availableGenesisHashes = [value.meta.genesisHash];
store.set(key, newValue);
}
};
Expand Down
85 changes: 68 additions & 17 deletions packages/extension-koni-ui/src/Popup/Account/ConnectLedger.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { LedgerNetwork } from '@subwallet/extension-base/background/KoniTypes';
import { LedgerNetwork, MigrationLedgerNetwork } from '@subwallet/extension-base/background/KoniTypes';
import { reformatAddress } from '@subwallet/extension-base/utils';
import { AccountItemWithName, AccountWithNameSkeleton, BasicOnChangeFunction, ChainSelector, CloseIcon, DualLogo, Layout, PageWrapper } from '@subwallet/extension-koni-ui/components';
import { ATTACH_ACCOUNT_MODAL } from '@subwallet/extension-koni-ui/constants';
import { ATTACH_ACCOUNT_MODAL, SUBSTRATE_MIGRATION_KEY } from '@subwallet/extension-koni-ui/constants';
import { useAutoNavigateToCreatePassword, useCompleteCreateAccount, useDefaultNavigate, useGetSupportedLedger, useGoBackFromCreateAccount, useLedger } from '@subwallet/extension-koni-ui/hooks';
import { createAccountHardwareMultiple } from '@subwallet/extension-koni-ui/messaging';
import { RootState } from '@subwallet/extension-koni-ui/stores';
Expand All @@ -27,6 +27,10 @@ interface ImportLedgerItem {
name: string;
}

export const funcSortByName = (a: ChainItemType, b: ChainItemType) => {
return ((a?.name || '').toLowerCase() > (b?.name || '').toLowerCase()) ? 1 : -1;
};

const LIMIT_PER_PAGE = 5;

const FooterIcon = (
Expand All @@ -44,18 +48,26 @@ const Component: React.FC<Props> = (props: Props) => {
const { t } = useTranslation();
const { goHome } = useDefaultNavigate();

const supportedLedger = useGetSupportedLedger();
const [supportedLedger, migrateSupportLedger] = useGetSupportedLedger();
const onComplete = useCompleteCreateAccount();
const onBack = useGoBackFromCreateAccount(ATTACH_ACCOUNT_MODAL);

const { accounts } = useSelector((state: RootState) => state.accountState);

const networks = useMemo((): ChainItemType[] => supportedLedger.map((network) => ({
name: !network.isGeneric ? network.networkName.replace(' network', '') : network.networkName,
const networks = useMemo((): ChainItemType[] => supportedLedger
.filter(({ isHide }) => !isHide)
.map((network) => ({
name: !network.isGeneric ? network.networkName.replace(' network', '') : network.networkName,
slug: network.slug
})), [supportedLedger]);

const networkMigrates = useMemo((): ChainItemType[] => migrateSupportLedger.map((network) => ({
name: network.networkName.replace(' network', ''),
slug: network.slug
})), [supportedLedger]);
})).sort(funcSortByName), [migrateSupportLedger]);

const [chain, setChain] = useState(supportedLedger[0].slug);
const [chainMigrateMode, setChainMigrateMode] = useState<string | undefined>();
const [ledgerAccounts, setLedgerAccounts] = useState<Array<ImportLedgerItem | null>>([]);
const [firstStep, setFirstStep] = useState(ledgerAccounts.length === 0);
const [page, setPage] = useState(0);
Expand All @@ -67,9 +79,19 @@ const Component: React.FC<Props> = (props: Props) => {
return supportedLedger.find((n) => n.slug === chain);
}, [chain, supportedLedger]);

const selectedChainMigrateMode = useMemo((): MigrationLedgerNetwork | undefined => {
return migrateSupportLedger.find((n) => n.slug === chainMigrateMode);
}, [chainMigrateMode, migrateSupportLedger]);

const accountName = useMemo(() => selectedChain?.accountName || 'Unknown', [selectedChain]);

const { error, getAllAddress, isLoading, isLocked, ledger, refresh, warning } = useLedger(chain);
const accountMigrateNetworkName = useMemo(() => {
const selectedChain = migrateSupportLedger.find((n) => n.slug === chainMigrateMode);

return chainMigrateMode && selectedChain ? `${selectedChain.accountName}` : '';
}, [chainMigrateMode, migrateSupportLedger]);

const { error, getAllAddress, isLoading, isLocked, ledger, refresh, warning } = useLedger(chain, true, false, false, selectedChainMigrateMode?.genesisHash);

const onPreviousStep = useCallback(() => {
setFirstStep(true);
Expand All @@ -79,7 +101,19 @@ const Component: React.FC<Props> = (props: Props) => {
const onChainChange: BasicOnChangeFunction = useCallback((event) => {
const value = event.target.value;

if (value === SUBSTRATE_MIGRATION_KEY) {
setChainMigrateMode(networkMigrates[0].slug);
} else {
setChainMigrateMode(undefined);
}

setChain(value);
}, [networkMigrates]);

const onMigrateChainChange: BasicOnChangeFunction = useCallback((event) => {
const value = event.target.value;

setChainMigrateMode(value);
}, []);

const onLoadMore = useCallback(async () => {
Expand All @@ -103,7 +137,7 @@ const Component: React.FC<Props> = (props: Props) => {
(await getAllAddress(start, end)).forEach(({ address }, index) => {
rs[start + index] = {
accountIndex: start + index,
name: `Ledger ${accountName} ${start + index + 1}`,
name: `Ledger ${accountMigrateNetworkName} ${accountMigrateNetworkName ? `(${accountName})` : accountName} ${start + index + 1}`,
address: address
};
});
Expand Down Expand Up @@ -132,7 +166,7 @@ const Component: React.FC<Props> = (props: Props) => {
});

loadingFlag.current = false;
}, [page, getAllAddress, accountName, refresh]);
}, [page, getAllAddress, accountName, accountMigrateNetworkName, refresh]);

const onNextStep = useCallback(() => {
setFirstStep(false);
Expand Down Expand Up @@ -173,12 +207,13 @@ const Component: React.FC<Props> = (props: Props) => {
const selected = !!selectedAccounts.find((it) => it.address === item.address);
const originAddress = reformatAddress(item.address, 42);

const disabled = !!accounts.find((acc) => acc.address === originAddress && acc.genesisHash === selectedChain?.genesisHash);
const existedAccount = accounts.find((acc) => acc.address === originAddress && acc.genesisHash === selectedChain?.genesisHash);
const disabled = !!existedAccount;

return (
<AccountItemWithName
accountName={item.name}
address={item.address}
address={chainMigrateMode ? originAddress : item.address}
className={CN({ disabled: disabled })}
direction='vertical'
genesisHash={selectedChain?.genesisHash}
Expand All @@ -189,7 +224,7 @@ const Component: React.FC<Props> = (props: Props) => {
/>
);
};
}, [accounts, onClickItem, selectedChain?.genesisHash]);
}, [accounts, chainMigrateMode, onClickItem, selectedChain?.genesisHash]);

const onSubmit = useCallback(() => {
if (!selectedAccounts.length || !selectedChain) {
Expand All @@ -205,6 +240,7 @@ const Component: React.FC<Props> = (props: Props) => {
address: item.address,
addressOffset: 0, // don't change
genesisHash: selectedChain.genesisHash,
originGenesisHash: selectedChainMigrateMode?.genesisHash || selectedChain.genesisHash,
hardwareType: 'ledger',
name: item.name,
isEthereum: selectedChain.isEthereum,
Expand All @@ -221,13 +257,13 @@ const Component: React.FC<Props> = (props: Props) => {
setIsSubmitting(false);
});
}, 300);
}, [selectedAccounts, selectedChain, onComplete]);
}, [selectedAccounts, selectedChain, selectedChainMigrateMode?.genesisHash, onComplete]);

useEffect(() => {
setSelectedAccounts([]);
setLedgerAccounts([]);
setPage(0);
}, [chain]);
}, [chain, chainMigrateMode]);

const isConnected = !isLocked && !isLoading && !!ledger;

Expand Down Expand Up @@ -279,11 +315,22 @@ const Component: React.FC<Props> = (props: Props) => {
</div>
<ChainSelector
items={networks}
label={t('Select network')}
label={t('Select Ledger app')}
onChange={onChainChange}
placeholder={t('Select network')}
placeholder={t('Select Ledger app')}
value={chain}
/>
{
!!chainMigrateMode && <ChainSelector
className={'ledger-chain-migrate-select'}
id={'migrate-chain-select-modal-id'}
items={networkMigrates}
label={t('Select network')}
onChange={onMigrateChainChange}
placeholder={t('Select network')}
value={chainMigrateMode}
/>
}
<Button
block={true}
className={CN('ledger-button', { connected: isConnected, loading: isLoading })}
Expand Down Expand Up @@ -355,7 +402,7 @@ const ConnectLedger = styled(Component)<Props>(({ theme: { token } }: Props) =>

'.container': {
padding: `${token.padding}px ${token.padding}px 0`,
overflow: 'hidden',
overflow: 'auto',
height: '100%',
display: 'flex',
flexDirection: 'column'
Expand Down Expand Up @@ -438,6 +485,10 @@ const ConnectLedger = styled(Component)<Props>(({ theme: { token } }: Props) =>
'.anticon': {
animation: 'spinner-loading 1s infinite linear'
}
},

'.ledger-chain-migrate-select': {
marginTop: token.marginXS - 2
}
};
});
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-koni-ui/src/Popup/Account/RestoreJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ const Component: React.FC<Props> = ({ className }: Props) => {

if (isKeyringPairs$Json(json)) {
const accounts: ResponseJsonGetAccountInfo[] = json.accounts.map((account) => {
const genesisHash: string = account.meta.originGenesisHash as string;
const genesisHash: string = account.meta.genesisHash as string;

let addressPrefix: number | undefined;

if (account.meta.originGenesisHash) {
if (account.meta.genesisHash) {
addressPrefix = findNetworkJsonByGenesisHash(chainInfoMap, genesisHash)?.substrateInfo?.addressPrefix;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/extension-koni-ui/src/Popup/BuyTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ function Component ({ className }: Props) {
const ledgerNetwork = useMemo((): string | undefined => {
const account = findAccountByAddress(accounts, selectedAddress);

if (account?.originGenesisHash) {
return findNetworkJsonByGenesisHash(chainInfoMap, account.originGenesisHash)?.slug;
if (account?.genesisHash) {
return findNetworkJsonByGenesisHash(chainInfoMap, account.genesisHash)?.slug;
}

return undefined;
Expand Down
5 changes: 2 additions & 3 deletions packages/extension-koni-ui/src/Popup/Confirmations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ConfirmationDefinitions, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
import { AccountJson, AuthorizeRequest, MetadataRequest, SigningRequest } from '@subwallet/extension-base/background/types';
import { WalletConnectNotSupportRequest, WalletConnectSessionRequest } from '@subwallet/extension-base/services/wallet-connect-service/types';
import { detectTranslate } from '@subwallet/extension-base/utils';
import { _isRuntimeUpdated, detectTranslate } from '@subwallet/extension-base/utils';
import { AlertModal } from '@subwallet/extension-koni-ui/components';
import { isProductionMode, NEED_SIGN_CONFIRMATION } from '@subwallet/extension-koni-ui/constants';
import { useAlert, useConfirmationsInfo, useSelector } from '@subwallet/extension-koni-ui/hooks';
Expand Down Expand Up @@ -44,7 +44,6 @@ const Component = function ({ className }: Props) {
const confirmation = confirmationQueue[index] || null;
const { t } = useTranslation();
const { alertProps, closeAlert, openAlert } = useAlert(alertModalId);

const { transactionRequest } = useSelector((state) => state.requestState);

const nextConfirmation = useCallback(() => {
Expand Down Expand Up @@ -82,7 +81,7 @@ const Component = function ({ className }: Props) {
const payload = request.request.payload as SignerPayloadJSON;

// Valid even with evm ledger account (evm - availableGenesisHashes is empty)
canSign = !!account.availableGenesisHashes?.includes(payload.genesisHash);
canSign = !!account.availableGenesisHashes?.includes(payload.genesisHash) || _isRuntimeUpdated(payload?.signedExtensions);
}
}
} else {
Expand Down
Loading

0 comments on commit 0f5a3c3

Please sign in to comment.