From 7ac0c2f33d3f609cdd9151c600bf7c560733b76c Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:54:54 +0100 Subject: [PATCH 1/4] chore: prepare for the first release --- README.md | 44 ++----- packages/web3-plugin-wallet-rpc/README.md | 19 ++- packages/web3-plugin-wallet-rpc/package.json | 3 +- .../src/WalletRpcPlugin.ts | 54 -------- packages/web3-plugin-wallet-rpc/src/types.ts | 118 ------------------ packages/web3-plugin-wallet-rpc/src/utils.ts | 5 - .../test/wallet_getOwnedAssets.test.ts | 34 ----- .../test/wallet_updateEthereumChain.test.ts | 60 --------- 8 files changed, 22 insertions(+), 315 deletions(-) delete mode 100644 packages/web3-plugin-wallet-rpc/src/utils.ts delete mode 100644 packages/web3-plugin-wallet-rpc/test/wallet_getOwnedAssets.test.ts delete mode 100644 packages/web3-plugin-wallet-rpc/test/wallet_updateEthereumChain.test.ts diff --git a/README.md b/README.md index 26b2059..bc19b7a 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,9 @@ web3.registerPlugin(new WalletRpcPlugin()); ### Methods -#### addEthereumChain +Click on the method name for detailed documentation. + +#### [addEthereumChain](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#addEthereumChain) Invokes the `wallet_addEthereumChain` method as defined in [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085#wallet_addethereumchain). @@ -65,7 +67,7 @@ await web3.walletRpc.addEthereumChain({ }); ``` -#### switchEthereumChain +#### [switchEthereumChain](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#switchEthereumChain) Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326#wallet_switchethereumchain). @@ -73,7 +75,7 @@ Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https:/ await web3.walletRpc.switchEthereumChain(5000); ``` -#### watchAsset +#### [watchAsset](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#watchAsset) Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747#specification). @@ -87,7 +89,7 @@ await web3.walletRpc.watchAsset({ }); ``` -#### requestPermissions +#### [requestPermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#requestPermissions) Invokes the `wallet_requestPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255#specification). @@ -97,7 +99,7 @@ const permissions = await web3.walletRpc.requestPermissions({ }); ``` -#### getPermissions +#### [getPermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#getPermissions) Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255#specification). @@ -105,7 +107,7 @@ Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips const permissions = await web3.walletRpc.getPermissions(); ``` -#### revokePermissions +#### [revokePermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#revokePermissions) Invokes the `wallet_revokePermissions` method as defined in [MetaMask docs](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/). @@ -115,36 +117,6 @@ const permissions = await web3.walletRpc.revokePermissions({ }); ``` -### Experimental methods - -#### updateEthereumChain - -Invokes the `wallet_updateEthereumChain` method as defined in [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015). - -```typescript -await web3.walletRpc.updateEthereumChain({ - chainId: 5000, - blockExplorerUrls: ['https://mantlescan.xyz'], - chainName: 'Mantle', - nativeCurrency: { - name: 'Mantle', - symbol: 'MNT', - decimals: 18, - }, - rpcUrls: ['https://rpc.mantle.xyz'], -}); -``` - -#### getOwnedAssets - -Invokes the `wallet_getOwnedAssets` method as defined in [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256). - -```typescript -const ownedAssets = await web3.walletRpc.getOwnedAssets({ - address: '0xa5653e88D9c352387deDdC79bcf99f0ada62e9c6', -}); -``` - ## Contributing We welcome pull requests! For major changes, please open an issue first to discuss the proposed modifications. diff --git a/packages/web3-plugin-wallet-rpc/README.md b/packages/web3-plugin-wallet-rpc/README.md index ac7267e..7d48434 100644 --- a/packages/web3-plugin-wallet-rpc/README.md +++ b/packages/web3-plugin-wallet-rpc/README.md @@ -39,7 +39,9 @@ web3.registerPlugin(new WalletRpcPlugin()); ### Methods -#### addEthereumChain +Click on the method name for detailed documentation. + +#### [addEthereumChain](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#addEthereumChain) Invokes the `wallet_addEthereumChain` method as defined in [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085#wallet_addethereumchain). @@ -58,7 +60,7 @@ await web3.walletRpc.addEthereumChain({ }); ``` -#### switchEthereumChain +#### [switchEthereumChain](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#switchEthereumChain) Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326#wallet_switchethereumchain). @@ -66,7 +68,7 @@ Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https:/ await web3.walletRpc.switchEthereumChain(5000); ``` -#### watchAsset +#### [watchAsset](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#watchAsset) Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747#specification). @@ -80,7 +82,7 @@ await web3.walletRpc.watchAsset({ }); ``` -#### requestPermissions +#### [requestPermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#requestPermissions) Invokes the `wallet_requestPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255#specification). @@ -90,7 +92,7 @@ const permissions = await web3.walletRpc.requestPermissions({ }); ``` -#### getPermissions +#### [getPermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#getPermissions) Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255#specification). @@ -98,7 +100,7 @@ Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips const permissions = await web3.walletRpc.getPermissions(); ``` -#### revokePermissions +#### [revokePermissions](https://web3.github.io/web3-wallet-rpc-utils/classes/WalletRpcPlugin.html#revokePermissions) Invokes the `wallet_revokePermissions` method as defined in [MetaMask docs](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/). @@ -108,6 +110,11 @@ const permissions = await web3.walletRpc.revokePermissions({ }); ``` +## Contributing + +We welcome pull requests! For major changes, please [open an issue](https://github.com/web3/web3-wallet-rpc-utils) first to discuss the proposed modifications. +Also, ensure that you update tests as needed to reflect the changes. + ## License [MIT](https://choosealicense.com/licenses/mit/) diff --git a/packages/web3-plugin-wallet-rpc/package.json b/packages/web3-plugin-wallet-rpc/package.json index 4fb642f..9761dea 100644 --- a/packages/web3-plugin-wallet-rpc/package.json +++ b/packages/web3-plugin-wallet-rpc/package.json @@ -35,8 +35,7 @@ "typescript": "5.3.x", "web3-core": "^4.7.0", "web3-types": "^1.8.1", - "web3-utils": "^4.3.2", - "web3-validator": "^2.0.6" + "web3-utils": "^4.3.2" }, "peerDependencies": { "web3": ">= 4.14.0" diff --git a/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts b/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts index a2896f2..64c4d58 100644 --- a/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts +++ b/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts @@ -1,18 +1,13 @@ import { Web3PluginBase } from 'web3-core'; import type { Numbers } from 'web3-types'; import { toHex } from 'web3-utils'; -import { validator } from 'web3-validator'; import type { AddEthereumChainRequest, - GetOwnedAssetsRequest, - OwnedAsset, Permission, PermissionRequest, - UpdateEthereumChainRequest, WatchAssetRequest, } from './types'; -import { parseToGetOwnedAssetsResult } from './utils'; type WalletRpcApi = { wallet_addEthereumChain: (param: AddEthereumChainRequest) => null; @@ -21,9 +16,6 @@ type WalletRpcApi = { wallet_requestPermissions: (param: PermissionRequest) => Permission[]; wallet_getPermissions: () => Permission[]; wallet_revokePermissions: (param: PermissionRequest) => null; - // experimental - wallet_getOwnedAssets: (param: GetOwnedAssetsRequest) => OwnedAsset[]; - wallet_updateEthereumChain: (param: UpdateEthereumChainRequest) => void; }; /** @@ -81,27 +73,6 @@ export class WalletRpcPlugin extends Web3PluginBase { }); } - /** - * Switch to a new chain and register it with the user’s wallet if it isn’t already recognized. - * - * See [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015) for more details. - * - * @param param - Details of the chain to switch to and possibly add. - * @returns A Promise that resolves if the request is successful. - * @experimental - */ - public async updateEthereumChain(param: UpdateEthereumChainRequest): Promise { - return this.requestManager.send({ - method: 'wallet_updateEthereumChain', - params: [ - { - ...param, - chainId: toHex(param.chainId), - }, - ], - }); - } - /** * Switch the wallet's currently active chain. * @@ -127,31 +98,6 @@ export class WalletRpcPlugin extends Web3PluginBase { }); } - /** - * Return a list of owned assets for the given address. - * - * See [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256) for more details. - * - * @param param - Details of the request for owned assets. - * @returns A Promise that resolves to a list of owned assets. - * @experimental - */ - public async getOwnedAssets(param: GetOwnedAssetsRequest): Promise { - validator.validate(['address'], [param.address]); - - const trueParam = { ...param }; - if (trueParam.options?.chainId) { - trueParam.options.chainId = toHex(trueParam.options.chainId); - } - - const result = await this.requestManager.send({ - method: 'wallet_getOwnedAssets', - params: [trueParam], - }); - - return parseToGetOwnedAssetsResult(result); - } - /** * Add an asset to the user's wallet. * diff --git a/packages/web3-plugin-wallet-rpc/src/types.ts b/packages/web3-plugin-wallet-rpc/src/types.ts index 4066f16..1d6df16 100644 --- a/packages/web3-plugin-wallet-rpc/src/types.ts +++ b/packages/web3-plugin-wallet-rpc/src/types.ts @@ -54,124 +54,6 @@ export type AddEthereumChainRequest = { rpcUrls?: string[]; }; -/** - * Request to switch to a specified blockchain network. Registers the network if it’s not recognized by the wallet. - * - * See [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015) for more details. - */ -export type UpdateEthereumChainRequest = { - /** - * Unique identifier for the chain, as per EIP-155. - */ - chainId: Numbers; - - /** - * URL for the primary block explorer of this chain. - */ - blockExplorerUrl?: string; - - /** - * The name of the chain. - */ - chainName?: string; - - /** - * Details of the native currency used on this chain. - */ - nativeCurrency?: NativeCurrencyData; - - /** - * URLs of the RPC nodes for this chain. - */ - rpcUrls?: string[]; -}; - -/** - * Request to retrieve a list of assets owned by a specified Ethereum address. - * - * See [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256) for more details. - */ -export type GetOwnedAssetsRequest = { - /** - * Ethereum address of the asset owner. - */ - address: Address; - - options?: { - /** - * Chain ID as per EIP-155, specifying the blockchain. - */ - chainId?: Numbers; - - /** - * Maximum number of assets to return. - */ - limit?: number; - - /** - * List of asset types (e.g., ['ERC20', 'ERC721']) to filter the response. - */ - types?: string[]; - - /** - * Description of the purpose of this request, provided by the dApp. - */ - justification?: string; - }; -}; - -/** - * Representation of a single asset owned by the user’s wallet. - * - * See [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256) for more details. - */ -export type OwnedAsset = { - /** - * Checksummed Ethereum address of the asset contract. - */ - address: Address; - - /** - * Identifier for the chain where this asset is deployed. - */ - chainId: Numbers; - - /** - * Identifier of the asset type (e.g., "ERC20"). - */ - type?: string; - - /** - * Asset-specific metadata. - */ - options: { - /** - * Name of the token. Optional if the token contract doesn’t implement it. - */ - name?: string; - - /** - * Symbol of the token. Optional if the token contract doesn’t implement it. - */ - symbol?: string; - - /** - * Token icon in base64 format or URL. Optional. - */ - icon?: string; - - /** - * Number of tokens owned, in the smallest denomination. - */ - balance: Numbers; - - /** - * Number of decimal places supported by the token. Optional. - */ - decimals?: number; - }; -}; - /** * Request to add a specified asset to the user’s wallet interface. * diff --git a/packages/web3-plugin-wallet-rpc/src/utils.ts b/packages/web3-plugin-wallet-rpc/src/utils.ts deleted file mode 100644 index 6500331..0000000 --- a/packages/web3-plugin-wallet-rpc/src/utils.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { OwnedAsset } from './types'; - -export function parseToGetOwnedAssetsResult(result: unknown[]): OwnedAsset[] { - return result as OwnedAsset[]; -} diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_getOwnedAssets.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_getOwnedAssets.test.ts deleted file mode 100644 index df44a04..0000000 --- a/packages/web3-plugin-wallet-rpc/test/wallet_getOwnedAssets.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Web3Context } from 'web3-core'; - -import { WalletRpcPlugin } from '../src'; - -describe('WalletRpcPlugin', () => { - describe('wallet_getOwnedAssets', () => { - const web3 = new Web3Context('http://127.0.0.1:8545'); - web3.registerPlugin(new WalletRpcPlugin()); - - const requestManagerSendSpy = jest.fn(); - web3.requestManager.send = requestManagerSendSpy; - - afterEach(() => { - requestManagerSendSpy.mockClear(); - }); - - it('should call the method with expected params', async () => { - await web3.walletRpc.getOwnedAssets({ - address: '0xa5653e88D9c352387deDdC79bcf99f0ada62e9c6', - }); - - expect(requestManagerSendSpy).toHaveBeenCalledWith({ - method: 'wallet_getOwnedAssets', - params: [{ address: '0xa5653e88D9c352387deDdC79bcf99f0ada62e9c6' }], - }); - }); - - it('should throw when called with invalid address', async () => { - await expect(web3.walletRpc.getOwnedAssets({ address: '' })).rejects.toThrow( - 'validator found 1 error', - ); - }); - }); -}); diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_updateEthereumChain.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_updateEthereumChain.test.ts deleted file mode 100644 index e44b60a..0000000 --- a/packages/web3-plugin-wallet-rpc/test/wallet_updateEthereumChain.test.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { Web3Context } from 'web3-core'; - -import { WalletRpcPlugin } from '../src'; - -describe('WalletRpcPlugin', () => { - describe('wallet_updateEthereumChain', () => { - const web3 = new Web3Context('http://127.0.0.1:8545'); - web3.registerPlugin(new WalletRpcPlugin()); - - const requestManagerSendSpy = jest.fn(); - web3.requestManager.send = requestManagerSendSpy; - - afterEach(() => { - requestManagerSendSpy.mockClear(); - }); - - it('should call the method with expected params', async () => { - await web3.walletRpc.updateEthereumChain({ chainId: 5000 }); - - expect(requestManagerSendSpy).toHaveBeenCalledWith({ - method: 'wallet_updateEthereumChain', - params: [{ chainId: '0x1388' }], - }); - }); - - it('should return correct result', async () => { - const result = await web3.walletRpc.updateEthereumChain({ - chainId: 5000, - }); - - expect(result).toBeUndefined(); - }); - - it('should pass all possible fields as a param', async () => { - const request = { - chainId: 5000, - blockExplorerUrls: ['https://mantlescan.xyz'], - chainName: 'Mantle', - nativeCurrency: { - name: 'Mantle', - symbol: 'MNT', - decimals: 18, - }, - rpcUrls: ['https://rpc.mantle.xyz'], - }; - - await web3.walletRpc.updateEthereumChain(request); - - expect(requestManagerSendSpy).toHaveBeenCalledWith({ - method: 'wallet_updateEthereumChain', - params: [ - { - ...request, - chainId: '0x1388', - }, - ], - }); - }); - }); -}); From ee9f5fc26b1666114395013983876c0081c18760 Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:57:34 +0100 Subject: [PATCH 2/4] remove useless tests --- .../test/wallet_addEthereumChain.test.ts | 6 ------ .../test/wallet_switchEthereumChain.test.ts | 6 ------ .../test/wallet_watchAsset.test.ts | 11 ----------- 3 files changed, 23 deletions(-) diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_addEthereumChain.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_addEthereumChain.test.ts index 5ba6be5..84df21a 100644 --- a/packages/web3-plugin-wallet-rpc/test/wallet_addEthereumChain.test.ts +++ b/packages/web3-plugin-wallet-rpc/test/wallet_addEthereumChain.test.ts @@ -23,12 +23,6 @@ describe('WalletRpcPlugin', () => { }); }); - it('should return correct result', async () => { - const result = await web3.walletRpc.addEthereumChain({ chainId: 5000 }); - - expect(result).toBeUndefined(); - }); - it('should pass all possible fields as a param', async () => { const request = { chainId: 5000, diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_switchEthereumChain.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_switchEthereumChain.test.ts index 442b1f9..7831545 100644 --- a/packages/web3-plugin-wallet-rpc/test/wallet_switchEthereumChain.test.ts +++ b/packages/web3-plugin-wallet-rpc/test/wallet_switchEthereumChain.test.ts @@ -22,11 +22,5 @@ describe('WalletRpcPlugin', () => { params: [{ chainId: '0x1388' }], }); }); - - it('should return correct result', async () => { - const result = await web3.walletRpc.switchEthereumChain(5000); - - expect(result).toBeUndefined(); - }); }); }); diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts index 795e325..06165dc 100644 --- a/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts +++ b/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts @@ -30,16 +30,5 @@ describe('WalletRpcPlugin', () => { params: [request], }); }); - - it('should return correct result', async () => { - const result = await web3.walletRpc.watchAsset({ - type: 'ERC20', - options: { - address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', - }, - }); - - expect(result).toBeUndefined(); - }); }); }); From 2620909447d5b3ee8e3695f163f96af9de6732a9 Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:09:32 +0100 Subject: [PATCH 3/4] add result of the test for uniswap extension --- WALLET_SUPPORT.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/WALLET_SUPPORT.md b/WALLET_SUPPORT.md index ae53930..0f5acdf 100644 --- a/WALLET_SUPPORT.md +++ b/WALLET_SUPPORT.md @@ -2,14 +2,14 @@ ## Browser extension wallets -| Method | Metamask | Coinbase Wallet | Trust Wallet | Enkrypt | Rainbow | Rabby Wallet | Exodus | Phantom | -| -------------------------- | -------- | --------------- | ------------ | ------- | ------- | ------------ | ------ | ------- | -| wallet_addEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ (\*) | ✓ | x (\*) | x (\*) | -| wallet_switchEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ | ✓ | ✓ (\*) | ✓ (\*) | -| wallet_watchAsset | ✓ (\*) | ✓ | ✓ (\*) | x | ✓ (\*) | ✓ (\*) | x | ✓ (\*) | -| wallet_requestPermissions | ✓ (\*) | x | ✓ | x (\*) | x | ✓ | ✓ | ✓ | -| wallet_getPermissions | ✓ | x | ✓ | x | x | ✓ | ✓ | ✓ | -| wallet_revokePermissions | ✓ | x | ✓ | x | x | ✓ | ✓ (\*) | x | +| Method | Metamask | Coinbase Wallet | Trust Wallet | Enkrypt | Rainbow | Rabby Wallet | Exodus | Phantom | Uniswap Extension | +| -------------------------- | -------- | --------------- | ------------ | ------- | ------- | ------------ | ------ | ------- | ----------------- | +| wallet_addEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ (\*) | ✓ | x (\*) | x (\*) | x | +| wallet_switchEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ | ✓ | ✓ (\*) | ✓ (\*) | ✓ (\*) | +| wallet_watchAsset | ✓ (\*) | ✓ | ✓ (\*) | x | ✓ (\*) | ✓ (\*) | x | ✓ (\*) | x | +| wallet_requestPermissions | ✓ (\*) | x | ✓ | x (\*) | x | ✓ | ✓ | ✓ | ✓ | +| wallet_getPermissions | ✓ | x | ✓ | x | x | ✓ | ✓ | ✓ | ✓ | +| wallet_revokePermissions | ✓ | x | ✓ | x | x | ✓ | ✓ (\*) | x | ✓ | - (✓) Supported - (x) Not supported @@ -96,6 +96,10 @@ WatchAsset.tsx:45 Ir: Missing or invalid parameters. at new Promise () ``` +### Uniswap Extension + +- `wallet_switchEthereumChain`: Only works with chains natively supported by the wallet; new chains cannot be added. + ## Mobile wallets using WalletConnect TBD From b36db0abb897c3e12e49643a89c6d79fe573313a Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:47:04 +0100 Subject: [PATCH 4/4] feat: fix watchAsset --- WALLET_SUPPORT.md | 39 ++----------------- .../src/wallet-components/WatchAsset.tsx | 17 +++++--- .../src/WalletRpcPlugin.ts | 2 +- packages/web3-plugin-wallet-rpc/src/types.ts | 7 +++- .../test/wallet_watchAsset.test.ts | 2 +- 5 files changed, 22 insertions(+), 45 deletions(-) diff --git a/WALLET_SUPPORT.md b/WALLET_SUPPORT.md index 0f5acdf..d0d62be 100644 --- a/WALLET_SUPPORT.md +++ b/WALLET_SUPPORT.md @@ -6,7 +6,7 @@ | -------------------------- | -------- | --------------- | ------------ | ------- | ------- | ------------ | ------ | ------- | ----------------- | | wallet_addEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ (\*) | ✓ | x (\*) | x (\*) | x | | wallet_switchEthereumChain | ✓ | ✓ (\*) | ✓ | ✓ | ✓ | ✓ | ✓ (\*) | ✓ (\*) | ✓ (\*) | -| wallet_watchAsset | ✓ (\*) | ✓ | ✓ (\*) | x | ✓ (\*) | ✓ (\*) | x | ✓ (\*) | x | +| wallet_watchAsset | ✓ | ✓ | ✓ (\*) | ✓ | ✓ | ✓ | x (\*) | x | x | | wallet_requestPermissions | ✓ (\*) | x | ✓ | x (\*) | x | ✓ | ✓ | ✓ | ✓ | | wallet_getPermissions | ✓ | x | ✓ | x | x | ✓ | ✓ | ✓ | ✓ | | wallet_revokePermissions | ✓ | x | ✓ | x | x | ✓ | ✓ (\*) | x | ✓ | @@ -29,14 +29,6 @@ #### Metamask - `wallet_requestPermissions` and `wallet_getPermissions`: Returns a full response containing all permission fields according to the specification. -- `wallet_watchAsset`: There’s an issue with Web3RequestManager, causing an error with the wallet's response: - -``` -ResponseError: Returned error: Cannot destructure property 'tokenId' of 'n' as it is undefined. -at Web3RequestManager. (web3_request_manager.ts:178:1) -at Generator.next () -at fulfilled (web3_request_manager.ts:1:1)` -``` #### Coinbase Wallet @@ -46,21 +38,7 @@ at fulfilled (web3_request_manager.ts:1:1)` ### Trust Wallet - General Behavior is very similarly to MetaMask. -- `wallet_watchAsset`: Produces an error due to an issue with destructuring the wallet’s response: - -``` -WatchAsset.tsx:45 TypeError: Cannot destructure property 'address' of 'A' as it is undefined. - at wi.request (inpage.js:247:201226) - at inpage.js:247:206755 - at yu.handleStaticRequests (inpage.js:247:207086) - at yu.request (inpage.js:247:206709) - at web3_request_manager.ts:271:1 - at new Promise () - at Web3RequestManager. (web3_request_manager.ts:250:1) - at Generator.next () - at web3_request_manager.ts:1:1 - at new Promise () -``` +- `wallet_watchAsset`: Requires `symbol` and `decimals` fields, which are optional according to the specification and in other wallets. ### Enkrypt @@ -69,32 +47,21 @@ WatchAsset.tsx:45 TypeError: Cannot destructure property 'address' of 'A' as it ### Rainbow - `wallet_addEthereumChain`: Adds a chain but does not switch to it; fails if the network is already known to the wallet. -- `wallet_watchAsset`: Fails with the error `"Internal error: Cannot read properties of undefined (reading 'address')"`. ### Rabby Wallet - `wallet_requestPermissions` and `wallet_getPermissions`: Returns a response object containing only `parentCapability`, but functions as expected. -- `wallet_watchAsset`: Opens a modal with an “Add Custom Token” header, but it remains stuck and does not complete the action. ### Exodus - `wallet_addEthereumChain` and `wallet_switchEthereumChain`: Only work with chains natively supported by the wallet; new chains cannot be added. +- `wallet_watchAsset`: The promise resolves successfully with a value of `true`, but no asset is actually added. - `wallet_requestPermissions` and `wallet_getPermissions`: Returns a response object containing only `parentCapability`, but functions as expected. - `wallet_revokePermissions`: Revokes permissions but also triggers an error: `DisconnectedError: The provider is disconnected from all chains`. This appears to be an issue with the wallet itself. ### Phantom - `wallet_addEthereumChain` and `wallet_switchEthereumChain`: Only work with chains natively supported by the wallet; new chains cannot be added. -- `wallet_watchAsset`: Fails with an error: - -``` -WatchAsset.tsx:45 Ir: Missing or invalid parameters. - at r.request (chrome-extension://bfnaelmomeimhlpmgjnjophhpkkoljpa/evmAsk.js:7:5094) - at Web3RequestManager. (http://localhost:3000/static/js/bundle.js:80237:25) - at Generator.next () - at http://localhost:3000/static/js/bundle.js:80100:67 - at new Promise () -``` ### Uniswap Extension diff --git a/packages/example-react-app/src/wallet-components/WatchAsset.tsx b/packages/example-react-app/src/wallet-components/WatchAsset.tsx index 18a2c31..6dc44b1 100644 --- a/packages/example-react-app/src/wallet-components/WatchAsset.tsx +++ b/packages/example-react-app/src/wallet-components/WatchAsset.tsx @@ -8,7 +8,14 @@ const tokens: Record = { type: 'ERC20', options: { address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', - symbol: 'USDC', + }, + }, + usdc_full: { + type: 'ERC20', + options: { + address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', + symbol: 'USDC', // This is required by some wallets, while optional for others and in the spec + decimals: 18, // This is required by some wallets, while optional for others and in the spec }, }, scroll: { @@ -35,10 +42,7 @@ function WatchAssetButton({ .watchAsset(asset) .then((response) => { // eslint-disable-next-line no-console - console.log( - `Successfully added ${asset.options.symbol ?? ''} with response`, - response, - ); + console.log('Successfully resolved watchAsset request with response', response); }) .catch((e) => { // eslint-disable-next-line no-console @@ -64,7 +68,8 @@ export function WatchAsset() { return (

