-
-
Notifications
You must be signed in to change notification settings - Fork 130
Implement Wallet Discovery For Multichain API #2970 #395
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b3e4754
feat: support CAIP294 (standardized messaging transport for browser e…
ffmcgee725 3a1eb6c
refactor: address caip294 announcement logic
ffmcgee725 dd97d6a
test: add unit test for inpage caip294 announcement func
ffmcgee725 1a56124
test: delete commented code
ffmcgee725 d01b2ce
refactor: code review changes
ffmcgee725 48651c5
feat: get extensionId from provider state
ffmcgee725 66332cf
refactor: minor test clean up
ffmcgee725 07abf08
refactor: improvement suggestions from jiexi
ffmcgee725 06e9771
refactor: minor fix
ffmcgee725 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { | ||
| announceWallet, | ||
| CAIP294EventNames, | ||
| type CAIP294WalletData, | ||
| requestWallet, | ||
| } from './CAIP294'; | ||
|
|
||
| const getWalletData = (): CAIP294WalletData => ({ | ||
| uuid: '350670db-19fa-4704-a166-e52e178b59d2', | ||
| name: 'Example Wallet', | ||
| icon: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>', | ||
| rdns: 'com.example.wallet', | ||
| extensionId: 'abcdefghijklmnopqrstuvwxyz', | ||
| }); | ||
|
|
||
| const walletDataValidationError = () => | ||
| new Error( | ||
| `Invalid CAIP-294 WalletData object received from ${CAIP294EventNames.Prompt}. See https://github.com/ChainAgnostic/CAIPs/blob/bc4942857a8e04593ed92f7dc66653577a1c4435/CAIPs/caip-294.md for requirements.`, | ||
| ); | ||
|
|
||
| describe('CAIP-294', () => { | ||
| describe('wallet data validation', () => { | ||
| it('throws if the wallet data is not a plain object', () => { | ||
| [null, undefined, Symbol('bar'), []].forEach((invalidInfo) => { | ||
| expect(() => announceWallet(invalidInfo as any)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if the `icon` field is invalid', () => { | ||
| [ | ||
| null, | ||
| undefined, | ||
| '', | ||
| 'not-a-data-uri', | ||
| 'https://example.com/logo.png', | ||
| 'data:text/plain;blah', | ||
| Symbol('bar'), | ||
| ].forEach((invalidIcon) => { | ||
| const walletInfo = getWalletData(); | ||
| walletInfo.icon = invalidIcon as any; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if the `name` field is invalid', () => { | ||
| [null, undefined, '', {}, [], Symbol('bar')].forEach((invalidName) => { | ||
| const walletInfo = getWalletData(); | ||
| walletInfo.name = invalidName as any; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if the `uuid` field is invalid', () => { | ||
| [null, undefined, '', 'foo', Symbol('bar')].forEach((invalidUuid) => { | ||
| const walletInfo = getWalletData(); | ||
| walletInfo.uuid = invalidUuid as any; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if the `rdns` field is invalid', () => { | ||
| [ | ||
| null, | ||
| undefined, | ||
| '', | ||
| 'not-a-valid-domain', | ||
| '..com', | ||
| 'com.', | ||
| Symbol('bar'), | ||
| ].forEach((invalidRdns) => { | ||
| const walletInfo = getWalletData(); | ||
| walletInfo.rdns = invalidRdns as any; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('allows `extensionId` to be undefined or a string', () => { | ||
| const walletInfo = getWalletData(); | ||
| expect(() => announceWallet(walletInfo)).not.toThrow(); | ||
|
|
||
| delete walletInfo.extensionId; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).not.toThrow(); | ||
|
|
||
| walletInfo.extensionId = 'valid-string'; | ||
| expect(() => announceWallet(walletInfo)).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if the `extensionId` field is invalid', () => { | ||
| [null, '', 42, Symbol('bar')].forEach((invalidExtensionId) => { | ||
| const walletInfo = getWalletData(); | ||
| walletInfo.extensionId = invalidExtensionId as any; | ||
|
|
||
| expect(() => announceWallet(walletInfo)).toThrow( | ||
| walletDataValidationError(), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('wallet is announced before dapp requests', async () => { | ||
| const walletData = getWalletData(); | ||
| const handleWallet = jest.fn(); | ||
| const dispatchEvent = jest.spyOn(window, 'dispatchEvent'); | ||
| const addEventListener = jest.spyOn(window, 'addEventListener'); | ||
|
|
||
| announceWallet(walletData); | ||
| requestWallet(handleWallet); | ||
| await delay(); | ||
|
|
||
| expect(dispatchEvent).toHaveBeenCalledTimes(3); | ||
| expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
| 1, | ||
| new CustomEvent(CAIP294EventNames.Announce, expect.any(Object)), | ||
| ); | ||
| expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
| 2, | ||
| new CustomEvent(CAIP294EventNames.Prompt, expect.any(Object)), | ||
| ); | ||
| expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
| 3, | ||
| new CustomEvent(CAIP294EventNames.Announce, expect.any(Object)), | ||
| ); | ||
|
|
||
| expect(addEventListener).toHaveBeenCalledTimes(2); | ||
| expect(addEventListener).toHaveBeenCalledWith( | ||
| CAIP294EventNames.Announce, | ||
| expect.any(Function), | ||
| ); | ||
| expect(addEventListener).toHaveBeenCalledWith( | ||
| CAIP294EventNames.Prompt, | ||
| expect.any(Function), | ||
| ); | ||
|
|
||
| expect(handleWallet).toHaveBeenCalledTimes(1); | ||
| expect(handleWallet).toHaveBeenCalledWith( | ||
| expect.objectContaining({ params: walletData }), | ||
| ); | ||
| }); | ||
|
|
||
| it('dapp requests before wallet is announced', async () => { | ||
| const walletData = getWalletData(); | ||
| const handleWallet = jest.fn(); | ||
| const dispatchEvent = jest.spyOn(window, 'dispatchEvent'); | ||
| const addEventListener = jest.spyOn(window, 'addEventListener'); | ||
|
|
||
| requestWallet(handleWallet); | ||
| announceWallet(walletData); | ||
| await delay(); | ||
|
|
||
| expect(dispatchEvent).toHaveBeenCalledTimes(2); | ||
| expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
| 1, | ||
| new CustomEvent(CAIP294EventNames.Prompt, expect.any(Object)), | ||
| ); | ||
| expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
| 2, | ||
| new CustomEvent(CAIP294EventNames.Announce, expect.any(Object)), | ||
| ); | ||
|
|
||
| expect(addEventListener).toHaveBeenCalledTimes(2); | ||
| expect(addEventListener).toHaveBeenCalledWith( | ||
| CAIP294EventNames.Announce, | ||
| expect.any(Function), | ||
| ); | ||
| expect(addEventListener).toHaveBeenCalledWith( | ||
| CAIP294EventNames.Prompt, | ||
| expect.any(Function), | ||
| ); | ||
|
|
||
| expect(handleWallet).toHaveBeenCalledTimes(1); | ||
| expect(handleWallet).toHaveBeenCalledWith( | ||
| expect.objectContaining({ params: walletData }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Delay for a number of milliseconds by awaiting a promise | ||
| * resolved after the specified number of milliseconds. | ||
| * | ||
| * @param ms - The number of milliseconds to delay for. | ||
| */ | ||
| async function delay(ms = 1) { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.