Skip to content

Commit

Permalink
execute social recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Nobuhito Kurose committed Apr 15, 2023
1 parent 555eb0b commit d644b2d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
9 changes: 4 additions & 5 deletions packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
setPkpIpfsCid,
setSocialRecoveryPkpPublicKey,
socialRecoveryLitAction,
getSocialRecoverySignature,
executeSocialRecovery,
signWithPkp,
get2FaPkpPublicKey,
createUnsignedUserOp,
Expand Down Expand Up @@ -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('');
Expand Down Expand Up @@ -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 (
<Container>
Expand Down
88 changes: 56 additions & 32 deletions packages/site/src/utils/lit.ts
Original file line number Diff line number Diff line change
@@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 '';
};
11 changes: 5 additions & 6 deletions packages/site/src/utils/socialRecoveryLitAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d644b2d

Please sign in to comment.