Skip to main content

Ethereum Jsonrpc API

Ethereum Jsonrpc API for the Wallet SDK.

eth_requestAccounts#

Request access to the user's Ethereum account in order to interact with the blockchain, sign transactions, or perform other operations.

The request will trigger a prompt in the Binance app asking the user to grant the requested account(s) access to the DApp. If the user approves the request, the API will return a promise that resolves to an array of Ethereum account addresses that have been granted access.

Parameters#

[]

Returns#

string[]: An array of Ethereum account addresses.

Example#

const accounts = await client.request({ method: 'eth_requestAccounts',  params: [] })

eth_accounts#

Request the available accounts.

This API call does not request user permission or access to Ethereum accounts. Instead, it returns an array of Ethereum account addresses that are available to the currently connected Ethereum node.

Parameters#

[]

Returns#

string[]: An array of Ethereum account addresses.

Example#

const accounts = await client.request({ method: 'eth_accounts',  params: [] })

eth_chainId#

Retrieve the chain ID of the currently connected Ethereum network. 0x0 will be returned use default value.

Parameters#

[]

Returns#

string: Hexadecimal encoded value.

Example#

const chainId = await client.request({ method: 'eth_chainId',  params: [] })

eth_signTransaction#

Request the user to sign a transaction that can be submitted to the network at a later time using eth_sendRawTransaction.

Parameters#

EthTransaction

interface EthTransaction {  from: string // The address the transaction is send from.  to: string // The address the transaction is directed to.  gas?: string // OPTIONAL Integer of the gas provided for the transaction execution. It will return unused gas.  gasPrice?: string // OPTIONAL Integer of the gasPrice used for each paid gas  maxFeePergas?: string // OPTIONAL Includes both the base fee and the priority fee  maxPriorityFeePergas?: string // OPTIONAL The maximum priority fee that a user is willing to pay for a transaction  value?: string // OPTIONAL Integer of the value sent with this transaction  data: string // The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI  nonce?: string // OPTIONAL Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.}

Returns#

string: Signed signature

Example#

const signature = await client.request({ method: 'eth_signTransaction',  params: [{  from: account,  to: targetAddress,  value: '1',  data: '0x00'}] })

eth_sendTransaction#

Request the user to sign and send the transaction to the blockchain. Return a promise that resolves the transaction hash.

It is used to transfer native tokens or to interact with smart contracts deployed on the blockchain. Once a user initiates the transaction, it is broadcast to the blockchain and processed by nodes on the network. Miners then compete to include the transaction in the next block on the blockchain. Once the transaction is confirmed and included in a block, the transfer of Ether or interaction with the smart contract is executed, and the transaction is considered complete.

Parameters#

EthTransaction

interface EthTransaction {  from: string // The address the transaction is send from.  to: string // The address the transaction is directed to.  gas?: string // OPTIONAL Integer of the gas provided for the transaction execution. It will return unused gas.  gasPrice?: string // OPTIONAL Integer of the gasPrice used for each paid gas  maxFeePergas?: string // OPTIONAL Includes both the base fee and the priority fee  maxPriorityFeePergas?: string // OPTIONAL The maximum priority fee that a user is willing to pay for a transaction  value?: string // OPTIONAL Integer of the value sent with this transaction  data: string // The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI  nonce?: string // OPTIONAL Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.}

Returns#

string: Transaction hash which starts with '0x' prefix.

Example#

const txHash = await client.request({ method: 'eth_sendTransaction',  params: [{  from: account,  to: targetAddress,  value: '1',  data: '0x00'}] })

eth_sign#

The sign method calculates an Ethereum-specific signature with the following: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).

Adding a prefix to the message makes the calculated signature recognizable as an Ethereum-specific signature. This prevents misuse where a malicious Dapp could sign arbitrary data (e.g., a transaction) and use the signature to impersonate the victim.

Note: This method will deprecate in the future due to potential security risks.

Parameters#

Address, Message

Returns#

string: Signature

Example#

const message = "Hello Binance SDK"const signature = await client.request({ method: 'eth_sign',  params: [account, message] })

personal_sign#

The sign method calculates an Ethereum-specific signature with the following: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))). This is the recommended method for signing messages on the DApp side.

