37 lines
970 B
Dart
37 lines
970 B
Dart
|
|
import 'dart:convert';
|
||
|
|
import 'dart:math';
|
||
|
|
|
||
|
|
import 'package:bip340/bip340.dart' as bip340;
|
||
|
|
import 'package:crypto/crypto.dart';
|
||
|
|
|
||
|
|
String signNostrEvent({
|
||
|
|
required String privateKey,
|
||
|
|
required String publicKey,
|
||
|
|
required int kind,
|
||
|
|
required List<List<String>> tags,
|
||
|
|
required String content,
|
||
|
|
int? createdAt,
|
||
|
|
}) {
|
||
|
|
final ts = createdAt ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||
|
|
final ser = jsonEncode([0, publicKey, ts, kind, tags, content]);
|
||
|
|
final idBytes = sha256.convert(utf8.encode(ser)).bytes;
|
||
|
|
final id = idBytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
||
|
|
|
||
|
|
final rng = Random.secure();
|
||
|
|
final aux = List.generate(32, (_) => rng.nextInt(256))
|
||
|
|
.map((b) => b.toRadixString(16).padLeft(2, '0'))
|
||
|
|
.join();
|
||
|
|
|
||
|
|
final sig = bip340.sign(privateKey, id, aux);
|
||
|
|
|
||
|
|
return jsonEncode({
|
||
|
|
'id': id,
|
||
|
|
'pubkey': publicKey,
|
||
|
|
'created_at': ts,
|
||
|
|
'kind': kind,
|
||
|
|
'tags': tags,
|
||
|
|
'content': content,
|
||
|
|
'sig': sig,
|
||
|
|
});
|
||
|
|
}
|