Skip to content

Commit

Permalink
refactor: invoke constructor in deserialize funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Sep 25, 2023
1 parent 5ee965d commit ba1358d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 43 deletions.
4 changes: 3 additions & 1 deletion packages/eddsa-pcd/src/EdDSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export async function serialize(
}

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

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

export function getDisplayOptions(pcd: EdDSAPCD): DisplayOptions {
Expand Down
18 changes: 10 additions & 8 deletions packages/halo-nonce-pcd/src/HaLoNoncePCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
PCD,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import { ec } from "elliptic";
import { sha256 } from "js-sha256";
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function prove(args: HaLoNoncePCDArgs): Promise<HaLoNoncePCD> {

const claim: HaLoNoncePCDClaim = {
nonce: parseInt(args.rnd.value.substring(0, 8), 16),
pubkeyHex: args.pk2.value,
pubkeyHex: args.pk2.value
};

if (isNaN(claim.nonce)) {
Expand All @@ -107,7 +107,7 @@ export async function prove(args: HaLoNoncePCDArgs): Promise<HaLoNoncePCD> {

const proof: HaLoNoncePCDProof = {
signedDigest: args.rnd.value,
cleanedSignature: cutSig,
cleanedSignature: cutSig
};

return new HaLoNoncePCD(uuid(), claim, proof);
Expand All @@ -128,7 +128,7 @@ export async function verify(pcd: HaLoNoncePCD): Promise<boolean> {
.update(
Buffer.concat([
Buffer.from([0x19]),
Buffer.from("Attest counter pk2:\n", "utf8"),
Buffer.from("Attest counter pk2:\n", "utf8")
])
)
.update(rndBuf)
Expand All @@ -149,17 +149,19 @@ export async function serialize(
): Promise<SerializedPCD<HaLoNoncePCD>> {
return {
type: HaLoNoncePCDTypeName,
pcd: JSON.stringify(pcd),
pcd: JSON.stringify(pcd)
} as SerializedPCD<HaLoNoncePCD>;
}

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

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

export function getDisplayOptions(pcd: HaLoNoncePCD): DisplayOptions {
return {
displayName: "halo-nonce-" + pcd.id.substring(0, 4),
displayName: "halo-nonce-" + pcd.id.substring(0, 4)
};
}

Expand All @@ -181,5 +183,5 @@ export const HaLoNoncePCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
12 changes: 7 additions & 5 deletions packages/rsa-pcd/src/RSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
PCD,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import JSONBig from "json-bigint";
import NodeRSA from "node-rsa";
Expand Down Expand Up @@ -92,18 +92,20 @@ export async function verify(pcd: RSAPCD): Promise<boolean> {
export async function serialize(pcd: RSAPCD): Promise<SerializedPCD<RSAPCD>> {
return {
type: RSAPCDTypeName,
pcd: JSONBig().stringify(pcd),
pcd: JSONBig().stringify(pcd)
} as SerializedPCD<RSAPCD>;
}

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

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

export function getDisplayOptions(pcd: RSAPCD): DisplayOptions {
return {
header: "RSA Signature",
displayName: "rsa-sig-" + pcd.id.substring(0, 4),
displayName: "rsa-sig-" + pcd.id.substring(0, 4)
};
}

Expand All @@ -122,5 +124,5 @@ export const RSAPCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
22 changes: 9 additions & 13 deletions packages/rsa-pcd/test/RSAPCD.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { v4 as uuid } from "uuid";
import { RSAPCD, RSAPCDPackage } from "../src";

async function copyPcd(pcd: RSAPCD): Promise<RSAPCD> {
return await RSAPCDPackage.deserialize(
await (
await RSAPCDPackage.serialize(pcd)
).pcd
);
return RSAPCDPackage.deserialize((await RSAPCDPackage.serialize(pcd)).pcd);
}

describe("RSA signature PCD should work", function () {
Expand All @@ -25,16 +21,16 @@ describe("RSA signature PCD should work", function () {
pcd = await RSAPCDPackage.prove({
privateKey: {
argumentType: ArgumentTypeName.String,
value: exportedKey,
value: exportedKey
},
signedMessage: {
argumentType: ArgumentTypeName.String,
value: message,
value: message
},
id: {
argumentType: ArgumentTypeName.String,
value: undefined,
},
value: undefined
}
});

expect(await copyPcd(pcd)).to.deep.eq(pcd);
Expand All @@ -47,16 +43,16 @@ describe("RSA signature PCD should work", function () {
pcd = await RSAPCDPackage.prove({
privateKey: {
argumentType: ArgumentTypeName.String,
value: exportedKey,
value: exportedKey
},
signedMessage: {
argumentType: ArgumentTypeName.String,
value: message,
value: message
},
id: {
argumentType: ArgumentTypeName.String,
value: customId,
},
value: customId
}
});

expect(pcd.id).to.eq(customId);
Expand Down
4 changes: 3 additions & 1 deletion packages/semaphore-group-pcd/src/SemaphoreGroupPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<SemaphoreGroupPCD> {
return JSONBig().parse(serialized);
const { id, claim, proof } = JSONBig().parse(serialized) as SemaphoreGroupPCD;

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

export function getDisplayOptions(pcd: SemaphoreGroupPCD): DisplayOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<SemaphoreSignaturePCD> {
return JSONBig().parse(serialized);
const { id, claim, proof } = JSONBig().parse(
serialized
) as SemaphoreSignaturePCD;

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

export function getDisplayOptions(pcd: SemaphoreSignaturePCD): DisplayOptions {
Expand Down
30 changes: 16 additions & 14 deletions packages/webauthn-pcd/src/WebAuthnPCD.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
arrayBufferToBase64,
base64ToArrayBuffer,
base64ToArrayBuffer
} from "@pcd/passport-crypto/src/utils";
import { DisplayOptions, PCD, PCDPackage, SerializedPCD } from "@pcd/pcd-types";
import { startAuthentication } from "@simplewebauthn/browser";
import {
generateAuthenticationOptions,
verifyAuthenticationResponse,
verifyAuthenticationResponse
} from "@simplewebauthn/server";
import {
AuthenticationResponseJSON,
AuthenticatorDevice,
AuthenticatorTransportFuture,
AuthenticatorTransportFuture
} from "@simplewebauthn/typescript-types";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -80,9 +80,9 @@ export async function prove(args: WebAuthnPCDArgs): Promise<WebAuthnPCD> {
allowCredentials: [
{
id: args.authenticator.credentialID,
type: "public-key",
},
],
type: "public-key"
}
]
});
const authenticationResponseJSON = await startAuthentication(
authenticationOptions
Expand All @@ -96,9 +96,9 @@ export async function prove(args: WebAuthnPCDArgs): Promise<WebAuthnPCD> {
args.authenticator.credentialPublicKey
),
counter: args.authenticator.counter,
transports: args.authenticator.transports,
transports: args.authenticator.transports
},
challenge: args.challenge,
challenge: args.challenge
};
const proof = authenticationResponseJSON;
return new WebAuthnPCD(uuid(), claim, proof);
Expand All @@ -118,8 +118,8 @@ export async function verify(pcd: WebAuthnPCD): Promise<boolean> {
pcd.claim.credentialDetails.credentialPublicKey
),
counter: pcd.claim.credentialDetails.counter,
transports: pcd.claim.credentialDetails.transports,
},
transports: pcd.claim.credentialDetails.transports
}
});
return verified;
}
Expand All @@ -129,18 +129,20 @@ export async function serialize(
): Promise<SerializedPCD<WebAuthnPCD>> {
return {
type: WebAuthnPCDTypeName,
pcd: JSONBig().stringify(pcd),
pcd: JSONBig().stringify(pcd)
} as SerializedPCD<WebAuthnPCD>;
}

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

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

export function getDisplayOptions(pcd: WebAuthnPCD): DisplayOptions {
return {
header: "WebAuthn Credential Signature",
displayName: "webauthn-" + pcd.id.substring(0, 4),
displayName: "webauthn-" + pcd.id.substring(0, 4)
};
}

Expand All @@ -159,5 +161,5 @@ export const WebAuthnPCDPackage: PCDPackage<
serialize,
deserialize,
renderCardBody: WebAuthnCardBody,
getDisplayOptions,
getDisplayOptions
};

0 comments on commit ba1358d

Please sign in to comment.