add PoW option
This commit is contained in:
parent
e144db938b
commit
1b9b51f063
43
index.js
43
index.js
@ -36,7 +36,7 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pk = getPublicKey(sk);
|
const pk = getPublicKey(sk);
|
||||||
const profileEvent = finalizeEvent(
|
const profileEvent = finalizeEventWithPoW(
|
||||||
{
|
{
|
||||||
kind: 0,
|
kind: 0,
|
||||||
content: BOT,
|
content: BOT,
|
||||||
@ -60,7 +60,7 @@ try {
|
|||||||
const dayOfWeek = today.getUTCDay();
|
const dayOfWeek = today.getUTCDay();
|
||||||
const message = `${dayOfWeek % 6 === 0 ? "gfy" : "GM"} fiatjaf`;
|
const message = `${dayOfWeek % 6 === 0 ? "gfy" : "GM"} fiatjaf`;
|
||||||
|
|
||||||
const event = finalizeEvent(
|
const event = finalizeEventWithPoW(
|
||||||
{
|
{
|
||||||
kind: 1,
|
kind: 1,
|
||||||
content: message,
|
content: message,
|
||||||
@ -90,3 +90,42 @@ try {
|
|||||||
clearInterval(intervalId);
|
clearInterval(intervalId);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function finalizeEventWithPoW(event, sk, difficulty = 0) {
|
||||||
|
if (difficulty === 0) return finalizeEvent(event, sk);
|
||||||
|
|
||||||
|
const pk = getPublicKey(sk);
|
||||||
|
let nonce = 0;
|
||||||
|
let leadingZeroes = 0;
|
||||||
|
let event = { ...event, pubkey: pk };
|
||||||
|
while (true) {
|
||||||
|
event.tags = [["nonce", nonce.toString(), difficulty.toString()]];
|
||||||
|
event.created_at = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
const hash = getEventHash(event);
|
||||||
|
leadingZeroes = countLeadingZeroes(hash);
|
||||||
|
|
||||||
|
if (leadingZeroes >= difficulty) {
|
||||||
|
return finalizeEvent(event, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
nonce++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hex should be a hexadecimal string (with no 0x prefix)
|
||||||
|
function countLeadingZeroes(hex) {
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < hex.length; i++) {
|
||||||
|
const nibble = parseInt(hex[i], 16);
|
||||||
|
if (nibble === 0) {
|
||||||
|
count += 4;
|
||||||
|
} else {
|
||||||
|
count += Math.clz32(nibble) - 28;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user