Server Wallet

The server wallet runs on Node.js using a private key and @bsv/wallet-toolbox. It's used for automated operations, payment processing, and funding flows where no user interaction is needed.

Creating a Server Wallet

import { ServerWallet } from '@bsv/simple/server'

const wallet = await ServerWallet.create({
  privateKey: process.env.SERVER_PRIVATE_KEY!,
  network: 'main',
  storageUrl: 'https://storage.babbage.systems'
})

console.log('Identity Key:', wallet.getIdentityKey())

Configuration

Parameter
Type
Default
Description

privateKey

string

required

Hex-encoded private key

network

'main' | 'testnet'

'main'

Network to operate on

storageUrl

string

'https://storage.babbage.systems'

Wallet storage provider URL

What Happens During Creation

  1. A PrivateKey and KeyDeriver are created from the hex key

  2. A WalletStorageManager, WalletSigner, and Services instance are configured

  3. A StorageClient connects to the storage URL and becomes available

  4. The ToolboxWallet is instantiated and all module methods are mixed in

  5. The composed ServerWallet is returned

Funding Flow

The server wallet uses a request/response pattern to receive funds from a browser wallet:

Step 1: Server Creates a Payment Request

This generates a random BRC-29 derivation suffix so the server can later prove ownership of the payment.

Step 2: Browser Wallet Funds the Server

Step 3: Client Sends Transaction to Server

Step 4: Server Internalizes the Payment

This uses the wallet payment protocol to internalize the output with proper derivation info, so the server wallet can spend it later.

Private Key Persistence

Key persistence is handled automatically by handler factories. If you need lower-level access, use generatePrivateKey() instead of importing @bsv/sdk:

For Next.js API routes, use the handler factory which manages key persistence automatically:

Key persistence order (automatic):

  1. process.env.SERVER_PRIVATE_KEY — Environment variable (production)

  2. .server-wallet.json file — Persisted from previous run (development)

  3. Auto-generated via generatePrivateKey() — Fresh key (first run)

Important: Add .server-wallet.json to your .gitignore. Never commit private keys.

Shared Methods

The server wallet has all the same methods as the browser wallet:

  • pay(), send()

  • createToken(), listTokenDetails(), sendToken(), redeemToken()

  • inscribeText(), inscribeJSON(), inscribeFileHash(), inscribeImageHash()

  • certifyForMessageBox(), sendMessageBoxPayment(), listIncomingPayments(), acceptIncomingPayment()

  • acquireCertificateFrom(), listCertificatesFrom(), relinquishCert()

  • getDID(), resolveDID(), registerDID()

  • acquireCredential(), listCredentials(), createPresentation()

  • advertiseSHIP(), advertiseSLAP(), broadcastAction(), withRetry()

Next.js API Route Example

See the Next.js Integration Guide for a complete API route implementation with caching and key persistence.

Last updated