Skip to content

Commit

Permalink
Add APIs for v4 Semaphore commitment and public key
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight committed Sep 17, 2024
1 parent 9ff6dda commit 853c173
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/client-web/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ParcnetClientProcessor implements ParcnetRPC {
this.subscriptions,
userIdentity
);
this.identity = new ParcnetIdentityProcessor();
this.identity = new ParcnetIdentityProcessor(userIdentity);
this.gpc = new ParcnetGPCProcessor(this.pods, dispatch, this.clientChannel);
}
}
10 changes: 10 additions & 0 deletions apps/client-web/src/client/identity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import type { ParcnetIdentityRPC } from "@parcnet-js/client-rpc";
import { encodePublicKey } from "@pcd/pod";
import type { Identity } from "@semaphore-protocol/identity";

export class ParcnetIdentityProcessor implements ParcnetIdentityRPC {
public constructor(private readonly v4Identity: Identity) {}

async getSemaphoreV3Commitment(): Promise<bigint> {
return BigInt(0);
}
async getSemaphoreV4Commitment(): Promise<bigint> {
return this.v4Identity.commitment;
}
async getSemaphoreV4PublicKey(): Promise<string> {
return encodePublicKey(this.v4Identity.publicKey);
}
}
60 changes: 54 additions & 6 deletions examples/test-app/src/apis/Identity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import { useParcnetClient } from "../hooks/useParcnetClient";

export function Identity(): ReactNode {
const { z, connected } = useParcnetClient();
const [commitment, setCommitment] = useState<bigint | undefined>(undefined);
const [commitmentV3, setCommitmentV3] = useState<bigint | undefined>(
undefined
);
const [commitmentV4, setCommitmentV4] = useState<bigint | undefined>(
undefined
);
const [publicKey, setPublicKey] = useState<string | undefined>(undefined);

return !connected ? null : (
<div>
<h1 className="text-xl font-bold mb-2">Identity</h1>
<div className="prose">
<div>
<p>
Getting the identity commitment is done like this:
Getting the v3 identity commitment is done like this:
<code className="block text-xs font-base rounded-md p-2">
await z.identity.getSemaphoreV3Commitment();
</code>
Expand All @@ -22,17 +28,59 @@ export function Identity(): ReactNode {
onClick={async () => {
try {
const commitment = await z.identity.getSemaphoreV3Commitment();
setCommitment(commitment);
setCommitmentV3(commitment);
} catch (e) {
console.log(e);
}
}}
label="Get identity commitment"
label="Get v3 identity commitment"
/>
{commitment !== undefined && (
<p>Commitment: {commitment.toString()}</p>
{commitmentV3 !== undefined && (
<p>Commitment: {commitmentV3.toString()}</p>
)}
</div>
<div>
<p>
Getting the v4 identity commitment is done like this:
<code className="block text-xs font-base rounded-md p-2">
await z.identity.getSemaphoreV4Commitment();
</code>
</p>
<TryIt
onClick={async () => {
try {
const commitment = await z.identity.getSemaphoreV4Commitment();
setCommitmentV4(commitment);
} catch (e) {
console.log(e);
}
}}
label="Get v4 identity commitment"
/>
{commitmentV4 !== undefined && (
<p>Commitment: {commitmentV4.toString()}</p>
)}
</div>
<div>
<p>
Getting the v4 identity public key is done like this:
<code className="block text-xs font-base rounded-md p-2">
await z.identity.getSemaphoreV4PublicKey();
</code>
</p>
<TryIt
onClick={async () => {
try {
const publicKey = await z.identity.getSemaphoreV4PublicKey();
setPublicKey(publicKey);
} catch (e) {
console.log(e);
}
}}
label="Get v4 public key"
/>
{publicKey !== undefined && <p>Public Key: {publicKey}</p>}
</div>
</div>
</div>
);
Expand Down
14 changes: 14 additions & 0 deletions packages/app-connector/src/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
[],
ParcnetRPCSchema.identity.getSemaphoreV3Commitment
);
},
getSemaphoreV4Commitment: async (): Promise<bigint> => {
return this.#typedInvoke(
"identity.getSemaphoreV4Commitment",
[],
ParcnetRPCSchema.identity.getSemaphoreV4Commitment
);
},
getSemaphoreV4PublicKey: async (): Promise<string> => {
return this.#typedInvoke(
"identity.getSemaphoreV4PublicKey",
[],
ParcnetRPCSchema.identity.getSemaphoreV4PublicKey
);
}
};
}
Expand Down
7 changes: 6 additions & 1 deletion packages/client-helpers/src/connection/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function getSchema(method: ParcnetRPCMethodName) {
return ParcnetRPCSchema.gpc.verify;
case "identity.getSemaphoreV3Commitment":
return ParcnetRPCSchema.identity.getSemaphoreV3Commitment;
case "identity.getSemaphoreV4Commitment":
return ParcnetRPCSchema.identity.getSemaphoreV4Commitment;
case "identity.getSemaphoreV4PublicKey":
return ParcnetRPCSchema.identity.getSemaphoreV4PublicKey;
case "pod.query":
return ParcnetRPCSchema.pod.query;
case "pod.insert":
Expand Down Expand Up @@ -135,7 +139,8 @@ async function handleMessage(
} catch (error) {
port.postMessage({
type: RPCMessageType.PARCNET_CLIENT_INVOKE_ERROR,
error: (error as Error).message
error: (error as Error).message,
serial: message.serial
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/client-rpc/src/rpc_interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface ParcnetGPCRPC {

export interface ParcnetIdentityRPC {
getSemaphoreV3Commitment: () => Promise<bigint>;
getSemaphoreV4Commitment: () => Promise<bigint>;
getSemaphoreV4PublicKey: () => Promise<string>;
}

export interface ParcnetPODRPC {
Expand Down
8 changes: 8 additions & 0 deletions packages/client-rpc/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export const ParcnetRPCSchema = {
getSemaphoreV3Commitment: {
input: v.tuple([]),
output: v.bigint()
},
getSemaphoreV4Commitment: {
input: v.tuple([]),
output: v.bigint()
},
getSemaphoreV4PublicKey: {
input: v.tuple([]),
output: v.string()
}
},
pod: {
Expand Down

0 comments on commit 853c173

Please sign in to comment.