2026-06-07 16:45:43 +03:00
|
|
|
import 'package:actors/actors.dart';
|
|
|
|
|
import 'package:bip340/bip340.dart' as bip340;
|
2026-06-10 11:20:20 +03:00
|
|
|
|
|
|
|
|
import 'nostr_event.dart';
|
2026-06-07 16:45:43 +03:00
|
|
|
|
|
|
|
|
sealed class SignerMessage {
|
|
|
|
|
const SignerMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class SignEvent extends SignerMessage {
|
|
|
|
|
final int kind;
|
|
|
|
|
final List<List<String>> tags;
|
|
|
|
|
final String content;
|
|
|
|
|
final int? createdAt;
|
|
|
|
|
const SignEvent({
|
|
|
|
|
required this.kind,
|
|
|
|
|
this.tags = const [],
|
|
|
|
|
required this.content,
|
|
|
|
|
this.createdAt,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class GetPublicKey extends SignerMessage {
|
|
|
|
|
const GetPublicKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed class SignerResult {
|
|
|
|
|
const SignerResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class EventSigned extends SignerResult {
|
|
|
|
|
final String event;
|
|
|
|
|
const EventSigned(this.event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class PublicKeyResult extends SignerResult {
|
|
|
|
|
final String publicKey;
|
|
|
|
|
const PublicKeyResult(this.publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NostrSignerActor with Handler<SignerMessage, SignerResult> {
|
|
|
|
|
final String _privateKey;
|
|
|
|
|
final String _publicKey;
|
|
|
|
|
|
|
|
|
|
NostrSignerActor(String privateKey)
|
|
|
|
|
: _privateKey = privateKey,
|
|
|
|
|
_publicKey = bip340.getPublicKey(privateKey);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<SignerResult> handle(SignerMessage message) => switch (message) {
|
|
|
|
|
SignEvent(:final kind, :final tags, :final content, :final createdAt) =>
|
|
|
|
|
Future.value(_signEvent(kind, tags, content, createdAt)),
|
|
|
|
|
GetPublicKey() => Future.value(PublicKeyResult(_publicKey)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EventSigned _signEvent(
|
|
|
|
|
int kind,
|
|
|
|
|
List<List<String>> tags,
|
|
|
|
|
String content,
|
|
|
|
|
int? createdAt,
|
2026-06-10 11:20:20 +03:00
|
|
|
) =>
|
|
|
|
|
EventSigned(signNostrEvent(
|
|
|
|
|
privateKey: _privateKey,
|
|
|
|
|
publicKey: _publicKey,
|
|
|
|
|
kind: kind,
|
|
|
|
|
tags: tags,
|
|
|
|
|
content: content,
|
|
|
|
|
createdAt: createdAt,
|
|
|
|
|
));
|
2026-06-07 16:45:43 +03:00
|
|
|
}
|