@unicitylabs/sphere-sdk 0.6.1 → 0.6.3

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.
package/dist/index.d.cts CHANGED
@@ -64,6 +64,17 @@ interface GroupChatModuleConfig {
64
64
  /** Max reconnect attempts (default: 5) */
65
65
  maxReconnectAttempts?: number;
66
66
  }
67
+ interface GroupMessagesPage {
68
+ messages: GroupMessageData[];
69
+ hasMore: boolean;
70
+ oldestTimestamp: number | null;
71
+ }
72
+ interface GetGroupMessagesPageOptions {
73
+ /** Max messages to return (default: 20) */
74
+ limit?: number;
75
+ /** Return messages older than this timestamp */
76
+ before?: number;
77
+ }
67
78
  interface CreateGroupOptions {
68
79
  name: string;
69
80
  description?: string;
@@ -1303,7 +1314,7 @@ interface TrackedAddress extends TrackedAddressEntry {
1303
1314
  /** Primary nametag (from nametag cache, without @ prefix) */
1304
1315
  readonly nametag?: string;
1305
1316
  }
1306
- type SphereEventType = 'transfer:incoming' | 'transfer:confirmed' | 'transfer:failed' | 'payment_request:incoming' | 'payment_request:accepted' | 'payment_request:rejected' | 'payment_request:paid' | 'payment_request:response' | 'message:dm' | 'message:read' | 'message:typing' | 'composing:started' | 'message:broadcast' | 'sync:started' | 'sync:completed' | 'sync:provider' | 'sync:error' | 'connection:changed' | 'nametag:registered' | 'nametag:recovered' | 'identity:changed' | 'address:activated' | 'address:hidden' | 'address:unhidden' | 'sync:remote-update' | 'groupchat:message' | 'groupchat:joined' | 'groupchat:left' | 'groupchat:kicked' | 'groupchat:group_deleted' | 'groupchat:updated' | 'groupchat:connection' | 'history:updated';
1317
+ type SphereEventType = 'transfer:incoming' | 'transfer:confirmed' | 'transfer:failed' | 'payment_request:incoming' | 'payment_request:accepted' | 'payment_request:rejected' | 'payment_request:paid' | 'payment_request:response' | 'message:dm' | 'message:read' | 'message:typing' | 'composing:started' | 'message:broadcast' | 'sync:started' | 'sync:completed' | 'sync:provider' | 'sync:error' | 'connection:changed' | 'nametag:registered' | 'nametag:recovered' | 'identity:changed' | 'address:activated' | 'address:hidden' | 'address:unhidden' | 'sync:remote-update' | 'groupchat:message' | 'groupchat:joined' | 'groupchat:left' | 'groupchat:kicked' | 'groupchat:group_deleted' | 'groupchat:updated' | 'groupchat:connection' | 'groupchat:ready' | 'communications:ready' | 'history:updated';
1307
1318
  interface SphereEventMap {
1308
1319
  'transfer:incoming': IncomingTransfer;
1309
1320
  'transfer:confirmed': TransferResult;
@@ -1403,6 +1414,12 @@ interface SphereEventMap {
1403
1414
  'groupchat:connection': {
1404
1415
  connected: boolean;
1405
1416
  };
1417
+ 'groupchat:ready': {
1418
+ groupCount: number;
1419
+ };
1420
+ 'communications:ready': {
1421
+ conversationCount: number;
1422
+ };
1406
1423
  'history:updated': TransactionHistoryEntry;
1407
1424
  }
1408
1425
  type SphereEventHandler<T extends SphereEventType> = (data: SphereEventMap[T]) => void;
@@ -1705,6 +1722,13 @@ interface TokenStorageProvider<TData = unknown> extends BaseProvider {
1705
1722
  * Clear all data
1706
1723
  */
1707
1724
  clear?(): Promise<boolean>;
1725
+ /**
1726
+ * Create a new independent instance of this provider for a different address.
1727
+ * Used by per-address module architecture — each address gets its own
1728
+ * TokenStorageProvider instance to avoid cross-address data contamination.
1729
+ * If not implemented, the provider cannot be used in multi-address mode.
1730
+ */
1731
+ createForAddress?(): TokenStorageProvider<TData>;
1708
1732
  /**
1709
1733
  * Subscribe to storage events
1710
1734
  */
@@ -1961,12 +1985,30 @@ interface TransportProvider extends BaseProvider {
1961
1985
  * @returns Unsubscribe function
1962
1986
  */
1963
1987
  onInstantSplitReceived?(handler: InstantSplitBundleHandler): () => void;
1988
+ /**
1989
+ * Set fallback 'since' timestamp for event subscriptions.
1990
+ * Used when switching to an address that has never subscribed before.
1991
+ * The transport uses this instead of 'now' as the initial since filter,
1992
+ * ensuring events sent while the address was inactive are not missed.
1993
+ * Consumed once by the next subscription setup, then cleared.
1994
+ *
1995
+ * @param sinceSeconds - Unix timestamp in seconds
1996
+ */
1997
+ setFallbackSince?(sinceSeconds: number): void;
1964
1998
  /**
1965
1999
  * Fetch pending events from transport (one-shot query).
1966
2000
  * Creates a temporary subscription, processes events through normal handlers,
1967
2001
  * and resolves after EOSE (End Of Stored Events).
1968
2002
  */
1969
2003
  fetchPendingEvents?(): Promise<void>;
2004
+ /**
2005
+ * Register a handler to be called when the chat subscription receives EOSE
2006
+ * (End Of Stored Events), indicating that historical DMs have been delivered.
2007
+ * The handler fires at most once per subscription lifecycle.
2008
+ *
2009
+ * @returns Unsubscribe function
2010
+ */
2011
+ onChatReady?(handler: () => void): () => void;
1970
2012
  }
1971
2013
  /**
1972
2014
  * Payload for sending instant split bundles
@@ -3656,6 +3698,7 @@ declare class GroupChatModule {
3656
3698
  getGroups(): GroupData[];
3657
3699
  getGroup(groupId: string): GroupData | null;
3658
3700
  getMessages(groupId: string): GroupMessageData[];
3701
+ getMessagesPage(groupId: string, options?: GetGroupMessagesPageOptions): GroupMessagesPage;
3659
3702
  getMembers(groupId: string): GroupMemberData[];
3660
3703
  getMember(groupId: string, pubkey: string): GroupMemberData | null;
3661
3704
  getTotalUnreadCount(): number;
@@ -4602,6 +4645,11 @@ declare class Sphere {
4602
4645
  private _communications;
4603
4646
  private _groupChat;
4604
4647
  private _market;
4648
+ private _addressModules;
4649
+ private _transportMux;
4650
+ private _l1Config;
4651
+ private _groupChatConfig;
4652
+ private _marketConfig;
4605
4653
  private eventHandlers;
4606
4654
  private _disabledProviders;
4607
4655
  private _providerEventCleanups;
@@ -4997,9 +5045,28 @@ declare class Sphere {
4997
5045
  */
4998
5046
  private postSwitchSync;
4999
5047
  /**
5000
- * Re-initialize modules after address switch
5048
+ * Create a new set of per-address modules for the given index.
5049
+ * Each address gets its own PaymentsModule, CommunicationsModule, etc.
5050
+ * Modules are fully independent — they have their own token storage,
5051
+ * and can sync/finalize/split in background regardless of active address.
5052
+ *
5053
+ * @param index - HD address index
5054
+ * @param identity - Full identity for this address
5055
+ * @param tokenStorageProviders - Token storage providers for this address
5056
+ */
5057
+ private initializeAddressModules;
5058
+ /**
5059
+ * Ensure the transport multiplexer exists and register an address.
5060
+ * Creates the mux on first call. Returns an AddressTransportAdapter
5061
+ * that routes events for this address independently.
5062
+ * @returns AddressTransportAdapter or null if transport is not Nostr-based
5063
+ */
5064
+ private ensureTransportMux;
5065
+ /**
5066
+ * Get per-address modules for any address index (creates lazily if needed).
5067
+ * This allows accessing any address's modules without switching.
5001
5068
  */
5002
- private reinitializeModulesForNewAddress;
5069
+ getAddressPayments(index: number): PaymentsModule | undefined;
5003
5070
  /**
5004
5071
  * Derive address at a specific index
5005
5072
  *
package/dist/index.d.ts CHANGED
@@ -64,6 +64,17 @@ interface GroupChatModuleConfig {
64
64
  /** Max reconnect attempts (default: 5) */
65
65
  maxReconnectAttempts?: number;
66
66
  }
67
+ interface GroupMessagesPage {
68
+ messages: GroupMessageData[];
69
+ hasMore: boolean;
70
+ oldestTimestamp: number | null;
71
+ }
72
+ interface GetGroupMessagesPageOptions {
73
+ /** Max messages to return (default: 20) */
74
+ limit?: number;
75
+ /** Return messages older than this timestamp */
76
+ before?: number;
77
+ }
67
78
  interface CreateGroupOptions {
68
79
  name: string;
69
80
  description?: string;
@@ -1303,7 +1314,7 @@ interface TrackedAddress extends TrackedAddressEntry {
1303
1314
  /** Primary nametag (from nametag cache, without @ prefix) */
1304
1315
  readonly nametag?: string;
1305
1316
  }
1306
- type SphereEventType = 'transfer:incoming' | 'transfer:confirmed' | 'transfer:failed' | 'payment_request:incoming' | 'payment_request:accepted' | 'payment_request:rejected' | 'payment_request:paid' | 'payment_request:response' | 'message:dm' | 'message:read' | 'message:typing' | 'composing:started' | 'message:broadcast' | 'sync:started' | 'sync:completed' | 'sync:provider' | 'sync:error' | 'connection:changed' | 'nametag:registered' | 'nametag:recovered' | 'identity:changed' | 'address:activated' | 'address:hidden' | 'address:unhidden' | 'sync:remote-update' | 'groupchat:message' | 'groupchat:joined' | 'groupchat:left' | 'groupchat:kicked' | 'groupchat:group_deleted' | 'groupchat:updated' | 'groupchat:connection' | 'history:updated';
1317
+ type SphereEventType = 'transfer:incoming' | 'transfer:confirmed' | 'transfer:failed' | 'payment_request:incoming' | 'payment_request:accepted' | 'payment_request:rejected' | 'payment_request:paid' | 'payment_request:response' | 'message:dm' | 'message:read' | 'message:typing' | 'composing:started' | 'message:broadcast' | 'sync:started' | 'sync:completed' | 'sync:provider' | 'sync:error' | 'connection:changed' | 'nametag:registered' | 'nametag:recovered' | 'identity:changed' | 'address:activated' | 'address:hidden' | 'address:unhidden' | 'sync:remote-update' | 'groupchat:message' | 'groupchat:joined' | 'groupchat:left' | 'groupchat:kicked' | 'groupchat:group_deleted' | 'groupchat:updated' | 'groupchat:connection' | 'groupchat:ready' | 'communications:ready' | 'history:updated';
1307
1318
  interface SphereEventMap {
1308
1319
  'transfer:incoming': IncomingTransfer;
1309
1320
  'transfer:confirmed': TransferResult;
@@ -1403,6 +1414,12 @@ interface SphereEventMap {
1403
1414
  'groupchat:connection': {
1404
1415
  connected: boolean;
1405
1416
  };
1417
+ 'groupchat:ready': {
1418
+ groupCount: number;
1419
+ };
1420
+ 'communications:ready': {
1421
+ conversationCount: number;
1422
+ };
1406
1423
  'history:updated': TransactionHistoryEntry;
1407
1424
  }
1408
1425
  type SphereEventHandler<T extends SphereEventType> = (data: SphereEventMap[T]) => void;
@@ -1705,6 +1722,13 @@ interface TokenStorageProvider<TData = unknown> extends BaseProvider {
1705
1722
  * Clear all data
1706
1723
  */
1707
1724
  clear?(): Promise<boolean>;
1725
+ /**
1726
+ * Create a new independent instance of this provider for a different address.
1727
+ * Used by per-address module architecture — each address gets its own
1728
+ * TokenStorageProvider instance to avoid cross-address data contamination.
1729
+ * If not implemented, the provider cannot be used in multi-address mode.
1730
+ */
1731
+ createForAddress?(): TokenStorageProvider<TData>;
1708
1732
  /**
1709
1733
  * Subscribe to storage events
1710
1734
  */
@@ -1961,12 +1985,30 @@ interface TransportProvider extends BaseProvider {
1961
1985
  * @returns Unsubscribe function
1962
1986
  */
1963
1987
  onInstantSplitReceived?(handler: InstantSplitBundleHandler): () => void;
1988
+ /**
1989
+ * Set fallback 'since' timestamp for event subscriptions.
1990
+ * Used when switching to an address that has never subscribed before.
1991
+ * The transport uses this instead of 'now' as the initial since filter,
1992
+ * ensuring events sent while the address was inactive are not missed.
1993
+ * Consumed once by the next subscription setup, then cleared.
1994
+ *
1995
+ * @param sinceSeconds - Unix timestamp in seconds
1996
+ */
1997
+ setFallbackSince?(sinceSeconds: number): void;
1964
1998
  /**
1965
1999
  * Fetch pending events from transport (one-shot query).
1966
2000
  * Creates a temporary subscription, processes events through normal handlers,
1967
2001
  * and resolves after EOSE (End Of Stored Events).
1968
2002
  */
1969
2003
  fetchPendingEvents?(): Promise<void>;
2004
+ /**
2005
+ * Register a handler to be called when the chat subscription receives EOSE
2006
+ * (End Of Stored Events), indicating that historical DMs have been delivered.
2007
+ * The handler fires at most once per subscription lifecycle.
2008
+ *
2009
+ * @returns Unsubscribe function
2010
+ */
2011
+ onChatReady?(handler: () => void): () => void;
1970
2012
  }
1971
2013
  /**
1972
2014
  * Payload for sending instant split bundles
@@ -3656,6 +3698,7 @@ declare class GroupChatModule {
3656
3698
  getGroups(): GroupData[];
3657
3699
  getGroup(groupId: string): GroupData | null;
3658
3700
  getMessages(groupId: string): GroupMessageData[];
3701
+ getMessagesPage(groupId: string, options?: GetGroupMessagesPageOptions): GroupMessagesPage;
3659
3702
  getMembers(groupId: string): GroupMemberData[];
3660
3703
  getMember(groupId: string, pubkey: string): GroupMemberData | null;
3661
3704
  getTotalUnreadCount(): number;
@@ -4602,6 +4645,11 @@ declare class Sphere {
4602
4645
  private _communications;
4603
4646
  private _groupChat;
4604
4647
  private _market;
4648
+ private _addressModules;
4649
+ private _transportMux;
4650
+ private _l1Config;
4651
+ private _groupChatConfig;
4652
+ private _marketConfig;
4605
4653
  private eventHandlers;
4606
4654
  private _disabledProviders;
4607
4655
  private _providerEventCleanups;
@@ -4997,9 +5045,28 @@ declare class Sphere {
4997
5045
  */
4998
5046
  private postSwitchSync;
4999
5047
  /**
5000
- * Re-initialize modules after address switch
5048
+ * Create a new set of per-address modules for the given index.
5049
+ * Each address gets its own PaymentsModule, CommunicationsModule, etc.
5050
+ * Modules are fully independent — they have their own token storage,
5051
+ * and can sync/finalize/split in background regardless of active address.
5052
+ *
5053
+ * @param index - HD address index
5054
+ * @param identity - Full identity for this address
5055
+ * @param tokenStorageProviders - Token storage providers for this address
5056
+ */
5057
+ private initializeAddressModules;
5058
+ /**
5059
+ * Ensure the transport multiplexer exists and register an address.
5060
+ * Creates the mux on first call. Returns an AddressTransportAdapter
5061
+ * that routes events for this address independently.
5062
+ * @returns AddressTransportAdapter or null if transport is not Nostr-based
5063
+ */
5064
+ private ensureTransportMux;
5065
+ /**
5066
+ * Get per-address modules for any address index (creates lazily if needed).
5067
+ * This allows accessing any address's modules without switching.
5001
5068
  */
5002
- private reinitializeModulesForNewAddress;
5069
+ getAddressPayments(index: number): PaymentsModule | undefined;
5003
5070
  /**
5004
5071
  * Derive address at a specific index
5005
5072
  *