Skip to content

Signing and Verifying Off-Ledger Payments | XRPL Development in JavaScript - Level 4

In this guide, we will introduce how to sign and verify off-ledger payments.

Creating the Script

  1. Create a new file named signPaymentChannelClaim.js in your project directory.

  2. Paste the following code into signPaymentChannelClaim.js.

    This code is a sample that performs 1,000 payments.

    • In the following loop, the payment amount increases by 0.001 each time, resulting in a total payment of up to 1 XRP.
    signPaymentChannelClaim.js
    import {
    signPaymentChannelClaim,
    verifyPaymentChannelClaim,
    xrpToDrops,
    } from 'xrpl';
    import { client, bobWallet } from './config.js';
    const signAndVerifyClaims = async (channelId) => {
    let paychanSignature = '';
    console.time();
    for (let i = 1; i <= 1000; i++) {
    const amount = (0.001 * i).toFixed(6); // Fixed to 6 decimal places
    const amountFormatted = parseFloat(amount).toString(); // Specified in XRP
    // Sender: Sign the off-ledger payment
    paychanSignature = signPaymentChannelClaim(
    channelId,
    amountFormatted, // Specified amount in XRP
    bobWallet.privateKey
    );
    // Recipient: Verify the off-ledger payment information
    if (
    !verifyPaymentChannelClaim(
    channelId,
    amountFormatted, // Specified amount in XRP
    paychanSignature,
    bobWallet.publicKey
    )
    ) {
    throw new Error('Invalid signature');
    }
    }
    console.timeEnd();
    return paychanSignature;
    };
    const main = async () => {
    try {
    await client.connect();
    const channelId = 'CHANNEL_ID_HERE'; // Enter the previously created channel ID
    const signature = await signAndVerifyClaims(channelId);
    console.log(`Generated signature: ${signature}`);
    } catch (error) {
    console.error('Error connecting to XRPL:', error);
    } finally {
    await client.disconnect();
    }
    };
    main();

Running the Script

  1. Run the script by executing the following command in your command line:

    Terminal window
    node signPaymentChannelClaim.js
  2. If successful, the console will display the following results, so check the string displayed to the right of Generated signature: and make a note of it.

    Terminal window
    default: 1.605s
    Generated signature: 7465B7BCC66F1053D4CB8EF3785BE951A235868420F546160EF446429DA58632EEBBA90810F6714500E7B0A73E9DBABFE47307DC6BEC9D911D24DB473988BE04

    It took only 1.605s to process 1,000 off-ledger payments (results from the author’s local environment).

    This demonstrates how off-ledger transactions can handle over 1,000 payments in just a few seconds.

    The signature value obtained in the result above, 7465B7BCC66F1053D4CB8EF3785BE951A235868420F546160EF446429DA58632EEBBA90810F6714500E7B0A73E9DBABFE47307DC6BEC9D911D24DB473988BE04, will be needed in the next chapter, so make a note of it.

Checking the Channel State

Let’s check the state of the channel at this point.

Creating the Script

  1. Create a new file named checkChannelBalance.js in your project directory.

  2. Paste the following code into checkChannelBalance.js.

    To check the state of the channel, use the account_channels command.

    checkChannelBalance.js
    import { client, aliceWallet, bobWallet } from './config.js';
    const checkChannelBalance = async (channelId) => {
    try {
    // Get channel information
    const response = await client.request({
    command: 'account_channels',
    account: bobWallet.address, // Sender's account of the channel
    destination_account: aliceWallet.address, // Recipient's account of the channel
    });
    const channel = response.result.channels.find(
    (ch) => ch.channel_id === channelId
    );
    if (channel) {
    console.log(`Channel ID: ${channel.channel_id}`);
    console.log(`Balance: ${channel.balance} drops`);
    console.log(`Amount: ${channel.amount} drops`);
    } else {
    console.log(`Channel with ID ${channelId} not found.`);
    }
    } catch (error) {
    console.error('Error checking channel balance:', error);
    }
    };
    const main = async () => {
    try {
    await client.connect();
    // Enter the previously created channel ID
    const channelId = 'CHANNEL_ID_HERE';
    await checkChannelBalance(channelId);
    } catch (error) {
    console.error('Error connecting to XRPL:', error);
    } finally {
    await client.disconnect();
    }
    };
    main();

Running the Script

  1. Run the script by executing the following command in your command line:

    Terminal window
    node checkChannelBalance.js
  2. If successful, the console will display the following:

    Terminal window
    Channel ID: E965B919E140BCF98280B29F6861C23BBB753B9D5515EDD57A285391BA20CCA2
    Balance: 1000000 drops // Total amount transacted off-ledger in the channel (currently: 1 XRP)
    Amount: 10000000 drops // Amount deposited (10 XRP)

Summary

In this chapter, we explained how to sign and verify off-ledger payments and how to check the state of the payment channel.

Off-ledger payments enable rapid and efficient execution of multiple payments by verifying the validity through signatures. Additionally, checking the state of the payment channel allows you to understand the current balance and the total amount deposited in the channel.

In the next chapter, we will introduce how to claim the funds. Learn the steps to claim the channel’s balance using the obtained signature.