Creating a Sign In With Web3 (SIWWeb3) Message
This section describes how to generate Sign-In with Web3 (SIWWeb3) message
Creating SIWWeb3 messages in JavaScript is straightforward when using the "@web3auth/sign-in-with-web3"
npm library. To begin, create a new project
called sign-in-project.
mkdir createSIWWeb3Message && cd createSIWWeb3Message/
npm init --yes
npm install --save @web3auth/sign-in-with-web3 @babel/runtime
We can then write the following into index.js
const { Header, Payload, SIWWeb3 } = require("@web3auth/sign-in-with-web3");
const domain = "localhost";
const origin = "https://localhost/login";
function createWeb3Message(address, statement) {
const header = new Header();
header.t = "eip191";
const payload = new Payload();
payload.domain = domain;
payload.address = address;
payload.uri = origin;
payload.statement = statement;
payload.version = "1";
payload.chainId = "1";
const message = new SIWWeb3({
header,
payload,
network: "ethereum", // allowed values "solana", "ethereum", "starkware"
});
return message.prepareMessage();
}
console.log(createWeb3Message("0xB22E9a1b8ee50C09a59a2fFACEeF561E9D69A493", "Hello world!"));
Now run the example:
node index.js
You should see output similar to the following message, with different values for the Nonce
and Issued At
fields:
localhost wants you to sign in with your Ethereum account:
0xB22E9a1b8ee50C09a59a2fFACEeF561E9D69A493
Hello world!
URI: https://localhost/login
Version: 1
Chain ID: 1
Nonce: vqhbWribneGHKl9uv
Issued At: 2022-06-02T05:24:24.638Z
Learn about all the available fields in a SIWWeb3 Message
The fields we are most interested in for this guide are the address and the statement. The address is the Web3 address which the user is signing in with, and the statement will describe to the user what action performed. Often, as in this example, we don't need to manipulate the message. We can immediately convert it into the textual representation that the user will sign.