Activates hash

This commit is contained in:
Vitor Pamplona 2024-01-28 12:47:10 -05:00
parent 1256a6b8b1
commit 23a501f0e3

View File

@ -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;
});
}