You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

import 'dart:convert';
import 'package:crypto/crypto.dart';
/// Represents a Nostr keypair (private and public keys).
class NostrKeyPair {
/// Private key in hex format (32 bytes, 64 hex characters).
final String privateKey;
/// Public key in hex format (32 bytes, 64 hex characters).
final String publicKey;
/// Creates a [NostrKeyPair] with the provided keys.
///
/// [privateKey] - Private key in hex format.
/// [publicKey] - Public key in hex format.
NostrKeyPair({
required this.privateKey,
required this.publicKey,
});
/// Generates a new Nostr keypair.
///
/// Returns a new [NostrKeyPair] with random private and public keys.
factory NostrKeyPair.generate() {
// Generate random 32-byte private key
final random = List<int>.generate(32, (i) => DateTime.now().microsecondsSinceEpoch % 256);
final privateKeyBytes = sha256.convert(utf8.encode(DateTime.now().toString() + random.toString())).bytes.sublist(0, 32);
final privateKey = privateKeyBytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
// Derive public key from private key (simplified - in real Nostr, use secp256k1)
final publicKeyBytes = sha256.convert(privateKeyBytes).bytes.sublist(0, 32);
final publicKey = publicKeyBytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
return NostrKeyPair(
privateKey: privateKey,
publicKey: publicKey,
);
}
/// Creates a [NostrKeyPair] from a JSON map.
factory NostrKeyPair.fromJson(Map<String, dynamic> json) {
return NostrKeyPair(
privateKey: json['privateKey'] as String,
publicKey: json['publicKey'] as String,
);
}
/// Converts the [NostrKeyPair] to a JSON map.
Map<String, dynamic> toJson() {
return {
'privateKey': privateKey,
'publicKey': publicKey,
};
}
@override
String toString() {
return 'NostrKeyPair(publicKey: ${publicKey.substring(0, 8)}...)';
}
}

Powered by TurnKey Linux.