add helper util to generate hashtag region keys

This commit is contained in:
liamcottle 2026-03-07 12:20:40 +13:00
parent 7450b880bb
commit 60dfbf55cb
2 changed files with 26 additions and 0 deletions

View file

@ -1769,6 +1769,13 @@ class Connection extends EventEmitter {
});
}
/**
* Set the flood scope to use by provided a 16-byte transport key.
* Passing an empty list will clear the scope.
* You can use `const transportKey = await TransportKeyUtil.getHashtagRegionKey("#region");` to easily generate keys
* @param transportKey Uint8Array 16-byte transport key, can be derived from first 16 bytes of sha256("#region")
* @returns {Promise<unknown>}
*/
setFloodScope(transportKey) {
return new Promise(async (resolve, reject) => {
try {

19
src/transport_key_util.js Normal file
View file

@ -0,0 +1,19 @@
class TransportKeyUtil {
static async getHashtagRegionKey(regionName) {
// public hashtag regions must start with #
if(!regionName.startsWith("#")){
regionName = `#${regionName}`;
}
// Hash the message using SHA-256
const bytes = new TextEncoder().encode(regionName);
const hash = await crypto.subtle.digest("SHA-256", bytes);
return new Uint8Array(hash);
}
}
export default TransportKeyUtil;