-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add get/request/revoke permissions methods #9
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"trailingComma": "all" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"eslint.workingDirectories": [ | ||
"packages/example-react-app", | ||
"packages/web3-plugin-wallet-rpc" | ||
] | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,16 @@ | |
This Web3.js plugin adds support for the following wallet-related RPC methods: | ||
|
||
- [wallet_addEthereumChain (EIP-3085)](https://eips.ethereum.org/EIPS/eip-3085) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you link the EIPs in the descriptions below, I think these links should point to the anchors for the respective method descriptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done per function in the examples below, here is the link to the full EIP |
||
- [wallet_updateEthereumChain (EIP-2015)](https://eips.ethereum.org/EIPS/eip-2015) | ||
- [wallet_switchEthereumChain (EIP-3326)](https://eips.ethereum.org/EIPS/eip-3326) | ||
- [wallet_getOwnedAssets (EIP-2256)](https://eips.ethereum.org/EIPS/eip-2256) | ||
- [wallet_watchAsset (EIP-747)](https://eips.ethereum.org/EIPS/eip-747) | ||
|
||
Not implemented yet: | ||
|
||
- [wallet_requestPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255) | ||
- [wallet_getPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255) | ||
- [wallet_revokePermissions](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/) | ||
|
||
Experimental: | ||
krzysu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [wallet_updateEthereumChain (EIP-2015)](https://eips.ethereum.org/EIPS/eip-2015) | ||
- [wallet_getOwnedAssets (EIP-2256)](https://eips.ethereum.org/EIPS/eip-2256) | ||
|
||
## Installation | ||
|
||
|
@@ -62,6 +63,58 @@ await web3.walletRpc.addEthereumChain({ | |
}); | ||
``` | ||
|
||
#### switchEthereumChain | ||
|
||
Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326). | ||
|
||
```typescript | ||
await web3.walletRpc.switchEthereumChain(5000); | ||
``` | ||
|
||
#### watchAsset | ||
|
||
Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747). | ||
|
||
```typescript | ||
await web3.walletRpc.watchAsset({ | ||
type: "ERC20", | ||
options: { | ||
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
symbol: "USDC", | ||
}, | ||
}); | ||
``` | ||
|
||
#### requestPermissions | ||
|
||
Invokes the `wallet_requestPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255). | ||
|
||
```typescript | ||
const permissions = await web3.walletRpc.requestPermissions({ | ||
eth_accounts: {}, | ||
}); | ||
``` | ||
|
||
#### getPermissions | ||
|
||
Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255). | ||
|
||
```typescript | ||
const permissions = await web3.walletRpc.getPermissions(); | ||
``` | ||
|
||
#### revokePermissions | ||
|
||
Invokes the `wallet_revokePermissions` method as defined in [MetaMask docs](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/). | ||
|
||
```typescript | ||
const permissions = await web3.walletRpc.revokePermissions({ | ||
eth_accounts: {}, | ||
}); | ||
``` | ||
|
||
### Experimental methods | ||
|
||
#### updateEthereumChain | ||
|
||
Invokes the `wallet_updateEthereumChain` method as defined in [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015). | ||
|
@@ -80,14 +133,6 @@ await web3.walletRpc.updateEthereumChain({ | |
}); | ||
``` | ||
|
||
#### switchEthereumChain | ||
|
||
Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326). | ||
|
||
```typescript | ||
await web3.walletRpc.switchEthereumChain({ chainId: 5000 }); | ||
``` | ||
|
||
#### getOwnedAssets | ||
|
||
Invokes the `wallet_getOwnedAssets` method as defined in [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256). | ||
|
@@ -98,20 +143,6 @@ const ownedAssets = await web3.walletRpc.getOwnedAssets({ | |
}); | ||
``` | ||
|
||
#### watchAsset | ||
|
||
Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747). | ||
|
||
```typescript | ||
await web3.walletRpc.watchAsset({ | ||
type: "ERC20", | ||
options: { | ||
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
symbol: "USDC", | ||
}, | ||
}); | ||
``` | ||
|
||
## Contributing | ||
|
||
We welcome pull requests! For major changes, please open an issue first to discuss the proposed modifications. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
lib | ||
lib | ||
docs |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import type { | |
AddEthereumChainRequest, | ||
GetOwnedAssetsRequest, | ||
OwnedAsset, | ||
Permission, | ||
PermissionRequest, | ||
UpdateEthereumChainRequest, | ||
WatchAssetRequest, | ||
} from "./types"; | ||
|
@@ -16,6 +18,9 @@ type WalletRpcApi = { | |
wallet_switchEthereumChain: (chainId: Numbers) => void; | ||
wallet_getOwnedAssets: (param: GetOwnedAssetsRequest) => OwnedAsset[]; | ||
wallet_watchAsset: (param: WatchAssetRequest) => boolean; | ||
wallet_requestPermissions: (param: PermissionRequest) => Permission[]; | ||
wallet_getPermissions: () => Permission[]; | ||
wallet_revokePermissions: (param: PermissionRequest) => void; | ||
}; | ||
|
||
/** | ||
|
@@ -44,8 +49,22 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | |
* | ||
* See [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for more details. | ||
* | ||
* @param param - Details of the chain to add | ||
* @returns a Promise that resolves if the request is successful | ||
* @param param - Details of the chain to add. | ||
* @returns A Promise that resolves if the request is successful. | ||
* | ||
* @example | ||
* await web3.walletRpc.addEthereumChain({ | ||
* chainId: 5000, | ||
* blockExplorerUrls: ["https://mantlescan.xyz"], | ||
* chainName: "Mantle", | ||
* iconUrls: ["https://icons.llamao.fi/icons/chains/rsz_mantle.jpg"], | ||
* nativeCurrency: { | ||
* name: "Mantle", | ||
* symbol: "MNT", | ||
* decimals: 18, | ||
* }, | ||
* rpcUrls: ["https://rpc.mantle.xyz"], | ||
* }); | ||
*/ | ||
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> { | ||
return this.requestManager.send({ | ||
|
@@ -64,8 +83,9 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | |
* | ||
* 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 | ||
* @param param - Details of the chain to switch to and possibly add. | ||
* @returns A Promise that resolves if the request is successful. | ||
* @experimental | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since other libraries don’t implement this method and wallets appear not to support them, I’ve moved it to experimental. I’ll eventually remove it if I don’t find a wallet that supports it. |
||
*/ | ||
public async updateEthereumChain( | ||
param: UpdateEthereumChainRequest, | ||
|
@@ -82,12 +102,17 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | |
} | ||
|
||
/** | ||
* Switch the wallet’s currently active chain. | ||
* Switch the wallet's currently active chain. | ||
* If the specified chain does not exist in the wallet, an error will be thrown. | ||
* To prevent errors, ensure the chain has been added first or handle the call within a try/catch block. | ||
* | ||
* See [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326) for more details. | ||
* | ||
* @param param - Chain ID of the chain to switch to | ||
* @returns a Promise that resolves if the request is successful | ||
* @param chainId - The ID of the chain to switch to. | ||
* @returns A Promise that resolves if the chain switch is successful. | ||
* | ||
* @example | ||
* await web3.walletRpc.switchEthereumChain(5000); | ||
*/ | ||
public async switchEthereumChain(chainId: Numbers): Promise<void> { | ||
return this.requestManager.send({ | ||
|
@@ -105,8 +130,9 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | |
* | ||
* 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 | ||
* @param param - Details of the request for owned assets. | ||
* @returns A Promise that resolves to a list of owned assets. | ||
* @experimental | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since other libraries don’t implement this method and wallets appear not to support them, I’ve moved it to experimental. I’ll eventually remove it if I don’t find a wallet that supports it. |
||
*/ | ||
public async getOwnedAssets( | ||
param: GetOwnedAssetsRequest, | ||
|
@@ -131,18 +157,83 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | |
* | ||
* See [EIP-747](https://eips.ethereum.org/EIPS/eip-747) for more details. | ||
* | ||
* @param param - Details of the asset to watch | ||
* @returns a Promise that resolves to `true` if the request is successful | ||
* @param param - Details of the asset to watch. | ||
* @returns A Promise that resolves to `true` if the request is successful. | ||
* | ||
* @example | ||
* await web3.walletRpc.watchAsset({ | ||
* type: "ERC20", | ||
* options: { | ||
* address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
* symbol: "USDC", | ||
* }, | ||
* }); | ||
*/ | ||
public async watchAsset(param: WatchAssetRequest): Promise<boolean> { | ||
return this.requestManager.send({ | ||
method: "wallet_watchAsset", | ||
params: [param], | ||
}); | ||
} | ||
|
||
/** | ||
* Request permissions for a dApp. | ||
* | ||
* See [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255) for more details. | ||
* | ||
* @param param - Details of the permission request. | ||
* @returns A Promise that resolves to an array of granted permissions. | ||
* | ||
* @example | ||
* const permissions = await web3.walletRpc.requestPermissions({ | ||
* eth_accounts: {} | ||
* }); | ||
*/ | ||
public async requestPermissions( | ||
param: PermissionRequest, | ||
): Promise<Permission[]> { | ||
return this.requestManager.send({ | ||
method: "wallet_requestPermissions", | ||
params: [param], | ||
}); | ||
} | ||
|
||
/** | ||
* Retrieve the list of permissions granted to the dApp. | ||
* | ||
* See [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255) for more details. | ||
* | ||
* @returns A Promise that resolves to an array of granted permissions. | ||
* | ||
* @example | ||
* const permissions = await web3.walletRpc.getPermissions(); | ||
*/ | ||
public async getPermissions(): Promise<Permission[]> { | ||
return this.requestManager.send({ | ||
method: "wallet_getPermissions", | ||
params: [], | ||
}); | ||
} | ||
|
||
/** | ||
* Revoke permissions granted to the dApp. | ||
* | ||
* @param param - Details of the permissions to revoke. | ||
* @returns A Promise that resolves if the request is successful. | ||
* | ||
* @example | ||
* await web3.walletRpc.revokePermissions({ | ||
* eth_accounts: {} | ||
* }); | ||
*/ | ||
public async revokePermissions(param: PermissionRequest): Promise<void> { | ||
return this.requestManager.send({ | ||
method: "wallet_revokePermissions", | ||
params: [param], | ||
}); | ||
} | ||
} | ||
|
||
// Module Augmentation | ||
declare module "web3" { | ||
interface Web3Context { | ||
walletRpc: WalletRpcPlugin; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this file be checked-in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed for now, let's wait if others will have similar problems