Skip to content

Commit

Permalink
chore: use context type in serializeInputContext
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmoro committed Oct 25, 2024
1 parent a7ceecf commit 7150b71
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/deserialize-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function deserializeInputContext(context: {

async function deserializeSpec(serializedSpecWithHash: string): Promise<Spec> {
if (!(await validateSpecHash(serializedSpecWithHash))) {
throw new Error('Invalid spec hash');
throw Error('Invalid spec hash');
}

const { spec: serializedSpec } = JSON.parse(serializedSpecWithHash);
Expand Down
50 changes: 28 additions & 22 deletions src/serialize-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function serializeInput(input: Input): any {
}
}
}
throw new Error('Invalid input type');
throw Error('Invalid input type');
}

function serializeNode(node: Node): any {
Expand Down Expand Up @@ -204,32 +204,38 @@ function serializeNode(node: Node): any {
}
}

function serializeInputContext(
context: ZkAppInputContext | HttpsInputContext
): {
function serializeInputContext(context: {
type: 'zk-app' | 'https';
presentationCircuitVKHash: Field;
action: Field | string;
serverNonce: Field;
}): {
type: string;
presentationCircuitVKHash: ReturnType<typeof serializeProvable>;
action: ReturnType<typeof serializeProvable> | string;
serverNonce: ReturnType<typeof serializeProvable>;
} {
if ('action' in context && typeof context.action === 'string') {
return {
type: 'https',
presentationCircuitVKHash: serializeProvable(
context.presentationCircuitVKHash
),
action: context.action,
serverNonce: serializeProvable(context.serverNonce),
};
} else {
return {
type: 'zk-app',
presentationCircuitVKHash: serializeProvable(
context.presentationCircuitVKHash
),
action: serializeProvable((context as ZkAppInputContext).action),
serverNonce: serializeProvable(context.serverNonce),
};
const serializedBase = {
type: context.type,
presentationCircuitVKHash: serializeProvable(
context.presentationCircuitVKHash
),
serverNonce: serializeProvable(context.serverNonce),
};

switch (context.type) {
case 'zk-app':
return {
...serializedBase,
action: serializeProvable(context.action as Field),
};
case 'https':
return {
...serializedBase,
action: context.action as string,
};
default:
throw Error(`Unsupported context type: ${context.type}`);
}
}

Expand Down

0 comments on commit 7150b71

Please sign in to comment.