# Accounts

high level

# Example


// generate a new key
KeyPair key1 = Bip340.generatePrivateKey();

// login using private key
ndk.accounts
    .loginPrivateKey(privkey: key1.privateKey!, pubkey: key1.publicKey);

// broadcast a new event using the logged in account with it's signer to sign
NdkBroadcastResponse response = ndk.broadcast.broadcast(
    nostrEvent: Nip01Event(
        pubKey: key1.publicKey,
        kind: Nip01Event.kTextNodeKind,
        tags: [],
        content: "test"),
    specificRelays: DEFAULT_BOOTSTRAP_RELAYS);
await response.broadcastDoneFuture;

// logout

# 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);
}

# When to use

Use it to log in an account. You can use several types of accounts:

  • loginPrivateKey
  • loginPublicKey (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:...)