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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hashchain/sdk

v1.2.5

Published

Hashchain Protocol SDK for trustless on-chain payment channels

Readme

⚡ Hashchain Protocol SDK

The Hashchain Protocol SDK enables developers to create and interact with on-chain payment channels using a hashchain-based commitment mechanism. Ideal for pay-per-use systems, streaming payments, or microtransactions.

✨ Features

  • Create secure payment channels with native tokens or ERC-20
  • Redeem payments using hash-based tokens
  • Reclaim unused funds after channel timeout
  • Generate and verify hashchains
  • Decode smart contract errors for better debugging
  • Utility methods for ETH/Wei conversions and secure randomness

📦 Installation

npm install @hashchain/sdk

Prerequisites

  1. Ensure you have Node.js installed. You can download it from nodejs.org.
  2. Ensure your .env file is set up with the following variables:
    • RPC_URL=<your_rpc_url>
    • PRIVATE_KEY=<payer_private_key>
    • CONTRACT_ADDRESS=<deployed_hashchain_contract>

🚀 Quick Start Example

import { ethers } from "ethers";
import * as dotenv from "dotenv";
import {
  HashchainProtocol,
  decodeContractError,
  HashchainProtocolABI,
} from "@hashchain/sdk";

// Load environment variables from .env file
dotenv.config();

// Load environment variables from .env file
const RPC_URL = process.env.RPC_URL!;
const PRIVATE_KEY = process.env.PRIVATE_KEY!;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS!;

// Ensure environment variables are set
if (!RPC_URL || !PRIVATE_KEY || !CONTRACT_ADDRESS) {
  throw new Error("Missing required environment variables.");
}

// Initialize Ethereum provider and wallet
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);

// Initialize the Hashchain SDK
const hashchainSDK = new HashchainProtocol(provider, CONTRACT_ADDRESS, wallet);

const createChannel = async ({
  merchant,
  tokenAddress = ethers.constants.AddressZero,
  trustAnchor,
  amount,
  numberOfTokens,
  merchantWithdrawAfterBlocks = 1,
  payerWithdrawAfterBlocks = 1,
}: {
  merchant: string;
  tokenAddress?: string;
  amount: ethers.BigNumber;
  trustAnchor: string;
  numberOfTokens: number;
  merchantWithdrawAfterBlocks?: number;
  payerWithdrawAfterBlocks?: number;
}) => {
  try {
    console.log("Creating a payment channel...");

    const tx = await hashchainSDK.createChannel({
      merchant,
      tokenAddress,
      trustAnchor,
      amount,
      numberOfTokens,
      merchantWithdrawAfterBlocks,
      payerWithdrawAfterBlocks,
    });

    console.log("Transaction sent! Hash:", tx.hash);

    const receipt = await tx.wait();
    console.log("Transaction confirmed in block:", receipt.blockNumber);
  } catch (error: any) {
    console.error("❌ Error creating channel");
    decodeContractError(error, HashchainProtocolABI);
  }
};

const redeemChannel = async ({
  payer,
  tokenAddress = ethers.constants.AddressZero,
  finalHashValue,
  numberOfTokensUsed,
}: {
  payer: string;
  tokenAddress?: string;
  finalHashValue: string;
  numberOfTokensUsed: number;
}) => {
  try {
    console.log("Redeeming a payment channel...");

    const tx = await hashchainSDK.redeemChannel({
      payer,
      tokenAddress,
      finalHashValue,
      numberOfTokensUsed,
    });

    console.log("Transaction sent! Hash:", tx.hash);

    const receipt = await tx.wait();
    console.log("Transaction confirmed in block:", receipt.blockNumber);
  } catch (error: any) {
    console.error("❌ Error redeeming channel");
    decodeContractError(error, HashchainProtocolABI);
  }
};

const main = async () => {
  // Demo addresses and values (replace with actual values in real usage)
  let merchant = "0x1bB38d9F94804A36EEE5FB8e18D012B5Aa687563";
  let tokenAddress = "0x8d0c9d1c17aE5e40ffF9bE350f57840E9E66Cd93"; // ERC-20 token (or AddressZero for ETH)
  let trustAnchor =
    "0x2b1947b36fe6950873690e63efad65293ad2369b0331b9f0d8643adc525738f6";
  let numberOfTokens = 9000;
  let amount = ethers.utils.parseEther("0.01"); // 0.01 token (ETH or ERC-20)

  let payer = "0x1bB38d9F94804A36EEE5FB8e18D012B5Aa687563";
  let finalHashValue =
    "0x766418c7e06d5661b1395effb4e17804a12e43be5a92b7226fcde8827d445765"; // Last revealed token by payer
  let numberOfTokensUsed = 8950;

  // If using an ERC-20 token, ensure that you have granted approval to the contract address
  // for the required amount before initiating the transaction.

  // Create a channel
  await createChannel({
    merchant: merchant,
    tokenAddress: tokenAddress,
    trustAnchor: trustAnchor,
    amount: amount,
    numberOfTokens: numberOfTokens,
  });

  // Redeem channel
  await redeemChannel({
    payer: payer,
    tokenAddress: tokenAddress,
    finalHashValue: finalHashValue,
    numberOfTokensUsed: numberOfTokensUsed,
  });
};

main();

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.