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.
| File | What |
|---|---|
/lib/lotus-node.js | One classic script, no dependencies: window.Lotus in a page, a CommonJS module in Node.js, usable in a Worker |
/lib/lotus-node.mjs | ES module face of the same file |
/lib/lotus-loader.js | Starts the library in the background, one node for all the tabs of a site, a status chip |
share/www/lib/lotus-node.d.ts | TypeScript types, in the lotusd source tree |
<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>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:
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();{"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.
| Option | Default | Meaning |
|---|---|---|
network | 'mainnet' | 'mainnet', 'napoleontest', 'testnet', or a network object |
peers | [] | ws:// or wss:// URLs tried first; the built-in list follows |
bootstrap | true | false: only peers |
maxPeers | 4 | 1 to 8 |
discover | true | Learn more endpoints from addr messages |
directory | the serving node | URL of a node's GET /api/v1/network/wsnodes; false never asks |
storage | none | true: IndexedDB (memory outside a browser, or when site data is blocked); or your own {get, set, del} |
checkpoint | built in | Your own checkpoint: 11 raw headers ending at a height |
rest | none | A node's REST base, for verifyTx without a block hint |
relay | true | Re-announce verified tips, answer getheaders |
txRelay | true (0.3.0) | Relay other people's transactions once full nodes in two network groups announced them and two served the same bytes |
showOnMap | true | Listed on the nodes' network map under its id and name, with the country of its connection; false: only counted |
identity | true | Keep 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, identityTimeout | none, 1500 ms | A store of your own for the identity, and how long a store may take |
sessionId, name | none | Your own 64-bit map id and three-word name: nothing is read or kept |
browserLinks | true (0.3.0) | Direct WebRTC links to other browsers |
desktopLinks | true (0.3.0) | Direct WebRTC links to Lotus Desktop apps |
maxBrowserLinks | 12 | 0 to 32; the node's list may lower it |
stun | the node's list | STUN servers for the links |
reportInterval | 30000 ms | Most often the name and links are sent to one node |
wait, timeout | true, 60000 ms | connect(): wait for the sync, and how long |
WebSocket, RTCPeerConnection, fetch | the globals | Other implementations (tests, Node.js) |
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 #
| Call | Result |
|---|---|
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).
| Event | Payload |
|---|---|
tip | The 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 #
<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 here | Not verified (trusted to full nodes) |
|---|---|
| Hash, target, difficulty rule, height, epoch hash, time rules, checkpoints of every header | Scripts, signatures, coins, double spends |
| Parent proofs of merged-mined blocks (from Napoleon): the parent's work, the commitment to this block, the anchor | Three 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(). browserLinksanddesktopLinksdefault totrue, with a switch each.txRelay(defaulttrue): relays other people's transactions under the vouched rule;txRelay: falseis the 0.2 behaviour.- Links to Lotus Desktop apps;
LinkInfo.appsaysbrowserordesktop. PeerInfo.meshId;node.voix(VOIX client, refused on mainnet, see VOIX).- With site data blocked,
storage: truefalls back to memory instead of failing to start.
