diff --git a/packages/site/src/pages/index.tsx b/packages/site/src/pages/index.tsx index 4c68b20..caeca6a 100644 --- a/packages/site/src/pages/index.tsx +++ b/packages/site/src/pages/index.tsx @@ -20,7 +20,7 @@ import { setPkpIpfsCid, setSocialRecoveryPkpPublicKey, socialRecoveryLitAction, - getSocialRecoverySignature, + executeSocialRecovery, signWithPkp, get2FaPkpPublicKey, createUnsignedUserOp, @@ -50,8 +50,7 @@ import { import { useGoogleAuth } from '../hooks/useGoogleAuth'; const DEFAULT_LIT_ACTION = socialRecoveryLitAction; -const DEFAULT_LIT_ACTION_IPFS_CID = - 'QmXqnNtfcQMfTAqk8bWyR4n1Rtkkbgwg7rTPZLTKUYpd85'; +const DEFAULT_LIT_ACTION_IPFS_CID = ''; const Index = () => { const [state, dispatch] = useContext(MetaMaskContext); const [aaAddress, setAaAddress] = useState(''); @@ -226,11 +225,11 @@ const Index = () => { }); return; } - const socialRecoverySignature = await getSocialRecoverySignature({ + const socialRecoveryReceipt = await executeSocialRecovery({ pkpPublicKey: socialRecoveryPkpPublicKey, ipfsId: socialRecoveryIpfsCid, }); - console.log('socialRecoverySignature', socialRecoverySignature); + console.log('socialRecoveryReceipt', socialRecoveryReceipt); }; return ( diff --git a/packages/site/src/utils/lit.ts b/packages/site/src/utils/lit.ts index bc23bcf..2e03c8e 100644 --- a/packages/site/src/utils/lit.ts +++ b/packages/site/src/utils/lit.ts @@ -1,5 +1,11 @@ -// import * as LitJsSdk from '@lit-protocol/lit-node-client'; -const LitJsSdk = {} as any; +import * as LitJsSdk from '@lit-protocol/lit-node-client'; +// const LitJsSdk = {} as any; +import { BaseProvider } from '@metamask/providers'; +import { serialize, UnsignedTransaction } from '@ethersproject/transactions'; +import { ethers } from 'ethers'; +import { ChainId, changeNetwork } from './metamask'; + +// TODO: enable multiple accounts export const set2FaPkpPublicKey = async (pkpPublicKey: string) => { localStorage.setItem('2faPkpPublicKey', pkpPublicKey); }; @@ -52,36 +58,6 @@ export const getPkpIpfsCid = () => { return ''; }; -export const getSocialRecoverySignature = async ({ - pkpPublicKey, - ipfsId, -}: { - pkpPublicKey: string; - ipfsId: string; -}) => { - const litNodeClient = new LitJsSdk.LitNodeClient({ - litNetwork: 'serrano', - debug: true, - }); - console.log('litNodeClient', litNodeClient); - - await litNodeClient.connect(); - - const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain: 'mumbai' }); - - const results = await litNodeClient.executeJs({ - ipfsId, - authSig, - jsParams: { - publicKey: pkpPublicKey, - sigName: 'sig1', - }, - }); - console.log('results', results); - // const txParams = results.response as UnsignedTransaction; - return results.signatures.sig1.signature as string; -}; - const litActionCodeForSign = ` const go = async () => { const fromAddress = ethers.utils.computeAddress(publicKey); @@ -137,3 +113,51 @@ export const signWithPkp = async ({ return results.signatures.sig1.signature as string; }; + +export const executeSocialRecovery = async ({ + pkpPublicKey, + ipfsId, +}: { + pkpPublicKey: string; + ipfsId: string; +}) => { + await changeNetwork(ChainId.mumbai); + const litNodeClient = new LitJsSdk.LitNodeClient({ + litNetwork: 'serrano', + debug: true, + }); + console.log('litNodeClient', litNodeClient); + + await litNodeClient.connect(); + + const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain: 'mumbai' }); + + // often get timeout error... + // {"errorKind":"Timeout","errorCode":"NodeJsTimeoutError","status":502,"message":"There was a timeout error executing the Javascript for this action","correlationId":"lit_05c152724478","details":["Your function exceeded the maximum runtime of 30000ms and was terminated."]} + const results = await litNodeClient.executeJs({ + ipfsId, + authSig, + jsParams: { + publicKey: pkpPublicKey, + sigName: 'sig1', + }, + }); + console.log('results', results); + const socialRecoverytxParams = results.response as UnsignedTransaction; + const proverSignature = results.signatures.sig1.signature; + + const provider = new ethers.providers.Web3Provider( + window.ethereum as BaseProvider, + ); + const serializedTx = serialize(socialRecoverytxParams, proverSignature); + console.log('Execute Social Recovery!!!!!'); + try { + const tx = await provider.sendTransaction(serializedTx); + const receipt = await tx.wait(); + console.log('receipt', receipt); + return receipt; + } catch (e) { + console.log('error', e); + } + return ''; +}; diff --git a/packages/site/src/utils/socialRecoveryLitAction.ts b/packages/site/src/utils/socialRecoveryLitAction.ts index 5d78c4f..3beff15 100644 --- a/packages/site/src/utils/socialRecoveryLitAction.ts +++ b/packages/site/src/utils/socialRecoveryLitAction.ts @@ -3,7 +3,8 @@ export const socialRecoveryLitAction = ` // Please rewrite the following variables! const publicationId = '0x1f5c-0x48'; const guardianUserId = '0x01b411'; - const targetAddress = '0x951444F56EF94FeC42e8cDBeDef1A4Dc1D1ea63B'; + // Your AA Wallet Address + const targetAddress = ''; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { @@ -64,13 +65,11 @@ export const socialRecoveryLitAction = ` }); const txParams = { nonce: latestNonce, - gasPrice: '0x987b4b98', - gasLimit: '0x987b4b98', + gasPrice: '0x6fc23ac00', // 30gewi + gasLimit: '0xf4240', // 1000000wei max: 0.03eth to: targetAddress, - value: '0x38d7ea4c68000', - //0.001, chainId: 80001, - data: '0x', + data: '0x4deafa38', }; LitActions.setResponse({ response: JSON.stringify(txParams) }); const serializedTx = ethers.utils.serializeTransaction(txParams);