@ottochain/sdk 1.0.3 → 1.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 (181) hide show
  1. package/dist/cjs/apps/contracts/state-machines/contract.json +21 -63
  2. package/dist/cjs/apps/contracts/state-machines/escrow.json +23 -69
  3. package/dist/cjs/apps/governance/state-machines/dao-multisig.json +22 -66
  4. package/dist/cjs/apps/governance/state-machines/dao-single.json +14 -42
  5. package/dist/cjs/apps/governance/state-machines/dao-threshold.json +18 -54
  6. package/dist/cjs/apps/governance/state-machines/dao-token.json +21 -63
  7. package/dist/cjs/apps/governance/state-machines/governance-constitution.json +22 -66
  8. package/dist/cjs/apps/governance/state-machines/governance-executive.json +27 -81
  9. package/dist/cjs/apps/governance/state-machines/governance-judiciary.json +34 -102
  10. package/dist/cjs/apps/governance/state-machines/governance-legislature.json +32 -96
  11. package/dist/cjs/apps/governance/state-machines/governance-simple.json +27 -81
  12. package/dist/cjs/apps/identity/state-machines/agent-identity.json +25 -75
  13. package/dist/cjs/apps/markets/state-machines/market-universal.json +377 -110
  14. package/dist/cjs/apps/oracles/state-machines/oracle.json +22 -66
  15. package/dist/cjs/generated/index.js +11 -1
  16. package/dist/cjs/metakit/drop-nulls.js +50 -0
  17. package/dist/cjs/metakit/normalize.js +147 -0
  18. package/dist/cjs/ottochain/metagraph-client.js +147 -0
  19. package/dist/cjs/validation.js +1 -1
  20. package/dist/esm/apps/contracts/state-machines/contract.json +21 -63
  21. package/dist/esm/apps/contracts/state-machines/escrow.json +23 -69
  22. package/dist/esm/apps/governance/state-machines/dao-multisig.json +22 -66
  23. package/dist/esm/apps/governance/state-machines/dao-single.json +14 -42
  24. package/dist/esm/apps/governance/state-machines/dao-threshold.json +18 -54
  25. package/dist/esm/apps/governance/state-machines/dao-token.json +21 -63
  26. package/dist/esm/apps/governance/state-machines/governance-constitution.json +22 -66
  27. package/dist/esm/apps/governance/state-machines/governance-executive.json +27 -81
  28. package/dist/esm/apps/governance/state-machines/governance-judiciary.json +34 -102
  29. package/dist/esm/apps/governance/state-machines/governance-legislature.json +32 -96
  30. package/dist/esm/apps/governance/state-machines/governance-simple.json +27 -81
  31. package/dist/esm/apps/identity/state-machines/agent-identity.json +25 -75
  32. package/dist/esm/apps/markets/state-machines/market-universal.json +377 -110
  33. package/dist/esm/apps/oracles/state-machines/oracle.json +22 -66
  34. package/dist/esm/generated/index.js +11 -1
  35. package/dist/esm/metakit/drop-nulls.js +46 -0
  36. package/dist/esm/metakit/normalize.js +140 -0
  37. package/dist/esm/ottochain/metagraph-client.js +147 -0
  38. package/dist/esm/validation.js +1 -1
  39. package/dist/types/generated/index.d.ts +11 -1
  40. package/dist/types/metakit/drop-nulls.d.ts +29 -0
  41. package/dist/types/metakit/normalize.d.ts +67 -0
  42. package/dist/types/ottochain/index.d.ts +1 -1
  43. package/dist/types/ottochain/metagraph-client.d.ts +90 -0
  44. package/package.json +12 -12
  45. package/dist/apps/contracts/index.d.ts +0 -21
  46. package/dist/apps/contracts/index.js +0 -39
  47. package/dist/apps/contracts/types.d.ts +0 -24
  48. package/dist/apps/contracts/types.js +0 -48
  49. package/dist/apps/identity/index.d.ts +0 -22
  50. package/dist/apps/identity/index.js +0 -40
  51. package/dist/apps/identity/types.d.ts +0 -30
  52. package/dist/apps/identity/types.js +0 -53
  53. package/dist/apps/index.d.ts +0 -29
  54. package/dist/apps/index.js +0 -60
  55. package/dist/apps/markets/index.d.ts +0 -26
  56. package/dist/apps/markets/index.js +0 -46
  57. package/dist/apps/markets/types.d.ts +0 -185
  58. package/dist/apps/markets/types.js +0 -252
  59. package/dist/apps/oracles/index.d.ts +0 -26
  60. package/dist/apps/oracles/index.js +0 -46
  61. package/dist/apps/oracles/types.d.ts +0 -211
  62. package/dist/apps/oracles/types.js +0 -306
  63. package/dist/cjs/apps/contracts/types.js +0 -48
  64. package/dist/cjs/apps/corporate/types.js +0 -44
  65. package/dist/cjs/apps/governance/types.js +0 -42
  66. package/dist/cjs/apps/identity/types.js +0 -53
  67. package/dist/cjs/apps/markets/types.js +0 -219
  68. package/dist/cjs/apps/oracles/types.js +0 -282
  69. package/dist/cjs/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -100
  70. package/dist/cjs/generated/ottochain/apps/corporate/v1/corporate_pb.js +0 -392
  71. package/dist/cjs/generated/ottochain/apps/governance/v1/governance_pb.js +0 -235
  72. package/dist/cjs/generated/ottochain/apps/identity/v1/agent_pb.js +0 -116
  73. package/dist/cjs/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -79
  74. package/dist/cjs/generated/ottochain/apps/markets/v1/market_pb.js +0 -151
  75. package/dist/cjs/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -109
  76. package/dist/cjs/generated/ottochain/v1/common_pb.js +0 -37
  77. package/dist/cjs/generated/ottochain/v1/fiber_pb.js +0 -86
  78. package/dist/cjs/generated/ottochain/v1/messages_pb.js +0 -44
  79. package/dist/cjs/generated/ottochain/v1/records_pb.js +0 -44
  80. package/dist/errors.d.ts +0 -221
  81. package/dist/errors.js +0 -293
  82. package/dist/esm/apps/contracts/types.js +0 -44
  83. package/dist/esm/apps/corporate/types.js +0 -38
  84. package/dist/esm/apps/governance/types.js +0 -35
  85. package/dist/esm/apps/identity/types.js +0 -50
  86. package/dist/esm/apps/markets/types.js +0 -206
  87. package/dist/esm/apps/oracles/types.js +0 -267
  88. package/dist/esm/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -97
  89. package/dist/esm/generated/ottochain/apps/corporate/v1/corporate_pb.js +0 -389
  90. package/dist/esm/generated/ottochain/apps/governance/v1/governance_pb.js +0 -232
  91. package/dist/esm/generated/ottochain/apps/identity/v1/agent_pb.js +0 -113
  92. package/dist/esm/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -76
  93. package/dist/esm/generated/ottochain/apps/markets/v1/market_pb.js +0 -148
  94. package/dist/esm/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -106
  95. package/dist/esm/generated/ottochain/v1/common_pb.js +0 -34
  96. package/dist/esm/generated/ottochain/v1/fiber_pb.js +0 -83
  97. package/dist/esm/generated/ottochain/v1/messages_pb.js +0 -41
  98. package/dist/esm/generated/ottochain/v1/records_pb.js +0 -41
  99. package/dist/generated/index.d.ts +0 -15
  100. package/dist/generated/index.js +0 -34
  101. package/dist/generated/ottochain/apps/contracts/v1/contract_pb.d.ts +0 -274
  102. package/dist/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -100
  103. package/dist/generated/ottochain/apps/identity/v1/agent_pb.d.ts +0 -211
  104. package/dist/generated/ottochain/apps/identity/v1/agent_pb.js +0 -116
  105. package/dist/generated/ottochain/apps/identity/v1/attestation_pb.d.ts +0 -238
  106. package/dist/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -79
  107. package/dist/generated/ottochain/apps/markets/v1/market_pb.d.ts +0 -436
  108. package/dist/generated/ottochain/apps/markets/v1/market_pb.js +0 -151
  109. package/dist/generated/ottochain/apps/oracles/v1/oracle_pb.d.ts +0 -393
  110. package/dist/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -109
  111. package/dist/generated/ottochain/v1/common_pb.d.ts +0 -86
  112. package/dist/generated/ottochain/v1/common_pb.js +0 -37
  113. package/dist/generated/ottochain/v1/fiber_pb.d.ts +0 -292
  114. package/dist/generated/ottochain/v1/fiber_pb.js +0 -86
  115. package/dist/generated/ottochain/v1/messages_pb.d.ts +0 -190
  116. package/dist/generated/ottochain/v1/messages_pb.js +0 -44
  117. package/dist/generated/ottochain/v1/records_pb.d.ts +0 -221
  118. package/dist/generated/ottochain/v1/records_pb.js +0 -44
  119. package/dist/index.d.ts +0 -21
  120. package/dist/index.js +0 -77
  121. package/dist/metakit/binary.d.ts +0 -38
  122. package/dist/metakit/binary.js +0 -58
  123. package/dist/metakit/canonicalize.d.ts +0 -26
  124. package/dist/metakit/canonicalize.js +0 -40
  125. package/dist/metakit/codec.d.ts +0 -16
  126. package/dist/metakit/codec.js +0 -45
  127. package/dist/metakit/currency-transaction.d.ts +0 -157
  128. package/dist/metakit/currency-transaction.js +0 -319
  129. package/dist/metakit/currency-types.d.ts +0 -55
  130. package/dist/metakit/currency-types.js +0 -13
  131. package/dist/metakit/hash.d.ts +0 -50
  132. package/dist/metakit/hash.js +0 -84
  133. package/dist/metakit/index.d.ts +0 -23
  134. package/dist/metakit/index.js +0 -74
  135. package/dist/metakit/network/client.d.ts +0 -23
  136. package/dist/metakit/network/client.js +0 -78
  137. package/dist/metakit/network/currency-l1-client.d.ts +0 -71
  138. package/dist/metakit/network/currency-l1-client.js +0 -101
  139. package/dist/metakit/network/data-l1-client.d.ts +0 -57
  140. package/dist/metakit/network/data-l1-client.js +0 -76
  141. package/dist/metakit/network/index.d.ts +0 -10
  142. package/dist/metakit/network/index.js +0 -16
  143. package/dist/metakit/network/types.d.ts +0 -74
  144. package/dist/metakit/network/types.js +0 -20
  145. package/dist/metakit/sign.d.ts +0 -65
  146. package/dist/metakit/sign.js +0 -120
  147. package/dist/metakit/signed-object.d.ts +0 -66
  148. package/dist/metakit/signed-object.js +0 -100
  149. package/dist/metakit/types.d.ts +0 -67
  150. package/dist/metakit/types.js +0 -14
  151. package/dist/metakit/verify.d.ts +0 -55
  152. package/dist/metakit/verify.js +0 -217
  153. package/dist/metakit/wallet.d.ts +0 -70
  154. package/dist/metakit/wallet.js +0 -127
  155. package/dist/ottochain/index.d.ts +0 -13
  156. package/dist/ottochain/index.js +0 -45
  157. package/dist/ottochain/metagraph-client.d.ts +0 -111
  158. package/dist/ottochain/metagraph-client.js +0 -157
  159. package/dist/ottochain/snapshot.d.ts +0 -86
  160. package/dist/ottochain/snapshot.js +0 -110
  161. package/dist/ottochain/types.d.ts +0 -278
  162. package/dist/ottochain/types.js +0 -11
  163. package/dist/types/apps/contracts/types.d.ts +0 -24
  164. package/dist/types/apps/corporate/types.d.ts +0 -9861
  165. package/dist/types/apps/governance/types.d.ts +0 -344
  166. package/dist/types/apps/identity/types.d.ts +0 -30
  167. package/dist/types/apps/markets/types.d.ts +0 -155
  168. package/dist/types/apps/oracles/types.d.ts +0 -193
  169. package/dist/types/generated/ottochain/apps/contracts/v1/contract_pb.d.ts +0 -274
  170. package/dist/types/generated/ottochain/apps/corporate/v1/corporate_pb.d.ts +0 -1172
  171. package/dist/types/generated/ottochain/apps/governance/v1/governance_pb.d.ts +0 -772
  172. package/dist/types/generated/ottochain/apps/identity/v1/agent_pb.d.ts +0 -211
  173. package/dist/types/generated/ottochain/apps/identity/v1/attestation_pb.d.ts +0 -238
  174. package/dist/types/generated/ottochain/apps/markets/v1/market_pb.d.ts +0 -436
  175. package/dist/types/generated/ottochain/apps/oracles/v1/oracle_pb.d.ts +0 -393
  176. package/dist/types/generated/ottochain/v1/common_pb.d.ts +0 -86
  177. package/dist/types/generated/ottochain/v1/fiber_pb.d.ts +0 -292
  178. package/dist/types/generated/ottochain/v1/messages_pb.d.ts +0 -190
  179. package/dist/types/generated/ottochain/v1/records_pb.d.ts +0 -221
  180. package/dist/validation.d.ts +0 -449
  181. package/dist/validation.js +0 -312
