Skip to content

Commit

Permalink
feat: get chain using the address (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
AMIRKHANEF authored Nov 25, 2024
1 parent cbe564b commit 1fa7798
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useApiWithChain, useTranslation } from '../../../hooks';
import { createAccountExternal, getMetadata } from '../../../messaging';
import { HeaderBrand, Name } from '../../../partials';
import getLogo from '../../../util/getLogo';
import { addressToChain, getSubstrateAddress } from '../../../util/utils';

export default function AddAddressOnly (): React.ReactElement {
const { t } = useTranslation();
Expand All @@ -35,6 +36,12 @@ export default function AddAddressOnly (): React.ReactElement {

const disabledItems = useMemo(() => (['Allow use on any chain']), []);

const getChain = useCallback((genesisHash: string) => {
getMetadata(genesisHash, true).then(setChain).catch((error): void => {
console.error(error);
});
}, []);

useEffect(() => {
cryptoWaitReady().then(() => {
keyring.loadAll({ store: new AccountsStore() });
Expand All @@ -46,39 +53,50 @@ export default function AddAddressOnly (): React.ReactElement {
}, [realAddress, chain]);

useEffect(() => {
realAddress && api?.query['proxy']?.['proxies'](realAddress).then((proxies) => {
const fetchedProxyItems = (JSON.parse(JSON.stringify((proxies as unknown as unknown[])?.[0])) as Proxy[])?.map((p: Proxy) => ({ proxy: p, status: 'current' })) as ProxyItem[];
if (realAddress) {
const addressChain = addressToChain(realAddress);

setProxies(fetchedProxyItems);
});
}, [api, chain, realAddress]);
addressChain?.genesisHash && getChain(addressChain.genesisHash);

api?.query['proxy']?.['proxies'](realAddress)
.then((proxies) => {
const fetchedProxyItems = (JSON.parse(JSON.stringify((proxies as unknown as unknown[])?.[0])) as Proxy[])?.map((p: Proxy) => ({ proxy: p, status: 'current' })) as ProxyItem[];

setProxies(fetchedProxyItems);
})
.catch(console.error);
}
}, [api, getChain, realAddress]);

const goHome = useCallback(() => onAction('/'), [onAction]);
const goToAccountDetail = useCallback((genesisHash: string, address: string) => onAction(`/account/${genesisHash}/${address}/`), [onAction]);

const onNameChange = useCallback((name: string | null) => setName(name), []);

const onChangeGenesis = useCallback((genesisHash?: string | null): void => {
setProxies(undefined);
genesisHash && getMetadata(genesisHash, true).then(setChain).catch((error): void => {
console.error(error);
});
}, []);
genesisHash && getChain(genesisHash);
}, [getChain]);

const handleAdd = useCallback(() => {
if (name && realAddress && chain?.genesisHash) {
setIsBusy(true);

const substrateAddress = getSubstrateAddress(realAddress);

createAccountExternal(name, realAddress, chain.genesisHash as HexString)
.then(() => {
setStorage('profile', PROFILE_TAGS.WATCH_ONLY).catch(console.error);
goHome();
})
.finally(() => chain.genesisHash && substrateAddress
? goToAccountDetail(chain.genesisHash, substrateAddress)
: goHome)
.catch((error: Error) => {
setIsBusy(false);
console.error(error);
});
}
}, [chain?.genesisHash, goHome, name, realAddress]);
}, [chain?.genesisHash, goHome, goToAccountDetail, name, realAddress]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useApiWithChain, useFullscreen, useTranslation } from '../../../hooks';
import { createAccountExternal, getMetadata } from '../../../messaging';
import { Name } from '../../../partials';
import getLogo from '../../../util/getLogo';
import { addressToChain, getSubstrateAddress } from '../../../util/utils';

