Add axios, try diff way of gen guard addr

This commit is contained in:
jacque006
2024-04-07 01:25:07 -04:00
parent dc0efd3ea7
commit b8d530cd2a
7 changed files with 60 additions and 76 deletions

View File

@@ -4,4 +4,4 @@
"emailAuthImpl": "0x1C76Aa365c17B40c7E944DcCdE4dC6e6D2A7b748",
"simpleWalletImpl": "0xabAA8B42d053a57DeC990906ebdF3efF6844A861",
"safeZkSafeZkEmailRecoveryPlugin": "0xFcfE6030952326c90fc615DDD15a3945f62AfCef"
}
}

View File

@@ -23,6 +23,7 @@
"wagmi": "^2.5.18"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/circomlibjs": "^0.1.6",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",

View File

@@ -62,8 +62,8 @@ export function ConfigureSafeModule() {
throw new Error('safe owner not found')
}
const { accountCode, accountCodeBytes } = await genAccountCode();
const guardianAddr = await getGuardianAddress(guardianEmail, accountCodeBytes);
const accountCode = await genAccountCode();
const guardianAddr = await getGuardianAddress(guardianEmail, accountCode);
const subject = getRequestGuardianSubject(address);
// TODO Should this be something else?
const previousOwnerInLinkedList = '0x1'

View File

