Skip to content

Latest commit

 

History

History
100 lines (82 loc) · 2.8 KB

README.md

File metadata and controls

100 lines (82 loc) · 2.8 KB

Web3 Token

Web3 Token authenticate using the EIP-4361 specification.

This package is intended to be used as a Turborepo internal package

Usage

import React from 'react';
import { EthersIntegration } from 'web3-token';
import { Web3Provider } from '@ethersproject/providers';

const Demo: React.FC<{ submit: (token: string) => void }> = ({ submit }) => {

  const provider = React.useMemo(() => {
    return new Web3Provider(window.ethereum, 'any');
  }, []);

  const onConnect = async () => {
    if (!provider) return;
    await provider.send("eth_requestAccounts", []);
    const signer = provider.getSigner();
    const address = await signer.getAddress();

    const token = await EthersIntegration.sign(
      signer,
      address,
      {
        statement: 'Welcome to Better T3 Stack!',
        expiresIn: '30m',
        issuedAt: true,
        nonce: true,
        requestId: 'request-id',
        domain: 'localhost',
        notBefore: Date.now(),
        chainId: 1,
      }
    );

    submit(token)

    // ...
    // Verify
    const { address, payload } = await EthersIntegration.verify(token, { domain: 'localhost' });
  }

  return (
    <button onClick={onConnect}>Sign with Web3 Token</button>
  )
}
// Omit statement from the token payload to reduce size
const token = await EthersIntegration.sign(
  signer,
  address,
  {
    statement: 'Web3 Token using omit statement!',
    omitStatementPayload: true,
  }
);

const { address, payload } = await EthersIntegration.verify(token, { statement: 'Web3 Token using omit statement!' });
// Predefined Integrations
import { EthersIntegration, EthereumjsIntegration, Web3Integration } from 'web3-token';

Reference

const token = await sign({
  expiresIn,
  expiresAt,
  issuedAt,
  notBefore,
  statement,
  domain,
  nonce,
  requestId,
  chainId,
  omitStatementPayload,
});
  • expiresIn?: number | string — time to expiry in milliseconds or using ms e.g. 30m
  • expiresAt?: number — the exact time of expiry in milliseconds
  • issuedAt?: number | boolean — specify the issued time or current time if set true
  • notBefore: number — after what time it should be valid
  • statement?: string — user facing statement, at the top of the signing message
  • domain?: string — domain the token is attact to
  • nonce?: number | string | boolean — nonce for the token, if set true uuidv4 will be used
  • requestId?: string — request id for the token
  • chainId?: number — chain id for the token
  • omitStatementPayload?: boolean — omit the statement from the token payload, the exact same statement used to sign will have to be defined when verifying the token