0.0.1 • Published 28 days ago

web3-safe-token v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
28 days ago

Web3Token

Client

import { Web3Token, generateNonce } from "web3token";

// All users have a same messageBody
const messageBody = {
    domain: "example.com",
    statement: "Please Sign the Message.",
    uri: "https://example.com",
    version: "1",
    chainId: 1
}

const w3token = new Web3Token(messageBody);

// Each user have different payload as the part of the message
const payload = {
    address: "0x1234567890123456789012345678901234567890",
    //nonce: generateNonce(), //siwe MUST have a nonce
    //issuedAt: iat.toISOString(),
    //expirationTime: exp.toISOString(),
}

const expiresIn = 12 * 60 * 60 * 1000;

const message = w3token.prepareMessage(payload, expiresIn);

//Invoke the wallet and sign the `message`
//...
//const signature = await sign(message, privateKey);

const token = w3token.create(signature); //Obtain the token

//Set the token in request headers and request the server
//... headers:{'Authorization': `Bear ${token}`}
;

Server

import { Web3Token } from "web3token";

//Get the token from the request headers
const token = req.headers.authorization.split(' ')[1]

//The server has the same messageBody
const messageBody = {
    domain: "example.com",
    statement: "Please Sign the Message.",
    uri: "https://example.com",
    version: "1",
    chainId: 1
}

const maxAge = 7*24*60*60; // The server can control the maxAge of the token

const w3token = new Web3Token(messageBody);

try{
    const decoded = w3token.verify(token, maxAge);
    const addr = decoded.payload.address;
    //...
}catch(e){
    //...
}
0.0.1

28 days ago