-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
459 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.