Skip to content

Commit

Permalink
try/catch block to not crash explorer if account id format does not fit
Browse files Browse the repository at this point in the history
  • Loading branch information
piggydoughnut committed Sep 30, 2024
1 parent 28e994a commit 0312e07
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/react-components/src/AccountName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ function createNumMatcher (prefix: string, name: string, add?: string): AddrMatc
const minLength = test.length + 4;

return (addr: unknown): string | null => {
const u8a = isCodec(addr)
? addr.toU8a()
: statics.registry.createType('AccountId', addr as string).toU8a();

return (u8a.length >= minLength) && u8aEq(test, u8a.subarray(0, test.length)) && u8aEmpty(u8a.subarray(minLength))
? `${name} ${formatNumber(u8aToBn(u8a.subarray(test.length, minLength)))}${add ? ` (${add})` : ''}`
: null;
try {
const u8a = isCodec(addr)
? addr.toU8a()
: statics.registry.createType('AccountId', addr as string).toU8a();

return (u8a.length >= minLength) && u8aEq(test, u8a.subarray(0, test.length)) && u8aEmpty(u8a.subarray(minLength))
? `${name} ${formatNumber(u8aToBn(u8a.subarray(test.length, minLength)))}${add ? ` (${add})` : ''}`
: null;
} catch (e) {
console.log(e);

return null;
}
};
}

Expand Down Expand Up @@ -97,7 +103,7 @@ function defaultOrAddr (defaultName = '', _address: AccountId | AccountIndex | A
return [defaultName, false, false, false];
}

const [isAddressExtracted,, extracted] = getAddressName(accountId, null, defaultName);
const [isAddressExtracted, , extracted] = getAddressName(accountId, null, defaultName);
const accountIndex = (_accountIndex || '').toString() || indexCache.get(accountId);

if (isAddressExtracted && accountIndex) {
Expand All @@ -110,7 +116,7 @@ function defaultOrAddr (defaultName = '', _address: AccountId | AccountIndex | A
}

function defaultOrAddrNode (defaultName = '', address: AccountId | AccountIndex | Address | string | Uint8Array, accountIndex?: AccountIndex | null): React.ReactNode {
const [node,, isAddress] = defaultOrAddr(defaultName, address, accountIndex);
const [node, , isAddress] = defaultOrAddr(defaultName, address, accountIndex);

return isAddress
? <span className='isAddress'>{node}</span>
Expand Down

0 comments on commit 0312e07

Please sign in to comment.