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

Jl/caip multichain/add eip155 reference regex #4843

Open
wants to merge 2 commits into
base: caip-multichain
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions packages/multichain/src/scope/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type NonWalletKnownCaipNamespace = Exclude<
KnownCaipNamespace.Wallet
>;

// Regexes for references for non-wallet known CAIP namespaces
export const CaipReferenceRegexes: Record<NonWalletKnownCaipNamespace, RegExp> = {
eip155: /^(0|[1-9][0-9]*)$/
}

// Methods that do not belong to an ecosystem
export const KnownWalletRpcMethods: string[] = [
'wallet_registerOnboarding',
Expand Down
10 changes: 10 additions & 0 deletions packages/multichain/src/scope/supported.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ describe('Scope Support', () => {
false,
);
});

it('returns false for the ethereum namespace when the reference is malformed', () => {
const isChainIdSupportedMock = jest.fn().mockReturnValue(true);
expect(isSupportedScopeString('eip155:01', isChainIdSupportedMock)).toBe(
false,
);
expect(isSupportedScopeString('eip155:1e1', isChainIdSupportedMock)).toBe(
false,
);
});
});

describe('isSupportedAccount', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/multichain/src/scope/supported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CaipAccountId, Hex, CaipChainId } from '@metamask/utils';
import { KnownCaipNamespace, parseCaipAccountId } from '@metamask/utils';

import {
CaipReferenceRegexes,
KnownNotifications,
KnownRpcMethods,
KnownWalletNamespaceRpcMethods,
Expand All @@ -21,7 +22,7 @@ export const isSupportedScopeString = (
case KnownCaipNamespace.Wallet:
return !reference || reference === KnownCaipNamespace.Eip155;
case KnownCaipNamespace.Eip155:
return !reference || isChainIdSupported(toHex(reference));
return !reference || (CaipReferenceRegexes.eip155.test(reference) && isChainIdSupported(toHex(reference)));
default:
return false;
}
Expand Down
Loading