3.4.5 • Published 8 months ago

@dcl/crypto v3.4.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

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
}
3.4.4

9 months ago

3.4.3

10 months ago

3.4.5

8 months ago

3.4.2

11 months ago

3.4.1

12 months ago

3.4.0

1 year ago

3.2.4

1 year ago

3.3.1

1 year ago

3.2.3

1 year ago

3.2.2

2 years ago

3.1.3

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago