diff --git a/nip13.test.ts b/nip13.test.ts index 281f5cc..7c4a805 100644 --- a/nip13.test.ts +++ b/nip13.test.ts @@ -2,9 +2,14 @@ import { test, expect } from 'bun:test' import { getPow, minePow } from './nip13.ts' test('identifies proof-of-work difficulty', async () => { - const id = '000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358' - const difficulty = getPow(id) - expect(difficulty).toEqual(21) + ;[ + ['000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358', 21], + ['6bf5b4f434813c64b523d2b0e6efe18f3bd0cbbd0a5effd8ece9e00fd2531996', 1], + ['00003479309ecdb46b1c04ce129d2709378518588bed6776e60474ebde3159ae', 18], + ['01a76167d41add96be4959d9e618b7a35f26551d62c43c11e5e64094c6b53c83', 7], + ['ac4f44bae06a45ebe88cfbd3c66358750159650a26c0d79e8ccaa92457fca4f6', 0], + ['0000000000000000006cfbd3c66358750159650a26c0d79e8ccaa92457fca4f6', 73], + ].forEach(([id, diff]) => expect(getPow(id as string)).toEqual(diff as number)) }) test('mines POW for an event', async () => { diff --git a/nip13.ts b/nip13.ts index 8f1bfc2..45003a7 100644 --- a/nip13.ts +++ b/nip13.ts @@ -1,15 +1,19 @@ -import { type UnsignedEvent, type Event, getEventHash } from './pure.ts' +import { bytesToHex } from '@noble/hashes/utils' +import { type UnsignedEvent, type Event } from './pure.ts' +import { sha256 } from '@noble/hashes/sha256' + +import { utf8Encoder } from './utils.ts' /** Get POW difficulty from a Nostr hex ID. */ export function getPow(hex: string): number { let count = 0 - for (let i = 0; i < hex.length; i++) { - const nibble = parseInt(hex[i], 16) + for (let i = 0; i < 64; i += 8) { + const nibble = parseInt(hex.substring(i, i + 8), 16) if (nibble === 0) { - count += 4 + count += 32 } else { - count += Math.clz32(nibble) - 28 + count += Math.clz32(nibble) break } } @@ -20,8 +24,6 @@ export function getPow(hex: string): number { /** * Mine an event with the desired POW. This function mutates the event. * Note that this operation is synchronous and should be run in a worker context to avoid blocking the main thread. - * - * Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36 */ export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit { let count = 0 @@ -41,7 +43,7 @@ export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit= difficulty) { break @@ -50,3 +52,9 @@ export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit