Skip to content

Commit ea2d469

Browse files
wenfixjiexiadonesky1
authored
fix: make MultichainCore's dapp name and url properties mandatory (#56)
* mark dapp's name and url property as non-optional * update changelog * simplify getDappId to use url first then fallback to name * Make dapp.url optional. Overwrite value if browser * throw if dapp name missing * fix type * changelog * changelog validation * changelog format fix --------- Co-authored-by: Jiexi Luan <jiexiluan@gmail.com> Co-authored-by: Alex Donesky <adonesky@gmail.com>
1 parent fe42517 commit ea2d469

5 files changed

Lines changed: 38 additions & 45 deletions

File tree

packages/connect-evm/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### Changed
2121

22+
- **BREAKING** Make the `dapp.name` and `dapp.url` properties required in `createMetamaskConnectEVM()` ([#56](https://github.com/MetaMask/connect-monorepo/pull/56))
23+
- The `dapp.url` property is now always overwritten with the value of the page's url when MetaMask Connect is running in a browser context ([#56](https://github.com/MetaMask/connect-monorepo/pull/56))
2224
- `connect()` now always returns `chainId`, previously it could undefined ([#53](https://github.com/MetaMask/connect-monorepo/pull/53))
2325

2426
### Fixed

packages/connect-multichain/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING** Make the `dapp.name` and `dapp.url` properties required in `createMetamaskConnect()` ([#56](https://github.com/MetaMask/connect-monorepo/pull/56))
13+
- The `dapp.url` property is now always overwritten with the value of the page's url when MetaMask Connect is running in a browser context ([#56](https://github.com/MetaMask/connect-monorepo/pull/56))
14+
1015
### Fixed
1116

1217
- Fixed mobile deeplink bug that occurred when `MultichainSDK.connect()` was called and the transport was already connected ([#57](https://github.com/MetaMask/connect-monorepo/pull/57))

packages/connect-multichain/src/domain/multichain/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type { SessionData } from '@metamask/multichain-api-client';
2222
* - Using a base64-encoded icon
2323
*/
2424
export type DappSettings = {
25-
name?: string;
25+
name: string;
2626
url?: string;
2727
} & ({ iconUrl?: string } | { base64Icon?: string });
2828

packages/connect-multichain/src/multichain/utils/index.test.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,21 @@ t.describe('Utils', () => {
3131
});
3232

3333
t.describe('getDappId', () => {
34-
const mockHostname = 'mockdapp.com';
3534
const mockDappName = 'Mock DApp Name';
3635
const mockDappUrl = 'http://mockdapp.com';
37-
const originalWindow = global.window;
3836

39-
t.afterEach(() => {
40-
global.window = originalWindow;
41-
});
42-
43-
t.it('should return window.location.hostname if window and window.location are defined', () => {
44-
global.window = {
45-
location: {
46-
hostname: mockHostname,
47-
},
48-
} as any;
49-
t.expect(utils.getDappId()).toBe(mockHostname);
50-
});
51-
52-
t.it('should return dappMetadata.name if window is undefined and name is available', () => {
37+
t.it('should return dappMetadata.name if defined and url is not', () => {
5338
global.window = undefined as any;
54-
const dappSettings = { name: mockDappName, url: mockDappUrl };
39+
const dappSettings = { name: mockDappName };
5540
t.expect(utils.getDappId(dappSettings)).toBe(mockDappName);
5641
});
5742

58-
t.it('should return dappMetadata.url if window is undefined and name is not available but url is', () => {
43+
t.it('should return dappMetadata.url if defined', () => {
5944
global.window = undefined as any;
60-
const dappSettings = { url: mockDappUrl };
45+
const dappSettings = { url: mockDappUrl, name: mockDappName };
6146
t.expect(utils.getDappId(dappSettings)).toBe(mockDappUrl);
6247
});
6348

64-
t.it('should return "N/A" if window is undefined and neither name nor url is available', () => {
65-
global.window = undefined as any;
66-
const dappSettings = {};
67-
t.expect(utils.getDappId(dappSettings)).toBe('N/A');
68-
});
6949
});
7050

7151
t.describe('getSDKVersion', () => {
@@ -161,7 +141,7 @@ t.describe('Utils', () => {
161141
t.expect(consoleWarnSpy).toHaveBeenCalledWith('Invalid dappMetadata.iconUrl: URL must start with http:// or https://');
162142
});
163143

164-
t.it('should set url to undenied if it does not start with http:// or https:// and favicon is undefined', async () => {
144+
t.it('should set url to undefined if it does not start with http:// or https:// and favicon is undefined', async () => {
165145
options.dapp.url = 'wrong';
166146
const consoleWarnSpy = t.vi.spyOn(console, 'warn');
167147

@@ -171,12 +151,17 @@ t.describe('Utils', () => {
171151
t.expect(consoleWarnSpy).toHaveBeenCalledWith('Invalid dappMetadata.url: URL must start with http:// or https://');
172152
});
173153

174-
t.it('should prove that dapp is mandatory is platform is not browser', async () => {
175-
(options.dapp as any) = undefined;
154+
t.it('throw if platform is not browser and dapp url is missing', async () => {
155+
(options.dapp as any) = { name: 'test' };
176156
await t.expect(() => utils.setupDappMetadata(options)).toThrow('You must provide dapp url');
177157
});
178158

179-
t.it('should prove that dapp is optional is platform is browser', async () => {
159+
t.it('throw if platform dapp name is missing', async () => {
160+
(options.dapp as any) = { url: 'https://example.com' };
161+
await t.expect(() => utils.setupDappMetadata(options)).toThrow('You must provide dapp name');
162+
});
163+
164+
t.it('should set the dapp url if not provided and platform is browser', async () => {
180165
const mockGetPlatformType = t.vi.mocked(getPlatformType);
181166
mockGetPlatformType.mockReturnValue(PlatformType.DesktopWeb);
182167
t.vi.stubGlobal('window', {
@@ -185,7 +170,9 @@ t.describe('Utils', () => {
185170
host: 'example.com',
186171
},
187172
});
188-
(options.dapp as any) = undefined;
173+
(options.dapp as any) = {
174+
name: 'test',
175+
};
189176
utils.setupDappMetadata(options);
190177
t.expect(options.dapp.url).toBe('https://example.com');
191178
});
@@ -195,6 +182,7 @@ t.describe('Utils', () => {
195182
const consoleWarnSpy = t.vi.spyOn(console, 'warn');
196183

197184
options.dapp = {
185+
name: 'test',
198186
iconUrl: 'https://example.com/favicon.ico',
199187
url: 'https://example.com',
200188
base64Icon: longString,
@@ -208,6 +196,7 @@ t.describe('Utils', () => {
208196

209197
t.it('should set iconUrl to the extracted favicon if iconUrl and base64Icon are not provided', async () => {
210198
options.dapp = {
199+
name: 'test',
211200
url: 'https://example.com',
212201
};
213202

packages/connect-multichain/src/multichain/utils/index.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ export function compressString(str: string): string {
4343
return base64Encode(binaryString);
4444
}
4545

46-
export function getDappId(dapp?: DappSettings) {
47-
if (typeof window === 'undefined' || typeof window.location === 'undefined') {
48-
return dapp?.name ?? dapp?.url ?? 'N/A';
49-
}
50-
51-
return window.location.hostname;
46+
export function getDappId(dapp: DappSettings) {
47+
return dapp.url ?? dapp.name;
5248
}
5349

5450
export function openDeeplink(
@@ -126,16 +122,17 @@ export function setupDappMetadata(
126122
platform === PlatformType.MobileWeb ||
127123
platform === PlatformType.MetaMaskMobileWebview;
128124

125+
if (!options.dapp?.name) {
126+
throw new Error('You must provide dapp name');
127+
}
128+
if (isBrowser) {
129+
options.dapp = {
130+
...options.dapp,
131+
url: `${window.location.protocol}//${window.location.host}`,
132+
};
133+
}
129134
if (!options.dapp?.url) {
130-
// Automatically set dappMetadata on web env if not defined
131-
if (isBrowser) {
132-
options.dapp = {
133-
...options.dapp,
134-
url: `${window.location.protocol}//${window.location.host}`,
135-
};
136-
} else {
137-
throw new Error('You must provide dapp url');
138-
}
135+
throw new Error('You must provide dapp url');
139136
}
140137
const BASE_64_ICON_MAX_LENGTH = 163400;
141138
// Check if iconUrl and url are valid

0 commit comments

Comments
 (0)