npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@didit-sdk/react

v1.0.11

Published

<a href="https://docs.didit.me/docs/sdk"> <img alt="didit-sdk" src="https://docs.didit.me/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fsdk_works.5dcf3190.png&w=3840&q=75" /> </a>

Downloads

259

Readme

Didit-SDK

The easiest way to connect to Didit protocol

Didit-SDK is a library that makes it easy to add wallet connection to your dapp.

  • Didit User Authentication flow
  • 🎉 Support for multiple frameworks. Easily integrate with React and vanilla JavaScript. Vue and Svelte are comming soon...
  • 🔥 Out-of-the-box wallet management
  • 🚀 EIP-6963. support for browser extension wallets.
  • 🎨 Easily customizable UI. light/dark or make it match your brand
  • 🦄 Built on top of wagmi

Repository

Didit SDK Github Repo

Try it out

You can use the CodeSandbox links below try out Didit Sdk:

  • with js // TODO: setup example on codesandbox

  • with react // TODO: setup example on codesandbox

  • with nextjs // TODO: setup example on codesandbox

React Installation

install didit-sdk and its peer dependencies, wagmi, viem and @tanstack/react-query

npm i @didit-sdk/react wagmi viem @tanstack/react-query

Configure your didit app

Create an app at Business Console and obtain your clientid and client secret

Implementation

For a quick integration you can use defaultWagmiConfig function which wraps Wagmi's createConfig function with a predefined configuration. This includes WalletConnect, Coinbase and Injected connectors In your main.ts file set up the following configuration.

On top of your app set up the following configuration, making sure that all functions are called outside any React component to avoid unwanted rerenders.

import { arbitrum, mainnet } from 'wagmi/chains'
import { defaultWagmiConfig, createDiditSdk } from '@didit-sdk/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { WagmiProvider } from 'wagmi'

// Get cleintId from https://business.didit.me
const clientId = process.env.DIDI_CLIENT_ID

// 1. Setup queryClient
const queryClient = new QueryClient()

// 2. Create wagmiConfig
const metadata = {
  name: 'React Example',
  url: 'https://react-example.me'
}

const wagmiConfig = defaultWagmiConfig({
  chains: [mainnet, arbitrum],
  metadata
})

// 3. creat didit sdk
createDiditSdk({
  wagmiConfig,
  clientId,
  metadata
})

export default function DiditSdkProvider({ children }) {
  return (
    <WagmiProvider config={wagmiConfig}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </WagmiProvider>
  )
}

<didit-callback> component is essential for socials auth to work check components for more

Next.js

Wagmi config

Create a new file for your Wagmi configuration, since we are going to be calling this function on the client and the server it cannot live inside a file with the 'use client' directive.

For this example we will create a file called config/index.tsx outside our app directory and set up the following configuration

import { arbitrum, mainnet } from 'wagmi/chains'
import { defaultWagmiConfig } from '@didit-sdk/react'
import { cookieStorage, createStorage } from 'wagmi'

// Get cleintId from https://business.didit.me
const clientId = process.env.NEXT_PUBLIC_DIDI_CLIENT_ID

// 2. Create wagmiConfig
const metadata = {
  name: 'React Example',
  url: 'https://react-example.me'
}

const wagmiConfig = defaultWagmiConfig({
  chains: [mainnet, arbitrum],
  metadata,
  ssr: true,
  storage: createStorage({
    storage: cookieStorage
  })
})
→ Notice that we are using here the [recommended configuration from Wagmi for SSR.](https://wagmi.sh/react/guides/ssr)
→ Using cookies is completely optional and by default Wagmi will use `localStorage` instead if the `storage` param is not defined.
→ The `ssr` flag will delay the hydration of the Wagmi's store to avoid hydration mismatch errors.

Context Provider

Let's create now a context provider that will wrap our application and initialized DiditSdk (createDiditSdk needs to be called inside a React Client Component file).

In this example we will create a file called context/index.tsx outside our app directory and set up the following configuration

use client'

import React, { ReactNode } from 'react'
import { config, clientId, metadata } from '@/config'

import { createDiditSdk } from '@didit-sdk/react'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

import { State, WagmiProvider } from 'wagmi'

// Setup queryClient
const queryClient = new QueryClient()

// Create sdk instance
createDiditSdk({
  wagmiConfig: config,
  clientId,
  metadata,
})

export default function DiditSdkProvider({
  children,
  initialState
}: {
  children: ReactNode
  initialState?: State
}) {
  return (
    <WagmiProvider config={config} initialState={initialState}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </WagmiProvider>
  )
}

Layout

Next, in our app/layout.tsx file, we will import our DiditSdkProvider component and call the Wagmi's function cookieToInitialState.

The initialState returned by cookieToInitialState, contains the optimistic values that will populate the Wagmi's store both on the server and client.

Hooks are functions that will help you control the sdk modal, subscribe to user session.

import './globals.css'
import type { Metadata } from 'next'
import { headers } from 'next/headers'

import { cookieToInitialState } from 'wagmi'

import { config } from '@/config'
import DiditSdkProvider from '@/context'

export const metadata: Metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app'
}

export default function RootLayout({
  children
}: Readonly<{
  children: React.ReactNode
}>) {
  const initialState = cookieToInitialState(config, headers().get('cookie'))
  return (
    <html lang="en">
      <body>
        <DiditSdkProvider initialState={initialState}>{children}</DiditSdkProvider>
      </body>
    </html>
  )
}

Callback

OAuth authentication require a redirection to the client application. redirectUri in the config should be a page in our app.

Let's create app/callback/index.tsx and add <didit-callback> web component

export default function Page() {
  return <didit-callback></didit-callback>
}

Web components are global html elements that don't require importing.

Trigger the modal

To open DiditSdk modal you can use our web component or build your own button with DiditSdk hooks. In this example we are going to use the <didit-button> component.

Web components are global html elements that don't require importing.

export default function ConnectButton() { return <didit-button /> }

Trigger the modal

To open DiditSdk modal you can use our web component or build your own button with DiditSdk hooks. In this example we are going to use the <didit-button> component.

Web components are global html elements that don't require importing.

export default function ConnectButton() { return <didit-button /> }

Hooks

Hooks are functions that will help you control the sdk modal, subscribe to user session.

useDiditSdk

Control the sdk modal with the useDiditSdk hook

import { useDiditSdk } from '@didit-sdk/react'

export default function Component() {
  const { isOpen, openModal, closeModal } = useDiditSdk()

  openModal()

  //...
}

DiditSdk state includes:

useDiditSignOut

import { useDiditSignOut } from '@didit-sdk/react'

const signOut = useDiditSignOut()

useDiditState

get Didit session state

import { useDiditState } from '@didit-sdk/react'

const { user, status, isAuthenticated, authMethod, selectedNetworkName } = useDiditState()

Didit state is an object of the following properties:

useDiditSdkTheme

import { useDiditSdkTheme } from '@didit-sdk/react'

export default function Component() {
  const { setThemeMode, themeMode, setThemeVariables, themeVariables } = useDiditSdkTheme()

  setThemeMode('dark')

  setThemeVariables({
    primaryColor: '#00BB7F'
  })
}

Ethereum Library

You can use Wagmi hooks to sign messages, interact with smart contracts, and much more.

useSignMessage

Hook for signing messages with connected account.

import { useSignMessage } from 'wagmi'

function App() {
  const { signMessage } = useSignMessage()

  return <button onClick={() => signMessage({ message: 'hello world' })}>Sign message</button>
}

Check more on wagmi docs

For more readt Didit Sdk docs