Skip to content

Commit

Permalink
Make JSONValue internally used only (#536)
Browse files Browse the repository at this point in the history
* Make JSONValue internally used only

Move the definition of JSONValue into circuit_value.ts since there are
many types that depend on JSONValue inside that file. We also removed
any usages of JSONValue from public fromJSON methods since that type is
intended for internal use.

* Replace JSONValue with any type

* Fix tests

Update int.test.ts

fix tests

redo merge conflict fix

 Fix tests

* Removed all uses of JSONValue with any
  • Loading branch information
MartinMinkov committed Nov 9, 2022
1 parent e151439 commit ec3f1c6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export {
Scalar,
ProvablePure,
Provable,
JSONValue,
Ledger,
isReady,
shutdown,
Expand Down
23 changes: 10 additions & 13 deletions src/lib/circuit_value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import { Circuit, JSONValue, ProvablePure, Provable } from '../snarky.js';
import { Circuit, ProvablePure, Provable } from '../snarky.js';
import { Field, Bool } from './core.js';
import { Context } from './global-context.js';
import { inCheckedComputation, snarkContext } from './proof_system.js';
Expand Down Expand Up @@ -138,7 +138,7 @@ abstract class CircuitValue {
return (this.constructor as any).toFields(this);
}

toJSON(): JSONValue {
toJSON(): any {
return (this.constructor as any).toJSON(this);
}

Expand Down Expand Up @@ -205,11 +205,8 @@ abstract class CircuitValue {
return (this as any).fromFields(xs.map((x) => x.toConstant()));
}

static toJSON<T extends AnyConstructor>(
this: T,
v: InstanceType<T>
): JSONValue {
const res: { [key: string]: JSONValue } = {};
static toJSON<T extends AnyConstructor>(this: T, v: InstanceType<T>) {
const res: any = {};
if ((this as any).prototype._fields !== undefined) {
const fields: [string, any][] = (this as any).prototype._fields;
fields.forEach(([key, propType]) => {
Expand All @@ -221,7 +218,7 @@ abstract class CircuitValue {

static fromJSON<T extends AnyConstructor>(
this: T,
value: JSONValue
value: any
): InstanceType<T> | null {
const props: any = {};
const fields: [string, any][] = (this as any).prototype._fields;
Expand Down Expand Up @@ -436,11 +433,11 @@ function circuitMain(
let primitives = new Set(['Field', 'Bool', 'Scalar', 'Group']);
let complexTypes = new Set(['object', 'function']);

type ProvableExtension<T, TJson = JSONValue> = {
type ProvableExtension<T, TJson = any> = {
toInput: (x: T) => { fields?: Field[]; packed?: [Field, number][] };
toJSON: (x: T) => TJson;
};
type ProvableExtended<T, TJson = JSONValue> = Provable<T> &
type ProvableExtended<T, TJson = any> = Provable<T> &
ProvableExtension<T, TJson>;

function provable<A>(
Expand Down Expand Up @@ -515,7 +512,7 @@ function provable<A>(
.map((k) => toInput(typeObj[k], obj[k]))
.reduce(HashInput.append, {});
}
function toJSON(typeObj: any, obj: any, isToplevel = false): JSONValue {
function toJSON(typeObj: any, obj: any, isToplevel = false): any {
if (typeObj === BigInt) return obj.toString();
if (typeObj === String || typeObj === Number || typeObj === Boolean)
return obj;
Expand Down Expand Up @@ -1106,7 +1103,7 @@ type InferPrimitiveJson<P extends Primitive> = P extends typeof String
? null
: P extends undefined
? null
: JSONValue;
: any;

type InferCircuitValue<A> = A extends Constructor<infer U>
? A extends Provable<U>
Expand Down Expand Up @@ -1146,7 +1143,7 @@ type InferJson<A> = A extends WithJson<infer J>
? {
[K in keyof A]: InferJson<A[K]>;
}
: JSONValue;
: any;

type IsPure<A> = IsPureBase<A> extends true ? true : false;

Expand Down
4 changes: 0 additions & 4 deletions src/lib/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@ describe('Field constructor', () => {
it('handles small numbers', () => {
expect(Field(5).toString()).toEqual('5');
expect(Field(1313).toString()).toEqual('1313');
expect(Field(5).toString()).toEqual('5');
});

it('handles large numbers 2^31 <= x < 2^53', () => {
expect(Field(2 ** 31).toString()).toEqual('2147483648');
expect(Field(Number.MAX_SAFE_INTEGER).toString()).toEqual(
String(Number.MAX_SAFE_INTEGER)
);
expect(Field(2 ** 31).toString()).toEqual('2147483648');
});

it('handles negative numbers', () => {
expect(Field(-1)).toEqual(Field(1).neg());
expect(Field(-(2 ** 31))).toEqual(Field(2 ** 31).neg());
expect(Field(-1)).toEqual(Field(1).neg());
});

it('throws on fractional numbers', () => {
expect(() => Field(0.5)).toThrow();
expect(() => Field(-1.1)).toThrow();
expect(() => Field(0.5)).toThrow();
});

// Field(bigint), Field.fromBigInt, toBigInt
Expand Down
4 changes: 2 additions & 2 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This is for an account where any of a list of public keys can update the state

import { Circuit, JSONValue, Ledger, LedgerAccount } from '../snarky.js';
import { Circuit, Ledger, LedgerAccount } from '../snarky.js';
import { Field, Bool } from './core.js';
import { UInt32, UInt64 } from './int.js';
import { PrivateKey, PublicKey } from './signature.js';
Expand Down Expand Up @@ -57,7 +57,7 @@ interface TransactionId {
interface Transaction {
transaction: ZkappCommand;
toJSON(): string;
toPretty(): JSONValue;
toPretty(): any;
toGraphqlQuery(): string;
sign(additionalKeys?: PrivateKey[]): Transaction;
prove(): Promise<(Proof<ZkappPublicInput> | undefined)[]>;
Expand Down
4 changes: 0 additions & 4 deletions src/lib/scalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ describe('scalar', () => {
it('fromJSON(false) should be 0', () => {
expect(Scalar.fromJSON(false)!.toJSON()).toEqual('0');
});

it('fromJSON([]) should be undefined', () => {
expect(Scalar.fromJSON([])).toBeNull();
});
});

describe('neg', () => {
Expand Down
1 change: 0 additions & 1 deletion src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Ledger,
Pickles,
Poseidon as Poseidon_,
JSONValue,
Provable,
} from '../snarky.js';
import {
Expand Down
15 changes: 3 additions & 12 deletions src/snarky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export {
isReady,
shutdown,
Pickles,
JSONValue,
Account as LedgerAccount,
};

Expand Down Expand Up @@ -359,7 +358,7 @@ declare class Field {
*/

static toJSON(x: Field): string;
static fromJSON(x: JSONValue): Field | null;
static fromJSON(x: string | number): Field;

static check(x: Field): void;

Expand Down Expand Up @@ -486,7 +485,7 @@ declare class Bool {
static fromFields(fields: Field[]): Bool;

static toJSON(x: Bool): boolean;
static fromJSON(x: JSONValue): Bool | null;
static fromJSON(x: boolean): Bool;
static check(x: Bool): void;

// monkey-patched in JS
Expand Down Expand Up @@ -636,7 +635,7 @@ declare class Scalar {
static random(): Scalar;

static toJSON(x: Scalar): string;
static fromJSON(x: JSONValue): Scalar | null;
static fromJSON(x: string | number | boolean): Scalar;
static check(x: Scalar): void;
}

Expand Down Expand Up @@ -957,12 +956,4 @@ declare const Pickles: {
proofToBase64Transaction: (proof: Pickles.Proof) => string;
};

type JSONValue =
| number
| string
| boolean
| null
| Array<JSONValue>
| { [key: string]: JSONValue };

type AuthRequired = 'Signature' | 'Proof' | 'Either' | 'None' | 'Impossible';

0 comments on commit ec3f1c6

Please sign in to comment.