Add token to wallet's list

- + +
); diff --git a/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts b/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts index 64c4d58..6c37fd6 100644 --- a/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts +++ b/packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts @@ -118,7 +118,7 @@ export class WalletRpcPlugin extends Web3PluginBase { public async watchAsset(param: WatchAssetRequest): Promise { return this.requestManager.send({ method: 'wallet_watchAsset', - params: [param], + params: param, }); } diff --git a/packages/web3-plugin-wallet-rpc/src/types.ts b/packages/web3-plugin-wallet-rpc/src/types.ts index 1d6df16..1d49a40 100644 --- a/packages/web3-plugin-wallet-rpc/src/types.ts +++ b/packages/web3-plugin-wallet-rpc/src/types.ts @@ -70,7 +70,7 @@ export type WatchAssetRequest = { */ options: { /** - * Ethereum address of the token contract. + * Address of the token contract. */ address: Address; @@ -88,6 +88,11 @@ export type WatchAssetRequest = { * URL or base64-encoded image of the token. */ image?: string; + + /** + * The unique identifier of the NFT (required for ERC-721 and ERC-1155 tokens). Supported by MetaMask. + */ + tokenId?: string; }; }; diff --git a/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts b/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts index 06165dc..502b320 100644 --- a/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts +++ b/packages/web3-plugin-wallet-rpc/test/wallet_watchAsset.test.ts @@ -27,7 +27,7 @@ describe('WalletRpcPlugin', () => { expect(requestManagerSendSpy).toHaveBeenCalledWith({ method: 'wallet_watchAsset', - params: [request], + params: request, }); }); });