@xyo-network/sdk 8.1.0 → 8.1.1

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.
Files changed (82) hide show
  1. package/README.md +25 -0
  2. package/dist/neutral/account-custody.d.ts +2 -0
  3. package/dist/neutral/account-custody.d.ts.map +1 -0
  4. package/dist/neutral/account-custody.mjs +542 -0
  5. package/dist/neutral/account-custody.mjs.map +7 -0
  6. package/dist/neutral/address-payload-plugin.mjs +3 -14
  7. package/dist/neutral/address-payload-plugin.mjs.map +4 -4
  8. package/dist/neutral/api-location-diviner.mjs +0 -2
  9. package/dist/neutral/api-location-diviner.mjs.map +2 -2
  10. package/dist/neutral/archivist-memory.mjs +1 -4
  11. package/dist/neutral/archivist-memory.mjs.map +2 -2
  12. package/dist/neutral/bridge-abstract.mjs +4 -7
  13. package/dist/neutral/bridge-abstract.mjs.map +2 -2
  14. package/dist/neutral/bridge-http.mjs +1 -1
  15. package/dist/neutral/bridge-http.mjs.map +2 -2
  16. package/dist/neutral/diviner-boundwitness.memory.mjs +1 -1
  17. package/dist/neutral/diviner-boundwitness.memory.mjs.map +2 -2
  18. package/dist/neutral/diviner-boundwitness.mjs +1 -1
  19. package/dist/neutral/diviner-boundwitness.mjs.map +2 -2
  20. package/dist/neutral/diviner-payload-generic.mjs +3 -3
  21. package/dist/neutral/diviner-payload-generic.mjs.map +2 -2
  22. package/dist/neutral/index.d.ts +1 -0
  23. package/dist/neutral/index.d.ts.map +1 -1
  24. package/dist/neutral/index.mjs +1 -0
  25. package/dist/neutral/index.mjs.map +2 -2
  26. package/dist/neutral/module-abstract.mjs +4 -4
  27. package/dist/neutral/module-abstract.mjs.map +3 -3
  28. package/dist/neutral/module-resolver.mjs +3 -6
  29. package/dist/neutral/module-resolver.mjs.map +2 -2
  30. package/dist/neutral/modules/account-custody/AccountCustody.d.ts +14 -0
  31. package/dist/neutral/modules/account-custody/AccountCustody.d.ts.map +1 -0
  32. package/dist/neutral/modules/account-custody/AccountCustodyClient.d.ts +4 -0
  33. package/dist/neutral/modules/account-custody/AccountCustodyClient.d.ts.map +1 -0
  34. package/dist/neutral/modules/account-custody/clientExecution.d.ts +11 -0
  35. package/dist/neutral/modules/account-custody/clientExecution.d.ts.map +1 -0
  36. package/dist/neutral/modules/account-custody/clientModel.d.ts +49 -0
  37. package/dist/neutral/modules/account-custody/clientModel.d.ts.map +1 -0
  38. package/dist/neutral/modules/account-custody/clientOwner.d.ts +7 -0
  39. package/dist/neutral/modules/account-custody/clientOwner.d.ts.map +1 -0
  40. package/dist/neutral/modules/account-custody/clientSignals.d.ts +4 -0
  41. package/dist/neutral/modules/account-custody/clientSignals.d.ts.map +1 -0
  42. package/dist/neutral/modules/account-custody/clientState.d.ts +31 -0
  43. package/dist/neutral/modules/account-custody/clientState.d.ts.map +1 -0
  44. package/dist/neutral/modules/account-custody/error.d.ts +8 -0
  45. package/dist/neutral/modules/account-custody/error.d.ts.map +1 -0
  46. package/dist/neutral/modules/account-custody/index.d.ts +7 -0
  47. package/dist/neutral/modules/account-custody/index.d.ts.map +1 -0
  48. package/dist/neutral/modules/account-custody/model.d.ts +65 -0
  49. package/dist/neutral/modules/account-custody/model.d.ts.map +1 -0
  50. package/dist/neutral/modules/account-custody/realmRegistry.d.ts +2 -0
  51. package/dist/neutral/modules/account-custody/realmRegistry.d.ts.map +1 -0
  52. package/dist/neutral/modules/address-payload-plugin/Plugin.d.ts.map +1 -1
  53. package/dist/neutral/modules/address-payload-plugin/index.d.ts +0 -1
  54. package/dist/neutral/modules/address-payload-plugin/index.d.ts.map +1 -1
  55. package/dist/neutral/modules/api-location-diviner/RemoteDivinerConfig.d.ts +0 -2
  56. package/dist/neutral/modules/api-location-diviner/RemoteDivinerConfig.d.ts.map +1 -1
  57. package/dist/neutral/modules/archivist-memory/MemoryDriver.d.ts.map +1 -1
  58. package/dist/neutral/modules/bridge-abstract/AbstractModuleProxy/ModuleProxyResolver.d.ts.map +1 -1
  59. package/dist/neutral/modules/bridge-http/HttpBridge.d.ts.map +1 -1
  60. package/dist/neutral/modules/diviner-boundwitness/applyBoundWitnessDivinerQueryPayload.d.ts.map +1 -1
  61. package/dist/neutral/modules/diviner-payload-generic/Diviner.d.ts.map +1 -1
  62. package/dist/neutral/modules/module-resolver/AbstractModuleResolver.d.ts.map +1 -1
  63. package/dist/neutral/modules/module-resolver/ResolverEventEmitter.d.ts.map +1 -1
  64. package/dist/neutral/modules/node-memory/MemoryNode.d.ts.map +1 -1
  65. package/dist/neutral/node-memory.mjs +1 -4
  66. package/dist/neutral/node-memory.mjs.map +2 -2
  67. package/dist/node/account-custody.d.ts +2 -0
  68. package/dist/node/account-custody.d.ts.map +1 -0
  69. package/dist/node/index.d.ts +1 -0
  70. package/dist/node/index.d.ts.map +1 -1
  71. package/dist/node/index.mjs +1 -0
  72. package/dist/node/index.mjs.map +2 -2
  73. package/dist/node/modules/account-custody/clientExecution.d.ts +11 -0
  74. package/dist/node/modules/account-custody/clientExecution.d.ts.map +1 -0
  75. package/dist/node/modules/account-custody/clientSignals.d.ts +4 -0
  76. package/dist/node/modules/account-custody/clientSignals.d.ts.map +1 -0
  77. package/dist/node/modules/account-custody/realmRegistry.d.ts +2 -0
  78. package/dist/node/modules/account-custody/realmRegistry.d.ts.map +1 -0
  79. package/dist/node/modules/archivist-memory/MemoryDriver.d.ts.map +1 -1
  80. package/package.json +17 -11
  81. package/dist/neutral/modules/address-payload-plugin/Template.d.ts +0 -3
  82. package/dist/neutral/modules/address-payload-plugin/Template.d.ts.map +0 -1
