From 0178f5d75911937d5b764a5a8714d676409fef88 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 16 Oct 2024 18:04:01 +0200 Subject: [PATCH] wip submit pubkey --- packages/secret-upload-contract/src/submit.js | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 packages/secret-upload-contract/src/submit.js diff --git a/packages/secret-upload-contract/src/submit.js b/packages/secret-upload-contract/src/submit.js new file mode 100644 index 0000000..00522ba --- /dev/null +++ b/packages/secret-upload-contract/src/submit.js @@ -0,0 +1,149 @@ +import { ethers } from "ethers"; +import abi from "../config/abi.js"; +import { generateKeys } from "../functions/secretpath/generateKeys"; +import { getPublicClientAddress } from "../functions/secretpath/getPublicClientAddress"; +import { constructPayload } from "../functions/secretpath/constructPayload"; +import { encryptPayload } from "../functions/secretpath/encryptPayload"; +import { hexlify } from "ethers/lib/utils"; +import { queryPubkey } from "../functions/query/queryPubkey"; + +// call unsafeGetSecretContractPubkey +export async function handleSubmit(e, setBallCount) { + e.preventDefault(); + // handlePlay(); + + const routing_contract = process.env.REACT_APP_SECRET_ADDRESS; + const routing_code_hash = process.env.REACT_APP_CODE_HASH; + const iface = new ethers.utils.Interface(abi); + const provider = new ethers.providers.Web3Provider(window.ethereum, "any"); + + const [myAddress] = await provider.send("eth_unsafeGetSecretContractPubkey", []); + + const { userPrivateKeyBytes, userPublicKeyBytes, sharedKey } = + await generateKeys(); + + const callbackSelector = iface.getSighash( + iface.getFunction("upgradeHandler") + ); + + console.log("callbackSelector: ", callbackSelector); + + const callbackGasLimit = 90000; + // The function name of the function that is called on the private contract + const handle = "request_pubkey"; + + // Data are the calldata/parameters that are passed into the contract + const data = JSON.stringify({ address: myAddress }); + + const chainId = (await provider.getNetwork()).chainId.toString(); + + const publicClientAddress = await getPublicClientAddress(chainId); + + const callbackAddress = publicClientAddress.toLowerCase(); + console.log("callback address: ", callbackAddress); + + // Payload construction + const payload = constructPayload( + data, + routing_contract, + routing_code_hash, + myAddress, + userPublicKeyBytes, + callbackAddress, + callbackSelector, + callbackGasLimit + ); + + const { + ciphertext, + payloadHash, + payloadSignature, + _info, + } = await encryptPayload( + payload, + sharedKey, + provider, + myAddress, + userPublicKeyBytes, + routing_code_hash, + handle, + callbackGasLimit, + iface, + callbackSelector + ); + + const functionData = iface.encodeFunctionData("send", [ + payloadHash, + myAddress, + routing_contract, + _info, + ]); + + const feeData = await provider.getFeeData(); + const maxFeePerGas = feeData.maxFeePerGas; + const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; + const gasFee = + maxFeePerGas && maxPriorityFeePerGas + ? maxFeePerGas.add(maxPriorityFeePerGas) + : await provider.getGasPrice(); + + let amountOfGas; + let my_gas = 150000; + + if (chainId === "4202") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2); + } + + if (chainId === "128123") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2); + my_gas = 15000000; + } + + if (chainId === "1287") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2); + my_gas = 15000000; + } + + if (chainId === "300") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2); + my_gas = 15000000; + } + + if (chainId === "5003") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(1000000).div(2); + my_gas = 1500000000; + } + + if (chainId === "80002") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); + my_gas = 200000; + } + + if (chainId === "1995") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); + my_gas = 200000; + } + + if (chainId === "713715") { + amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2); + my_gas = 200000; + } + + else { + amountOfGas = gasFee.mul(callbackGasLimit).mul(3).div(2); + } + + const tx_params = { + gas: hexlify(my_gas), + to: publicClientAddress, + from: myAddress, + value: hexlify(amountOfGas), + data: functionData, + }; + + const txHash = await provider.send("eth_sendTransaction", [tx_params]); + const publicKey = await queryPubkey(); + setBallCount(publicKey); // Directly set the new ball count from the query. + console.log("Public key of Private secret contract:", publicKey); + console.log(`Transaction Hash: ${txHash}`); +}