@unicitylabs/sphere-sdk 0.17.2-dev.3 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4624,6 +4624,26 @@ var GroupVisibility = {
4624
4624
  PRIVATE: "PRIVATE"
4625
4625
  };
4626
4626
 
4627
+ // modules/groupchat/group-id.ts
4628
+ var MAX_SLUG_LENGTH = 24;
4629
+ var RANDOM_BYTES = 8;
4630
+ function groupIdSlug(name) {
4631
+ const slug = name.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/['\u2019]/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
4632
+ if (slug.length <= MAX_SLUG_LENGTH) return slug;
4633
+ const head = slug.slice(0, MAX_SLUG_LENGTH + 1);
4634
+ const hyphen = head.lastIndexOf("-");
4635
+ return hyphen > 0 ? head.slice(0, hyphen) : slug.slice(0, MAX_SLUG_LENGTH);
4636
+ }
4637
+ function newGroupId(name) {
4638
+ const crypto2 = globalThis.crypto;
4639
+ if (typeof crypto2?.getRandomValues !== "function") {
4640
+ throw new SphereError("Creating a group requires crypto.getRandomValues", "INVALID_CONFIG");
4641
+ }
4642
+ const bytes = crypto2.getRandomValues(new Uint8Array(RANDOM_BYTES));
4643
+ const random = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
4644
+ return `${groupIdSlug(name) || "group"}-${random}`;
4645
+ }
4646
+
4627
4647
  // modules/groupchat/GroupChatModule.ts
4628
4648
  function createNip29Filter(data) {
4629
4649
  return new import_nostr_js_sdk3.Filter(data);
@@ -5235,9 +5255,9 @@ var GroupChatModule = class {
5235
5255
  if (!this.client) return null;
5236
5256
  const creatorPubkey = this.getMyPublicKey();
5237
5257
  if (!creatorPubkey) return null;
5238
- const proposedGroupId = options.name.toLowerCase().replace(/[^a-z0-9]/g, "").slice(0, 20) || this.randomId();
5239
5258
  try {
5240
5259
  const isPrivate = options.visibility === GroupVisibility.PRIVATE;
5260
+ const proposedGroupId = newGroupId(options.name);
5241
5261
  const eventId = await this.client.createAndPublishEvent({
5242
5262
  kind: NIP29_KINDS.CREATE_GROUP,
5243
5263
  tags: [["h", proposedGroupId]],