#
Accounts
high level
#
Example
#
External Signer (web)
import 'package:ndk/ndk.dart';
/// import web signer package
import 'package:nip07_event_signer/nip07_event_signer.dart';
void main() async {
/// create ndk obj
final ndk = Ndk.defaultConfig();
/// create web signer
final webSigner = Nip07EventSigner();
/// On the web, you need to call this first!
/// getPublicKeyAsync() caches the public key, and ndk uses the cached version
/// If you want to update from the real signer, call getPublicKeyAsync() again
await webSigner.getPublicKeyAsync();
/// login with the web signer
ndk.accounts.loginExternalSigner(signer: webSigner);
}
#
External Signer (nip-46 bunker)
login with bunker url
final bunkerConnection = await ndk.accounts.loginWithBunkerUrl(
bunkerUrl: "bunker://xxx",
bunkers: ndk.bunkers,
authCallback: (challenge) {},
);
login with nostr connect
final nostrConnect = NostrConnect(relays: ["wss://relay.example.com"]);
final bunkerConnection = await ndk.accounts.loginWithNostrConnect(
nostrConnect: nostrConnect,
bunkers: ndk.bunkers,
authCallback: (challenge) {},
);
login with bunker connection
final bunkerConnection = BunkerConnection(
privateKey: "privateKey",
remotePubkey: "remotePubkey",
relays: ["wss://relay.example.com"]
);
await ndk.accounts.loginWithBunkerConnection(
connection: bunkerConnection,
bunkers: ndk.bunkers,
authCallback: (challenge) {},
);
Important
Store the BunkerConnection details locally to re-establish the connection in future sessions. Use bunkerConnection.toJson() to serialize and BunkerConnection.fromJson() to restore. Without storing these, users will need to re-authenticate each time.
#
Authentication state
ndk.accounts.authStateChanges.listen((account) {
if (account == null) {
print('No active user');
} else {
print('Active user: ${account.pubkey}');
}
});
Events are fired when the following occurs:
- On login
- On logout
- On switch account
#
When to use
Use it to log in an account. You can use several types of accounts:
loginPrivateKeyloginPublicKey(read-only)loginExternalSigner(capabilities will be managed by external signer)
An account logged in is needed in order to use broadcast, and only if the signer used is able to sign.
You can switch between several logged in accounts with switchAccount(pubkey:...)