Browser node library
Developers12.5.9+

Browser node library

lotus-node.js 0.3.0 reference for sites and apps: how to load it, every option with its default, the methods, the events, the TypeScript types, and what changed in 0.3.0.

lotus-node.js makes a web page, a Worker or a Node.js 22+ program a very thin Lotus node: it speaks P2P over WebSocket to several full nodes, verifies every block header itself (proof of work, difficulty, checkpoints, parent proofs of merged-mined blocks), follows the chain with the most work, proves that a transaction is in a block, and warns when nodes disagree. It does not validate scripts or coins. What it is and what it shows your visitors is in Browser node; this page is the programming reference. Version 0.3.0 ships with lotusd 12.5.9.

Load it #

Every full node serves the library; any origin may load it.

FileWhat
/lib/lotus-node.jsOne classic script, no dependencies: window.Lotus in a page, a CommonJS module in Node.js, usable in a Worker
/lib/lotus-node.mjsES module face of the same file
/lib/lotus-loader.jsStarts the library in the background, one node for all the tabs of a site, a status chip
share/www/lib/lotus-node.d.tsTypeScript types, in the lotusd source tree
html
<script src="https://givelotus.org/lib/lotus-node.js"
        integrity="sha256-..." crossorigin="anonymous"></script>
<script>
Lotus.connect({network: 'mainnet', storage: true}).then(node => {
  node.on('tip', t => console.log(t.height, t.hash));
});
</script>
JavaScript
import Lotus, {connect} from 'https://givelotus.org/lib/lotus-node.mjs';

Pin the code: GET /api/v1/network/wsnodes lists each file with its SHA-256 and integrity value. A site can also host a copy itself; the copy then announces itself as /lotus-node.js:0.3.0/ instead of the node release that served it.

In Node.js:

JavaScript
const Lotus = require('./lotus-node.js');   // a copy of /lib/lotus-node.js
const node = await Lotus.connect({network: 'mainnet', storage: false, identity: false,
                                  showOnMap: false, browserLinks: false, desktopLinks: false});
const t = node.getTip();
console.log(t.height, t.hash, t.peers);
const r = await node.verifyTx('133d43d6249a12197575abe90d08ee08a6279995e58794b86f754e6943eb23b9',
                              {block: 1433870});
console.log(r.confirmed, r.verified, r.position, r.confirmations);
await node.close();
JSON
{"txid":"133d43d6249a12197575abe90d08ee08a6279995e58794b86f754e6943eb23b9","confirmed":true,"verified":true,
 "height":1433870,"block":"00000000013dd0428a3bf73cc412573cb21509a76aec803882863d8b89fb4f63","position":0,
 "txCount":1,"confirmations":10,"provenConfirmations":10,"peer":"wss://nng-116.givelotus.org:10605/p2p"}

This example ran against mainnet with Node.js 22. The library serves mainnet, napoleontest and testnet; it does not run on regtest.

Options #

Lotus.connect(options) resolves when the header chain is synced and proven; new Lotus.LotusNode(options) then start() gives the node at once.

