Skip to content

Commit

Permalink
refactor: check if serialized pcd parameters exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Sep 26, 2023
1 parent 0e69377 commit 62edb43
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 60 deletions.
8 changes: 6 additions & 2 deletions packages/eddsa-pcd/src/EdDSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
StringArgument,
StringArrayArgument
} from "@pcd/pcd-types";
import { fromHexString, toHexString } from "@pcd/util";
import { fromHexString, toHexString, requireDefinedParameter } from "@pcd/util";
import { buildEddsa, buildPoseidon, Eddsa, Point, Poseidon } from "circomlibjs";
import { v4 as uuid } from "uuid";
import { EdDSACardBody } from "./CardBody";
Expand Down Expand Up @@ -143,7 +143,11 @@ export async function serialize(
}

export async function deserialize(serialized: string): Promise<EdDSAPCD> {
const { id, claim, proof } = JSON.parse(serialized, reviver) as EdDSAPCD;
const { id, claim, proof } = JSON.parse(serialized, reviver);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new EdDSAPCD(id, claim, proof);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-group-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@pcd/pcd-types": "0.7.0",
"@pcd/semaphore-identity-pcd": "0.7.0",
"@pcd/semaphore-signature-pcd": "0.7.0",
"@pcd/util": "0.1.0",
"@personaelabs/spartan-ecdsa": "^2.1.4",
"@semaphore-protocol/identity": "^3.11.0",
"ethers": "^5.7.2",
Expand Down
32 changes: 21 additions & 11 deletions packages/ethereum-group-pcd/src/EthereumGroupPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SemaphoreSignaturePCD,
SemaphoreSignaturePCDPackage
} from "@pcd/semaphore-signature-pcd";
import { requireDefinedParameter } from "@pcd/util";
import {
CircuitPubInput,
MembershipProver,
Expand Down Expand Up @@ -279,21 +280,30 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<EthereumGroupPCD> {
const parsed = JSONBig({ useNativeBigInt: true }).parse(serialized);
const { id, claim, proof } = JSONBig({ useNativeBigInt: true }).parse(
serialized
);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

const publicInput = new PublicInput(
parsed.claim.publicInput.r,
parsed.claim.publicInput.rV,
Buffer.from(parsed.claim.publicInput.msgHash),
claim.publicInput.r,
claim.publicInput.rV,
Buffer.from(claim.publicInput.msgHash),
new CircuitPubInput(
parsed.claim.publicInput.circuitPubInput.merkleRoot,
parsed.claim.publicInput.circuitPubInput.Tx,
parsed.claim.publicInput.circuitPubInput.Ty,
parsed.claim.publicInput.circuitPubInput.Ux,
parsed.claim.publicInput.circuitPubInput.Uy
claim.publicInput.circuitPubInput.merkleRoot,
claim.publicInput.circuitPubInput.Tx,
claim.publicInput.circuitPubInput.Ty,
claim.publicInput.circuitPubInput.Ux,
claim.publicInput.circuitPubInput.Uy
)
);
parsed.claim.publicInput = publicInput;
return new EthereumGroupPCD(parsed.id, parsed.claim, parsed.proof);

claim.publicInput = publicInput;

return new EthereumGroupPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: EthereumGroupPCD): DisplayOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-ownership-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@pcd/pcd-types": "0.7.0",
"@pcd/semaphore-identity-pcd": "0.7.0",
"@pcd/semaphore-signature-pcd": "0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/identity": "^3.11.0",
"ethers": "^5.7.2",
"json-bigint": "^1.0.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/ethereum-ownership-pcd/src/EthereumOwnershipPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SemaphoreSignaturePCD,
SemaphoreSignaturePCDPackage
} from "@pcd/semaphore-signature-pcd";
import { requireDefinedParameter } from "@pcd/util";
import { ethers } from "ethers";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -200,7 +201,13 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<EthereumOwnershipPCD> {
return JSONBig().parse(serialized);
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new EthereumOwnershipPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: EthereumOwnershipPCD): DisplayOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/halo-nonce-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@pcd/passport-ui": "0.7.0",
"@pcd/pcd-types": "0.7.0",
"@pcd/tsconfig": "*",
"@pcd/util": "0.1.0",
"@types/elliptic": "^6.4.14",
"@types/mocha": "^10.0.1",
"@types/react": "^18.0.22",
Expand Down
5 changes: 5 additions & 0 deletions packages/halo-nonce-pcd/src/HaLoNoncePCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SerializedPCD,
StringArgument
} from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import { ec } from "elliptic";
import { sha256 } from "js-sha256";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -156,6 +157,10 @@ export async function serialize(
export async function deserialize(serialized: string): Promise<HaLoNoncePCD> {
const { id, claim, proof } = JSON.parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new HaLoNoncePCD(id, claim, proof);
}

Expand Down
1 change: 1 addition & 0 deletions packages/rln-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@pcd/pcd-types": "^0.7.0",
"@pcd/semaphore-group-pcd": "^0.7.0",
"@pcd/semaphore-identity-pcd": "^0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/group": "^3.11.0",
"@semaphore-protocol/identity": "^3.11.0",
"json-bigint": "^1.0.0",
Expand Down
34 changes: 20 additions & 14 deletions packages/rln-pcd/src/RLNPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import {
PCDArgument,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import {
SemaphoreIdentityPCD,
SemaphoreIdentityPCDPackage,
SemaphoreIdentityPCDPackage
} from "@pcd/semaphore-identity-pcd";
import {
deserializeSemaphoreGroup,
SerializedSemaphoreGroup,
SerializedSemaphoreGroup
} from "@pcd/semaphore-group-pcd";

import verificationKeyJSON from "../artifacts/16.json";
import { requireDefinedParameter } from "@pcd/util";

let initArgs: RLNPCDInitArgs | undefined = undefined;

Expand Down Expand Up @@ -87,11 +88,11 @@ export class RLNPCD implements PCD<RLNPCDClaim, RLNPCDProof> {
rlnIdentifier: rlnFullProof.rlnIdentifier,
yShare: BigInt(publicSignals.yShare),
merkleRoot: BigInt(publicSignals.merkleRoot),
internalNullifier: BigInt(publicSignals.internalNullifier),
internalNullifier: BigInt(publicSignals.internalNullifier)
};
const proof: RLNPCDProof = {
proof: rlnFullProof.snarkProof.proof,
externalNullifier: BigInt(publicSignals.externalNullifier),
externalNullifier: BigInt(publicSignals.externalNullifier)
};
return new RLNPCD(uuid(), claim, proof);
}
Expand All @@ -105,11 +106,11 @@ export class RLNPCD implements PCD<RLNPCDClaim, RLNPCDProof> {
merkleRoot: this.claim.merkleRoot,
internalNullifier: this.claim.internalNullifier,
signalHash: this.claim.x,
externalNullifier: this.proof.externalNullifier,
},
externalNullifier: this.proof.externalNullifier
}
},
epoch: this.claim.epoch,
rlnIdentifier: this.claim.rlnIdentifier,
rlnIdentifier: this.claim.rlnIdentifier
};
}
}
Expand Down Expand Up @@ -206,15 +207,20 @@ function getRLNInstance(rlnIdentifier: bigint, identity?: Identity) {
export async function serialize(pcd: RLNPCD): Promise<SerializedPCD<RLNPCD>> {
return {
type: RLNPCDTypeName,
pcd: JSONBig({ useNativeBigInt: true }).stringify(pcd),
pcd: JSONBig({ useNativeBigInt: true }).stringify(pcd)
} as SerializedPCD<RLNPCD>;
}

export async function deserialize(serialized: string): Promise<RLNPCD> {
const parsed = JSONBig({ useNativeBigInt: true }).parse(serialized);
const proof = parsed.proof;
const claim = parsed.claim;
return new RLNPCD(parsed.id, claim, proof);
const { id, claim, proof } = JSONBig({ useNativeBigInt: true }).parse(
serialized
);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new RLNPCD(id, claim, proof);
}

/**
Expand All @@ -232,5 +238,5 @@ export const RLNPCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
1 change: 1 addition & 0 deletions packages/rsa-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@pcd/passport-ui": "0.7.0",
"@pcd/pcd-types": "0.7.0",
"@pcd/util": "0.1.0",
"chai": "^4.3.7",
"json-bigint": "^1.0.0",
"node-rsa": "^1.1.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/rsa-pcd/src/RSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SerializedPCD,
StringArgument
} from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import JSONBig from "json-bigint";
import NodeRSA from "node-rsa";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -97,7 +98,11 @@ export async function serialize(pcd: RSAPCD): Promise<SerializedPCD<RSAPCD>> {
}

export async function deserialize(serialized: string): Promise<RSAPCD> {
const { id, claim, proof } = JSONBig().parse(serialized) as RSAPCD;
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new RSAPCD(id, claim, proof);
}
Expand Down
1 change: 1 addition & 0 deletions packages/semaphore-group-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@pcd/pcd-types": "0.7.0",
"@pcd/semaphore-identity-pcd": "0.7.0",
"@pcd/semaphore-signature-pcd": "0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/group": "^3.11.0",
"@semaphore-protocol/identity": "^3.11.0",
"@semaphore-protocol/proof": "^3.11.0",
Expand Down
7 changes: 6 additions & 1 deletion packages/semaphore-group-pcd/src/SemaphoreGroupPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SemaphoreIdentityPCDPackage
} from "@pcd/semaphore-identity-pcd";
import { STATIC_SIGNATURE_PCD_NULLIFIER } from "@pcd/semaphore-signature-pcd";
import { requireDefinedParameter } from "@pcd/util";
import {
FullProof,
Proof,
Expand Down Expand Up @@ -196,7 +197,11 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<SemaphoreGroupPCD> {
const { id, claim, proof } = JSONBig().parse(serialized) as SemaphoreGroupPCD;
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new SemaphoreGroupPCD(id, claim, proof);
}
Expand Down
1 change: 1 addition & 0 deletions packages/semaphore-identity-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@pcd/passport-ui": "0.7.0",
"@pcd/pcd-types": "0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/identity": "^3.11.0",
"json-bigint": "^1.0.0",
"react": "^18.2.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/semaphore-identity-pcd/src/SemaphoreIdentityPCD.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DisplayOptions, PCD, PCDPackage, SerializedPCD } from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import { Identity } from "@semaphore-protocol/identity";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -49,26 +50,29 @@ export async function serialize(
pcd: JSONBig.stringify({
type: pcd.type,
id: pcd.id,
identity: pcd.claim.identity.toString(),
}),
identity: pcd.claim.identity.toString()
})
} as SerializedPCD<SemaphoreIdentityPCD>;
}

export async function deserialize(
serialized: string
): Promise<SemaphoreIdentityPCD> {
const parsed = JSONBig.parse(serialized);
return new SemaphoreIdentityPCD(parsed.id, {
identity: new Identity(parsed.identity),
const { id, identity } = JSONBig.parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(identity, "identity");

return new SemaphoreIdentityPCD(id, {
identity: new Identity(identity)
});
}

export function getDisplayOptions(pcd: SemaphoreIdentityPCD): DisplayOptions {
return {
header: "Semaphore Identity",
displayName:
"semaphore-id-" +
pcd.claim.identity.commitment.toString().substring(0, 8),
"semaphore-id-" + pcd.claim.identity.commitment.toString().substring(0, 8)
};
}

Expand All @@ -87,5 +91,5 @@ export const SemaphoreIdentityPCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
10 changes: 6 additions & 4 deletions packages/semaphore-signature-pcd/src/SemaphoreSignaturePCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SemaphoreIdentityPCD,
SemaphoreIdentityPCDPackage
} from "@pcd/semaphore-identity-pcd";
import { generateSnarkMessageHash } from "@pcd/util";
import { generateSnarkMessageHash, requireDefinedParameter } from "@pcd/util";
import { Group } from "@semaphore-protocol/group";
import {
FullProof,
Expand Down Expand Up @@ -185,9 +185,11 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<SemaphoreSignaturePCD> {
const { id, claim, proof } = JSONBig().parse(
serialized
) as SemaphoreSignaturePCD;
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new SemaphoreSignaturePCD(id, claim, proof);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/util/src/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Either extracts the error message out of an error, or converts
* the value passed in into a string.
*/
export function getErrorMessage(e: any | Error): string {
if (e instanceof Error) {
return e.message;
}

return e + "";
}

/**
* Check if a parameter is defined. If not, it throws an error.
* @param parameter Parameter to be checked.
* @param parameterName Name of the parameter.
*/
export function requireDefinedParameter(parameter: any, parameterName: string) {
if (typeof parameter === "undefined") {
throw new Error(`${parameterName} must be defined`);
}
}
Loading

0 comments on commit 62edb43

Please sign in to comment.