Creating SIWE Messages
This section describes how to generate Sign-In with Ethereum messages and print them to the console.
Creating SIWE messages in JavaScript is straightforward when using the
siwe
library in npm. To begin, create a new project called siwe-print
. mkdir siwe-print && cd siwe-print/
yarn init --yes
yarn add siwe ethers
mkdir src/
We can then write the following into
./src/index.js
:src/index.js
const siwe = require('siwe');
const domain = "localhost";
const origin = "https://localhost/login";
function createSiweMessage (address, statement) {
const siweMessage = new siwe.SiweMessage({
domain,
address,
statement,
uri: origin,
version: '1',
chainId: '1'
});
return siweMessage.prepareMessage();
}
console.log(createSiweMessage(
"0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94",
"This is a test statement."
));
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 Ethereum account:
0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94