package/README.md CHANGED
@@ -42,6 +42,31 @@ pnpm xy build @xyo-network/sdk # vendors using already-compiled child d
42
42
  npm install @xyo-network/sdk
43
43
  ```
44
44
 
45
+ ## Account custody execution clients
46
+
47
+ `@xyo-network/sdk/account-custody` exports `createInRealmAccountCustodyClientOwner`
48
+ and `createAccountCustodyClient` for one resident account owner with successive
49
+ execution clients. The trusted provisioner supplies the actual Account, explicit
50
+ native-state declaration, finite limits, immutable address/domain binding and
51
+ bounded snapshot/whole-operation adapter. Workers receive only a client's
52
+ content `execute`, status, outstanding count, `stop` and `drain`.
53
+
54
+ Client `stop()` permanently closes that client's admission and cancels queued
55
+ pre-effect work. Started operations keep their genuine account admission until
56
+ they settle; `drain()` waits that settlement. A replacement client requires its
57
+ predecessor client closed and completely drained. The same resident owner and
58
+ native Account predecessor continue through a clean handover. Owner `stop()`,
59
+ unknown native state, or active-operation cancellation/failure permanently stops
60
+ all future account operations. Late completion cannot restore the owner.
61
+
62
+ This separate profile shares the legacy permanent address reservation. Existing
63
+ `createInRealmAccountCustodyOwner`/`createAccountCustody` retain their global-stop
64
+ behavior. The scoped owner cannot be mixed with legacy contexts or re-created
65
+ for the same address to bypass fencing. No execution client exposes the Account,
66
+ owner controller or signing adapter. The guarantee covers participating module
67
+ instances in one JavaScript realm; it adds no durable recovery, cross-host
68
+ ownership or physical key isolation.
69
+
45
70
  ## License
46
71
 
47
72
  See the [LICENSE](LICENSE) file for license rights and limitations (LGPL-3.0-only).
@@ -0,0 +1,2 @@
1
+ export * from './modules/account-custody/index.js';
2
+ //# sourceMappingURL=account-custody.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account-custody.d.ts","sourceRoot":"","sources":["../../src/account-custody.ts"],"names":[],"mappings":"AACA,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,542 @@
1
+ // src/modules/account-custody/error.ts
2
+ var AccountCustodyError = class extends Error {
3
+ code;
4
+ name = "AccountCustodyError";
5
+ constructor(code) {
6
+ super(`Account custody: ${code}`);
7
+ this.code = code;
8
+ }
9
+ };
10
+
11
+ // src/modules/account-custody/realmRegistry.ts
12
+ function realmRegistry() {
13
+ const key = /* @__PURE__ */ Symbol.for("network.xyo.sdk.account-custody.in-realm.v1");
14
+ const existing = Reflect.get(globalThis, key);
15
+ if (existing === void 0) {
16
+ const reservations = /* @__PURE__ */ new Set();
17
+ const registry2 = Object.freeze({
18
+ version: 1,
19
+ reserve(key2, _fence) {
20
+ if (reservations.has(key2)) return false;
21
+ reservations.add(key2);
22
+ return true;
23
+ }
24
+ });
25
+ Object.defineProperty(globalThis, key, {
26
+ value: registry2,
27
+ configurable: false,
28
+ writable: false
29
+ });
30
+ return registry2;
31
+ }
32
+ if (typeof existing !== "object" || existing === null || !Object.isFrozen(existing) || !("version" in existing) || existing.version !== 1 || !("reserve" in existing) || typeof existing.reserve !== "function") throw new AccountCustodyError("stopped");
33
+ return existing;
34
+ }
35
+ var registry = realmRegistry();
36
+ function reserveInRealmAccount(address, fence = Object.freeze({})) {
37
+ return registry.reserve(address.toLowerCase(), fence);
38
+ }
39
+
40
+ // src/modules/account-custody/AccountCustody.ts
41
+ function intrinsic(prototype, property, member) {
42
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, property);
43
+ return typeof descriptor !== "object" || descriptor === null ? void 0 : descriptor[member];
44
+ }
45
+ var nativeAbortedGetter = intrinsic(AbortSignal.prototype, "aborted", "get");
46
+ var nativeAddListener = intrinsic(EventTarget.prototype, "addEventListener", "value");
47
+ var nativeRemoveListener = intrinsic(EventTarget.prototype, "removeEventListener", "value");
48
+ function nativeCall(method, receiver, args) {
49
+ if (typeof method !== "function") throw new AccountCustodyError("invalid-request");
50
+ return Reflect.apply(method, receiver, args);
51
+ }
52
+ function isSignalAborted(signal) {
53
+ const result = nativeCall(nativeAbortedGetter, signal, []);
54
+ if (typeof result !== "boolean") throw new AccountCustodyError("invalid-request");
55
+ return result;
56
+ }
57
+ function captureSignal(options) {
58
+ try {
59
+ const signal = options.signal;
60
+ if (signal !== void 0) isSignalAborted(signal);
61
+ return signal;
62
+ } catch {
63
+ throw new AccountCustodyError("invalid-request");
64
+ }
65
+ }
66
+ var owners = /* @__PURE__ */ new WeakMap();
67
+ var assurance = Object.freeze({
68
+ profile: "in-realm-exclusive-v1",
69
+ scope: "participating instances and module copies in one globalThis realm",
70
+ physicalKeyIsolation: "unqualified",
71
+ crossHostOwnership: "unqualified"
72
+ });
73
+ function createInRealmAccountCustodyOwner(options) {
74
+ const limits = finiteLimits(options.limits);
75
+ const address = options.account.address;
76
+ const key = address.toLowerCase();
77
+ const reservation = Object.freeze({});
78
+ const fence = {
79
+ executions: /* @__PURE__ */ new Set(),
80
+ reservation,
81
+ isStopped: options.nativeState !== "established-live",
82
+ tail: Promise.resolve()
83
+ };
84
+ if (!reserveInRealmAccount(key, fence)) throw new AccountCustodyError("account-reserved");
85
+ const owner = Object.freeze({
86
+ address,
87
+ assurance,
88
+ limits
89
+ });
90
+ owners.set(owner, {
91
+ account: options.account,
92
+ fence,
93
+ limits,
94
+ reservation
95
+ });
96
+ return owner;
97
+ }
98
+ function finiteLimits(selected) {
99
+ const maxQueuedOperations = selected?.maxQueuedOperations ?? 8;
100
+ const maxOperationMilliseconds = selected?.maxOperationMilliseconds ?? 3e4;
101
+ if (!Number.isSafeInteger(maxQueuedOperations) || maxQueuedOperations < 0 || maxQueuedOperations > 1024 || !Number.isSafeInteger(maxOperationMilliseconds) || maxOperationMilliseconds <= 0 || maxOperationMilliseconds > 2147483647) {
102
+ throw new AccountCustodyError("invalid-limits");
103
+ }
104
+ return Object.freeze({ maxQueuedOperations, maxOperationMilliseconds });
105
+ }
106
+ function stopOwner(state) {
107
+ if (state.fence.isStopped) return;
108
+ state.fence.isStopped = true;
109
+ for (const execution of state.fence.executions) execution.controller.abort();
110
+ }
111
+ function assertReady(state, owner) {
112
+ if (!state.fence.isStopped && state.fence.reservation === state.reservation && state.account.address === owner.address) return;
113
+ stopOwner(state);
114
+ throw new AccountCustodyError("stopped");
115
+ }
116
+ function operationControl(state, execution, deadline) {
117
+ return Object.freeze({
118
+ signal: execution.controller.signal,
119
+ assertActive() {
120
+ assertWithinDeadline(deadline);
121
+ if (execution.controller.signal.aborted || state.fence.isStopped) throw new AccountCustodyError("uncertain-operation");
122
+ }
123
+ });
124
+ }
125
+ function assertWithinDeadline(deadline) {
126
+ if (performance.now() >= deadline) throw new AccountCustodyError("uncertain-operation");
127
+ }
128
+ async function perform(state, options, execution, request) {
129
+ if (execution.controller.signal.aborted) throw new AccountCustodyError("cancelled-before-operation");
130
+ assertReady(state, options.owner);
131
+ execution.hasStarted = true;
132
+ state.fence.active = execution.controller;
133
+ const deadline = performance.now() + state.limits.maxOperationMilliseconds;
134
+ const timer = setTimeout(() => execution.controller.abort(), state.limits.maxOperationMilliseconds);
135
+ try {
136
+ const control = operationControl(state, execution, deadline);
137
+ const operation = options.createOperation(state.account, control);
138
+ const result = await operation(request);
139
+ const cloned = structuredClone(result);
140
+ assertWithinDeadline(deadline);
141
+ control.assertActive();
142
+ execution.hasCompleted = true;
143
+ return cloned;
144
+ } catch {
145
+ stopOwner(state);
146
+ throw new AccountCustodyError("uncertain-operation");
147
+ } finally {
148
+ clearTimeout(timer);
149
+ if (state.fence.active === execution.controller) state.fence.active = void 0;
150
+ }
151
+ }
152
+ function prepareExecution(state, execution, signal) {
153
+ const deferred = Promise.withResolvers();
154
+ const local = execution.controller.signal;
155
+ const abort = () => execution.controller.abort();
156
+ const aborted = () => {
157
+ if (execution.hasCompleted) return;
158
+ if (execution.hasStarted) stopOwner(state);
159
+ const code = execution.hasStarted ? "uncertain-operation" : state.fence.isStopped ? "stopped" : "cancelled-before-operation";
160
+ deferred.reject(new AccountCustodyError(code));
161
+ };
162
+ const cleanup = () => {
163
+ try {
164
+ if (signal !== void 0) nativeCall(nativeRemoveListener, signal, ["abort", abort]);
165
+ nativeCall(nativeRemoveListener, local, ["abort", aborted]);
166
+ } catch {
167
+ }
168
+ };
169
+ try {
170
+ nativeCall(nativeAddListener, local, ["abort", aborted, { once: true }]);
171
+ if (signal !== void 0) nativeCall(nativeAddListener, signal, ["abort", abort, { once: true }]);
172
+ if (signal !== void 0 && isSignalAborted(signal)) abort();
173
+ } catch {
174
+ cleanup();
175
+ throw new AccountCustodyError("invalid-request");
176
+ }
177
+ return {
178
+ result: deferred.promise,
179
+ resolve: deferred.resolve,
180
+ reject: deferred.reject,
181
+ cleanup
182
+ };
183
+ }
184
+ async function settleExecution(pending, resolve, reject, cleanup) {
185
+ try {
186
+ resolve(await pending);
187
+ } catch (error) {
188
+ reject(error);
189
+ } finally {
190
+ cleanup();
191
+ }
192
+ }
193
+ async function afterQueue(before, state, options, execution, request) {
194
+ try {
195
+ await before;
196
+ return await perform(state, options, execution, request);
197
+ } finally {
198
+ state.fence.executions.delete(execution);
199
+ }
200
+ }
201
+ async function drainPending(pending) {
202
+ try {
203
+ await pending;
204
+ } catch {
205
+ }
206
+ }
207
+ function createAccountCustody(options) {
208
+ const state = owners.get(options.owner);
209
+ if (state === void 0) throw new AccountCustodyError("unissued-owner");
210
+ const binding = Object.freeze({ address: options.binding.address, domain: options.binding.domain });
211
+ if (binding.address !== options.owner.address || binding.domain.length === 0) throw new AccountCustodyError("binding-mismatch");
212
+ const captured = {
213
+ owner: options.owner,
214
+ binding,
215
+ createOperation: options.createOperation,
216
+ snapshot: options.snapshot
217
+ };
218
+ return Object.freeze({
219
+ assurance,
220
+ binding,
221
+ get outstandingOperations() {
222
+ return state.fence.executions.size;
223
+ },
224
+ get status() {
225
+ return state.fence.isStopped ? "stopped" : state.fence.active === void 0 ? "ready" : "running";
226
+ },
227
+ async execute(request, executionOptions = {}) {
228
+ assertReady(state, captured.owner);
229
+ if (state.fence.executions.size >= state.limits.maxQueuedOperations + 1) throw new AccountCustodyError("capacity-exhausted");
230
+ const signal = captureSignal(executionOptions);
231
+ if (signal !== void 0 && isSignalAborted(signal)) throw new AccountCustodyError("cancelled-before-operation");
232
+ let original;
233
+ try {
234
+ original = structuredClone(captured.snapshot(request));
235
+ } catch {
236
+ throw new AccountCustodyError("invalid-request");
237
+ }
238
+ const execution = {
239
+ controller: new AbortController(),
240
+ hasCompleted: false,
241
+ hasStarted: false
242
+ };
243
+ const observer = prepareExecution(state, execution, signal);
244
+ state.fence.executions.add(execution);
245
+ const pending = afterQueue(state.fence.tail, state, captured, execution, original);
246
+ state.fence.tail = drainPending(pending);
247
+ void settleExecution(pending, observer.resolve, observer.reject, observer.cleanup);
248
+ return await observer.result;
249
+ },
250
+ stop() {
251
+ stopOwner(state);
252
+ },
253
+ async drain() {
254
+ await state.fence.tail;
255
+ }
256
+ });
257
+ }
258
+
259
+ // src/modules/account-custody/clientState.ts
260
+ var AccountCustodyClientError = class extends Error {
261
+ code;
262
+ name = "AccountCustodyClientError";
263
+ constructor(code) {
264
+ super(`Account custody client: ${code}`);
265
+ this.code = code;
266
+ }
267
+ };
268
+ function stopClientOwner(state) {
269
+ state.stopped = true;
270
+ for (const execution of state.executions) execution.controller.abort();
271
+ }
272
+ function assertClientOwnerReady(state) {
273
+ try {
274
+ if (!state.stopped && state.account.address === state.address) return;
275
+ } catch {
276
+ }
277
+ stopClientOwner(state);
278
+ throw new AccountCustodyClientError("stopped");
279
+ }
280
+ async function settleClientTail(pending) {
281
+ try {
282
+ await pending;
283
+ } catch {
284
+ }
285
+ }
286
+
287
+ // src/modules/account-custody/clientSignals.ts
288
+ function intrinsic2(prototype, property, member) {
289
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, property);
290
+ return typeof descriptor !== "object" || descriptor === null ? void 0 : descriptor[member];
291
+ }
292
+ var abortedGetter = intrinsic2(AbortSignal.prototype, "aborted", "get");
293
+ var addListener = intrinsic2(EventTarget.prototype, "addEventListener", "value");
294
+ var removeListener = intrinsic2(EventTarget.prototype, "removeEventListener", "value");
295
+ function nativeCall2(method, receiver, args) {
296
+ if (typeof method !== "function") throw new AccountCustodyClientError("invalid-request");
297
+ return Reflect.apply(method, receiver, args);
298
+ }
299
+ function clientSignalAborted(signal) {
300
+ const result = nativeCall2(abortedGetter, signal, []);
301
+ if (typeof result !== "boolean") throw new AccountCustodyClientError("invalid-request");
302
+ return result;
303
+ }
304
+ function listenClientSignal(signal, listener) {
305
+ nativeCall2(addListener, signal, ["abort", listener, { once: true }]);
306
+ }
307
+ function removeClientSignal(signal, listener) {
308
+ try {
309
+ nativeCall2(removeListener, signal, ["abort", listener]);
310
+ } catch {
311
+ }
312
+ }
313
+
314
+ // src/modules/account-custody/clientExecution.ts
315
+ function operationControl2(owner, execution, deadline) {
316
+ return Object.freeze({
317
+ signal: execution.controller.signal,
318
+ assertActive() {
319
+ assertClientOwnerReady(owner);
320
+ if (performance.now() >= deadline || execution.controller.signal.aborted || owner.stopped)
321
+ throw new AccountCustodyClientError("uncertain-operation");
322
+ }
323
+ });
324
+ }
325
+ async function perform2(owner, client, options, execution, request) {
326
+ if (client.closed || execution.controller.signal.aborted) throw new AccountCustodyClientError("cancelled-before-operation");
327
+ assertClientOwnerReady(owner);
328
+ execution.hasStarted = true;
329
+ const deadline = performance.now() + owner.limits.maxOperationMilliseconds;
330
+ const timer = setTimeout(() => execution.controller.abort(), owner.limits.maxOperationMilliseconds);
331
+ try {
332
+ const control = operationControl2(owner, execution, deadline);
333
+ const result = structuredClone(await options.createOperation(owner.account, control)(request));
334
+ control.assertActive();
335
+ execution.hasCompleted = true;
336
+ return result;
337
+ } catch {
338
+ stopClientOwner(owner);
339
+ throw new AccountCustodyClientError("uncertain-operation");
340
+ } finally {
341
+ clearTimeout(timer);
342
+ }
343
+ }
344
+ async function afterClientQueue(before, owner, client, options, execution, request) {
345
+ try {
346
+ await before;
347
+ return await perform2(owner, client, options, execution, request);
348
+ } finally {
349
+ owner.executions.delete(execution);
350
+ client.executions.delete(execution);
351
+ }
352
+ }
353
+ function observeClientExecution(owner, execution, caller) {
354
+ const deferred = Promise.withResolvers();
355
+ const local = execution.controller.signal;
356
+ const abort = () => execution.controller.abort();
357
+ const aborted = () => {
358
+ if (execution.hasCompleted) return;
359
+ if (execution.hasStarted) stopClientOwner(owner);
360
+ deferred.reject(new AccountCustodyClientError(execution.hasStarted ? "uncertain-operation" : owner.stopped ? "stopped" : "cancelled-before-operation"));
361
+ };
362
+ const cleanup = () => {
363
+ removeClientSignal(local, aborted);
364
+ if (caller !== void 0) removeClientSignal(caller, abort);
365
+ };
366
+ try {
367
+ listenClientSignal(local, aborted);
368
+ if (caller !== void 0) listenClientSignal(caller, abort);
369
+ if (caller !== void 0 && clientSignalAborted(caller)) abort();
370
+ } catch {
371
+ cleanup();
372
+ throw new AccountCustodyClientError("invalid-request");
373
+ }
374
+ return {
375
+ result: deferred.promise,
376
+ resolve: deferred.resolve,
377
+ reject: deferred.reject,
378
+ cleanup
379
+ };
380
+ }
381
+ async function settleClientExecution(pending, observer) {
382
+ try {
383
+ observer.resolve(await pending);
384
+ } catch (error) {
385
+ observer.reject(error);
386
+ } finally {
387
+ observer.cleanup();
388
+ }
389
+ }
390
+
391
+ // src/modules/account-custody/clientOwner.ts
392
+ var scopedClientAssurance = Object.freeze({
393
+ profile: "in-realm-exclusive-scoped-client-v1",
394
+ scope: "participating instances and module copies in one globalThis realm",
395
+ physicalKeyIsolation: "unqualified",
396
+ crossHostOwnership: "unqualified"
397
+ });
398
+ var owners2 = /* @__PURE__ */ new WeakMap();
399
+ function finiteLimits2(selected) {
400
+ const maxQueuedOperations = selected?.maxQueuedOperations ?? 8;
401
+ const maxOperationMilliseconds = selected?.maxOperationMilliseconds ?? 3e4;
402
+ if (!Number.isSafeInteger(maxQueuedOperations) || maxQueuedOperations < 0 || maxQueuedOperations > 1024 || !Number.isSafeInteger(maxOperationMilliseconds) || maxOperationMilliseconds <= 0 || maxOperationMilliseconds > 2147483647)
403
+ throw new AccountCustodyClientError("invalid-limits");
404
+ return Object.freeze({ maxQueuedOperations, maxOperationMilliseconds });
405
+ }
406
+ function createInRealmAccountCustodyClientOwner(options) {
407
+ const limits = finiteLimits2(options.limits);
408
+ const account = options.account;
409
+ const address = account.address;
410
+ if (!reserveInRealmAccount(address)) throw new AccountCustodyClientError("account-reserved");
411
+ const state = {
412
+ account,
413
+ address,
414
+ limits,
415
+ stopped: options.nativeState !== "established-live",
416
+ executions: /* @__PURE__ */ new Set(),
417
+ tail: Promise.resolve()
418
+ };
419
+ const owner = Object.freeze({
420
+ address,
421
+ limits,
422
+ assurance: scopedClientAssurance,
423
+ get outstandingOperations() {
424
+ return state.executions.size;
425
+ },
426
+ get status() {
427
+ return state.stopped ? "stopped" : state.executions.size > 0 ? "running" : "ready";
428
+ },
429
+ drain: async () => {
430
+ await state.tail;
431
+ },
432
+ stop: () => {
433
+ stopClientOwner(state);
434
+ }
435
+ });
436
+ owners2.set(owner, state);
437
+ return owner;
438
+ }
439
+ function getClientOwnerState(owner) {
440
+ const state = owners2.get(owner);
441
+ if (state === void 0) throw new AccountCustodyClientError("unissued-owner");
442
+ return state;
443
+ }
444
+
445
+ // src/modules/account-custody/AccountCustodyClient.ts
446
+ function captureSignal2(options) {
447
+ try {
448
+ const signal = options.signal;
449
+ if (signal !== void 0) clientSignalAborted(signal);
450
+ return signal;
451
+ } catch {
452
+ throw new AccountCustodyClientError("invalid-request");
453
+ }
454
+ }
455
+ function claimClient(owner) {
456
+ assertClientOwnerReady(owner);
457
+ if (owner.current !== void 0 && !owner.current.closed) throw new AccountCustodyClientError("client-active");
458
+ if (owner.executions.size > 0 || (owner.current?.executions.size ?? 0) > 0) throw new AccountCustodyClientError("client-not-drained");
459
+ const client = {
460
+ closed: false,
461
+ executions: /* @__PURE__ */ new Set(),
462
+ tail: Promise.resolve()
463
+ };
464
+ owner.current = client;
465
+ return client;
466
+ }
467
+ function closeClient(client) {
468
+ client.closed = true;
469
+ for (const execution of client.executions) {
470
+ if (!execution.hasStarted) execution.controller.abort();
471
+ }
472
+ }
473
+ async function execute(owner, client, options, request, executeOptions) {
474
+ if (client.closed) throw new AccountCustodyClientError("stopped");
475
+ assertClientOwnerReady(owner);
476
+ if (owner.executions.size >= owner.limits.maxQueuedOperations + 1) throw new AccountCustodyClientError("capacity-exhausted");
477
+ const signal = captureSignal2(executeOptions);
478
+ if (signal !== void 0 && clientSignalAborted(signal)) throw new AccountCustodyClientError("cancelled-before-operation");
479
+ let original;
480
+ try {
481
+ original = structuredClone(options.snapshot(request));
482
+ } catch {
483
+ throw new AccountCustodyClientError("invalid-request");
484
+ }
485
+ if (client.closed || owner.current !== client) throw new AccountCustodyClientError("stopped");
486
+ assertClientOwnerReady(owner);
487
+ if (owner.executions.size >= owner.limits.maxQueuedOperations + 1) throw new AccountCustodyClientError("capacity-exhausted");
488
+ const execution = {
489
+ controller: new AbortController(),
490
+ hasCompleted: false,
491
+ hasStarted: false
492
+ };
493
+ const observer = observeClientExecution(owner, execution, signal);
494
+ owner.executions.add(execution);
495
+ client.executions.add(execution);
496
+ const pending = afterClientQueue(owner.tail, owner, client, options, execution, original);
497
+ owner.tail = client.tail = settleClientTail(pending);
498
+ void settleClientExecution(pending, observer);
499
+ return await observer.result;
500
+ }
501
+ function createAccountCustodyClient(input) {
502
+ const ownerRef = input.owner;
503
+ const owner = getClientOwnerState(ownerRef);
504
+ const bindingRef = input.binding;
505
+ const binding = Object.freeze({ address: bindingRef.address, domain: bindingRef.domain });
506
+ if (binding.address !== owner.address || typeof binding.domain !== "string" || binding.domain.length === 0)
507
+ throw new AccountCustodyClientError("binding-mismatch");
508
+ const options = Object.freeze({
509
+ owner: ownerRef,
510
+ binding,
511
+ snapshot: input.snapshot,
512
+ createOperation: input.createOperation
513
+ });
514
+ if (typeof options.snapshot !== "function" || typeof options.createOperation !== "function") throw new AccountCustodyClientError("invalid-request");
515
+ const client = claimClient(owner);
516
+ return Object.freeze({
517
+ binding,
518
+ assurance: scopedClientAssurance,
519
+ get outstandingOperations() {
520
+ return client.executions.size;
521
+ },
522
+ get status() {
523
+ return client.closed || owner.stopped ? "stopped" : client.executions.size > 0 ? "running" : "ready";
524
+ },
525
+ execute: async (request, executeOptions = {}) => await execute(owner, client, options, request, executeOptions),
526
+ stop: () => {
527
+ closeClient(client);
528
+ },
529
+ drain: async () => {
530
+ await client.tail;
531
+ }
532
+ });
533
+ }
534
+ export {
535
+ AccountCustodyClientError,
536
+ AccountCustodyError,
537
+ createAccountCustody,
538
+ createAccountCustodyClient,
539
+ createInRealmAccountCustodyClientOwner,
540
+ createInRealmAccountCustodyOwner
541
+ };
542
+ //# sourceMappingURL=account-custody.mjs.map