diff --git a/js/nostr-utils.js b/js/nostr-utils.js index af6adbb..057c72f 100644 --- a/js/nostr-utils.js +++ b/js/nostr-utils.js @@ -403,4 +403,32 @@ function hexToBytes(hex) { } return signed_msg; + } + + async function generateNostrEventId(msg) { + const digest = [ + 0, + msg.pubkey, + msg.created_at, + msg.kind, + msg.tags, + msg.content, + ]; + const digest_str = JSON.stringify(digest); + const hash = await sha256Hex(digest_str); + + return hash; + } + + function sha256Hex(string) { + const utf8 = new TextEncoder().encode(string); + + return crypto.subtle.digest('SHA-256', utf8).then((hashBuffer) => { + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray + .map((bytes) => bytes.toString(16).padStart(2, '0')) + .join(''); + + return hashHex; + }); } \ No newline at end of file