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

@packetpay/express

v0.2.24

Published

Pay with Bitcoin for HTTP requests

Downloads

820

Readme

@packetpay/express

Pay with Bitcoin for HTTP requests

The code is available on GitHub and the package is published on NPM.

Overview

PacketPay is a system for micropayment-based HTTPS request monetization. @packetpay/express provides a way to easily add request monetization to the routes of an express server.

The middleware relies on Authrite for verifying the legitimacy of users. When users provide the x-bsv-payment header, the PackePay middleware processes and validates the payment.

When no x-bsv-payment header is provided, the middleware terminates the request with a 402: Payment Required error containing the amount for the payment.

The format of the x-bsv-payment header is a JSON object containing a payment envelope that complies with a Babbage payment protocol. More details on this protocol are coming soon.

There is a complementary client library called @packetpay/js that interfaces with this middleware and generates the correct payment information.

Installation

npm i @packetpay/express

Example Usage

This example demonstrates creating a simple express server that makes use of the @packetpay/express middleware.

const authrite = require('authrite-express')
const PacketPay = require('@packetpay/express')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 5000
const TEST_SERVER_PRIVATE_KEY = 
'6dcc124be5f382be631d49ba12f61adbce33a5ac14f6ddee12de25272f943f8b'
const TEST_SERVER_BASEURL = `http://localhost:${port}`

app.use(bodyParser.json())
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', '*')
  res.header('Access-Control-Allow-Methods', '*')
  res.header('Access-Control-Expose-Headers', '*')
  res.header('Access-Control-Allow-Private-Network', 'true')
  if (req.method === 'OPTIONS') {
    res.sendStatus(200)
  } else {
    next()
  }
})

// Before any PacketPay middleware, set up the server for Authrite
app.use(authrite.middleware({
    serverPrivateKey: TEST_SERVER_PRIVATE_KEY,
    baseUrl: TEST_SERVER_BASEURL
}))

// Configure the express server to use the PacketPay middleware
app.use(PacketPay({
    serverPrivateKey: TEST_SERVER_PRIVATE_KEY,
    ninjaConfig: {
      // Use the Babbage staging testnet Dojo
      dojoURL: 'https://staging-dojo.babbage.systems'
    },
    calculateRequestPrice: req => {
        if (req.originalUrl === '/buyTShirt') {
            return 5 * 1e8 // 5 BSV for T-shirts
        } else if (req.originalUrl === '/specialFile.pdf') {
            return 3301 // 3,301 satoshis for the PDF file
        } else {
            return 200 // 200 satoshis for everything else
        }
    }
}))

// Example Routes
app.get('/buyTShirt', (req, res) => { // costs 5 BSV
    // Verify the payment
    if (req.packetpay.satoshisPaid === 5 * 1e8) {
        /* Mark the T-shirt order as ready to ship */
        res.status(200).json({
            message: `Your T-shirt is on the way! Payment reference number: ${req.packetpay.reference}`
        })
    } else {
        res.status(400).json({
            message: 'Payment for T-shirt not received!'
        })
    }
})
app.post('/sendSomeData', (req, res) => { // costs 200 sats
    res.json({
        message: 'Hello, this is the server.',
        clientData: req.body
    })
})

// Every file costs 200 sats. But "/specialFile.pdf" costs 3301 sats.
app.use(express.static('files'))

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

API

Table of Contents

PacketPay

Initializes an instance of the BSV Payment Middleware.

The payment middleware should be installed after the Authrite middleware.

Parameters

  • obj Object All parameters are provided in an object (optional, default {})

    • obj.calculateRequestPrice Function? A function that returns the price of the request in satoshis, given the request object as a parameter. If it returns a Promise, the middleware will wait for the Promise to resolve. If it returns 0, the middleware will proceed without requiring payment. (optional, default ()=>100)
    • obj.serverPrivateKey String A hex-formatted 256-bit server private key. This should be the same key used to initialize the Authrite middleware.
    • obj.ninjaConfig Object? Config object for the internal UTXONinja (optional, default {})

Returns Function The HTTP middleware that enforces a BSV payment

License

The license for the code in this repository is the Open BSV License.