@ottochain/sdk 2.1.0 → 2.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.
@@ -6,7 +6,7 @@
6
6
  * no JSON, no code generation. Import types directly.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.toJSON = exports.isFinalState = exports.getEventsFrom = exports.getTransitionsFrom = exports.defineFiberApp = void 0;
9
+ exports.toProtoDefinition = exports.toJSON = exports.isFinalState = exports.getEventsFrom = exports.getTransitionsFrom = exports.defineFiberApp = void 0;
10
10
  // =============================================================================
11
11
  // Helper: defineFiberApp
12
12
  // =============================================================================
@@ -53,3 +53,43 @@ function toJSON(def) {
53
53
  return JSON.parse(JSON.stringify(def));
54
54
  }
55
55
  exports.toJSON = toJSON;
56
+ /**
57
+ * Extract proto-compatible StateMachineDefinition from a FiberAppDefinition.
58
+ * Use this when submitting to the metagraph.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const def = getContractDefinition('agreement');
63
+ * const protoDef = toProtoDefinition(def);
64
+ * // Submit protoDef to metagraph
65
+ * ```
66
+ */
67
+ function toProtoDefinition(def) {
68
+ // Extract only the proto-compatible fields
69
+ const protoDef = {
70
+ states: {},
71
+ initialState: def.initialState,
72
+ transitions: def.transitions.map(t => ({
73
+ from: t.from,
74
+ to: t.to,
75
+ eventName: t.eventName,
76
+ ...(t.guard && { guard: t.guard }),
77
+ ...(t.effect && { effect: t.effect }),
78
+ ...(t.dependencies?.length && { dependencies: [...t.dependencies] }),
79
+ ...(t.emits?.length && { emits: [...t.emits] }),
80
+ })),
81
+ };
82
+ // Copy states (only id and isFinal)
83
+ for (const [key, state] of Object.entries(def.states)) {
84
+ protoDef.states[key] = {
85
+ id: state.id,
86
+ isFinal: state.isFinal,
87
+ };
88
+ }
89
+ // Pass metadata through unchanged - it's an optional unstructured object
90
+ if (def.metadata) {
91
+ protoDef.metadata = def.metadata;
92
+ }
93
+ return protoDef;
94
+ }
95
+ exports.toProtoDefinition = toProtoDefinition;
@@ -45,3 +45,42 @@ export function isFinalState(def, state) {
45
45
  export function toJSON(def) {
46
46
  return JSON.parse(JSON.stringify(def));
47
47
  }
48
+ /**
49
+ * Extract proto-compatible StateMachineDefinition from a FiberAppDefinition.
50
+ * Use this when submitting to the metagraph.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * const def = getContractDefinition('agreement');
55
+ * const protoDef = toProtoDefinition(def);
56
+ * // Submit protoDef to metagraph
57
+ * ```
58
+ */
59
+ export function toProtoDefinition(def) {
60
+ // Extract only the proto-compatible fields
61
+ const protoDef = {
62
+ states: {},
63
+ initialState: def.initialState,
64
+ transitions: def.transitions.map(t => ({
65
+ from: t.from,
66
+ to: t.to,
67
+ eventName: t.eventName,
68
+ ...(t.guard && { guard: t.guard }),
69
+ ...(t.effect && { effect: t.effect }),
70
+ ...(t.dependencies?.length && { dependencies: [...t.dependencies] }),
71
+ ...(t.emits?.length && { emits: [...t.emits] }),
72
+ })),
73
+ };
74
+ // Copy states (only id and isFinal)
75
+ for (const [key, state] of Object.entries(def.states)) {
76
+ protoDef.states[key] = {
77
+ id: state.id,
78
+ isFinal: state.isFinal,
79
+ };
80
+ }
81
+ // Pass metadata through unchanged - it's an optional unstructured object
82
+ if (def.metadata) {
83
+ protoDef.metadata = def.metadata;
84
+ }
85
+ return protoDef;
86
+ }
@@ -137,3 +137,36 @@ export declare function getEventsFrom<T extends FiberAppDefinition>(def: T, stat
137
137
  export declare function isFinalState<T extends FiberAppDefinition>(def: T, state: StateNames<T>): boolean;
138
138
  /** Convert definition to plain JSON (for serialization) */
139
139
  export declare function toJSON<T extends FiberAppDefinition>(def: T): object;
140
+ /**
141
+ * Proto-compatible state machine definition for metagraph submission.
142
+ * Matches StateMachineDefinition from ottochain/v1/fiber.proto
143
+ */
144
+ export interface ProtoStateMachineDefinition {
145
+ states: Record<string, {
146
+ id: string;
147
+ isFinal: boolean;
148
+ }>;
149
+ initialState: string;
150
+ transitions: Array<{
151
+ from: string;
152
+ to: string;
153
+ eventName: string;
154
+ guard?: unknown;
155
+ effect?: unknown;
156
+ dependencies?: unknown[];
157
+ emits?: unknown[];
158
+ }>;
159
+ metadata?: Record<string, unknown>;
160
+ }
161
+ /**
162
+ * Extract proto-compatible StateMachineDefinition from a FiberAppDefinition.
163
+ * Use this when submitting to the metagraph.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * const def = getContractDefinition('agreement');
168
+ * const protoDef = toProtoDefinition(def);
169
+ * // Submit protoDef to metagraph
170
+ * ```
171
+ */
172
+ export declare function toProtoDefinition<T extends FiberAppDefinition>(def: T): ProtoStateMachineDefinition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ottochain/sdk",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "TypeScript SDK for ottochain metagraph operations - signing, encoding, and network interactions",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",