mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add helper util to generate hashtag region keys
This commit is contained in:
parent
7450b880bb
commit
60dfbf55cb
2 changed files with 26 additions and 0 deletions
|
|
@ -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
19
src/transport_key_util.js
Normal 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue