@parity/product-sdk-signer 0.2.4 → 0.4.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.
package/src/types.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { PolkadotSigner } from "polkadot-api";
2
2
 
3
3
  import type { SS58String } from "@parity/product-sdk-address";
4
+ import type { AllocatableResource, AllocationOutcome } from "@parity/product-sdk-host";
4
5
 
5
6
  import type { SignerError } from "./errors.js";
6
7
 
@@ -105,8 +106,63 @@ export interface SignerManagerOptions {
105
106
  * Set to `null` to disable persistence entirely.
106
107
  */
107
108
  persistence?: AccountPersistence | null;
109
+ /**
110
+ * Callback fired exactly when the manager transitions to `connected`
111
+ * with a selected account — not on subsequent state mutations while
112
+ * still connected. Fires again after auto-reconnect, so a fresh host
113
+ * session re-runs the callback.
114
+ *
115
+ * Common use: request product resource allocations once per session.
116
+ * The `ctx` exposes a pre-bound `requestResourceAllocation` helper
117
+ * plus an `AbortSignal` that fires if the user disconnects or
118
+ * destroys the manager mid-flight.
119
+ *
120
+ * `requestResourceAllocation` throws on failure (matches the
121
+ * `@parity/product-sdk-host` export of the same name); errors thrown
122
+ * from `onConnect` are logged but do not affect the connected state —
123
+ * the next reconnect retries.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * new SignerManager({
128
+ * onConnect: async (_account, { requestResourceAllocation, signal }) => {
129
+ * try {
130
+ * const outcomes = await requestResourceAllocation([
131
+ * { tag: "AutoSigning", value: undefined },
132
+ * ]);
133
+ * if (signal.aborted) return;
134
+ * if (outcomes.some((o) => o.tag !== "Allocated")) {
135
+ * logWarning("partial permissions", outcomes);
136
+ * }
137
+ * } catch (cause) {
138
+ * logWarning("resource allocation failed", cause);
139
+ * }
140
+ * },
141
+ * });
142
+ * ```
143
+ */
144
+ onConnect?: OnConnect;
145
+ }
146
+
147
+ /** Context passed to the `onConnect` callback. */
148
+ export interface ConnectContext {
149
+ /**
150
+ * Aborted when the manager disconnects or is destroyed while the
151
+ * callback is still running. Pass through to `fetch` / cancellation
152
+ * primitives so mid-flight work stops promptly.
153
+ */
154
+ signal: AbortSignal;
155
+ /**
156
+ * Request a batch of host resource allocations. Bound shorthand for
157
+ * `requestResourceAllocation` from `@parity/product-sdk-host` —
158
+ * throws on failure, returns the unwrapped outcomes on success.
159
+ */
160
+ requestResourceAllocation: (resources: AllocatableResource[]) => Promise<AllocationOutcome[]>;
108
161
  }
109
162
 
163
+ /** Callback signature for {@link SignerManagerOptions.onConnect}. */
164
+ export type OnConnect = (account: SignerAccount, ctx: ConnectContext) => void | Promise<void>;
165
+
110
166
  if (import.meta.vitest) {
111
167
  const { test, expect, describe } = import.meta.vitest;
112
168