This page installs the SDK on Node.js, creates a wallet, syncs one chain, and reads the private balance.
npm add @railgun-reloaded/wallet-sdk better-sqlite3 viem
For the browser, see Browser.
RailgunClient.create() opens the wallet
database under dataDir. The default dataDir is ./.railgun.
import { RailgunClient } from '@railgun-reloaded/wallet-sdk/node'
const client = await RailgunClient.create({ dataDir: './.railgun' })
The client initializes the cryptography libraries on the first call to
loadWallet, decrypt, or sync. To do this at startup, call
await client.initialize().
A wallet is a BIP39 mnemonic and an index. The SDK encrypts the mnemonic with
a 32-byte encryptionKey and stores it. You keep the key. The SDK does not
store it.
import { randomBytes } from 'node:crypto'
import { Mnemonic } from '@railgun-reloaded/wallet-sdk/node'
const mnemonic = Mnemonic.generate()
const encryptionKey = new Uint8Array(randomBytes(32))
const { walletId } = await client.createWallet({ mnemonic, encryptionKey, name: 'primary' })
const wallet = await client.loadWallet(walletId, encryptionKey)
console.log(wallet.railgunAddress) // '0zk1...'
createWallet throws WalletAlreadyExistsError when the same mnemonic and
index already exist. The error has the walletId of the existing wallet.
sync() scans the chain into the local chain
database and then decrypts the notes of the wallet. Chains 137 (Polygon) and
8453 (Base) have a default indexer. For other chains, pass a dataSource.
See Syncing.
const summary = await client.sync(walletId, encryptionKey, { chainId: 8453 })
console.log(summary.scan.lastBlock, summary.decrypt.notesAdded)
const balances = await client.getBalances(walletId, 8453)
for (const { token, balance } of balances) {
console.log(token, balance) // bigint, in base units of the token
}
getBalances returns only the Spendable balance by default. Pass
'all' or one WalletBalanceBucket as the third argument for other notes.
await client.close()
close() closes only the databases that the client opened. Wait for each
scan() or sync() to finish before you call close().