@@ -83,7 +83,7 @@ export function PerformRecovery() {
throw new Error('guardian email not set')
}
const { accountCode } = await genAccountCode()
const accountCode = await genAccountCode()
const subject = getRequestGuardianSubject(simpleWalletAddress);
const { requestId } = await relayer.acceptanceRequest(
@@ -156,14 +156,14 @@ export function PerformRecovery() {
/>
</label>
<Button
disabled={!simpleWalletAddress || !guardianEmail}
disabled={!simpleWalletAddress || !guardianEmail || !!gurdianRequestId}
onClick={requestGuardian}>
TEST Request Guardian
</Button>
</div>
<div>
<Button onClick={checkGuardianAcceptance}>
Check for guardian acceptance
TEST Check for guardian acceptance
</Button>
</div>
<label>

View File

@@ -1,15 +1,11 @@
// TODO replace fetch
// import { get, post } from "axios"
class RelayerError extends Error {
constructor(msg: string) {
super(msg);
this.name = 'RelayerError';
}
}
import axios from "axios"
// Spec: https://www.notion.so/proofofemail/Email-Sender-Auth-c87063cd6cdc4c5987ea3bc881c68813#d7407d31e1354167be61612f5a16995b
// TODO Do we need to use bigints to prevent possible overflows?
// TODO Consider using a bigint for templateIdx as it *could* overflow JS number, but practically seems unlikely
type RequestIDData = {
request_id: number;
}
class Relayer {
private readonly apiRoute = 'api';
apiUrl: string;
@@ -18,29 +14,25 @@ class Relayer {
this.apiUrl = `${relayerUrl}${this.apiRoute}`
}
private async throwErrFromRes(res: Response) {
const msg = `${res.url} ${res.status} ${await res.text()}`;
throw new RelayerError(msg);
}
// Similar to a ping or health endpoint
async echo() {
const res = await fetch(`${this.apiUrl}/echo`);
if (!res.ok) {
await this.throwErrFromRes(res);
}
const res = await axios({
method: 'GET',
url: `${this.apiUrl}/echo`
})
return res.data;
}
async requestStatus(requestId: number) {
const res = await fetch(`${this.apiUrl}/requestStatus`, {
body: JSON.stringify({
// TODO type res body
const { data } = await axios({
method: 'GET',
url: `${this.apiUrl}/requestStatus`,
data: {
request_id: requestId
})
});
if (!res.ok) {
await this.throwErrFromRes(res);
}
return res.json();
}
})
return data;
}
async acceptanceRequest(
@@ -50,23 +42,19 @@ class Relayer {
templateIdx: number,
subject: string
): Promise<{ requestId: number }> {
const res = await fetch(`${this.apiUrl}/acceptanceRequest`, {
const { data } = await axios({
method: "POST",
body: JSON.stringify({
url: `${this.apiUrl}/acceptanceRequest`,
data: {
wallet_eth_addr: walletEthAddr,
guardian_email_addr: guardianEmailAddr,
account_code: accountCode,
template_idx: templateIdx,
subject,
})
});
if (!res.ok) {
await this.throwErrFromRes(res);
}
const { request_id: requestId } = await res.json();
return {
requestId,
}
}
})
const { request_id: requestId } = data;
return { requestId };
}
async recoveryRequest(
@@ -75,35 +63,30 @@ class Relayer {
templateIdx: number,
subject: string
) {
const res = await fetch(`${this.apiUrl}/recoveryRequest`, {
const {
request_id: requestId
} = await axios<unknown, RequestIDData>({
method: "POST",
body: JSON.stringify({
url: `${this.apiUrl}/recoveryRequest`,
data: {
wallet_eth_addr: walletEthAddr,
guardian_email_addr: guardianEmailAddr,
template_idx: templateIdx,
subject,
})
});
if (!res.ok) {
await this.throwErrFromRes(res);
}
const { request_id: requestId } = await res.json();
return {
requestId,
}
}
})
return { requestId };
}
async completeRequest(walletEthAddr: string) {
const res = await fetch(`${this.apiUrl}/completeRequest`, {
const data = await axios<unknown, unknown>({
method: "POST",
body: JSON.stringify({
url: `${this.apiUrl}/completeRequest`,
data: {
wallet_eth_addr: walletEthAddr,
})
});
if (!res.ok) {
await this.throwErrFromRes(res);
}
return res.json();
}
})
return data;
}
}

View File

@@ -28,24 +28,17 @@ export function bytesToHex(bytes: Uint8Array) {
.join("");
}
export async function genAccountCode(): Promise<{
accountCodeBytes: Uint8Array, accountCode: string
}> {
export async function genAccountCode(): Promise<string> {
const poseidon = await buildPoseidon();
const F = poseidon.F;
const accountCodeBytes: Uint8Array = F.random();
const accountCode = bytesToHex(accountCodeBytes);
return {
accountCodeBytes,
accountCode,
}
const accountCodeBytes: Uint8Array = poseidon.F.random();
return bytesToHex(accountCodeBytes);
}
export async function getGuardianAddress(guardianEmail: string, accountCodeBytes: Uint8Array) {
export async function getGuardianAddress(guardianEmail: string, accountCode: string) {
const poseidon = await buildPoseidon();
const emailField = bytes2fields(padStringToBytes(guardianEmail, 256), poseidon.F);
const guardianAddressBytes = poseidon([
...emailField, ...accountCodeBytes, 0
...emailField, accountCode, 0
]);
const guardianAddress: `0x${string}` = `0x${bytesToHex(guardianAddressBytes)}`
return guardianAddress;

View File

@@ -1784,6 +1784,13 @@
dependencies:
"@tanstack/query-core" "5.28.13"
"@types/axios@^0.14.0":
version "0.14.0"
resolved "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz#ec2300fbe7d7dddd7eb9d3abf87999964cafce46"
integrity sha512-KqQnQbdYE54D7oa/UmYVMZKq7CO4l8DEENzOKc4aBRwxCXSlJXGz83flFx5L7AWrOQnmuN3kVsRdt+GZPPjiVQ==
dependencies:
axios "*"
"@types/babel__core@^7.20.5":
version "7.20.5"
resolved "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz#3df15f27ba85319caa07ba08d0721889bb39c017"
@@ -2469,7 +2476,7 @@ available-typed-arrays@^1.0.7:
dependencies:
possible-typed-array-names "^1.0.0"
axios@^1.6.8:
axios@*, axios@^1.6.8:
version "1.6.8"
resolved "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
integrity sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==