We can then write the following into ./src/index.js:
src/index.js
constsiwe=require('siwe');constdomain="localhost";constorigin="https://localhost/login";functioncreateSiweMessage(address,statement){constsiweMessage=newsiwe.SiweMessage({domain,address,statement,uri:origin,version:'1',chainId:'1'});returnsiweMessage.prepareMessage();}console.log(createSiweMessage("0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94","This is a test statement." ));
Now run the example:
You should see output similar to the following message, with different values for the Nonce and Issued At fields:
To learn about all the available fields in a SiweMessage, check out the information in EIP-4361
The fields we are most interested in for the purposes of this guide are address and statement. address is the Ethereum address which the user is signing in with, and the statement as this will describe to the user what action we wish to perform on their behalf.
Often, as in this example, we don't need to do any manipulation of the message, so we can immediately convert it into the textual representation that the user will sign.
localhost wants you to sign in with your Ethereum account:
0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94
This is a test statement.
URI: https://localhost/login
Version: 1
Chain ID: 1
Nonce: oNCEHm5jzQU2WvuBB
Issued At: 2022-01-28T23:28:16.013Z