RankButton, a pay button for any web page
Developers12.5.13+

RankButton, a pay button for any web page

A Lotus pay button in one script tag, with QR code, extension, Desktop, phone and web wallet; payment detected over P2P; funding and emptying a game wallet.

Every node serves the RankButton at /lib/lotus-rankbutton.js. It is one ES module with no build step and no framework. It needs no account, server or key: you give it your Lotus address.

html
<script type="module" src="https://<node>/lib/lotus-rankbutton.js"></script>

<button data-rankbutton
        data-to="lotus_16PSJMStv9sve3DfhDpiwUCa7RtqkyNBoS8RjFZSt"
        data-amount="10"
        data-label="Order 7"
        data-confirmations="1"></button>

A click opens a dialog with:

  • the amount (fixed, or typed by the visitor with data-editable);
  • an optional tip the visitor chooses (data-tip="0,5,10", in percent);
  • the QR code and the lotus: link of the payment, which Lotus Desktop and the phone app open;
  • Pay with the Lotus extension, when the visitor connected your site in the extension;
  • Web wallet, a redirect to a node's web wallet that comes back to your page;
  • the state of the payment.

A demo is at /lib/demo-rankbutton.html on every node.

Attributes #

They are read as data, through a fixed list. Nothing is evaluated: there are no callback attributes.

AttributeMeaning
data-rankbuttonMakes the element a button. Elements added later are found too.
data-toYour Lotus address: lotus_… on mainnet, lotusT… on the test network. CashAddr (ecash:, bitcoincash:) is refused. Base58 is case-sensitive: copy the address as it is.
data-amountThe price in XPI, at most 6 decimals. Leave it out (or add data-editable) to let the visitor type it.
data-labelThe wallets show it as "the site's description". It is not written on chain.
data-textA short text written on chain with the payment (public).
data-confirmationsYour "paid" moment: 0 = as soon as full nodes relay the payment; N = N confirmations, proven against the block headers the page checked itself. Default 1. Lotus has no instant finality for a plain payment: use 0 only when nothing is delivered (a donation).
data-tipThe tip choices, in percent, for example 0,5,10. The tip is added to the same output.
data-payment-idnone: no payment ID; the amount then gets 1 to 999 extra sats so that it is unique. Default: 8 random bytes for each dialog.
data-langen, fr, es, it, de, ru or cn. Default: the page's lang, then the browser's language.
data-walletThe web wallet to use. Default: /wallet/ of the node that serves the script.
data-peerswss:// endpoints of nodes, separated by spaces, tried first for the detection.
data-button-textThe button's text when the element is empty.

Events #

The element receives DOM events. Each detail holds txid, paymentId, amountSats, paidSats, confirmations and to.

EventWhen
rankbutton:openThe dialog opened (detail.paymentId).
rankbutton:seenA full payment was seen, before your "paid" moment.
rankbutton:paidYour moment (data-confirmations).
rankbutton:closeThe dialog closed (detail.paid).
JavaScript
document.querySelector('[data-rankbutton]').addEventListener('rankbutton:paid', (e) => {
    fetch('/order/7/paid', { method: 'POST', body: JSON.stringify({ txid: e.detail.txid }) });
});

These events are what the page saw. Before you deliver anything, check the payment on your own node: the transaction pays your address at least the amount, and its PAY output carries the payment ID (getrawtransaction <txid> 1, or GET /api/v1/txs/<txid>).

From code:

JavaScript
import { RankButton } from 'https://<node>/lib/lotus-rankbutton.js';
RankButton.render(element, { to, amountXpi: '10', label: 'Order 7', confirmations: 1 });
RankButton.open({ to, amountXpi: '2.5' });   // a dialog without a button
RankButton.useNode(node);                    // a LotusNode your page already runs

The payment data (PAY) #

A payment from the button carries an OP_RETURN output with no coins on it. Other pay buttons and the tools that read them use the same layout:

Text
6a 04 50415900  00        <text push or 00>  <08 + 8-byte payment ID, or 00>
   "PAY\0"      version 0

With a payment ID and no text it is 17 bytes, inside the free data bytes of a transaction: nothing is burnt. lotus-pay.js writes and reads it (payData, parsePay). Every Lotus wallet decodes it before you sign: the web wallet, the extension, Desktop and the phone app.

Detection over P2P #

The button finds the payment with a browser node (/lib/lotus-node.mjs). It connects to nodes over their P2P WebSocket and uses no HTTP API.

  • It asks its peers for the transactions that pay your address or carry the payment ID (a bloom filter, and their mempool).
  • It counts confirmations on the block headers it verified itself, with a merkle proof of the transaction.
  • A reorganisation that removes the block takes "paid" back to "seen".

The same core is a module of its own, /lib/lotus-pay.js:

JavaScript
import { paymentRequest, watchPayment, matchPayment } from 'https://<node>/lib/lotus-pay.js';
const req = paymentRequest({ to, amountXpi: '10', label: 'Order 7', confirmations: 1 });
req.uri      // lotus:lotus_…?amount=10&label=Order%207&op_return_raw=045041590000…
req.intent   // for wallet_fulfillIntent (Connect a web page to a Lotus wallet)
const stop = watchPayment(node, req, (s) => { /* waiting, partial, seen, paid */ });

matchPayment compares output scripts exactly. An output that merely contains your key hash is not a payment to your address.

Donations to a node's operator #

Start a node with -donationaddress=<your Lotus address>. The node checks the address at start: it must be a Lotus address of the node's network, and CashAddr is refused. Network › Connected peers then shows a Donate button (data-confirmations="0"), and GET /api/v1/network returns the address as donation_address.

Funding a game wallet, and the way back #

A game page can keep a small wallet of its own (a key the page generated). The player funds it from their real wallet and gets the rest back with one button.

  1. Fund. The page shows a RankButton (or a plain paymentRequest) to the game wallet's address, with data-label="Fund your game wallet". The player's wallet shows your site, the amount and the purpose before it signs. For tokens, ask the extension or the web wallet for a lotus-token-payment (SLP or ALP, by token ID, in atoms; see Connect a web page to a Lotus wallet).
  2. Where the money goes back. Never let the page or the player type the return address. Read it from the funding transaction with fundingReturnScript(rawTx, netChar, lookup): the coin spent by its first input belongs to the wallet that paid. For a key coin (P2PKH) the address comes from the input itself. Other coins need lookup(txid, index), which returns the previous output's script, for example through the browser node's node.ui('txs/<txid>').
  3. Withdraw. The game wallet sends everything back to that script: its XPI, and each token with its own token message. A token coin is never spent as XPI.

A game wallet is a hot wallet in a web page: say so to players, and keep the amounts small.

Security notes #

  • Keep third-party scripts off pages with a button. They run as your page and could change what the button shows. The wallets always show the real address and amount before signing.
  • A payment ID is random for each dialog, so one payment cannot count for two orders. Mark an order paid once, by its payment ID.
  • Wallets refuse data that is not plain pushes, data over 223 bytes, RANK votes and token messages in op_return_raw.
  • The lotus: link carries the network in the address prefix. A wallet of another network refuses it.
Source share/www/docs/rankbutton.md in the lotusd repository
Checked against share/www/lib/lotus-rankbutton.js share/www/lib/lotus-pay.js share/www/lib/lotus-qr.js share/www/lib/lotus-wallet-connect.js share/www/wallet/src/lib/walletConnect.ts desktop/lotus-desktop/src/payuri.rs desktop/lotus-wallet-core/src/paylink.rs mobile/lotus-mobile-core/src/bonds.rs