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

littlefish-nft-auth-framework

v0.1.11

Published

Open Source NFT & Wallet Auth Framework for Cardano

Downloads

745

Readme

Next JS Implementation

Install latest Next JS version

npx create-next-app@latest

For the demonstration we used the following values:

Image 1

Install littlefish npm package

npm install littlefish-nft-auth-framework

In your app directory create a “providers.jsx” file.

Image 2

"use client"
import { WalletProvider} from 'littlefish-nft-auth-framework';

export default function Providers({children}) {
  return (
    <WalletProvider>
      {children}
    </WalletProvider>
  )
};

In your “app/layout.tsx” file import the Providers and wrap your {children} around the wallet provider.

Image 3

Now you are ready to use the package.

React App Implementation

Create a React project.

npx create-react-app <YOUR-APP-NAME>

Then, install littlefish-nft-auth-framework package.

npm install littlefish-nft-auth-framework

After installing the package, go to src/ directory and index.js file and add the provider like this:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { WalletProvider } from 'littlefish-nft-auth-framework';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <WalletProvider>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </WalletProvider>
);

reportWebVitals();

Now you are ready to use the package.

Returned Values and Function Descriptions

Hooks

  • useWallet()
    • Returns: An object containing:
      • isConnected (boolean): Indicates if a wallet is currently connected.
      • wallets (Array<string>): List of detected wallet identifiers.
      • connectedWalletId (string|null): Identifier of the currently connected wallet.
      • assets (Array<Array<string, string, number>>): Decoded assets from the connected wallet, each represented as [policyID, assetName, amount].

Provider Component

  • WalletProvider
    • Purpose: Provides wallet context to all child components in the application, allowing them access to wallet data and functions.

Utility Functions

  • connectWallet(walletName: string)

    • Returns: Promise<void>
    • Description: Attempts to connect to the specified wallet and updates the context with wallet details if successful. The function is asynchronous and resolves without returning any specific data.
  • disconnectWallet()

    • Returns: void
    • Description: Resets connection-related state variables to reflect no active wallet connection. Does not return any data.
  • decodeHexToAscii(processedArray: Array)

    • Returns: Array<Array<string>>
    • Description: Converts hex-encoded assetName strings within the processed array into ASCII strings. Returns an array with elements formatted as [policyID, assetName, amount].

Example Usage

The only difference between NextJS and React App usage is, when using NextJS, you need to use "use client" on the beginning of the pages you want to use littlefish-nft-auth-framework package.

First you need to import the useWallet hook to your component.

import { useWallet } from "littlefish-nft-auth-framework";

Use the 'useWallet()' hook inside your component to get access to several properties and methods such as isConnected, wallets, assets, and functions like connectWallet, 00disconnectWallet**, and decodeHexToAscii.

const {
  isConnected,
  connectWallet,
  disconnectWallet,
  wallets,
  assets,
  decodeHexToAscii,
} = useWallet();

Wallet Connect

Here is an example of connecting wallet. The wallets array will be displayed to give the user the option which wallet they want to connect.

wallets.map((wallet, index) => (
  <a key={index} onClick={() => connectWallet(wallet)}>{wallet}</a>
))

After the wallet connection, these will be updated:

  • isConnected: It will be True
  • assets: If there are any assets in the wallet, this will be an array of arrays.

Wallet Disconnect

In order to disconnect wallet:

<a onClick={() => disconnectWallet()}>Disconnect Wallet</a>

This action will update:

  • isConnected to False.
  • assets to an empty array.

Displaying Assets

In order to display asset information and use decodeHexToAscii function, we initialized these states:

const [walletAssets, setWalletAssets] = useState("");
const [isHex, setIsHex] = useState(true);

useEffect(() => {
  setWalletAssets(assets || []);
}, [assets]);

Here is how the decodeHexToAscii function updates walletAssets state:

{isHex ? 
  <a onClick={() => {setWalletAssets(decodeHexToAscii(walletAssets)); setIsHex(false)}}>Decode Hex to Ascii</a> : 
  <a onClick={() => {setWalletAssets(assets); setIsHex(true)}}>Show Hex</a>
}

Displaying the assets:

walletAssets.map((item, index) => (
  <pre key={index}>PolicyID: {item[0]}, Name: {item[1]}, Amount: {item[2]}</pre>
))