RPC API
TokenPocket Extension uses the ethereum.request(args) to wrap an RPC API.
The API is based on an interface exposed by all Ethereum clients, along with a growing number of methods that may or may not be supported by other wallets.
Tip
All RPC method requests can return errors. Make sure to handle errors for every call to ethereum.request(args)
Ethereum JSON-RPC Methods
For the Ethereum JSON-RPC API, please see the Ethereum wiki
Important methods from this API include:
Restricted Methods
TokenPocket Extension introduced Web3.0 Wallet Permissions via EIP-2255. In this permissions system, each RPC method is either restricted or unrestricted. If a method is restricted, the caller must have the corresponding permission in order to call it. Unrestricted methods, meanwhile, have no corresponding permission. Some of them still rely upon permissions to succeed though (e.g., the signing methods require that you have the eth_accounts permission for the signer account), and some require confirmation by the user (e.g., wallet_addEthereumChain).
Under the hood, permissions are plain, JSON-compatible objects, with a number of fields that are mostly used internally by TokenPocket Extension. The following interface lists the fields that may be of interest to consumers:
If you're interested in learning more about the theory behind this capability-inspired permissions system, we encourage you to take a look at EIP-2255
eth_requestAccounts
eth_requestAccounts
EIP-1102
This method is specified by EIP-1102. It is equivalent to the deprecated ethereum.enable() provider API method.
Under the hood, it calls wallet_requestPermissions for the eth_accounts permission. Since eth_accounts is currently the only permission, this method is all you need for now.
Returns
string[]
- An array of a single, hexadecimal Ethereum address string.
Description
Requests that the user provides an Ethereum address to be identified by. Returns a Promise that resolves to an array of a single Ethereum address string. If the user denies the request, the Promise will reject with a 4001 error.
The request causes a TokenPocket Extension pop-up to appear. You should only request the user's accounts in response to user action, such as a button click. You should always disable the button that caused the request to be dispatched, while the request is still pending.
Example
wallet_getPermissions
wallet_getPermissions
Platform Availability
This RPC method is not yet available in TokenPocket Extension Mobile.
Returns
Web3WalletPermission[]
- An array of the caller's permissions.
Description
Gets the caller's current permissions. Returns a Promise that resolves to an array of Web3.0 Wallet Permission objects. If the caller has no permissions, the array will be empty.
wallet_requestPermissions
wallet_requestPermissions
Platform Availability
This RPC method is not yet available in TokenPocket Extension Mobile.
Parameters
Array
RequestedPermissions
- The requested permissions.
Returns
Web3WalletPermission[]
- An array of the caller's permissions.
Description
Requests the given permissions from the user. Returns a Promise that resolves to a non-empty array of Web3.0WalletPermission objects, corresponding to the caller's current permissions. If the user denies the request, the Promise will reject with a 4001 error.
The request causes a TokenPocket Extension popup to appear. You should only request permissions in response to user action, such as a button click.
Example
Unrestricted Methods
eth_decrypt
eth_decrypt
Platform Availability
This RPC method is not yet available in TokenPocket Extension Mobile.
Parameters
Array
string
- An encrypted message.string
- The address of the Ethereum account that can decrypt the message.
Returns
string
- The decrypted message.
Description
Requests that TokenPocket Extension decrypts the given encrypted message. The message must have been encrypted using the public encryption key of the given Ethereum address. Returns a Promise that resolves to the decrypted message, or rejects if the decryption attempt fails.
See eth_getEncryptionPublicKey for more information.
Example
eth_getEncryptionPublicKey
eth_getEncryptionPublicKey
Platform Availability
This RPC method is not yet available in TokenPocket Extension Mobile.
Parameters
Array
string
- The address of the Ethereum account whose encryption key should be retrieved.
Returns
string
- The public encryption key of the specified Ethereum account.
Description
Requests that the user shares their public encryption key. Returns a Promise that resolves to the public encryption key, or rejects if the user denies the request.
The public key is computed from entropy associated with the specified user account, using the nacl implementation of the X25519_XSalsa20_Poly1305 algorithm.
Example
Encrypting
wallet_addEthereumChain
wallet_addEthereumChain
EIP-3085
This method is specified by EIP-3085
Parameters
Array
AddEthereumChainParameter
- Metadata about the chain that will be added to TokenPocket Extension.
For the rpcUrls
and blockExplorerUrls
arrays, at least one element is required, and only the first element will be used.
Returns
null
- The method returns null if the request was successful, and an error otherwise.
Description
Creates a confirmation asking the user to add the specified chain to TokenPocket Extension. The user may choose to switch to the chain once it has been added.
As with any method that causes a confirmation to appear, wallet_addEthereumChain
should only be called as a result of direct user action, such as the click of a button.
TokenPocket Extension stringently validates the parameters for this method, and will reject the request if any parameter is incorrectly formatted.
If the RPC endpoint doesn't respond to RPC calls.
If the RPC endpoint returns a different chain ID when eth_chainId is called.
If the chain ID corresponds to any default TokenPocket Extension chains.
TokenPocket Extension does not yet support chains with native currencies that do not have 18 decimals, but may do so in the future.
Useage with wallet_switchEthereumChain
We recommend using this method with wallet_addEthereumChain:
wallet_switchEthereumChain
wallet_switchEthereumChain
EIP-3326
This method is specified by EIP-3326
Parameters
Array
SwitchEthereumChainParameter
- Metadata about the chain that TokenPocket Extension will switch to.
Returns
Returns
null
- The method returns null if the request was successful, and an error otherwise.
If the error code (error.code) is 4902, then the requested chain has not been added by TokenPocket Extension, and you have to request to add it via wallet_addEthereumChain.
Description
Tip
See above for how to use this method with wallet_addEthereumChain
Creates a confirmation asking the user to switch to the chain with the specified chainId.
As with any method that causes a confirmation to appear, wallet_switchEthereumChain
should only be called as a result of a direct user action, such as the click of a button.
TokenPocket Extension will automatically reject the request under the following circumstances:
If the chain ID is malformed
If the chain with the specified chain ID has not been added to TokenPocket Extension
wallet_registerOnboarding
wallet_registerOnboarding
Tip
As an API consumer, you are unlikely to have to call this method yourself.
Returns
boolean
- true
if the request was successful, false
otherwise.
Description
Registers the requesting site with TokenPocket Extension as the initiator of onboarding. Returns a Promise that resolves to true, or rejects if there's an error.
This method is intended to be called after TokenPocket Extension has been installed, but before the TokenPocket Extension onboarding has been completed. You can use this method to inform TokenPocket Extension that you were the one that suggested installing TokenPocket Extension. This lets TokenPocket Extension redirect the user back to your site after onboarding has been completed.
wallet_watchAsset
wallet_watchAsset
EIP-747
This method is specified by EIP-747
Parameters
WatchAssetParams
- The metadata of the asset to watch.
Returns
boolean
- true
if the token was added, false
otherwise.
Description
Requests that the user tracks the token in TokenPocket Extension. Returns a boolean indicating if the token was successfully added.
Most Ethereum wallets support some set of tokens, usually from a centrally curated registry of tokens. wallet_watchAsset enables Web3.0 application developers to ask their users to track tokens in their wallets, at runtime. Once added, the token is indistinguishable from those added via legacy methods, such as a centralized registry.
Example
Last updated