Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
fix invalid account imports (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidra authored Aug 23, 2023
1 parent 0769121 commit e878815
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/contexts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type { SigningAccount } from '../types';
import React, { useContext, createContext, useState, useEffect } from 'react';
import { extractChainInfo, useAppLifeCycle } from '../lifecycle/index.js';
import { useWallets } from './Wallets.js';
import {
isSubstrateAccount,
isAccountAllowedOnChain,
} from '../utils/polkadot-api.js';

export interface IAccountContext {
connectedAccount: SigningAccount | undefined;
Expand Down Expand Up @@ -58,9 +62,8 @@ const AccountProvider = ({ children }: { children: React.ReactNode }) => {
// filter accounts that match the genesis hash.
const accounts = (await wallet.getAccounts()).filter(
(account) =>
!account.genesisHash ||
!genesisHash ||
account.genesisHash === genesisHash
isSubstrateAccount(account) &&
isAccountAllowedOnChain(account, genesisHash)
);

if (accounts.length > 0) {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/polkadot-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { addressEq } from '@polkadot/util-crypto';
import BN from 'bn.js';
import { Conviction } from '../types';
import { DispatchError } from '@polkadot/types/interfaces';
import { Account } from '@polkadot-onboard/core';

const DEFAULT_OPTIONS = {
noInitWarn: true,
Expand Down Expand Up @@ -110,6 +111,21 @@ export function formatConviction(conviction: Conviction): string {
}
}

export function isSubstrateAccount(account: Account) {
return !!account?.type && ['ed25519', 'sr25519'].includes(account.type);
}

export function isAccountAllowedOnChain(
account: Account,
chainGenesisHash: string | undefined | null
) {
return (
!account.genesisHash ||
!chainGenesisHash ||
account.genesisHash === chainGenesisHash
);
}

export function isValidAddress(address: string): boolean {
try {
encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
Expand Down

0 comments on commit e878815

Please sign in to comment.