mineEvent static method
Implementation
static Nip01Event mineEvent(
Nip01Event event,
int targetDifficulty, {
int? maxIterations,
}) {
final random = Random();
int nonce = 0;
int iterations = 0;
final maxIter = maxIterations ?? 1000000;
List<List<String>> tags = List.from(event.tags);
tags.removeWhere((tag) => tag.isNotEmpty && tag[0] == 'nonce');
while (iterations < maxIter) {
// Use 0x100000000 instead of 1 << 32 for web compatibility
// On web, 1 << 32 evaluates to 0 due to JS bitwise operation limits
nonce = random.nextInt(0x100000000);
final updatedTags = List<List<String>>.from(tags);
updatedTags.add(['nonce', nonce.toString(), targetDifficulty.toString()]);
final minedEvent = Nip01Event(
pubKey: event.pubKey,
kind: event.kind,
tags: updatedTags,
content: event.content,
createdAt: event.createdAt,
);
if (checkDifficulty(minedEvent.id, targetDifficulty)) {
return minedEvent;
}
iterations++;
}
throw Exception(
'Failed to mine event with difficulty $targetDifficulty after $maxIter iterations');
}