Adding a prefix to the message makes the calculated signature recognizable as an Ethereum-specific signature. This prevents misuse where a malicious Dapp can sign arbitrary data (e.g., a transaction) and use the signature to impersonate the victim.

Note: See ecRecover to verify the signature.

Parameters#

Message, Address

Returns#

string: Signature

Example#

const message = "Hello Binance SDK"const signature = await client.request({ method: 'eth_sign',  params: [message, account] })

eth_signTypedData#

Calculates an Ethereum-specific signature in the form of keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))

Adding a prefix to the message makes the calculated signature recognizable as an Ethereum-specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g., a transaction) and use the signature to impersonate the victim.

Parameters#

Address, TypedData

  1. Address, 20 Bytes - address.
  2. TypedData, N Bytes - message to sign containing type information, a domain separator, and data

Returns#

string: Signature

Example#

const message = JSON.stringify({  domain: {    // Defining the chain aka Rinkeby testnet or Ethereum Main Net    chainId: '0x38',    // Give a user friendly name to the specific contract you are signing for.    name: 'Ether Mail',    // If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity    verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',    // Just let's you know the latest version. Definitely make sure the field name is correct.    version: '1',  },
  // Defining the message signing data content.  message: {    /*      - Anything you want. Just a JSON Blob that encodes the data you want to send      - No required fields      - This is DApp Specific      - Be as explicit as possible when building out the message schema.    */    contents: 'Hi, Starriv!',    attachedMoneyInEth: 4.2,    from: {      name: 'Starriv',      wallets: ['0x745191aA5dCbF685D27648370694e0b0C1b87A13'],    },    to: [      {        name: 'Leon',        wallets: [          '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',          '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57',          '0xB0B0b0b0b0b0B000000000000000000000000000',        ],      },    ],  },  // Refers to the keys of the *types* object below.  primaryType: 'Mail',  types: {    // TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on    EIP712Domain: [      { name: 'name', type: 'string' },      { name: 'version', type: 'string' },      { name: 'chainId', type: 'uint256' },      { name: 'verifyingContract', type: 'address' },    ],    // Not an EIP712Domain definition    Group: [      { name: 'name', type: 'string' },      { name: 'members', type: 'Person[]' },    ],    // Refer to PrimaryType    Mail: [      { name: 'from', type: 'Person' },      { name: 'to', type: 'Person[]' },      { name: 'contents', type: 'string' },    ],    // Not an EIP712Domain definition    Person: [      { name: 'name', type: 'string' },      { name: 'wallets', type: 'address[]' },    ],  },})const signature = await client.request({ method: 'eth_signTypedData',  params: [account, message] })

wallet_switchEthereumChain#

Prompt the user to switch to the new network. It returns a promise that resolves when the user has switched to the new network or rejects it with an error if the user cancels the switch or the switch is unsuccessful.

Parameters#

SwitchEthereumChainParameter

interface SwitchEthereumChainParameter {  chainId: string // A 0x-prefixed hexadecimal string}

Chain IDs#

These are the IDs of the Ethereum chains that Wallet SDK supports by default

NetworkChain ID
BNB Chain Mainnet0x38
Ethereum Mainnet0x1

Returns#

null: Null will be returned if switch successfully

Example#

await client.request({ method: 'wallet_switchEthereumChain',  params: {  chainId: '0x38'} })

wallet_watchAsset#

Request the user to add a token to the Binance Defi Wallet. It returns a promise that resolves when the user has added the asset to the wallet or rejects it with an error if the user cancels the operation or the operation is unsuccessful.

Only the token address is required when requested; the wallet will determine the token symbol and token decimals automatically.

Note: Currently, Binance Defi Wallet only supports ERC20 tokens. Other standards will be supported in the future.

Parameters#

WatchAssetParameter

interface WatchAssetParameter {  type: 'ERC20' // In the future, other standards will be supported  options: {    address: string // The address of the token contract  }}

Returns#

boolean: true if the the token was added, false otherwise.

Example#

await client.request({ method: 'wallet_watchAsset',  params: {  type: 'ERC20',  options: {    address: '0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c'  }} })