OptionDefaultMeaning
network'mainnet''mainnet', 'napoleontest', 'testnet', or a network object
peers[]ws:// or wss:// URLs tried first; the built-in list follows
bootstraptruefalse: only peers
maxPeers41 to 8
discovertrueLearn more endpoints from addr messages
directorythe serving nodeURL of a node's GET /api/v1/network/wsnodes; false never asks
storagenonetrue: IndexedDB (memory outside a browser, or when site data is blocked); or your own {get, set, del}
checkpointbuilt inYour own checkpoint: 11 raw headers ending at a height
restnoneA node's REST base, for verifyTx without a block hint
relaytrueRe-announce verified tips, answer getheaders
txRelaytrue (0.3.0)Relay other people's transactions once full nodes in two network groups announced them and two served the same bytes
showOnMaptrueListed on the nodes' network map under its id and name, with the country of its connection; false: only counted
identitytrueKeep the map id and name for the site (localStorage, IndexedDB, the extension's storage, a cookie last); false: new ones for this page only
identityStore, identityTimeoutnone, 1500 msA store of your own for the identity, and how long a store may take
sessionId, namenoneYour own 64-bit map id and three-word name: nothing is read or kept
browserLinkstrue (0.3.0)Direct WebRTC links to other browsers
desktopLinkstrue (0.3.0)Direct WebRTC links to Lotus Desktop apps
maxBrowserLinks120 to 32; the node's list may lower it
stunthe node's listSTUN servers for the links
reportInterval30000 msMost often the name and links are sent to one node
wait, timeouttrue, 60000 msconnect(): wait for the sync, and how long
WebSocket, RTCPeerConnection, fetchthe globalsOther implementations (tests, Node.js)
Warning

A directly linked peer sees the visitor's public IP address, as in any direct connection. With browserLinks and desktopLinks on (the default since 0.3.0), say so on the site's privacy page, together with the fact that the browser keeps a node name and id in local storage and how to reset them (forgetMe()). Turn the links off for pages where this is not acceptable.

Methods #

CallResult
getTip(){height, hash, time, bits, work, proof, merkleRoot, header, proven, headerHeight, pending, agreement, peers, links, lanes, parentContext}
getHeader(heightOrHash)A verified header, or null. proof is own-hash, checkpoint, parent-proof, or null while a merged-mined header waits for its proof
verifyTx(txid, {block, rest}){confirmed, verified, height, block, position, txCount, confirmations, peer, reason}. The block is a hint only: a wrong one cannot produce a false "confirmed"
watchAddress(address)tx events for the address (BIP37 filter: the peers learn the address)
broadcast(rawTxHex, {wait}){txid, sentTo, seenBy}: sent to half the peers, then to those that did not announce it back
peers()[{url, state, userAgent, services, bestHeight, onTip, headers, bytesIn, bytesOut, latencyMs, meshId}]
links()Direct links: [{id, app, handle, via, state, initiator, since, bytesIn, bytesOut, headers, relayed, latencyMs}]
stats()Counters, including txRelay, userAgent, nodeVersion, libVersion
identity(), name, sessionId{id, name, keptIn}
pickAnotherName(), forgetMe()A new name; a new id and name (the links close)
setShowOnMap(on), setBrowserLinks(on), setDesktopLinks(on)The visitor's switches
p2p.send(command, payload, peer), p2p.onMessage(fn)Raw P2P messages, for your own protocol work
close()Disconnects everything

Events #

node.on(event, fn), node.off(event, fn).

EventPayload
tipThe new tip (as getTip())
reorg{from, to, forkHeight, depth}
sync{synced, height, ms, target}
peer{url, state, reason, userAgent, height}
fork-alert{type, time, message, peer, code, forkHeight, ours, theirs}; type is fork, eclipse (fewer than 2 peers), stale-tip (no block for an hour), checkpoint-conflict, bad-peer, storage, needs-proof-stuck
tx{txid, raw, outputs, spends, confirmed, height, block, peer} for a watched address
reject{peer, message, code, reason}
identity{id, name, keptIn} after pickAnotherName() or forgetMe()

The loader #

html
<script src="https://givelotus.org/lib/lotus-loader.js" defer
        data-chip="#lotus-status" data-browser-links="0"></script>
<span id="lotus-status"></span>
<script>
addEventListener('load', () => LotusLoader.on(s => {
  if (s.tip) console.log(s.role, s.tip.height, s.tip.peers);
}));
</script>

Attributes: data-network, data-peers, data-directory, data-chip, data-page, data-autostart="0", data-bootstrap="0", data-integrity, data-browser-links="0", data-desktop-links="0", data-labels, data-locale (or window.LotusLoaderConfig). API: LotusLoader.status (role: leader, follower, waiting, off), on(fn), enable(), disable(), showOnMap(on), identity(), pickAnotherName(), forgetMe(), call('verifyTx' | 'getHeader' | 'broadcast' | 'watchAddress', ...).

An https:// page can only reach wss:// endpoints; on such a site with no wss:// node answering, the loader waits quietly and starts when one appears.

What it verifies, and what it does not #

Verified hereNot verified (trusted to full nodes)
Hash, target, difficulty rule, height, epoch hash, time rules, checkpoints of every headerScripts, signatures, coins, double spends
Parent proofs of merged-mined blocks (from Napoleon): the parent's work, the commitment to this block, the anchorThree parent-chain rules that need the parent chains' history: one use per parent header, building on a recorded parent header, the exact lane difficulty (getTip().parentContext is unchecked once they apply)
That a transaction is in a block (merkle proof against a verified header)That an address's list of transactions is complete (watchAddress can be told less)

One honest reachable node is enough to learn the best chain. Peers that control every endpoint can withhold blocks (stale-tip) and hide transactions, never forge a heavier chain.

How it appears on the network #

The library announces the node release that served it: /lotusd:12.5.9-browser(lotus-node.js 0.3.0)/. Nodes report it as kind browser (see Network map API), under a random map id and a three-word name, never its address.

Changes in 0.3.0 #

  • The map id and name are kept per site (they were new at each visit): identity, pickAnotherName(), forgetMe().
  • browserLinks and desktopLinks default to true, with a switch each.
  • txRelay (default true): relays other people's transactions under the vouched rule; txRelay: false is the 0.2 behaviour.
  • Links to Lotus Desktop apps; LinkInfo.app says browser or desktop.
  • PeerInfo.meshId; node.voix (VOIX client, refused on mainnet, see VOIX).
  • With site data blocked, storage: true falls back to memory instead of failing to start.
Source share/www/docs/browser-node-library.md in the lotusd repository
Checked against share/www/lib/lotus-node.d.ts share/www/lib/lotus-node.mjs share/www/lib/lotus-loader.js share/www/lib/src/40-node.js share/www/lib/src/45-txrelay.js share/www/lib/src/35-identity.js share/www/lib/src/90-api.js doc/browser-node.md src/api/browser_node_handler.cpp