export interface AccountInfo {
address: string;
Expand All @@ -48,6 +49,12 @@ export default function AddWatchOnlyFullScreen (): React.ReactElement {

const disabledItems = useMemo(() => (['Allow use on any chain']), []);

const getChain = useCallback((genesisHash: string) => {
getMetadata(genesisHash, true).then(setChain).catch((error): void => {
console.error(error);
});
}, []);

useEffect(() => {
// eslint-disable-next-line no-void
void cryptoWaitReady().then(() => {
Expand All @@ -60,23 +67,36 @@ export default function AddWatchOnlyFullScreen (): React.ReactElement {
}, [realAddress, chain]);

useEffect(() => {
realAddress && api?.query['proxy']?.['proxies'](realAddress).then((proxies) => {
// @ts-expect-error will be resolved later
const fetchedProxyItems = (JSON.parse(JSON.stringify(proxies[0])))?.map((p: Proxy) => ({ proxy: p, status: 'current' })) as ProxyItem[];
if (realAddress) {
const addressChain = addressToChain(realAddress);

setProxies(fetchedProxyItems);
});
}, [api, chain, realAddress]);
addressChain?.genesisHash && getChain(addressChain.genesisHash);

api?.query['proxy']?.['proxies'](realAddress)
.then((proxies) => {
// @ts-expect-error will be resolved later
const fetchedProxyItems = (JSON.parse(JSON.stringify(proxies[0])) as Proxy[])?.map((p: Proxy) => ({ proxy: p, status: 'current' })) as ProxyItem[];

setProxies(fetchedProxyItems);
})
.catch(console.error);
}
}, [api, getChain, realAddress]);

const onAdd = useCallback(() => {
if (name && realAddress && chain?.genesisHash) {
setIsBusy(true);

const substrateAddress = getSubstrateAddress(realAddress);

createAccountExternal(name, realAddress, chain.genesisHash as HexString)
.then(() => {
setStorage('profile', PROFILE_TAGS.WATCH_ONLY).catch(console.error);
openOrFocusTab('/', true);
})
.finally(() => substrateAddress
? openOrFocusTab(`/accountfs/${substrateAddress}/0`, true)
: openOrFocusTab('/', true)
)
.catch((error: Error) => {
setIsBusy(false);
console.error(error);
Expand All @@ -90,10 +110,8 @@ export default function AddWatchOnlyFullScreen (): React.ReactElement {

const onChangeGenesis = useCallback((genesisHash?: string | null): void => {
setProxies(undefined);
genesisHash && getMetadata(genesisHash, true).then(setChain).catch((error): void => {
console.error(error);
});
}, []);
genesisHash && getChain(genesisHash);
}, [getChain]);

return (
<Grid bgcolor='backgroundFL.primary' container item justifyContent='center'>
Expand All @@ -108,7 +126,7 @@ export default function AddWatchOnlyFullScreen (): React.ReactElement {
logo={
<VaadinIcon icon='vaadin:tag' style={{ color: `${theme.palette.text.primary}`, height: '25px', width: '25px' }} />
}
text= {t('Add Watch-Only Account')}
text={t('Add Watch-Only Account')}
/>
<Typography fontSize='16px' fontWeight={400} width='100%'>
{t('Enter the watch-only address. It can also serve as a proxied account, but without transaction signing. A proxy account in the extension is needed for signing.')}
Expand Down
29 changes: 27 additions & 2 deletions packages/extension-polkagate/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { BN, BN_TEN, BN_ZERO, hexToBn, hexToString, hexToU8a, isHex, stringToU8a
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';

import { EXTRA_PRICE_IDS } from './api/getPrices';
import { ASSET_HUBS, BLOCK_RATE, FLOATING_POINT_DIGIT, INITIAL_RECENT_CHAINS_GENESISHASH, PROFILE_COLORS, RELAY_CHAINS_GENESISHASH, SHORT_ADDRESS_CHARACTERS } from './constants';
import allChains from './chains';
import { ASSET_HUBS, BLOCK_RATE, FLOATING_POINT_DIGIT, INITIAL_RECENT_CHAINS_GENESISHASH, PROFILE_COLORS, RELAY_CHAINS_GENESISHASH, SHORT_ADDRESS_CHARACTERS, WESTEND_GENESIS_HASH } from './constants';

interface Meta {
docs: Text[];
Expand Down Expand Up @@ -577,4 +578,28 @@ export const decodeMultiLocation = (hexString: HexString) => {
}

return decodeHexValues(decodedMultiLocation);
};
};

export const addressToChain = (address: string) => {
if (!isValidAddress(address)) {
console.log('Not a valid address');

return null;
}

if (getSubstrateAddress(address) === address) {
return {
chainName: 'Westend',
genesisHash: WESTEND_GENESIS_HASH
};
}

const publicKey = decodeAddress(address, true);

const chain = allChains.find(({ ss58Format }) => encodeAddress(publicKey, ss58Format) === address);

return {
chainName: chain?.chain,
genesisHash: chain?.genesisHash
};
};

0 comments on commit 1fa7798

Please sign in to comment.