add support for v2 hashing algorithm.

update README.md and demo file to be more in sync.
update to v0.10.0.
This commit is contained in:
2026-03-29 22:34:43 -04:00
parent d5363c3960
commit c776b5df6a
33 changed files with 2255 additions and 1524 deletions

View File

@@ -1,5 +1,6 @@
import { blake2b } from "@exodus/blakejs";
import { base32Encode } from "./utils.js";
import { HASH_V2_EPOCH } from "./constants.js";
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})([tT ])(\d{2}):(\d{2}):(\d{2})\.?(\d{3})?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;
const formatRFC3339 = (date) => {
const pad = (num = 0) => `${+num < 10 ? 0 : ""}${+num}`;
@@ -28,10 +29,14 @@ const formatRFC3339 = (date) => {
offset
].join("");
};
function hashTwt(twt) {
function hashTwt(twt, version = 0) {
const created = formatRFC3339(twt.created);
const payload = [twt.url, created, twt.content].join("\n");
return base32Encode(blake2b(payload, void 0, 32)).toLowerCase().slice(-7);
const encoded = base32Encode(blake2b(payload, void 0, 32)).toLowerCase();
const createdDate = new Date(twt.created), epochDate = new Date(HASH_V2_EPOCH);
if (version === 1 || !version && createdDate < epochDate)
return encoded.slice(-7);
return encoded.slice(0, 12);
}
export {
hashTwt as default