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

ilp-plugin-payment

v0.0.1

Published

Base class for settling ILP connection with payments

Downloads

3

Readme

ILP Plugin Payment

Overview

ILP Plugin Payment is a base class that can be extended to integrate different payment systems. These integrations are assumed to be payment-based, in that one side sends a payment to the other side and then the other side detects the incoming settlement.

Configuration

  • If you want anybody to be able to connect to you without updating your configuration, run the Server plugin. Pass "role": "server" into the plugin opts.

  • If you're peering 1:1 or are connecting to someone running a server plugin, use the Client/Peer plugin. Pass "role": "client" or no "role" field into the plugin opts.

The examples below will not work with the base class, but will work with the derived classes.

Client/Peer

const PluginPayment = require('ilp-plugin-payment')

const plugin = new PluginPayment({
  role: 'client',
  server: 'btp+ws://example.com:1234',
  /* ... */
})

Server

const PluginPayment = require('ilp-plugin-payment')
const plugin = new PluginPayment({
  role: 'server',
  port: 1234,
  /* ... */
})

Integrating Other Systems

The following methods and events must be implemented in order to integrate a new payment system via this plugin.

For a blank starter, you can copy the snippet below. If you want to work off of an XRP ledger integration, use the XRP example code.

class MyPaymentPlugin extends PluginPayment {
  constructor (opts) {
    super(opts)
  }

  // You must also have a method in here somewhere which calls
  // this.emit('money', userId, amount) on incoming payments.

  async connectPayment () {
  }

  async sendPayment (details, amount) {
  }

  async getPaymentDetails (userId) {
  }
}

connectPayment

async connectPayment () -> Promise<void>

Connect to the settlement network. For example, on XRP:

async connect () {
  await this._api.connect()
  await this._api.connection.request({
    command: 'subscribe',
    accounts: [ this._address ]
  })

  // This is how we detect an incoming transaction. You'll need some equivalent of this
  // that calls this._handleMoney whenever a payment destined for this plugin comes in.
  this._api.connection.on('transaction', ev => {
    if (ev.validated && ev.transaction &&
      ev.transaction.TransactionType === 'Payment' &&
      ev.transaction.Destination === this._address &&
      ev.transaction.Amount.currency === 'XRP') {
      const userId = this._destinationTagToUserId(ev.transaction.DestinationTag)
      const value = new BigNumber(ev.transaction.Amount.value).times(1e6).toString()
      this.emitAsync('money', userId, value)
    }
  })
}

getPaymentDetails

async getPaymentDetails (userId: String) -> Promise<Object>

Create payment details associated with a given userId. There may be any number of different userIds, so the details given should be able to differentiate incoming payments from each other.

In the connectPayment example for XRP, for instance, the destinationTag is used to look up the userId for an incoming payment.

The result of this function will be passed into the peer's sendPayment function. On XRP, this function would be implemented like so:

async getPaymentDetails (userId) {
  return {
    address: this._address,
    destinationTag: this._userIdToDestinationTag(userId)
  }
}

sendPayment

async sendPayment (details: Object, amount: String) -> Promise<Object>

Before sendPayment is called, a get_payment_details RPC call is made. This calls the getPaymentDetails function on the peer's plugin, and returns the result. The result is passed into sendPayment as the details argument.

The amount is a a string-integer denominated in base ledger units. These base ledger units can be anything you want (so long as they're used consistently), but are typically the lowest divisible unit of the ledger. For example, XRP would use drops and Bitcoin would use Satoshis.

On XRP, the sendPayment function would look like this:

async sendMoney (details, amount) {
  const xrpAmount = new BigNumber(amount).div(1e6).toString()

  await this._txSubmitter('preparePayment', {
    source: {
      address: this._address,
      maxAmount: {
        value: xrpAmount,
        currency: 'XRP'
      }
    },
    destination: {
      address: details.address,
      tag: details.destinationTag,
      amount: {
        value: xrpAmount,
        currency: 'XRP'
      }
    }
  })
}

Event: Money

event 'money' (userId: String, amount: String)

This event is emitted when incoming funds are detected.

If the details resulting from getPaymentDetails(userId) are passed (as details) into your peer's sendPayment(details, amount), then you MUST be able to detect the incoming payment and emit it as userId, amount.

You can see how this is implemented in the example XRP plugin:

this._api.connection.on('transaction', ev => {
  if (ev.validated && ev.transaction &&
    ev.transaction.TransactionType === 'Payment' &&
    ev.transaction.Destination === this._address &&
    ev.transaction.Amount.currency === 'XRP') {
    const userId = this._destinationTagToUserId(ev.transaction.DestinationTag)
    const value = new BigNumber(ev.transaction.Amount.value).times(1e6).toString()
    this.emitAsync('money', userId, value)
  }
})