Nip44Encrypt
This commit is contained in:
parent
60f9e4ccfa
commit
1fee9e5d34
2 changed files with 32 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:actors/actors.dart';
|
||||
import 'package:bip340/bip340.dart' as bip340;
|
||||
|
||||
import 'nip44.dart';
|
||||
import 'nostr_event.dart';
|
||||
|
||||
sealed class SignerMessage {
|
||||
|
|
@ -24,6 +25,20 @@ final class GetPublicKey extends SignerMessage {
|
|||
const GetPublicKey();
|
||||
}
|
||||
|
||||
/// NIP-44 v2 encrypt-to-self: encrypts [plaintext] with this signer's own key
|
||||
/// (conversation key derived from its private key and public key).
|
||||
final class Nip44Encrypt extends SignerMessage {
|
||||
final String plaintext;
|
||||
const Nip44Encrypt(this.plaintext);
|
||||
}
|
||||
|
||||
/// NIP-44 v2 decrypt-from-self: inverse of [Nip44Encrypt]. Throws on a version
|
||||
/// mismatch or HMAC failure (e.g. a payload sealed with a different key).
|
||||
final class Nip44Decrypt extends SignerMessage {
|
||||
final String payload;
|
||||
const Nip44Decrypt(this.payload);
|
||||
}
|
||||
|
||||
sealed class SignerResult {
|
||||
const SignerResult();
|
||||
}
|
||||
|
|
@ -38,6 +53,12 @@ final class PublicKeyResult extends SignerResult {
|
|||
const PublicKeyResult(this.publicKey);
|
||||
}
|
||||
|
||||
/// Result of a [Nip44Encrypt] (base64 payload) or [Nip44Decrypt] (plaintext).
|
||||
final class Nip44Text extends SignerResult {
|
||||
final String value;
|
||||
const Nip44Text(this.value);
|
||||
}
|
||||
|
||||
class NostrSignerActor with Handler<SignerMessage, SignerResult> {
|
||||
final String _privateKey;
|
||||
final String _publicKey;
|
||||
|
|
@ -51,6 +72,12 @@ class NostrSignerActor with Handler<SignerMessage, SignerResult> {
|
|||
SignEvent(:final kind, :final tags, :final content, :final createdAt) =>
|
||||
Future.value(_signEvent(kind, tags, content, createdAt)),
|
||||
GetPublicKey() => Future.value(PublicKeyResult(_publicKey)),
|
||||
Nip44Encrypt(:final plaintext) => Future.value(
|
||||
Nip44Text(nip44Encrypt(plaintext, _privateKey, _publicKey)),
|
||||
),
|
||||
Nip44Decrypt(:final payload) => Future.value(
|
||||
Nip44Text(nip44Decrypt(payload, _privateKey, _publicKey)),
|
||||
),
|
||||
};
|
||||
|
||||
EventSigned _signEvent(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue