Sign-In with EVM-Compatible Chains
Part of our continued work at Spruce is to make it easy for developers and the broader Ethereum community to use Sign-In With Ethereum. Sign-In with Ethereum, otherwise known as SIWE or EIP-4361, describes how Ethereum accounts authenticate with off-chain services by signing a standard message format. It gives users the power to authenticate with their Ethereum keys in a simple workflow:
You may be thinking, “It’s Sign-In with Ethereum, but can it work with Layer 2 solutions and other EVM-compatible chains?”
If you are a developer, you might want to enable users of your app to:
- Sign-In with Polygon
- Sign-In with Avalanche
- Sign-In with Optimism
- Sign-In with Binance Smart Chain
Fortunately, Sign-In with Ethereum isn’t just for Ethereum. It does, in fact, work with L2s and other EVM-compatible chains, and it’s quite easy to set up with the existing SIWE libraries.
Ethereum Layer 2 (L2s) scaling solutions are off-chain scaling networks built on top of the Ethereum blockchain to improve scalability and efficiency and reduce the cost of performing transactions on Ethereum. These networks leverage Ethereum Mainnet’s security guarantee and technical advancements while affording higher transactional throughput. The L2s are built using various scaling solutions, such as rollups (ZK-rollups and optimistic rollups) and state channels. Some examples of L2s are Optimism, Arbitrum, and Polygon.
Ethereum Virtual Machine (EVM) is the computational layer that sits on top of the hardware and node network layer for Ethereum. It executes smart contracts and computes the current state of the network, allowing developers to create a wide range of secure and programmable applications. An EVM-compatible chain is a blockchain or a network that has the ability to run and deploy the smart contract code that can be recognized by Ethereum nodes. Some examples of EVM-compatible chains are Binance Smart Chain, Avalanche, and Fantom. EVM networks do also include the previously outlined L2s, such as Optimism, Arbitrum, and Polygon.
How can you use SIWE to enable users to sign in with L2s or other EVM-compatible chains?
SIWE messages have a set of required fields, one of which is “chainID.” chainID is a unique identifier assigned to each blockchain network. This identifier protects from transactions in one chain being accidentally included in another chain.
This concept was first introduced in EIP-155 as a means to prevent replay attacks. Replay attacks are exploits in which an attacker intercepts a legitimate transaction and replays it on another network, potentially exploiting a vulnerability or causing damage. There are several ways to prevent replay attacks, such as using a nonce to ensure each transaction is unique or implementing validation checks to verify the legitimacy of the signature and sender’s address.
A unique chainID assigned to each EVM-compatible chain ensures that transactions created for one chain aren’t replayed on another.
The easiest way to install SIWE (for Ethereum or, as we’ll show here, for L2s or other EVM-compatible chains) into your dapp is with our SSX library. If you’re new to SSX, check out our documentation or our guide on setting up SSX with Next.js:
Assuming you have an SSX-enabled dapp, let’s get started.
Customizing Fields in the SIWE Message
First, you can customize fields, such as the chainID, with the siweConfig
option.
SSX enables developers to configure the fields of their SIWE message using the siweConfig
option. This option allows you to overwrite the fields found in the SIWE message:
const signIn = async () => {
const ssx = new SSX){
siweConfig: {
chainId: 1, // set the chainid to your preferred chain
},
});
};
A dapp developer can change the chainID to any L2 or EVM-compatible chain and let users authenticate using that chain.
A list of EVM networks and their associated chainID can be found here. For example, you’d set chainID to 137 if you want to let your users Sign-In with Polygon. Here are a few other examples:
- Sign-In with Avalanche, set chainID to 43114
- Sign-In with Optimism, set chainID to 10
- Sign In with Arbitrum, set chainID to 42161
- Sign-In Fantom, set chainID to 250
- Sign-In with Binance Smart Chain, set chainID to 56
And you’re done - it’s that simple! Your users can now authenticate with any EVM-compatible chains you want to enable.
About Spruce: Spruce is building a future where users control their identity and data across all digital interactions. If you're curious about integrating Spruce's open-source libraries, such as Sign-In with Ethereum or SSX (as described here) into your project, chat with us in Discord or check out our libraries on Github.