Skip to main content

Creating a Sign In With Solana (SIWS) Message


This section describes how to generate Sign-In with Solana (SIWS) message

Creating SIWS messages in JavaScript is straightforward when using the "@web3auth/sign-in-with-solana" npm library. To begin, create a new project called sign-in-project.

mkdir sign-in-project && cd sign-in-project/
npm init --yes
npm install --save @web3auth/sign-in-with-solana
mkdir src/

We can then write the following into ./src/index.js:

import { Header, Payload, SIWS } from "@web3auth/sign-in-with-solana";

const domain = "localhost";
const origin = "https://localhost/login";

function createSolanaMessage(address, statement) {
const header = new Header();
header.t = "sip99";

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 SIWS({
header,
payload,
});
return message.prepareMessage();
}

console.log(createSolanaMessage("4Cw1koUQtqybLFem7uqhzMBznMPGARbFS4cjaYbM9RnR", "Hello world!"));

Now run the example:

node src/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 Solana account:
4Cw1koUQtqybLFem7uqhzMBznMPGARbFS4cjaYbM9RnR

Hello world!

URI: https://localhost/login
Version: 1
Chain ID: 1
Nonce: oNCEHm5jzQU2WvuBB
Issued At: 2022-01-28T23:28:16.013Z
Learn about all the available fields in a SIWS Message

The fields we are most interested in for this guide are the address and the statement. The address is the Solana 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.