@@ -6,52 +6,36 @@
6
6
  },
7
7
  "states": {
8
8
  "UNREGISTERED": {
9
- "id": {
10
- "value": "UNREGISTERED"
11
- },
9
+ "id": "UNREGISTERED",
12
10
  "isFinal": false,
13
11
  "metadata": null
14
12
  },
15
13
  "REGISTERED": {
16
- "id": {
17
- "value": "REGISTERED"
18
- },
14
+ "id": "REGISTERED",
19
15
  "isFinal": false,
20
16
  "metadata": null
21
17
  },
22
18
  "ACTIVE": {
23
- "id": {
24
- "value": "ACTIVE"
25
- },
19
+ "id": "ACTIVE",
26
20
  "isFinal": false,
27
21
  "metadata": null
28
22
  },
29
23
  "SLASHED": {
30
- "id": {
31
- "value": "SLASHED"
32
- },
24
+ "id": "SLASHED",
33
25
  "isFinal": false,
34
26
  "metadata": null
35
27
  },
36
28
  "WITHDRAWN": {
37
- "id": {
38
- "value": "WITHDRAWN"
39
- },
29
+ "id": "WITHDRAWN",
40
30
  "isFinal": true,
41
31
  "metadata": null
42
32
  }
43
33
  },
44
- "initialState": {
45
- "value": "UNREGISTERED"
46
- },
34
+ "initialState": "UNREGISTERED",
47
35
  "transitions": [
48
36
  {
49
- "from": {
50
- "value": "UNREGISTERED"
51
- },
52
- "to": {
53
- "value": "REGISTERED"
54
- },
37
+ "from": "UNREGISTERED",
38
+ "to": "REGISTERED",
55
39
  "eventName": "register",
56
40
  "guard": {
57
41
  ">=": [
@@ -95,12 +79,8 @@
95
79
  "dependencies": []
96
80
  },
97
81
  {
98
- "from": {
99
- "value": "REGISTERED"
100
- },
101
- "to": {
102
- "value": "ACTIVE"
103
- },
82
+ "from": "REGISTERED",
83
+ "to": "ACTIVE",
104
84
  "eventName": "activate",
105
85
  "guard": {
106
86
  "or": [
@@ -135,12 +115,8 @@
135
115
  "dependencies": []
136
116
  },
137
117
  {
138
- "from": {
139
- "value": "ACTIVE"
140
- },
141
- "to": {
142
- "value": "ACTIVE"
143
- },
118
+ "from": "ACTIVE",
119
+ "to": "ACTIVE",
144
120
  "eventName": "add_stake",
145
121
  "guard": {
146
122
  "and": [
@@ -189,12 +165,8 @@
189
165
  "dependencies": []
190
166
  },
191
167
  {
192
- "from": {
193
- "value": "ACTIVE"
194
- },
195
- "to": {
196
- "value": "ACTIVE"
197
- },
168
+ "from": "ACTIVE",
169
+ "to": "ACTIVE",
198
170
  "eventName": "record_resolution",
199
171
  "guard": {
200
172
  "var": "event.marketId"
@@ -249,12 +221,8 @@
249
221
  "dependencies": []
250
222
  },
251
223
  {
252
- "from": {
253
- "value": "ACTIVE"
254
- },
255
- "to": {
256
- "value": "SLASHED"
257
- },
224
+ "from": "ACTIVE",
225
+ "to": "SLASHED",
258
226
  "eventName": "slash",
259
227
  "guard": {
260
228
  "and": [
@@ -330,12 +298,8 @@
330
298
  "dependencies": []
331
299
  },
332
300
  {
333
- "from": {
334
- "value": "SLASHED"
335
- },
336
- "to": {
337
- "value": "ACTIVE"
338
- },
301
+ "from": "SLASHED",
302
+ "to": "ACTIVE",
339
303
  "eventName": "reactivate",
340
304
  "guard": {
341
305
  "and": [
@@ -377,12 +341,8 @@
377
341
  "dependencies": []
378
342
  },
379
343
  {
380
- "from": {
381
- "value": "ACTIVE"
382
- },
383
- "to": {
384
- "value": "WITHDRAWN"
385
- },
344
+ "from": "ACTIVE",
345
+ "to": "WITHDRAWN",
386
346
  "eventName": "withdraw",
387
347
  "guard": {
388
348
  "===": [
@@ -413,12 +373,8 @@
413
373
  "dependencies": []
414
374
  },
415
375
  {
416
- "from": {
417
- "value": "SLASHED"
418
- },
419
- "to": {
420
- "value": "WITHDRAWN"
421
- },
376
+ "from": "SLASHED",
377
+ "to": "WITHDRAWN",
422
378
  "eventName": "withdraw",
423
379
  "guard": {
424
380
  "===": [
@@ -3,9 +3,19 @@
3
3
  * Generated Protobuf Types
4
4
  *
5
5
  * Auto-generated from proto/ definitions using ts-proto.
6
- * DO NOT EDIT - regenerate with `pnpm run generate`
6
+ * DO NOT EDIT - regenerate with `npm run generate`
7
7
  *
8
8
  * @packageDocumentation
9
+ *
10
+ * NOTE — Dual-type architecture:
11
+ * These generated types use proto conventions (FIBER_STATUS_ACTIVE, plain string StateId).
12
+ * The wire-format REST API types in src/ottochain/types.ts use plain strings ('Active').
13
+ * Both coexist intentionally until PR #89 (Migrate fiber-engine to generated Scala types) merges.
14
+ *
15
+ * TODO PR #89 migration: after PR #89 merges and cluster confirms new format,
16
+ * migrate all state-machine JSON files in src/apps/ from Circe-format
17
+ * ({ value: '...' } wrapped initialState) to plain string format.
18
+ * See docs/type-architecture.md for full migration plan.
9
19
  */
10
20
  Object.defineProperty(exports, "__esModule", { value: true });
11
21
  exports.MarketDefinition = exports.CancelMarketRequest = exports.SubmitResolutionRequest = exports.CommitToMarketRequest = exports.CreateMarketRequest = exports.Market = exports.Resolution = exports.Commitment = exports.MarketState = exports.MarketType = exports.ContractDefinition = exports.DisputeContractRequest = exports.RejectContractRequest = exports.CompleteContractRequest = exports.AcceptContractRequest = exports.ProposeContractRequest = exports.Contract = exports.ContractState = exports.ReputationConfig = exports.ChallengeRequest = exports.VouchRequest = exports.Attestation = exports.ReputationDelta = exports.AttestationType = exports.AgentIdentityDefinition = exports.AgentIdentity = exports.PlatformLink = exports.Platform = exports.AgentState = exports.CalculatedState = exports.OnChainState = exports.FiberCommit = exports.ScriptFiberRecord = exports.StateMachineFiberRecord = exports.OttochainMessage = exports.InvokeScript = exports.CreateScript = exports.ArchiveStateMachine = exports.TransitionStateMachine = exports.CreateStateMachine = exports.FiberLogEntry = exports.ScriptInvocation = exports.EventReceipt = exports.EmittedEvent = exports.StateMachineDefinition = exports.FiberOwnedAccess = exports.WhitelistAccess = exports.PublicAccess = exports.AccessControlPolicy = exports.FiberStatus = void 0;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /**
3
+ * Drop Null Values from JSON Objects
4
+ *
5
+ * Recursively removes null-valued keys from objects to match
6
+ * the Scala-side `JsonBinaryCodec.dropNulls` behavior in metakit.
7
+ *
8
+ * This ensures that the canonical JSON used for signing on the
9
+ * TypeScript side matches what the Scala metagraph produces when
10
+ * verifying signatures.
11
+ *
12
+ * Note: null values inside arrays are preserved (to maintain index
13
+ * positions). Only object field values that are null are removed.
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.dropNulls = void 0;
17
+ /**
18
+ * Recursively remove null values from objects
19
+ *
20
+ * @param value - Any JSON-serializable value
21
+ * @returns A deep copy with null object fields removed
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * dropNulls({ a: 1, b: null, c: { d: null, e: 2 } })
26
+ * // => { a: 1, c: { e: 2 } }
27
+ *
28
+ * dropNulls([1, null, 3])
29
+ * // => [1, null, 3] (array nulls preserved)
30
+ * ```
31
+ */
32
+ function dropNulls(value) {
33
+ if (value === null || value === undefined) {
34
+ return value;
35
+ }
36
+ if (Array.isArray(value)) {
37
+ return value.map(dropNulls);
38
+ }
39
+ if (typeof value === 'object') {
40
+ const result = {};
41
+ for (const [k, v] of Object.entries(value)) {
42
+ if (v !== null && v !== undefined) {
43
+ result[k] = dropNulls(v);
44
+ }
45
+ }
46
+ return result;
47
+ }
48
+ return value;
49
+ }
50
+ exports.dropNulls = dropNulls;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ /**
3
+ * Normalize OttoChain Messages for Signing
4
+ *
5
+ * Converts OttoChain message objects to the wire format expected by the
6
+ * Scala metagraph. Specifically, ensures all `Option[A]=None` fields are
7
+ * present as explicit `null` values, matching what circe's magnolia encoder
8
+ * produces on the Scala side.
9
+ *
10
+ * This is necessary because metakit's `JsonBinaryCodec.deriveDataUpdate`
11
+ * (rc.8) canonicalizes the circe-encoded JSON **including null fields**.
12
+ * If the TypeScript client omits optional fields (undefined), the canonical
13
+ * JSON won't match and signature verification will fail.
14
+ *
15
+ * Schema mapping (Scala → TypeScript):
16
+ * - `Option[A] = None` → `null` (must be explicit, not undefined/absent)
17
+ * - `Option[A] = Some(v)` → `v`
18
+ * - `List.empty` → `[]`
19
+ * - `Map.empty` → `{}`
20
+ */
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.normalizeMessage = exports.normalizeArchiveStateMachine = exports.normalizeTransitionStateMachine = exports.normalizeCreateStateMachine = void 0;
23
+ /**
24
+ * Normalize a State object for wire format
25
+ */
26
+ function normalizeState(state) {
27
+ return {
28
+ id: state.id,
29
+ isFinal: state.isFinal ?? false,
30
+ metadata: state.metadata ?? null,
31
+ };
32
+ }
33
+ /**
34
+ * Normalize a Transition object for wire format
35
+ */
36
+ function normalizeTransition(t) {
37
+ return {
38
+ from: t.from,
39
+ eventName: t.eventName,
40
+ to: t.to,
41
+ guard: t.guard ?? null,
42
+ actions: t.actions ?? null,
43
+ metadata: t.metadata ?? null,
44
+ };
45
+ }
46
+ /**
47
+ * Normalize a StateMachineDefinition for wire format
48
+ */
49
+ function normalizeDefinition(def) {
50
+ const states = def.states;
51
+ const normalizedStates = {};
52
+ if (states) {
53
+ for (const [key, state] of Object.entries(states)) {
54
+ normalizedStates[key] = normalizeState(state);
55
+ }
56
+ }
57
+ const transitions = def.transitions ?? [];
58
+ return {
59
+ states: normalizedStates,
60
+ initialState: def.initialState,
61
+ transitions: transitions.map(normalizeTransition),
62
+ metadata: def.metadata ?? null,
63
+ };
64
+ }
65
+ /**
66
+ * Normalize a CreateStateMachine message for wire format
67
+ *
68
+ * Ensures all Option fields are explicit null:
69
+ * - definition.metadata
70
+ * - definition.states[*].metadata
71
+ * - definition.transitions[*].guard
72
+ * - definition.transitions[*].actions
73
+ * - definition.transitions[*].metadata
74
+ * - parentFiberId
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const message = normalizeCreateStateMachine({
79
+ * fiberId: '...',
80
+ * definition: { states: { INIT: { id: { value: 'INIT' }, isFinal: false } }, ... },
81
+ * initialData: {}
82
+ * });
83
+ * // message now has parentFiberId: null, definition.metadata: null, etc.
84
+ * ```
85
+ */
86
+ function normalizeCreateStateMachine(msg) {
87
+ return {
88
+ fiberId: msg.fiberId,
89
+ definition: normalizeDefinition(msg.definition),
90
+ initialData: msg.initialData ?? {},
91
+ parentFiberId: msg.parentFiberId ?? null,
92
+ };
93
+ }
94
+ exports.normalizeCreateStateMachine = normalizeCreateStateMachine;
95
+ /**
96
+ * Normalize a TransitionStateMachine message for wire format
97
+ */
98
+ function normalizeTransitionStateMachine(msg) {
99
+ return {
100
+ fiberId: msg.fiberId,
101
+ eventName: msg.eventName,
102
+ eventData: msg.eventData ?? null,
103
+ fiberOrdinal: msg.fiberOrdinal,
104
+ };
105
+ }
106
+ exports.normalizeTransitionStateMachine = normalizeTransitionStateMachine;
107
+ /**
108
+ * Normalize an ArchiveStateMachine message for wire format
109
+ */
110
+ function normalizeArchiveStateMachine(msg) {
111
+ return {
112
+ fiberId: msg.fiberId,
113
+ reason: msg.reason ?? null,
114
+ };
115
+ }
116
+ exports.normalizeArchiveStateMachine = normalizeArchiveStateMachine;
117
+ /**
118
+ * Normalize any OttochainMessage wrapper for wire format.
119
+ *
120
+ * Detects the message type from the wrapper key and applies the
121
+ * appropriate normalization.
122
+ *
123
+ * @param message - OttochainMessage in wrapper format: `{ CreateStateMachine: {...} }`
124
+ * @returns Normalized message with all Option fields as explicit null
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const normalized = normalizeMessage({
129
+ * CreateStateMachine: { fiberId: '...', definition: {...}, initialData: {} }
130
+ * });
131
+ * const signed = await signDataUpdate(normalized, privateKey);
132
+ * ```
133
+ */
134
+ function normalizeMessage(message) {
135
+ if ('CreateStateMachine' in message) {
136
+ return { CreateStateMachine: normalizeCreateStateMachine(message.CreateStateMachine) };
137
+ }
138
+ if ('TransitionStateMachine' in message) {
139
+ return { TransitionStateMachine: normalizeTransitionStateMachine(message.TransitionStateMachine) };
140
+ }
141
+ if ('ArchiveStateMachine' in message) {
142
+ return { ArchiveStateMachine: normalizeArchiveStateMachine(message.ArchiveStateMachine) };
143
+ }
144
+ // CreateScript and InvokeScript — pass through (no optional fields)
145
+ return message;
146
+ }
147
+ exports.normalizeMessage = normalizeMessage;
@@ -138,6 +138,153 @@ class MetagraphClient {
138
138
  return snapshot.value.ordinal;
139
139
  }
140
140
  // -------------------------------------------------------------------------
141
+ // Fiber state subscription
142
+ // -------------------------------------------------------------------------
143
+ /**
144
+ * Subscribe to state changes for a state machine fiber.
145
+ *
146
+ * Polls ML0 at the configured interval and compares `sequenceNumber` to
147
+ * detect changes. Fires the callback with `(current, previous)` on the
148
+ * first poll (if `fireImmediately` is true — the default) and on every
149
+ * subsequent change.
150
+ *
151
+ * ML0 consistency: state is read from snapshot-level `CalculatedState`
152
+ * (via `checkpointService.get`), so a poll never returns a mid-transition
153
+ * value — every observation is a fully committed state.
154
+ *
155
+ * Uses `setTimeout` recursion to prevent overlapping polls when a request
156
+ * takes longer than `pollIntervalMs`.
157
+ *
158
+ * @param fiberId - UUID of the state machine fiber to watch
159
+ * @param callback - Called on first observation and on every state change
160
+ * @param options - Polling interval, error handler, fireImmediately flag
161
+ * @returns Unsubscribe function — call to stop polling immediately
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * const unsub = client.subscribeFiberState(fiberId, (current, prev) => {
166
+ * if (current?.currentState === 'Completed') {
167
+ * unsub();
168
+ * }
169
+ * });
170
+ * // Later:
171
+ * unsub();
172
+ * ```
173
+ */
174
+ subscribeFiberState(fiberId, callback, options) {
175
+ const intervalMs = options?.pollIntervalMs ?? 2000;
176
+ const onError = options?.onError ?? ((e) => console.warn('[subscribeFiberState]', e));
177
+ const fireImmediately = options?.fireImmediately ?? true;
178
+ let previous = null;
179
+ let lastSeqNum = null;
180
+ let firstPoll = true; // Track first poll separately — handles null seqNum on missing fiber
181
+ let active = true;
182
+ const invokeCallback = (current, prev) => {
183
+ try {
184
+ callback(current, prev);
185
+ }
186
+ catch (cbErr) {
187
+ try {
188
+ onError(cbErr instanceof Error ? cbErr : new Error(String(cbErr)));
189
+ }
190
+ catch { /* ignore */ }
191
+ }
192
+ };
193
+ const poll = async () => {
194
+ if (!active)
195
+ return;
196
+ let current;
197
+ try {
198
+ current = await this.getStateMachine(fiberId);
199
+ }
200
+ catch (err) {
201
+ // Network / timeout error — notify and reschedule
202
+ if (!active)
203
+ return;
204
+ try {
205
+ onError(err instanceof Error ? err : new Error(String(err)));
206
+ }
207
+ catch { /* ignore */ }
208
+ if (active)
209
+ setTimeout(poll, intervalMs);
210
+ return;
211
+ }
212
+ // Guard: unsubscribe may have been called while awaiting
213
+ if (!active)
214
+ return;
215
+ const currentSeq = current?.sequenceNumber ?? null;
216
+ if (firstPoll) {
217
+ firstPoll = false;
218
+ lastSeqNum = currentSeq;
219
+ previous = current;
220
+ if (fireImmediately) {
221
+ invokeCallback(current, null);
222
+ }
223
+ }
224
+ else if (currentSeq !== lastSeqNum) {
225
+ // State changed — sequenceNumber differs (handles null → value and value → null)
226
+ const prev = previous;
227
+ previous = current;
228
+ lastSeqNum = currentSeq;
229
+ invokeCallback(current, prev);
230
+ }
231
+ // No change → no callback
232
+ // Schedule next poll (setTimeout recursion prevents overlapping polls)
233
+ if (active)
234
+ setTimeout(poll, intervalMs);
235
+ };
236
+ // Start first poll via setTimeout(0) so Jest fake-timer control works correctly:
237
+ // callers can unsubscribe before the first poll runs, and each
238
+ // jest.runOnlyPendingTimersAsync() advances exactly one poll cycle.
239
+ setTimeout(poll, 0);
240
+ return () => { active = false; };
241
+ }
242
+ /**
243
+ * Wait for a fiber to reach a specific state, with an optional timeout.
244
+ *
245
+ * Builds on `subscribeFiberState` — resolves with the fiber record once
246
+ * `currentState === targetState`, or resolves with `null` after `timeoutMs`.
247
+ * Always calls unsubscribe on resolution or timeout (no memory leaks).
248
+ *
249
+ * @param fiberId - UUID of the state machine fiber
250
+ * @param targetState - State name to wait for (e.g. `'Completed'`)
251
+ * @param timeoutMs - Maximum wait in milliseconds (default: 30 000)
252
+ * @returns Fiber record when target state reached, or `null` on timeout
253
+ *
254
+ * @example
255
+ * ```typescript
256
+ * const result = await client.waitForState(fiberId, 'Completed', 15_000);
257
+ * if (result === null) console.warn('Timed out waiting for Completed');
258
+ * ```
259
+ */
260
+ waitForState(fiberId, targetState, timeoutMs = 30000, options) {
261
+ return new Promise((resolve) => {
262
+ let resolved = false;
263
+ let timer = null;
264
+ // unsub starts as a no-op so it's safe to call even if subscribeFiberState
265
+ // invokes the callback synchronously (before returning the real unsubscribe).
266
+ let unsub = () => { };
267
+ const done = (result) => {
268
+ if (resolved)
269
+ return;
270
+ resolved = true;
271
+ if (timer !== null)
272
+ clearTimeout(timer);
273
+ unsub(); // safe: either the real unsub or the initial no-op
274
+ resolve(result);
275
+ };
276
+ unsub = this.subscribeFiberState(fiberId, (current) => {
277
+ if (current?.currentState === targetState) {
278
+ done(current);
279
+ }
280
+ }, { fireImmediately: true, ...options });
281
+ // Only arm the timeout if the callback hasn't already resolved (synchronous case)
282
+ if (!resolved) {
283
+ timer = setTimeout(() => done(null), timeoutMs);
284
+ }
285
+ });
286
+ }
287
+ // -------------------------------------------------------------------------
141
288
  // DL1 data submission (framework POST /data)
142
289
  // -------------------------------------------------------------------------
143
290
  /**
@@ -135,7 +135,7 @@ exports.PlatformLinkSchema = zod_1.z.object({
135
135
  /**
136
136
  * Schema for contract terms (flexible structure)
137
137
  */
138
- exports.ContractTermsSchema = zod_1.z.record(zod_1.z.unknown());
138
+ exports.ContractTermsSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.unknown());
139
139
  /**
140
140
  * Schema for ProposeContractRequest
141
141
  */