Skip to content

Commit

Permalink
(feat) store identicall secret
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMonkeyCoder committed Apr 21, 2024
1 parent ff9ca7f commit 1c2032b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 73 deletions.
92 changes: 46 additions & 46 deletions packages/nextjs/app/nillion-compute/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import React, { useCallback, useEffect, useState } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import type { NextPage } from "next";
import { useAccount } from "wagmi";
Expand Down Expand Up @@ -31,12 +31,12 @@ const Home: NextPage = () => {
const [programId, setProgramId] = useState<string | null>(null);
const [computeResult, setComputeResult] = useState<string | null>(null);
const [identifier, setIdentifier] = useState("");

const [brightId, setBrightId] = useState("");
const [storedSecretsNameToStoreId, setStoredSecretsNameToStoreId] = useState<StringObject>({
my_int1: null,
my_int2: null,
});
const [parties] = useState<string[]>(["Party1"]);
const [parties] = useState<string[]>(["Responder0"]);
const [outputs] = useState<string[]>(["my_output"]);

// connect to snap
Expand All @@ -62,11 +62,11 @@ const Home: NextPage = () => {
useEffect(() => {
const pArg = searchParams.get("p");
if (pArg) {
setProgramId(pArg);
setProgramId(decodeURIComponent(pArg));
}
const iArg = searchParams.get("i");
if (iArg) {
setIdentifier(iArg);
setIdentifier(decodeURIComponent(iArg));
}
}, [searchParams]);

Expand Down Expand Up @@ -122,10 +122,18 @@ const Home: NextPage = () => {
) {
if (programId) {
const partyName = parties[0];

const encoder = new TextEncoder();
const data = encoder.encode(secretValue);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer)); // Convert buffer to byte array
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, "0")).join(""); // Convert bytes to hex string
const secretValueParsed = BigInt("0x" + hashHex).toString();

await storeSecretsInteger(
nillion,
nillionClient,
[{ name: secretName, value: secretValue }],
[{ name: secretName, value: secretValueParsed }],
programId,
partyName,
permissionedUserIdForRetrieveSecret ? [permissionedUserIdForRetrieveSecret] : [],
Expand All @@ -151,6 +159,8 @@ const Home: NextPage = () => {
}
}

const key = useMemo(() => "r0_response", []);

return (
<>
<div className="flex items-center flex-col pt-10">
Expand Down Expand Up @@ -238,50 +248,40 @@ const Home: NextPage = () => {
</div>
) : (
<div>
<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center max-w-m rounded-3xl my-2">
<h1 className="text-xl">Step 1: Store a Nada program</h1>
<div>
{programName} program stored <br />
<span className="flex">
<CopyString str={programId} start={5} end={programName.length + 5} textBefore="program_id: " />
</span>
</div>
{/*<CodeSnippet program_name={programName} />*/}
</div>

<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center w-full rounded-3xl my-2 justify-between">
<h1 className="text-xl">
Step 2: Store secret integers with program bindings to the {programName} program
</h1>
<h1 className="text-xl">Investigating person with identifier {identifier}</h1>
<p>
Submit the BrightID that you know from this person. The BrightID you enter is hashed before being
sent to the server
</p>

<div className="flex flex-row w-full justify-between items-center my-10 mx-10">
{Object.keys(storedSecretsNameToStoreId).map(key => (
<div className="flex-1 px-2" key={key}>
{!!storedSecretsNameToStoreId[key] && userKey ? (
<>
<RetrieveSecretCommand
secretType="SecretInteger"
userKey={userKey}
storeId={storedSecretsNameToStoreId[key]}
secretName={key}
/>
<button
className="btn btn-sm btn-primary mt-4"
onClick={() => handleRetrieveInt(key, storedSecretsNameToStoreId[key])}
>
👀 Retrieve SecretInteger
</button>
</>
) : (
<SecretForm
<div className="flex flex-row w-full justify-between items-center mx-10">
<div className="flex-1 px-2">
{!!storedSecretsNameToStoreId[key] && userKey ? (
<>
<RetrieveSecretCommand
secretType="SecretInteger"
userKey={userKey}
storeId={storedSecretsNameToStoreId[key]}
secretName={key}
onSubmit={handleSecretFormSubmit}
isDisabled={!programId}
secretType="number"
/>
)}
</div>
))}
<button
className="btn btn-sm btn-primary mt-4"
onClick={() => handleRetrieveInt(key, storedSecretsNameToStoreId[key])}
>
👀 Retrieve SecretInteger
</button>
</>
) : (
<SecretForm
secretName={key}
hidePermissions={true}
onSubmit={handleSecretFormSubmit}
isDisabled={!programId}
secretType="text"
/>
)}
</div>
</div>
</div>

Expand Down
24 changes: 1 addition & 23 deletions packages/nextjs/components/nillion/SecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,8 @@ const SecretForm: React.FC<SecretFormProps> = ({
return loading ? (
"Storing secret..."
) : (
<form onSubmit={handleSubmit} className={isDisabled ? "opacity-50" : ""}>
{!customSecretName && <h1>Store secret: {secretName}</h1>}
{customSecretName && (
<div>
<label htmlFor="secret" className="block text-sm font-medium text-gray-700">
Set {itemName} name
</label>
<input
id="secret"
value={secretNameFromForm}
onChange={e => setSecretNameFromForm(e.target.value)}
required
disabled={isDisabled}
className={`mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm ${
isDisabled ? "cursor-not-allowed bg-gray-100" : "bg-white"
}`}
/>
</div>
)}

<form onSubmit={handleSubmit} className={`text-black ${isDisabled ? "opacity-50" : ""}`}>
<div>
<label htmlFor="secret" className="block text-sm font-medium text-gray-700">
Set {itemName} value
</label>
<input
type={secretType}
id="secret"
Expand Down
Binary file modified packages/nextjs/public/programs/identicall.nada.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/nextjs/public/programs/identicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def nada_main():

# 1. Parties initialization
responders = initialize_responders(nr_responders)
outparty = responders[0]
outparty = responders[nr_responders - 1]

# 2. Inputs initialization
responses = inputs_initialization(nr_responders, responders)
Expand Down
5 changes: 3 additions & 2 deletions packages/nextjs/utils/nillion/storeSecretsInteger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export async function storeSecretsInteger(

// iterate through secretsToStore, inserting each into the secrets object
for (const secret of secretsToStore) {
console.log({ secret });
// create new SecretInteger with value cast to string
const newSecret = nillion.Secret.new_integer(secret.value.toString());

const newSecret = nillion.Secret.new_unsigned_integer(secret.value.toString());
console.log({ newSecret });
// insert the SecretInteger into secrets object
secrets.insert(secret.name, newSecret);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nillion/next-project-programs/src/identicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def nada_main():

# 1. Parties initialization
responders = initialize_responders(nr_responders)
outparty = responders[0]
outparty = responders[nr_responders - 1]

# 2. Inputs initialization
responses = inputs_initialization(nr_responders, responders)
Expand Down
Binary file modified packages/nillion/next-project-programs/target/identicall.nada.bin
Binary file not shown.

0 comments on commit 1c2032b

Please sign in to comment.