Web3 Token authenticate using the EIP-4361 specification.
This package is intended to be used as a Turborepo internal package
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';
const token = await sign({
expiresIn,
expiresAt,
issuedAt,
notBefore,
statement,
domain,
nonce,
requestId,
chainId,
omitStatementPayload,
});
expiresIn?: number | string
— time to expiry in milliseconds or usingms
e.g.30m
expiresAt?: number
— the exact time of expiry in millisecondsissuedAt?: number | boolean
— specify the issued time or current time if set truenotBefore: number
— after what time it should be validstatement?: string
— user facing statement, at the top of the signing messagedomain?: string
— domain the token is attact tononce?: number | string | boolean
— nonce for the token, if set true uuidv4 will be usedrequestId?: string
— request id for the tokenchainId?: number
— chain id for the tokenomitStatementPayload?: boolean
— omit the statement from the token payload, the exact same statement used to sign will have to be defined when verifying the token