# Wallet Switch Starknet Chain RPC Method (\`wallet\_switchStarknetChain\`)

**URL:** <https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034>\
**Category:** SNIPs\
**Created:** [October 29, 2023, 2:45am UTC](https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034 "2023-10-29T02:45:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darlington02](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/darlington02/32/77632_2.png) [@Darlington02](https://community.starknet.io/u/Darlington02)\
**Post date:** [October 29, 2023, 2:45am UTC](https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034/1 "2023-10-29T02:45:02Z")

</div>

## Simple Summary

An RPC method for switching the wallet’s active Starknet Chain.

Inspired by [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326).

## Abstract

The `wallet_switchStarknetChain` RPC method allows Starknet decentralized applications (“dapps”) to request that the wallet switches its active Starknet chain. The caller must specify a chain ID, and the method returns `null` if the active chain was switched, and an error otherwise.

Important cautions for implementers of this method are included in the [Security Considerations](#security-consideration) section.

## Motivation

With the deprecation of `goerli2`, all dapps on Starknet are built on either `mainnet` or testnet - `goerli`, with some dapps implementing both e.g block explorers.

Most wallets only supports interacting with one chain at a time. We call this the wallet’s “active chain”. Since Starknet users are expected to interact with either of both chains, [`mainnet` or `goerli`], it’s important to enable dapps to be able to switch the user’s chain to the appropriate one.

`wallet_switchStarknetChain` enables dapps to request that the wallet switches its active chain to whichever one is required by the dapp. This improves the UX for both dapps and wallets by a long mile.

## Specification

### Method - `wallet_switchStarknetChain`

The method accepts a single object parameter with a `chainId` field. The method returns `null` if the wallet switched its active chain, and an error otherwise.

**NB:** The method presupposes that the wallet has a concept of a single “active chain”. The active chain is defined as the chain that the wallet is forwarding RPC requests to.

```ts
export interface SwitchStarknetChainParameter {
  chainId: string // A 0x-prefixed hexadecimal string
}

```

### Parameters

The method as specified above, accepts a single parameter `chainId`.

The `chainId`:

- MUST be the integer ID of the chain, specified as a 0x-prefixed hexadecimal string.

The wallet:

- MUST recognize the specified ID, able to switch to the specified chain and capable of servicing RPC requests to it.

### Returns

The method MUST return `null` if the request was successful, and an error otherwise.

### Example

These examples use JSON-RPC, but the method could be implemented using other RPC protocols.

To switch to Mainnet:

```JSON
{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "wallet_switchStarknetChain",
  "params": [
    {
      "chainId": "0x534e5f4d41494e",
    }
  ]
}

```

To switch to the Goerli testnet:

```JSON
{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "wallet_switchStarknetChain",
  "params": [
    {
      "chainId": "0x534e5f474f45524c49",
    }
  ]
}

```

Code example with `get-starknet`:

**Request**

```ts
await window.starknet.request({
    type: "wallet_switchStarknetChain",
    params: {
        chainId: "SN_MAIN"
    }
});

```

**Result**

```bash
"null"

```

### Practical example as implemented by Element

A dapp that currently implements this standard, is [Element](https://element.market/starknet). When a user connects his wallet, but on the wrong chain, he gets prompted to connect to the right chain.

 ![Screenshot 2023-10-28 at 22.37.06](https://europe1.discourse-cdn.com/flex005/uploads/starknet1/original/3X/4/8/48631d73a1303323d2015aa0b2b0528c89201dc2.png)

The request to switch network, looks like this on the Argent X wallet, but could be improved:

 ![Screenshot 2023-10-28 at 22.37.49](https://europe1.discourse-cdn.com/flex005/uploads/starknet1/original/3X/b/3/b366fd470bafd5da849a7e75fae60b1372d88c14.png)

## Security Consideration

For wallets with a concept of an active chain, switching the active chain has significant implications for pending RPC requests and the user’s experience. If the active chain switches without the user’s awareness, a malicious dapp could induce the user to take actions for unintended chains.

In light of this, the wallet:

- SHOULD display a confirmation whenever a `wallet_switchStarknetChain` request is received, clearly identifying the requester and the chain that will be switched to.
- SHOULD cancel all pending RPC requests and chain-specific user confirmations when switching.

---

<div class="post-metadata">

**Author:** ![avimak](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/avimak/32/539_2.png) [@avimak](https://community.starknet.io/u/avimak)\
**Post date:** [December 6, 2023, 7:06pm UTC](https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034/2 "2023-12-06T19:06:17Z")

</div>

> [@Darlington02](#):
>
> `chainId: "SN_MAIN"`

Shouldn’t it be `"0x534e5f4d41494e"` instead of `"SN_MAIN"`?

---

<div class="post-metadata">

**Author:** ![Darlington02](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/darlington02/32/77632_2.png) [@Darlington02](https://community.starknet.io/u/Darlington02)\
**Post date:** [December 11, 2023, 1:22am UTC](https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034/3 "2023-12-11T01:22:44Z")

</div>

Hello @avimak, apologies for late replies. With Argent X, we support both the hex value `0x534e5f4d41494e` and the short string ‘SN\_MAIN’. I was hoping we could finalize on one of both, and ‘SN\_MAIN’ seemed like a better way to go, but let me know what you think:)

---

<div class="post-metadata">

**Author:** ![avimak](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/avimak/32/539_2.png) [@avimak](https://community.starknet.io/u/avimak)\
**Post date:** [December 27, 2023, 8:11pm UTC](https://community.starknet.io/t/wallet-switch-starknet-chain-rpc-method-wallet-switchstarknetchain/102034/4 "2023-12-27T20:11:06Z")

</div>

It makes sense to follow the `starknet_chainId` specifications, which work with `schemas/CHAIN_ID`, defined as hex -

```auto
            "CHAIN_ID": {
                "title": "Chain id",
                "description": "StarkNet chain id, given in hex representation.",
                "type": "string",
                "pattern": "^0x[a-fA-F0-9]+$"
            },

```

Hence, I would’ve gone with the hex value (only).
