Quick start
This page gets libspiffy running in one Dart file: you start the actor system, create a wallet, create an invoice, query the balance, handle a failure and shut down. For a full payment between two wallets, follow the tutorial.
How you talk to libspiffy
Section titled “How you talk to libspiffy”libspiffy.coordinator is a WalletCoordinator. It has three methods:
| Method | What it does |
|---|---|
ask(request, {timeout}) |
Sends a command or query and returns a Future of its own reply. |
tell(command) |
Sends without waiting. A reply, if there is one, arrives only on the event stream. |
on<E>({walletId}) |
A Stream<E> of one kind of event, of one wallet when you name it. Use it for what happens without a request: a balance change, an invoice paid, a channel request from a peer. |
Every command or query with an answer is a CoordinatorRequest<R>. It names its reply type R (for
CreateWalletCommand, a WalletCreatedEvent), and its requestId is fixed when you create it: generated,
unless you pass one. The coordinator answers each request with exactly one R carrying that requestId,
so two requests of the same kind running at once each get their own reply.
// Sketch: the shape of every interaction.final balance = await libspiffy.coordinator.ask(GetBalanceQuery(walletId: 'shop'));print(balance.totalBalance);The complete program
Section titled “The complete program”This program runs on mainnet, the default network, with P2P off, so it needs no peers to finish. Its keys are held in memory and lost when it exits, so never pay the address it prints. It writes its event store to a fresh temporary directory, so you can run it again. To run it on testnet, see Testnet below.
import 'dart:io';
import 'package:libspiffy/coordinator.dart';import 'package:libspiffy/libspiffy.dart';
Future<void> main() async { final libspiffy = LibSpiffyActorSystem(); await libspiffy.initialize( dataDirectory: Directory.systemTemp.createTempSync('quick_start_').path, networkType: 'main', // the default; 'test' for testnet arcConfig: ArcServiceConfig.bsvaArcadeMainnet(), enableP2P: false, ); final coordinator = libspiffy.coordinator;
// Events nobody asked for: a balance change, a failure no request caused. final balances = coordinator .on<BalanceUpdatedEvent>(walletId: 'shop') .listen((e) => print('shop balance is now ${e.totalBalance} sats')); final errors = coordinator .on<ErrorEvent>() .listen((e) => print('error from ${e.source}: ${e.message}'));
// 1. Create an HD wallet. The app supplies the mnemonic and must keep it. final mnemonic = await libspiffy.cryptoService.generateMnemonic(); final wallet = await coordinator.ask(CreateWalletCommand( walletId: 'shop', name: 'Shop wallet', mnemonic: mnemonic, )); print('Root address: ${wallet.rootAddress}');
// 2. Create an invoice for 10,000 sats. final invoice = await coordinator.ask(CreateInvoiceCommand( walletId: 'shop', amount: BigInt.from(10000), description: 'Order 1001', expiresIn: const Duration(hours: 1), )); print('Invoice ${invoice.invoiceId}: pay ${invoice.amount} sats ' 'to ${invoice.addresses.first}');
// 3. Read the balance. final balance = await coordinator.ask(GetBalanceQuery(walletId: 'shop')); print('Spendable: ${balance.totalBalance} sats ' '(${balance.confirmedBalance} confirmed, ' '${balance.unconfirmedBalance} unconfirmed)');
// 4. A request that fails throws CoordinatorFailure. try { await coordinator.ask(CreateWalletCommand( walletId: 'shop', // already exists name: 'Shop wallet', mnemonic: mnemonic, )); } on CoordinatorFailure catch (failure) { print('Not created: ${failure.message}'); // "Wallet already exists" }
// 5. Shut down: stops the actors and closes the stores libspiffy opened. await balances.cancel(); await errors.cancel(); await libspiffy.shutdown();}A reply that ask returns is always a success: you do not check success on it. WalletCreatedEvent
arrives once the read model holds the wallet and its rootAddress, and InvoiceCreatedEvent once it holds
the invoice, so a query you send next sees them.
Testnet
Section titled “Testnet”libspiffy runs on mainnet unless you name another network. To try it on testnet, name testnet when you
start the system. Wallets you create or import follow the network initialize() was given, unless the
command names another one.
// Sketch: the same program on testnet.await libspiffy.initialize( dataDirectory: Directory.systemTemp.createTempSync('quick_start_').path, networkType: 'test', arcConfig: ArcServiceConfig.bsvaArcadeTestnet(), enableP2P: false,);final wallet = await coordinator.ask(CreateWalletCommand( walletId: 'shop', name: 'Shop wallet', mnemonic: mnemonic,)); // a testnet wallet: it follows initialize()Handling failures
Section titled “Handling failures”ask throws CoordinatorFailure when the request failed. It has three fields and one getter:
| Member | What it holds |
|---|---|
requestId |
The failed request’s requestId. |
message |
Why it failed. |
event |
What reported the failure: the request’s own reply (for example a WalletCreatedEvent with success: false), or an ErrorEvent that names the request. Null when the coordinator stopped first. |
closed |
True when the coordinator stopped (shutdown()) before it answered. |
// Sketch: tell a refusal from a shutdown.try { await coordinator.ask(DeleteWalletCommand(walletId: 'old-wallet'));} on CoordinatorFailure catch (failure) { if (failure.closed) return; // libspiffy is shutting down showError(failure.message); // showError is your own UI code}Failures that no request caused, such as a broadcast retried later or a channel step the counterparty
started, arrive as an ErrorEvent with a null requestId. Follow them with coordinator.on<ErrorEvent>(),
as the program above does. source names the operation that failed and walletId, when set, the wallet.
Timeouts
Section titled “Timeouts”ask throws TimeoutException (from dart:async) when no reply arrives in time. Each request has its own
default, its replyTimeout: one minute for most requests, more for those that wait on the network
(CoordinatorRequest.paymentTimeout is 3 minutes, receiveTimeout 5, importTimeout an hour). Pass
timeout: to choose your own.
// Sketch: wait at most 10 seconds for the balance.try { final balance = await coordinator.ask( GetBalanceQuery(walletId: 'shop'), timeout: const Duration(seconds: 10), ); render(balance); // render is your own UI code} on TimeoutException { showStale(); // the request may still finish}Following events with on()
Section titled “Following events with on()”on<E>() filters the coordinator’s event stream by type, and by walletId when you pass one. Replies are
published there too, so on<InvoiceCreatedEvent>() sees every invoice created, whoever asked.
libspiffy.coordinatorEvents is the whole stream, if you want every event in one listener.
// Sketch: what an app usually follows.coordinator.on<InvoicePaidEvent>(walletId: 'shop').listen(markOrderPaid);coordinator.on<BalanceUpdatedEvent>(walletId: 'shop').listen(showBalance);coordinator.on<ErrorEvent>().listen(logError);A reply type can also arrive with no request behind it, for example a payment replayed when its block
header arrives. Its requestId is null then.
initialize() parameters
Section titled “initialize() parameters”LibSpiffyActorSystem.initialize() takes only named parameters. The ones you use first:
| Parameter | Default | What it does |
|---|---|---|
networkType |
'main' |
'main', 'test' or 'regtest' ('mainnet', 'livenet' and 'testnet' are accepted too). Selects the genesis header, DNS seeds and address encoding. Before 6.0.0 the default was 'test'. |
arcConfig |
ArcServiceConfig.bsvaArcadeMainnet() on mainnet (the default), otherwise ArcServiceConfig.bsvaArcadeTestnet() |
The ARC or Arcade endpoint for broadcasts and fee rates. Presets: bsvaArcadeMainnet, bsvaArcadeTestnet, or ArcServiceConfig.custom(baseUrl: ...). The TAAL and GorillaPool ARC presets stop working when ARC is deprecated on 30 November 2026; see Network, ARC and data sources. |
enableP2P |
true |
Connect to BSV peers and sync block headers. |
peerAddresses |
the network’s DNS seeds | Peers as 'host:port'. Regtest has no seeds, so name your peers there. |
userAgent |
'/LibSpiffy-BSV:1.0/' |
Sent in the version handshake. Teranode bans peers whose user agent lacks BSV or Bitcoin SV. |
startHeight |
none | The height reported to peers in the handshake. Header sync does not start from it; it continues from the stored chain’s tip. |
isar |
none | An Isar instance your app opened with LibSpiffySchemas.allSchemas. libspiffy stores events, read models, headers and the broadcast retry queue in it. |
dataDirectory |
'./data' when needed |
Used only when you pass no isar: libspiffy opens its own Isar event store here. |
secureStorage |
InMemorySecureStorage() |
Where mnemonics, WIFs and xprivs live. The default loses them on restart. |
storageBackend |
StorageBackend.isar |
isar, postgres (with postgresConfig) or inMemory. |
blockchainDataSource |
none | Where ImportWalletCommand fetches a wallet’s history, for example a WhatsOnChainDataSource. Without one, an import fails. |
cdnBaseUrl |
none | A header CDN to sync from before peers. See header sync via CDN. |
The full list, including payment channel and deferred payment settings, is in the API docs.
When libspiffy is ready
Section titled “When libspiffy is ready”initialize() returns after the actors start, the optional CDN header sync finishes, and the P2P layer has
connected to at least one peer. On a first install with a header CDN that can take minutes.
You do not have to wait that long to send commands. libspiffy.ready is a Future that completes as soon
as the actors take commands, before header sync; libspiffy.isReady tells you whether it has. Creating or
importing a wallet needs no headers, and a received payment whose block header has not arrived waits for
it.
// Sketch: start in the background, act as soon as the actors run.final started = libspiffy.initialize(networkType: 'main', isar: isar);await libspiffy.ready;final wallet = await libspiffy.coordinator.ask( CreateWalletCommand(walletId: 'shop', name: 'Shop', mnemonic: mnemonic));await started; // header sync and P2P are upIf enableP2P is true and no peer can be reached, initialize() throws a StateError (“P2P
initialization failed …”) after the actors have started. The system stays usable without P2P; catch the
error if your app can live with that.
Using tell() and the event stream
Section titled “Using tell() and the event stream”ask is what most code needs. If you would rather send and move on, tell the request and find its reply
on the stream by requestId. Pass your own requestId, or read the generated one from the request:
// Sketch: fire and forget, then match the reply yourself.final query = GetBalanceQuery(walletId: 'shop');coordinator .on<BalanceResponse>(walletId: 'shop') .where((r) => r.requestId == query.requestId) .first .then((r) => print('balance ${r.totalBalance}'));coordinator.tell(query);With tell, a failure is on the reply itself: success and error on most replies, and
CoordinatorReply.failure gives the reason for any reply type. An ErrorEvent that a request caused
carries the request’s requestId. Subscribe before you tell, or the reply can pass before you listen.
Shutting down
Section titled “Shutting down”await libspiffy.shutdown() disconnects from peers, stops the projections and actors, and closes what
libspiffy opened. A request still waiting in ask then fails with a CoordinatorFailure whose closed is
true. shutdown() does not shut down an actor system or close an Isar instance that your app passed in. A
shut-down LibSpiffyActorSystem cannot be initialized again; create a new instance.
Upgrading from 5.17 to 6.0
Section titled “Upgrading from 5.17 to 6.0”6.0.0 changes the default network: up to 5.17.x, a network you did not name was testnet. Now it is mainnet, where coins are real. A wallet now follows the network the system was started with.
initialize()andinitializeLibSpiffy()withoutnetworkTyperun on mainnet: mainnet genesis header and DNS seeds, mainnet addresses, and the mainnet Arcade (bsvaArcadeMainnet()) when you pass noarcConfig.- A wallet follows
initialize()unless its command names a network. ACreateWalletCommandwithout a'network'entry inwalletMetadata, and anImportWalletCommandwithoutnetworkType, create the wallet on the networkinitialize()was given. Before 6.0 such a wallet was always testnet, so a mainnet app had to name mainnet on every command. A network the command names still wins.ImportWalletCommand.networkTypeis now aString?: null means the system’s network. BroadcastCallbackConfig.bridgeTestnet()is removed. libspiffy names no callback bridge. If you used the preset, build aBroadcastCallbackConfig(...)from your own bridge settings: itscallbackUrl,bridgePeerIdandbridgeX25519PubHex.- The lower-level defaults follow: a wallet row stored without a network
(
ReadModelStorage.storeWallet),NetworkParams.forNetwork(null),NetworkName.canonical(null), and thedartsv.NetworkTypeparameters ofDartSVCryptoService,CryptoService,TransactionAnalyzerandScriptTypeRegistryare all mainnet when no network is given.
A network you pass behaves exactly as before. To keep running on testnet, pass networkType: 'test' to
initialize(); the coordinator’s wallet commands follow it. Where you build those services yourself,
pass dartsv.NetworkType.TEST. Wallets already created keep the network they were created with.
Upgrading from 5.5 to 5.17
Section titled “Upgrading from 5.5 to 5.17”These releases add features and change no command you send. What an app has to act on:
- ARC is deprecated on 30 November 2026. Since 5.15.0,
initialize()with noarcConfiguses the BSV Association’s Arcade on both networks. If you pass a TAAL or GorillaPool preset, change tobsvaArcadeMainnet()orbsvaArcadeTestnet()before then. Arcade publishes the network’s own rate (1 sat/kB), so setminimumFeeRateif you want to pay more. See Network, ARC and data sources. - libspiffy 5.12.0 and later need eventador 4.2.0. With 4.1.0, an event of about 2 MiB (a transaction
with many outputs and its parents) was written outside the journal’s index, and the wallet’s next write
failed. If your app depends on
eventadoritself, raise its constraint to^4.2.0. - Handle the new error codes. Since 5.10.0 replies carry
errorCodeandCoordinatorFailurehasspiffyCode. 5.17.0 addsSpiffyErrorCode.noSuchPayment; an exhaustiveswitchover the codes needs a case for it. Where libspiffy threwStateErrororArgumentError, it now throwsSpiffyStateErrororSpiffyArgumentError, whichon StateErrorandon ArgumentErrorstill catch. See Error codes. - Restored coins can be pending (since 5.17.0). A coin an import or address scan finds is available at once only when its merkle proof checks against a header the wallet holds; otherwise it waits for the header. With a header checkpoint, that is every coin older than the checkpoint until the older headers are filled in.
- ARC’s fee policy is read once, when the system starts (since 5.13.0), not for every payment. A test that serves its own policy must set it before the system starts.
- A subclass of
ArcServicethat overridessubmitTransactionorsubmitBatchTransactionsadds their newcallbackTokenparameter (5.17.0). lockedBalance(HODLocker coins) is onBalanceResponseandBalanceUpdatedEvent, and is not part oftotalBalance.
What’s new: locked coins, SignMessageCommand,
RecordType42DestinationsCommand, PaymentAcceptedEvent, ImportWalletCommand.addresses,
ExportTransactionQuery(requireProof: false), and header checkpoints.
Upgrading from 5.0 to 5.5
Section titled “Upgrading from 5.0 to 5.5”These releases change how payment channels behave. What an app has to act on:
- Mixed versions break channel opens. Since 5.2.0 a client opens a channel only when its server sends
channel_opened, which a 5.0 or 5.1 server never sends: the client resendschannel_openunanswered until the settlement margin begins. A 5.0 or 5.1 client still opens with a newer server. Upgrade your servers first. ChannelPayCommandis answered when the server acknowledges the payment (since 5.1.0), not when it is handed to your transport. A payment with no acknowledgement in time fails withCoordinatorFailure.stillSenttrue, and is still sent. See Payment channels.- Report what your transport could not send, and when a peer is back. Tell the coordinator
P2PSendFailedandChannelPeerReachable, and followChannelPeerAwayEvent(since 5.1.0 and 5.3.3). See Carry the peer messages. - libspiffy 5.3.2 and later need eventador 4.1.0. If your app depends on
eventadoritself, raise its constraint to^4.1.0. - 5.5.0 adds commands, and changes nothing you call. If your app reached the wallet’s internals for
what they do, use them instead:
RecordType42AddressesCommand,RenameWalletCommand,BroadcastTransactionCommandandCheckPendingTransactionsCommand(Commands).Bip32,AddressDiscoveryServiceandAnchorKeysare exported frompackage:libspiffy/libspiffy.dart.
The changelog has every change.
Upgrading from 4.x
Section titled “Upgrading from 4.x”libspiffy 5.0.0 changed how you talk to the coordinator. The breaking changes an app usually hits:
libspiffy.coordinatoris aWalletCoordinator, not anActorRef.tellworks as before. Change any variable or field typedActorRefthat holds it.queryIdis nowrequestIdonGetBalanceQuery,GetTransactionsQuery,GetTransactionDetailQuery,ExportTransactionQuery,GetDeferredPaymentsQuery,GetHeaderSyncStatusQueryand their replies. On a reply,requestIdis nullable.- Replace your own “send, then wait for the matching event” helpers with
ask. Each request now gets exactly one reply carrying itsrequestId, and anErrorEventit causes carries it too. - Some commands have new replies.
DeleteWalletCommandis answered withWalletDeletedEvent,ReleaseUTXOsCommandwithUTXOsReleasedEvent,AcceptChannelCommandwithChannelAcceptedEvent,RejectChannelCommandwithChannelRejectedEventandExpireChannelCommandwithChannelExpiredEvent. A refusal ofDeleteWalletCommand,ReleaseUTXOsCommandorRecordOutgoingCommandis the reply’s failure, no longer anErrorEvent. ImportWalletCommandwith a mnemonic imports the wallet’s history, answered withImportCompleteEvent. In 4.x it only created the wallet. Without ablockchainDataSourceit fails.TimestampCommandis answered with ARC’s answer to the broadcast, not when the broadcast is handed to ARC.- Removed:
RefreshWalletCommand,NodeRpcDataSource(useWhatsOnChainDataSourceor your ownBlockchainDataSource),LibSpiffyActorSystem.broadcastChannelEvent, thePaymentReadyEvent.errorandProvisioningCompleteEvent.errorconstructors, andCdnHeaderSyncConfig.concurrentDownloads. ChannelP2PReceivedandChannelP2PMessageToSendEventare removed. Deliver what your transport receives asP2PMessageReceived, and send everyP2PMessageToSendEvent.
The full list, including the removed Isar where clauses and actor constructor parameters, is in the 5.0.0 entry of the changelog.
- Follow the tutorial to fund a wallet and send an SPV payment end to end.
- Read about event type registration before you add events of your own.