GithubHelp home page GithubHelp logo

decentraland-crypto's Introduction

DCL Auth Crypto Lib

NPM version Install Size

How to Install

  npm i dcl-crypto

Create a new Identity using Ethers

import type { AuthChain, AuthIdentity } from 'dcl-crypto/dist/types'
import Authenticator from 'dcl-crypto/dist/Authenticator'
import { Wallet } from '@ethersproject/wallet'
import { Web3Provider, ExternalProvider } from '@ethersproject/providers'

/**
 *
 * @params provider - any ethereum provider (e.g: window.ethereum)
 * @params expiration - ttl in seconds of the identity
 */
export async function createIdentity(provider: ExternalProvider, expiration: number): Promise<AuthIdentity> {
  const signer = new Web3Provider(provider).getSigner()
  const address = await signer.getAddress()

  const wallet = Wallet.createRandom()
  const payload = {
    address: wallet.address,
    privateKey: wallet.privateKey,
    publicKey: wallet.publicKey,
  }

  const identity = await Authenticator.initializeAuthChain(
    address,
    payload,
    expiration,
    (message) => new Web3Provider(provider).getSigner().signMessage(message)
  )

  return identity
}

Create a new Identity using Web3x

import type { AuthChain, AuthIdentity } from 'dcl-crypto/dist/types'
import Authenticator from 'dcl-crypto/dist/Authenticator'
import { Eth } from 'web3x/eth'
import { Address } from 'web3x/address'
import { Account } from 'web3x/account'
import { Personal } from 'web3x/personal'
import { EthereumProvider } from 'web3x/providers'
import { bufferToHex } from 'web3x/utils/hex-buffer'

/**
 *
 * @params provider - any ethereum provider (e.g: window.ethereum)
 * @params expiration - ttl in seconds of the identity
 */
export async function createIdentity(provider: EthereumProvider, expiration: number): Promise<AuthIdentity> {
  const eth = new Eth(provider)
  const addresses = await eth.getAccounts()

  const provider = connection.provider
  const account = Account.create()
  const payload = {
    address: account.address.toString(),
    publicKey: bufferToHex(account.publicKey),
    privateKey: bufferToHex(account.privateKey),
  }

  const identity = await Authenticator.initializeAuthChain(
    addresses[0],
    payload,
    expiration,
    (message) =>
      new Personal(provider as any).sign(
        message,
        Address.fromString(address),
        ''
      )
  )

  return identity
}

decentraland-crypto's People

Contributors

2fd avatar marcosnc avatar nachomazzara avatar nchamo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.