Skip to content

Server Integration

This guide covers setting up PipeGate middleware in your typescript application for server integration of pipegate middlewares.

Installation

Install pipegate-sdk :

npm
npm install pipegate-sdk

Setup

Stream Middleware

Import the SDK

import { initWasm, StreamVerifier } from "../src";

Configure & Initialize the client

Configure the verification settings for the stream and initialize the verifier.

initWasm();
 
let config_json = {
  recipient: "YOUR_ADDRESS",
  token_address: "TOKEN_ADDRESS",
  cfa_forwarder: "0xcfA132E353cB4E398080B9700609bb008eceB125",
  rpc_url: "rpc_url",
  amount: BigInt(761035007610), // rate in flow per second
  cache_time: 86400,
};
 
const verifier = new StreamVerifier(JSON.stringify(config_json));

More info on configuration can be found here

Attach the listener

Attach the listener to cache verification results and invalidate the cache if the stream is terminated.

let listener_config_json = {
  wss_url: "wss://base-sepolia-rpc.publicnode.com",
  cfa: "0x6836F23d6171D74Ef62FcF776655aBcD2bcd62Ef",
};
 
verifier.start_listener(JSON.stringify(listener_config_json));

Implement the middleware

Headers for the incoming API calls needs to be parsed to extract signature & sender fields.

const signature = request.headers.get("X-Signature");
const sender = request.headers.get("X-Sender");

Verify the stream using the verify method.

const result = await verifier.verify_request(signature, sender);

That's it, you can convert this into a proper middleware for your server and verify incoming requests.

Payment Channel Middleware

Import the SDK

import { initWasm, PaymentChannelVerifier } from "../src";

Configure & Initialize the client

Configure the verification settings for the payment channel and initialize the verifier.

initWasm();
 
let config_json = {
  recipient: "YOUR_ADDRESS",
  token_address: "TOKEN_ADDRESS",
  rpc_url: "rpc_url",
  amount: BigInt(1000), // rate per call
};
 
const verifier = new PaymentChannelVerifier(JSON.stringify(config_json));

More info on configuration can be found here

Implement the middleware

Headers for the incoming API calls needs to be parsed to extract signature, message, timestamp & paymentChannel fields.

const timestamp = request.headers.get("X-Timestamp");
const signature = request.headers.get("X-Signature");
const message = request.headers.get("X-Message");
const paymentChannel = request.headers.get("X-Payment");
 
const bodyBytes = request.body
  ? new Uint8Array(await request.arrayBuffer())
  : new Uint8Array(0);

Verify the channel using the verify method.

const result = await verifier.verify_request(
  message,
  signature,
  paymentChannel,
  config_json.amount,
  timestamp,
  bodyBytes
);

That's it, you can convert this into a proper middleware for your server and verify incoming requests.

OneTimePayment Middleware

Import the SDK

import { initWasm, verify_onetime_payment_tx } from "../src";

Configure

Configure the verification settings for one time Payment

initWasm();
 
let config_json = {
  recipient: "YOUR_ADDRESS",
  token_address: "TOKEN_ADDRESS",
  rpc_url: "rpc_url",
  amount: BigInt(1000000), // rate per call
  period: 600,
};

More info on configuration can be found here

Implement the middleware

Headers for the incoming API calls needs to be parsed to extract signature & txHash fields.

const signature = request.headers.get("X-Signature");
const txHash = request.headers.get("X-Transaction");

Verify the payment using the verify method.

const result = await verify_onetime_payment_tx(
  JSON.stringify(config_json),
  signature,
  tx_hash
);

That's it, you can convert this into a proper middleware for your server and verify incoming requests.

Other utilities

You can now use the verifier to verify incoming requests in your server, there are other verifiers available as well for verification of different types of transactions if you don't want to intialise the whole client and maintain a state

  1. verify_channel_no_state - For verifying payment channel transactions without maintaining a state
  2. verify_stream_tx - For verifying superfluid stream transactions

They accept the same parameters as the verify method along with configuration in the sections above and can be used in the same way.