nostr_bunker 2.0.0
nostr_bunker: ^2.0.0 copied to clipboard
With this package your app can act as a bunker and will be able to sign events from other Nostr apps.
With this package your app can act as a bunker, the remote signer of NIP-46, and will be able to sign events from other Nostr apps.
Usage #
The bunker keeps its apps and keys in memory only: save them each time changes emits, and give them back to the constructor.
final relayManager = RelayManager(
nostrTransportFactory: webSocketTransportFactory(),
);
final bunker = Bunker(
relayReqs: RelayReqs(
relayManager: relayManager,
eventVerifier: Bip340EventVerifier(),
),
relayPublishes: RelayPublishes(relayManager: relayManager),
signerFactory: const Bip340SignerFactory(),
privateKeys: ["private_key"],
);
// listen to the streams before starting: they do not replay past requests
bunker.start();
await bunker.stop();
// add and remove accounts
bunker.addPrivateKey("private_key");
bunker.removePrivateKey("public_key");
// or keep the key in a NostrSigner of your own, such as a hardware one
bunker.addSigner(signer);
bunker.removeSigner("public_key");
// connect an app with bunker://
final bunkerUrl = bunker.getBunkerUrl(
userPubkey: "public_key_to_connect",
onConnected: (app) => print("Connected: ${app.name}"),
);
bunker.cancelBunkerUrl(bunkerUrl); // if no app used it
// connect an app with nostrconnect://
final nostrConnect = NostrConnectUrl.fromUrl("nostrconnect://...");
// nothing is granted: show nostrConnect.requestedPermissions, pass the accepted ones
final app = await bunker.connectApp(
userPubkey: "public_key_to_connect",
nostrConnect: nostrConnect,
permissions: [Permission(command: "sign_event:1")],
appName: "new_name", // rename the app
);
// an app cannot change: edit it through the bunker, so that changes emits
bunker.addApp(savedApp); // its bunker key must be in privateKeys
bunker.enableApp(app);
bunker.disableApp(app);
bunker.allowForever(command: "sign_event:1", app: app);
bunker.rejectForever(command: "sign_event", app: app); // the kinds without their own permission
bunker.updateApp(app.copyWith(listenOnUserPubkey: true)); // for a client that addresses the user pubkey
bunker.updateApp(app.copyWith(userPubkey: "other_public_key")); // move the app to another account
bunker.updateApp(app.copyWith(relays: ["wss://relay.example.com"])); // see switch_relays below
bunker.removeApp(app); // also forgets its bunker key
bunker.apps; // the current version of each app
// pending requests wait for the user, blocked ones already got an error
bunker.pendingRequestsStream.listen((request) {
if (request.useNip44) {
bunker.processRequest(request);
} else {
bunker.rejectRequest(request);
}
});
// store this, each time it changes
bunker.changes.listen((_) {
bunker.apps;
bunker.privateKeys;
});
await bunker.dispose();
await relayManager.dispose();
Requests #
The bunker answers the NIP-46 methods connect, ping, get_public_key, sign_event, nip04_encrypt, nip04_decrypt, nip44_encrypt, nip44_decrypt, switch_relays and logout, and answers any other method with an error. It only answers while it is started, and skips the requests older than five minutes that a relay kept.
Some requests never wait for the user:
switch_relaysgets the relays of the app. WhenupdateAppchanges them, the bunker keeps listening and answering on the previous ones, kept inApp.previousRelays, until the app sendsswitch_relaysand moves.logoutgetsack, then the app is removed.- The first
get_public_keyof a new app is answered even before the app is enabled, so its login completes.
Any other request of a disabled app gets an error. For an enabled app, its permissions decide first: a permission is a method such as nip44_encrypt, or sign_event:<kind>, and sign_event alone covers the kinds that have no permission of their own. A refused permission gets an error, an allowed one runs. When no permission matches, the AuthorisationMode of the app decides:
| Mode | Request without a permission |
|---|---|
alwaysAsk |
pending |
allowCommonRequests |
runs when it is connect, ping, get_public_key, nip04_encrypt, nip44_encrypt or sign_event of kind 1, 4, 6 or 7, pending otherwise |
fullyTrust |
runs |
A pending request goes to pendingRequestsStream, for the user to run with processRequest or refuse with rejectRequest.
Additional information #
This package uses Dostr internally: dostr_core for the relays, and any LocalSignerFactory and EventVerifier you choose, such as the pure Dart ones of dostr_bip340.
dependencies:
dostr_bip340: ^0.1.0
dostr_core: ^0.1.0
nostr_bunker: ^2.0.0
Share one RelayManager with the rest of your app: the bunker only opens REQs and publishes on it, and never disposes it.