countDifficultyOfHex method

  1. @override
int countDifficultyOfHex(
  1. String hexString
)
override

Counts the difficulty of the given hexString, this wis intebded t be used in the NIP 13 with this package.

Example:

final difficulty = Nostr.instance.utilsService.countDifficultyOfHex("002f");
print(difficulty); // 36

Implementation

@override
int countDifficultyOfHex(String hexString) {
  final idChars = hexString.split('');

  // encode to bits.
  var idCharsBinary = idChars.map((char) {
    final charCode = int.parse(char, radix: 16);
    final charBinary = charCode.toRadixString(2);
    return charBinary;
  }).toList();

  idCharsBinary = idCharsBinary.map((charBinary) {
    final charBinaryLength = charBinary.length;
    final charBinaryLengthDiff = 4 - charBinaryLength;
    final charBinaryPadded =
        charBinary.padLeft(charBinaryLength + charBinaryLengthDiff, '0');
    return charBinaryPadded;
  }).toList();

  return idCharsBinary.join().split('1').first.length;
}