open source is social v-it.org
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 sol pbc
3
4import { sha256 } from '@noble/hashes/sha256';
5import { wordlist } from '@scure/bip39/wordlists/english.js';
6
7export const REF_PATTERN = /^[a-z]+-[a-z]+-[a-z]+$/;
8
9export function hashTo3Words(input) {
10 const hash = sha256(new TextEncoder().encode(input));
11 // Take first 33 bits -> 3 x 11-bit indices into BIP39 wordlist (2048 words)
12 const i0 = (hash[0] << 3) | (hash[1] >> 5);
13 const i1 = ((hash[1] & 0x1f) << 6) | (hash[2] >> 2);
14 const i2 = ((hash[2] & 0x03) << 9) | (hash[3] << 1) | (hash[4] >> 7);
15 return `${wordlist[i0]}-${wordlist[i1]}-${wordlist[i2]}`;
16}
17
18export function resolveRef(record, cid) {
19 if (record?.ref && REF_PATTERN.test(record.ref)) {
20 return record.ref;
21 }
22 return hashTo3Words(cid);
23}