From 60dfbf55cb90fc48cfa2bc6d9bc9451676d24f6d Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 7 Mar 2026 12:20:40 +1300 Subject: [PATCH] add helper util to generate hashtag region keys --- src/connection/connection.js | 7 +++++++ src/transport_key_util.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/transport_key_util.js diff --git a/src/connection/connection.js b/src/connection/connection.js index f802e1f..5b9a19c 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -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} + */ setFloodScope(transportKey) { return new Promise(async (resolve, reject) => { try { diff --git a/src/transport_key_util.js b/src/transport_key_util.js new file mode 100644 index 0000000..56ae4cf --- /dev/null +++ b/src/transport_key_util.js @@ -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;