Socket
Socket
Sign inDemoInstall

@dcl/crypto

Package Overview
Dependencies
16
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @dcl/crypto

Crypto auth for Decentraland


Version published
Weekly downloads
1.5K
decreased by-19.4%
Maintainers
1
Install size
4.73 MB
Created
Weekly downloads
 

Readme

Source

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
}

Keywords

FAQs

Last updated on 04 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc