@polkadot-apps/statement-store 0.2.0

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.
@@ -0,0 +1,133 @@
1
+ import type { PublishOptions, ReceivedStatement, StatementSignerWithKey, StatementStoreConfig, Unsubscribable } from "./types.js";
2
+ /**
3
+ * High-level client for the Polkadot Statement Store.
4
+ *
5
+ * Provides a simple publish/subscribe API over the ephemeral statement store,
6
+ * handling SCALE encoding, Sr25519 signing, topic management, and resilient
7
+ * delivery (subscription + polling fallback).
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { StatementStoreClient } from "@polkadot-apps/statement-store";
12
+ *
13
+ * const client = new StatementStoreClient({ appName: "my-app" });
14
+ * await client.connect(signer);
15
+ *
16
+ * // Publish
17
+ * await client.publish({ type: "presence", peerId: "abc" }, {
18
+ * channel: "presence/abc",
19
+ * topic2: "room-123",
20
+ * });
21
+ *
22
+ * // Subscribe
23
+ * client.subscribe<{ type: string }>(statement => {
24
+ * console.log(statement.data.type);
25
+ * });
26
+ *
27
+ * // Cleanup
28
+ * client.destroy();
29
+ * ```
30
+ */
31
+ export declare class StatementStoreClient {
32
+ private readonly config;
33
+ private transport;
34
+ private signer;
35
+ private subscription;
36
+ private pollTimer;
37
+ private callbacks;
38
+ private connected;
39
+ private connectPromise;
40
+ /**
41
+ * Track seen statements by channel hex to avoid re-delivering the same statement.
42
+ * Maps channel hex (or statement data hash) to the expiry value.
43
+ */
44
+ private seen;
45
+ /** Monotonic counter to ensure unique sequence numbers even within the same millisecond. */
46
+ private sequenceCounter;
47
+ /** Cached blake2b hash of the appName, used as topic1. */
48
+ private readonly appTopic;
49
+ constructor(config: StatementStoreConfig);
50
+ /**
51
+ * Connect to the statement store and start receiving statements.
52
+ *
53
+ * Establishes the transport connection, starts a real-time subscription
54
+ * on the application's topic, fetches existing statements, and begins
55
+ * the polling fallback (if enabled).
56
+ *
57
+ * @param signer - The Sr25519 signer used to sign published statements.
58
+ * @throws {StatementConnectionError} If the transport cannot be established.
59
+ */
60
+ connect(signer: StatementSignerWithKey): Promise<void>;
61
+ private doConnect;
62
+ /**
63
+ * Publish typed data to the statement store.
64
+ *
65
+ * Encodes the data as JSON, builds a SCALE-encoded statement with the
66
+ * configured topics and TTL, signs it with Sr25519, and submits it
67
+ * to the network.
68
+ *
69
+ * @typeParam T - The type of data being published.
70
+ * @param data - The value to publish (must be JSON-serializable, max 512 bytes).
71
+ * @param options - Optional channel, topic2, TTL, and decryption key overrides.
72
+ * @returns `true` if accepted ("new" or "known"), `false` if rejected.
73
+ * @throws {StatementConnectionError} If not connected.
74
+ * @throws {StatementDataTooLargeError} If the encoded data exceeds 512 bytes.
75
+ */
76
+ publish<T>(data: T, options?: PublishOptions): Promise<boolean>;
77
+ /**
78
+ * Subscribe to incoming statements on this application's topic.
79
+ *
80
+ * Receives both real-time subscription events and polling results.
81
+ * Statements are deduplicated by channel + expiry to prevent double delivery.
82
+ *
83
+ * @typeParam T - The expected data type (decoded from JSON).
84
+ * @param callback - Called for each new statement.
85
+ * @param options - Optional secondary topic filter.
86
+ * @returns A handle to unsubscribe.
87
+ */
88
+ subscribe<T>(callback: (statement: ReceivedStatement<T>) => void, options?: {
89
+ topic2?: string;
90
+ }): Unsubscribable;
91
+ /**
92
+ * Query existing statements from the store.
93
+ *
94
+ * Fetches statements that were published before the subscription started.
95
+ * Useful for catching up on state (e.g., existing presence announcements).
96
+ *
97
+ * @typeParam T - The expected data type.
98
+ * @param options - Optional secondary topic filter.
99
+ * @returns Array of received statements.
100
+ */
101
+ query<T>(options?: {
102
+ topic2?: string;
103
+ /** Explicit decryption key for `statement_posted` filtering. If omitted, derived from topic2. */
104
+ decryptionKey?: Uint8Array;
105
+ }): Promise<ReceivedStatement<T>[]>;
106
+ /** Whether the client is connected and ready to publish/subscribe. */
107
+ isConnected(): boolean;
108
+ /**
109
+ * Get the signer's public key as a hex string (with 0x prefix).
110
+ *
111
+ * @returns The hex-encoded public key, or empty string if not connected.
112
+ */
113
+ getPublicKeyHex(): string;
114
+ /**
115
+ * Destroy the client, stopping polling, unsubscribing, and closing the transport.
116
+ *
117
+ * Safe to call multiple times. After destruction, the client cannot be reused.
118
+ */
119
+ destroy(): void;
120
+ private startSubscription;
121
+ private startPolling;
122
+ private stopPolling;
123
+ private poll;
124
+ /** Remove entries from the seen map whose expiry timestamp is in the past. */
125
+ private pruneSeenMap;
126
+ /**
127
+ * Process a received statement hex, dedup, parse, and deliver to callbacks.
128
+ * Returns true if the statement was new and delivered.
129
+ */
130
+ private handleStatementReceived;
131
+ private parseStatement;
132
+ }
133
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAER,cAAc,EACd,iBAAiB,EAEjB,sBAAsB,EACtB,oBAAoB,EAEpB,cAAc,EACjB,MAAM,YAAY,CAAC;AAKpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,oBAAoB;IAC7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAKG;IAE1B,OAAO,CAAC,SAAS,CAAmC;IACpD,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,YAAY,CAA+B;IACnD,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,SAAS,CAA8D;IAC/E,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,cAAc,CAA8B;IAEpD;;;OAGG;IACH,OAAO,CAAC,IAAI,CAA6B;IAEzC,4FAA4F;IAC5F,OAAO,CAAC,eAAe,CAAK;IAE5B,0DAA0D;IAC1D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBAEd,MAAM,EAAE,oBAAoB;IAWxC;;;;;;;;;OASG;IACG,OAAO,CAAC,MAAM,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;YAgB9C,SAAS;IA6BvB;;;;;;;;;;;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