Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add comments, validate uuid field
  • Loading branch information
rekmarks committed May 24, 2023
commit 2aa85d52eaca23176efc5828ed99418cc475ed08
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const baseConfig = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 60.86,
branches: 61.49,
functions: 62.92,
lines: 64.55,
statements: 64.69,
lines: 64.82,
statements: 64.95,
},
},

Expand Down
14 changes: 13 additions & 1 deletion src/EIP6963.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ const getProviderInfo = () => ({
name: 'test',
icon: 'https://wallet.io/icon.svg',
walletId: 'testWalletId',
uuid: 'testUuid',
uuid: '1449211e-5560-4235-9ab1-582cbe2b165f',
});

describe('EIP6963', () => {
describe('announceProvider', () => {
it('throws if the UUID is invalid', () => {
['foo', null, undefined, Symbol('bar')].forEach((invalidUuid) => {
const provider: any = { name: 'test' };
const providerDetail = { info: getProviderInfo(), provider };
providerDetail.info.uuid = invalidUuid as any;

expect(() => announceProvider(providerDetail)).toThrow(
new Error('Invalid `uuid` field. Must be a v4 UUID.'),
);
});
});

it('should announce a provider', () => {
const provider: any = { name: 'test' };
const providerDetail = { info: getProviderInfo(), provider };
Expand Down
28 changes: 27 additions & 1 deletion src/EIP6963.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@ enum EIP6963EventNames {
}

/**
* Represents the assets needed to display a wallet
* Represents the assets needed to display and identify a wallet.
*/
export type EIP6963ProviderInfo = {
/**
* The global identifier for the wallet. SHOULD be in reverse domain format,
* e.g. `com.wallet.example`.
*/
walletId: string;
/**
* A locally unique identifier for the wallet. MUST be a v4 UUID.
*/
uuid: string;
/**
* The name of the wallet.
*/
name: string;
/**
* The URL of an icon for the wallet.
*/
icon: string;
};

/**
* Represents a provider and the information relevant for the dapp.
*/
export type EIP6963ProviderDetail = {
info: EIP6963ProviderInfo;
provider: BaseProvider;
Expand Down Expand Up @@ -58,6 +74,12 @@ export function requestProvider<HandlerReturnType>(
window.dispatchEvent(new Event(EIP6963EventNames.Request));
}

/**
* Courtesy https://github.com/thenativeweb/uuidv4/blob/bdcf3a3138bef4fb7c51f389a170666f9012c478/lib/uuidv4.ts#L5
*/
const UUID_V4_REGEX =
/(?:^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$)|(?:^0{8}-0{4}-0{4}-0{4}-0{12}$)/u;

/**
* Announces a provider by dispatching an {@link EIP6963AnnounceProviderEvent}, and
* listening for {@link EIP6963RequestProviderEvent} to re-announce.
Expand All @@ -70,6 +92,10 @@ export function announceProvider({
info,
provider,
}: EIP6963ProviderDetail): void {
if (typeof info.uuid !== 'string' || !UUID_V4_REGEX.test(info.uuid)) {
throw new Error('Invalid `uuid` field. Must be a v4 UUID.');
}

const _announceProvider = () =>
window.dispatchEvent(
new CustomEvent(EIP6963EventNames.Announce, {
Expand Down