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

Validate local storage data #105

Merged
merged 3 commits into from
May 14, 2024
Merged
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
28 changes: 27 additions & 1 deletion src/contexts/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Signer } from '@polkadot/api/types';
import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types';
import React, { createContext, useContext, useEffect, useReducer } from 'react';

import { isValidAddress } from '@/utils/functions';

const APP_NAME = 'Corehub';
const LOCAL_STORAGE_ACCOUNTS = 'accounts';
const LOCAL_STORAGE_ACTIVE_ACCOUNT = 'active-account';
Expand Down Expand Up @@ -137,6 +139,30 @@ const AccountProvider = ({ children }: Props) => {
getInjector();
}, [state.activeAccount]);

const validate = (jsonStr: string): boolean => {
const items = JSON.parse(jsonStr);
const len = items.length;

if (len === 0 || len === undefined) return false;

for (const item of items) {
const keys = ['address', 'type', 'meta'];
const metaKeys = ['genesisHash', 'name', 'source'];

for (const key of keys) if (!Object.hasOwn(item, key)) return false;
for (const key of Object.keys(item))
if (keys.indexOf(key) === -1) return false;

if (!isValidAddress(item.address)) return false;

if (!Object.hasOwn(item.meta, 'source')) return false;
for (const key of Object.keys(item.meta))
if (metaKeys.indexOf(key) === -1) return false;
}

return true;
};

useEffect(() => {
const asyncLoadAccounts = async () => {
const { web3Enable } = await import('@polkadot/extension-dapp');
Expand All @@ -146,8 +172,8 @@ const AccountProvider = ({ children }: Props) => {
if (!item) return;

try {
if (!validate(item)) return;
const accounts = JSON.parse(item) as InjectedAccountWithMeta[];
if (!accounts || accounts.length === 0) return;

let injectCounter = 0;
const injectedWeb3Interval = setInterval(() => {
Expand Down
Loading