nostr-tools
Version:
Tools for making a Nostr client.
63 lines (60 loc) • 1.49 kB
JavaScript
// nip13.ts
import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils.js";
import { sha256 } from "@noble/hashes/sha2.js";
// utils.ts
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
var utf8Decoder = new TextDecoder("utf-8");
var utf8Encoder = new TextEncoder();
// nip13.ts
function getPow(hex) {
let count = 0;
for (let i = 0; i < 64; i += 8) {
const nibble = parseInt(hex.substring(i, i + 8), 16);
if (nibble === 0) {
count += 32;
} else {
count += Math.clz32(nibble);
break;
}
}
return count;
}
function getPowFromBytes(hash) {
let count = 0;
for (let i = 0; i < hash.length; i++) {
const byte = hash[i];
if (byte === 0) {
count += 8;
} else {
count += Math.clz32(byte) - 24;
break;
}
}
return count;
}
function minePow(unsigned, difficulty) {
let count = 0;
const event = unsigned;
const tag = ["nonce", count.toString(), difficulty.toString()];
event.tags.push(tag);
while (true) {
const now = Math.floor(new Date().getTime() / 1e3);
if (now !== event.created_at) {
count = 0;
event.created_at = now;
}
tag[1] = (++count).toString();
const hash = sha256(
utf8Encoder.encode(JSON.stringify([0, event.pubkey, event.created_at, event.kind, event.tags, event.content]))
);
if (getPowFromBytes(hash) >= difficulty) {
event.id = bytesToHex2(hash);
break;
}
}
return event;
}
export {
getPow,
minePow
};