Code Monkey home page Code Monkey logo

402-router-starter's Introduction

Bitcoin Payment Middleware for API Calls

This project showcases different Bitcoin-based payment methods for accessing API services. The payment methods are categorized into Ecash and Lightning, each suitable for various use cases.

Ecash

Ecash payments are handled through chaumian mint systems like Cashu and Fedimint, which provide privacy and scalability. They are ideal for paying for individual API calls, because you just include the ecash in the header which is validated and redeemed by the server prior to processing the request.

Cashu (/src/middleware/Cashu.ts)

  • Implements a single round push payment system using encoded tokens.
  • Clients send a cashu token for an exact amount in the request' "X-Cashu" header, which the server must validate and redeem.
  • Validation: Token proofs must match the configured mint URL, and the token amount must be equal to the exact route cost.

Fedimint (/src/middleware/Fedimint.ts)

  • TO DEFINE

Lightning

Lightning payments use the L402 protocol, which is ideal for sessions or API keys that have a validity period.

L402 (/src/middleware/L402.ts)

  • Description: A protocol for paying for a session or API key valid for a certain time frame.
  • Operation: The server returns a WWW-Authenticate header with a token and an invoice. The client pays the invoice, receives a preimage, and sends it back with the token in the Authorization header.
  • Validation: The server checks if the hash of the preimage matches the payment hash in the token. The token may include additional caveats like expiry time or number of uses.

Integrating these middlewares into your API server allows for the processing of microtransactions using Bitcoin, offering a range of payment options to developers and users alike.

Setup

Install nix

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

In this directory, run

nix develop

Which will throw you into the nix shell with all the dependencies you need to run the project.

Running

bun run dev

402-router-starter's People

Contributors

kodylow avatar hwinsby avatar

Stargazers

22388o⚡️  avatar

Watchers

 avatar

Forkers

22388o

402-router-starter's Issues

Finish Cashu 402 Middleware

Cashu 402 uses the X-Cashu protocol. See example at https://demo.proxnut.com/

Request should have an X-Cashu header structured as:

X-Cashu: cashuAtokenblahblahblah

use https://github.com/cashubtc/cashu-ts to decode and reissue the token.

where cashutoken MUST be the exact amount required for the route.

If the cashu token is NOT of the exact amount, returns error:

402 Payment Required.

With a response body JSON:

{
"mint": mintUrl,
"amount": amountRequired
}

(Would be better to put this in a header response separated like L402 does)

www-authorization: Cashu402 mintUrl=mintUrl, amount=exactAmount

Add README

Needs to explain how to setup and run using bun.

Walk through docs on how each of the 3 402 payment types work: Fedimint, Cashu, and L402.

Provide examples for all 3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.