encryptMessage function

Future<EncryptMessageResponse> encryptMessage({
  1. required Secp256k1KeyIdentity identity,
  2. required Secp256k1PublicKey theirPublicKey,
  3. required String text,
})

Implementation

Future<EncryptMessageResponse> encryptMessage({
  required Secp256k1KeyIdentity identity,
  required Secp256k1PublicKey theirPublicKey,
  required String text,
}) async {
  final sharedPoint = await getECShareSecret(
    identity.getKeyPair().secretKey,
    theirPublicKey.toRaw(),
  );

  final sharedX = sharedPoint.sublist(0, 32);

  final List<int> iv = randomAsU8a(12);
  // randomAsU8a(16);

  final encryptedMessage256 = await _encryptPhraseAsync256(
    key: sharedX,
    iv: Uint8List.fromList(iv),
    message: text,
  );
  final encryptedMessage = base64Encode(encryptedMessage256);
  final ivBase64 = base64Encode(iv);

  return EncryptMessageResponse(
    content: '$encryptedMessage?iv=$ivBase64',
    tags: [
      ['p', theirPublicKey.toRaw().toHex()],
    ],
    kind: 4,
    createdAt: (DateTime.now().millisecondsSinceEpoch / 1000).floor(),
    pubKey: identity.getPublicKey().toRaw().toHex(),
  );
}