Skip to content

Commit

Permalink
feat: updated to fhevmjs 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jatZama committed Jun 30, 2024
1 parent 41fc847 commit 150e5b5
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 130 deletions.
33 changes: 22 additions & 11 deletions app/components/Devnet/Devnet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ const toHexString = (bytes: Uint8Array) =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");

export const Devnet = () => {
const [handles, setHandles] = useState<Uint8Array[]>([]);
const [encryption, setEncryption] = useState<Uint8Array>();
const [token, setToken] =
useState<ReturnType<typeof instance.generatePublicKey>>();
const [eip712, setEip712] =
useState<ReturnType<typeof instance.createEIP712>>();
const instance = getInstance();

useEffect(() => {
setEncryption(instance.encrypt32(1337));
setToken(
instance.generatePublicKey({
verifyingContract: "0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482",
})
const { handles, inputProof } = instance
.createEncryptedInput(
"0x309cf2aae85ad8a1db70ca88cfd4225bf17a7456",
"0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482"
)
.add64(32)
.encrypt();
setHandles(handles);
setEncryption(inputProof);
const { publicKey } = instance.generateKeypair();
const eip = instance.createEIP712(
publicKey,
"0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482"
);
setEip712(eip);
}, [instance]);

return (
Expand All @@ -27,14 +37,15 @@ export const Devnet = () => {
<dt className="Devnet__title">This is an encryption of 1337:</dt>
<dd className="Devnet__dd">
<pre className="Devnet__pre">
{encryption && toHexString(encryption)}
Handle: {handles.length && toHexString(handles[0])}
</pre>
<pre className="Devnet__pre">
Input Proof: {encryption && toHexString(encryption)}
</pre>
</dd>
<dt className="Devnet__title">And this is a EIP-712 token</dt>
<dd className="Devnet__dd">
<pre className="Devnet__pre">
{token && JSON.stringify(token.eip712)}
</pre>
<pre className="Devnet__pre">{eip712 && JSON.stringify(eip712)}</pre>
</dd>
</dl>
</div>
Expand Down
64 changes: 34 additions & 30 deletions app/fhevmjs.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
import { BrowserProvider, AbiCoder } from "ethers";
import { isAddress } from "ethers";
import { initFhevm, createInstance, FhevmInstance } from "fhevmjs";

const FHE_LIB_ADDRESS = "0x000000000000000000000000000000000000005d";
export type Keypair = {
publicKey: string;
privateKey: string;
signature: string;
};

type Keypairs = {
[key: string]: {
[key: string]: Keypair;
};
};

export const init = async () => {
await initFhevm();
};

let instancePromise: Promise<FhevmInstance>;
let instance: FhevmInstance;

const keypairs: Keypairs = {};

export const createFhevmInstance = async () => {
const provider = new BrowserProvider(window.ethereum);
const network = await provider.getNetwork();
const chainId = +network.chainId.toString();
const ret = await provider.call({
to: FHE_LIB_ADDRESS,
// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library
data: "0xd9d47bb001",
});
const decoded = AbiCoder.defaultAbiCoder().decode(["bytes"], ret);
const publicKey = decoded[0];
instance = await createInstance({ chainId, publicKey });
if (instancePromise) return instancePromise;
instancePromise = createInstance({ network: window.ethereum });
instance = await instancePromise;
};

export const getSignature = async (
export const setKeypair = (
contractAddress: string,
userAddress: string
userAddress: string,
keypair: Keypair
) => {
if (getInstance().hasKeypair(contractAddress)) {
return getInstance().getPublicKey(contractAddress)!;
} else {
const { publicKey, eip712 } = getInstance().generatePublicKey({
verifyingContract: contractAddress,
});
const params = [userAddress, JSON.stringify(eip712)];
const signature: string = await window.ethereum.request({
method: "eth_signTypedData_v4",
params,
});
getInstance().setSignature(contractAddress, signature);
return { signature, publicKey };
}
if (!isAddress(contractAddress) || !isAddress(userAddress)) return;
keypairs[userAddress][contractAddress] = keypair;
};

export const getKeypair = (
contractAddress: string,
userAddress: string
): Keypair | null => {
if (!isAddress(contractAddress) || !isAddress(userAddress)) return null;
return keypairs[userAddress]
? keypairs[userAddress][contractAddress] || null
: null;
};

export const getInstance = () => {
export const getInstance = (): FhevmInstance => {
return instance;
};
Loading

0 comments on commit 150e5b5

Please sign in to comment.