wrapEvent static method

Future<Nip01Event> wrapEvent({
  1. required String recipientPublicKey,
  2. required Nip01Event sealEvent,
  3. List<List<String>>? additionalTags,
})

wraps a sealed msg
recipientPublicKey the reciever of the rumor
sealEvent not wrapped event
returns giftWrapEvent

Implementation

static Future<Nip01Event> wrapEvent({
  required String recipientPublicKey,
  required Nip01Event sealEvent,
  List<List<String>>? additionalTags,
}) async {
  // Generate a random one-time-use keypair
  final ephemeralKeys = Bip340.generatePrivateKey();
  final ephemeralSigner = Bip340EventSigner(
    privateKey: ephemeralKeys.privateKey,
    publicKey: ephemeralKeys.publicKey,
  );

  final encryptedSeal = await ephemeralSigner.encryptNip44(
    plaintext: jsonEncode(sealEvent),
    recipientPubKey: recipientPublicKey,
  );

  if (encryptedSeal == null) {
    throw Exception("encryptedSeal is null");
  }

  final tags = <List<String>>[
    ['p', recipientPublicKey]
  ];

  // Add any additional tags if provided
  if (additionalTags != null) {
    tags.addAll(additionalTags);
  }

  final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;

  // Create the gift wrap event with ephemeral keys
  final giftWrapEvent = Nip01Event(
    kind: kGiftWrapEventkind,
    content: encryptedSeal,
    tags: tags,
    createdAt: now,
    pubKey: ephemeralKeys.publicKey,
  );

  // Sign with ephemeral key
  final signature = Bip340.sign(giftWrapEvent.id, ephemeralKeys.privateKey!);

  giftWrapEvent.sig = signature;

  return giftWrapEvent;
}