All Projects → bytesbay → web3-token

bytesbay / web3-token

Licence: other
Web3 Token is a new way to authenticate users in a hybrid dApps using signed messages. Implementation of EIP-4361.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to web3-token

dxvote
Governance Dapp of DXdao
Stars: ✭ 28 (-92.13%)
Mutual labels:  web3
Before-Bitcoin
Book about the ideological context of cryptocurrency. 'Before Bitcoin' written by Pet3rpan.
Stars: ✭ 22 (-93.82%)
Mutual labels:  web3
geesome-node
🦈 Your self-hosted decentralized Messenger, Social network, Media file storage on top of IPFS! Freely communicate in encrypted chat groups, share images, video, text or any data without a risk of censorship or blocking.
Stars: ✭ 90 (-74.72%)
Mutual labels:  web3
trystero
🤝 Serverless WebRTC matchmaking for painless P2P — Make any site multiplayer in a few lines — Use BitTorrent, IPFS, or Firebase
Stars: ✭ 512 (+43.82%)
Mutual labels:  web3
tool-db
A peer-to-peer decentralized database
Stars: ✭ 15 (-95.79%)
Mutual labels:  web3
useWeb3
useWeb3 provides a curated overview of the best and latest resources on Ethereum, blockchain and Web3 development.
Stars: ✭ 325 (-8.71%)
Mutual labels:  web3
Web3swift
Elegant Web3js functionality in Swift. Native ABI parsing and smart contract interactions.
Stars: ✭ 237 (-33.43%)
Mutual labels:  web3
oxo-chat-client
基于websocket、json、blockchain的公告、聊天(客户端到客户端加密)客户端。账号无需注册,本地生成!
Stars: ✭ 52 (-85.39%)
Mutual labels:  web3
erebos
JavaScript client and CLI for Swarm
Stars: ✭ 47 (-86.8%)
Mutual labels:  web3
frontend-v2
Frontend app for the Balancer protocol
Stars: ✭ 127 (-64.33%)
Mutual labels:  web3
resources
A living collection of resources for participants (and anyone who's interested) in Truffle University's courses 📚
Stars: ✭ 27 (-92.42%)
Mutual labels:  web3
play
playproject
Stars: ✭ 22 (-93.82%)
Mutual labels:  web3
nft-swap-sdk
Ethereum's missing p2p NFT and token swap library for web3 developers. Written in TypeScript. Powered by 0x.
Stars: ✭ 200 (-43.82%)
Mutual labels:  web3
polkadot-wiki-old
The Polkadot wiki.
Stars: ✭ 56 (-84.27%)
Mutual labels:  web3
mev-inspect-rs
Discover historic Miner Extractable Value (MEV) opportunities
Stars: ✭ 443 (+24.44%)
Mutual labels:  web3
Web3Pass
Your Web3 in one shot 🍻
Stars: ✭ 54 (-84.83%)
Mutual labels:  web3
airswap-web
AirSwap Web App
Stars: ✭ 94 (-73.6%)
Mutual labels:  web3
react-mix
Everything you need to use React with Brownie!
Stars: ✭ 58 (-83.71%)
Mutual labels:  web3
circles-myxogastria
Webapp and mobile client for Circles
Stars: ✭ 32 (-91.01%)
Mutual labels:  web3
crypto-high-score
Add your name to the global, immutable leaderboard by paying in ETH.
Stars: ✭ 18 (-94.94%)
Mutual labels:  web3

Project Presentation

Web3 Token

Web3 Token is a new way to authenticate users. See this article for more info. Implementation of EIP-4361.

Version 0.2 updates 🎉

  • I'm now 90% following EIP-4361. Why 90%? Because i don't like some things in that standard that makes it more difficult to use it for developers.
  • body (3rd parameter) is now deprecated.

Version 1.0 updates 🥂

  • Package codebase moved to Typescript

Install

With web3 package:

$ npm i web3-token web3

or with ethers package:

$ npm i web3-token ethers

Example usage (Client side)

Using Web3 package:

import Web3 from 'web3';
import Web3Token from 'web3-token';

// Connection to MetaMask wallet
const web3 = new Web3(ethereum);
await ethereum.request({ method: 'eth_requestAccounts'});

// getting address from which we will sign message
const address = (await web3.eth.getAccounts())[0];

// generating a token with 1 day of expiration time
const token = await Web3Token.sign(msg => web3.eth.personal.sign(msg, address), '1d');

// attaching token to authorization header ... for example

Using Ethers package:

import { ethers } from "ethers";
import Web3Token from 'web3-token';

// Connection to MetaMask wallet
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();

// generating a token with 1 day of expiration time
const token = await Web3Token.sign(async msg => await signer.signMessage(msg), '1d');

// attaching token to authorization header ... for example

Example usage (Server side)

const Web3Token = require('web3-token');

// getting token from authorization header ... for example
const token = req.headers['Authorization']

const { address, body } = await Web3Token.verify(token);

// now you can find that user by his address 
// (better to do it case insensitive)
req.user = await User.findOne({ address });

Handle exceptions

const generateToken = async () => {
  if (!window.ethereum) {
    return console.log('Please install and activate the metamask extension!');
  }

  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const signer = provider.getSigner();

  try {
    return await Web3Token.sign(async msg => {
      try {
        return await signer.signMessage(msg);
      }
      catch (err) {
        const { reason } = err;
        if (reason === "unknown account #0") {
          return console.log('Have you unlocked metamask and are connected to this page?')
        }

        console.log(err.toString());
      }
    }, '1d');
  }
  catch (err) {
    if (/returns a signature/.test(err.toString())) {
      return;
    }
    console.log(err.toString());
  }
}

Advanced usage with options (Client&Server side)

// I assume here a lot of things to be imported 😀

const token = await Web3Token.sign(async msg => await signer.signMessage(msg), {
  domain: 'worldofdefish.com',
  statement: 'I accept the WoD Terms of Service: https://service.org/tos',
  expires_in: '3 days',
  // won't be able to use this token for one hour
  not_before: new Date(Date.now() + (3600 * 1000)),
  nonce: 11111111,
});

const { address, body } = await Web3Token.verify(token, {
  // verify that received token is signed only for our domain
  domain: 'worldofdefish.com'
});

API

sign(signer, options)

Name Description Required Example
signer A function that returns a promise with signature string eg: web3.personal.sign(data, address) required (body) => web3.personal.sign(body, '0x23..1234')
options An options object or, if passed a string, will be used as an expires_in option optional (default: '1d') {} or '1 day'
options.expires_in A string that represents a time span (see ms module) or a number of milliseconds optional (default: 1d) '1 day'
options.not_before A date after which the token becomes usable optional new Date('12-12-2012')
options.expiration_time A date till when token is valid. Overwrites expires_in parameter optional new Date('12-12-2012')
options.statement A human-readable ASCII assertion that the user will sign, and it must not contain '\n' optional 'I accept the ServiceOrg Terms of Service: https://service.org/tos'
options.domain Authority that is requesting the signing. optional(Unless verifier won't ask for it) 'example.com'
options.nonce A randomized token used to prevent replay attacks, at least 8 alphanumeric characters. optional 12345678
options.request_id A system-specific identifier that may be used to uniquely refer to the sign-in request. optional 231

verify(token, options)

Name Description Required Example
token A token string that is generated from sign() required ...
options An options object optional { domain: 'example.com' }
options.domain The domain you want to accept optional 'example.com'

License

Web3 Token is released under the MIT license. © 2023 Miroslaw Shpak

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].