-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #462 from social-tw/feat/UST-138-claim-reputaion-i…
…n-background [UST-138] [Frontend] Claim reputation in background
- Loading branch information
Showing
14 changed files
with
400 additions
and
6 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
48 changes: 48 additions & 0 deletions
48
packages/frontend/src/features/core/utils/genReportNonNullifierProof.ts
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { UserState } from '@unirep/core' | ||
import { toDecString } from '@unirep/core/src/Synchronizer' | ||
import { UnirepSocialCircuit } from '@unirep-app/circuits/dist/src/types' | ||
import { stringifyBigInts } from '@unirep/utils' | ||
import { ReportNonNullifierProof } from '@unirep-app/circuits' | ||
|
||
export async function genReportNonNullifierProof( | ||
userState: UserState, | ||
params: { | ||
reportId: string | ||
reportedEpochKey: bigint | ||
reportedEpoch: number | ||
nonce: number | ||
}, | ||
options: { | ||
epoch?: number | ||
attesterId?: bigint | string | ||
} = {}, | ||
) { | ||
const attesterId = toDecString( | ||
options.attesterId ?? userState.sync.attesterId, | ||
) | ||
const epoch = | ||
options.epoch ?? (await userState.latestTransitionedEpoch(attesterId)) | ||
|
||
const identitySecret = userState.id.secret | ||
|
||
const circuitInputs = { | ||
reported_epoch_key: params.reportedEpochKey, | ||
identity_secret: identitySecret, | ||
reported_epoch: params.reportedEpoch, | ||
current_epoch: epoch, | ||
current_nonce: params.nonce, | ||
attester_id: attesterId, | ||
chain_id: userState.chainId, | ||
} | ||
|
||
const results = await userState.prover.genProofAndPublicSignals( | ||
UnirepSocialCircuit.reportNonNullifierProof, | ||
stringifyBigInts(circuitInputs), | ||
) | ||
|
||
return new ReportNonNullifierProof( | ||
results.publicSignals, | ||
results.proof, | ||
userState.prover, | ||
) | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/frontend/src/features/core/utils/genReportNullifierProof.ts
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { UserState } from '@unirep/core' | ||
import { toDecString } from '@unirep/core/src/Synchronizer' | ||
import { genReportNullifier } from '@/features/core' | ||
import { UnirepSocialCircuit } from '@unirep-app/circuits/dist/src/types' | ||
import { stringifyBigInts } from '@unirep/utils' | ||
import { ReportNullifierProof } from '@unirep-app/circuits' | ||
|
||
export async function genReportNullifierProof( | ||
userState: UserState, | ||
params: { | ||
reportId: string | ||
nonce: number | ||
}, | ||
options: { | ||
epoch?: number | ||
attesterId?: bigint | string | ||
} = {}, | ||
) { | ||
const reportId = params.reportId | ||
|
||
const attesterId = toDecString( | ||
options.attesterId ?? userState.sync.attesterId, | ||
) | ||
const epoch = | ||
options.epoch ?? (await userState.latestTransitionedEpoch(attesterId)) | ||
|
||
const identitySecret = userState.id.secret | ||
|
||
const reportNullifier = genReportNullifier(identitySecret, reportId) | ||
|
||
const circuitInputs = { | ||
report_nullifier: reportNullifier, | ||
identity_secret: identitySecret, | ||
report_id: reportId, | ||
current_epoch: epoch, | ||
current_nonce: params.nonce, | ||
attester_id: attesterId, | ||
chain_id: userState.chainId, | ||
} | ||
|
||
const results = await userState.prover.genProofAndPublicSignals( | ||
UnirepSocialCircuit.reportNullifierProof, | ||
stringifyBigInts(circuitInputs), | ||
) | ||
|
||
return new ReportNullifierProof( | ||
results.publicSignals, | ||
results.proof, | ||
userState.prover, | ||
) | ||
} |
70 changes: 70 additions & 0 deletions
70
...src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useUserState } from '@/features/core' | ||
import { RepUserType } from '@/types/Report' | ||
import { useCallback, useEffect } from 'react' | ||
import { useWaitForTransactionReport } from '@/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport' | ||
import { useReportAdjucatorsReputation } from '@/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation' | ||
import { useReportEpochKeyRepuation } from '@/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation' | ||
import { isMyEpochKey } from '@/utils/helpers/epochKey' | ||
import { isMyAdjudicateNullifier } from '@/features/reporting/utils/helpers' | ||
|
||
export function useBackgroundReputationClaim() { | ||
const { data: reports } = useWaitForTransactionReport() | ||
const { mutateAsync: claimAdjucatorRepuation } = | ||
useReportAdjucatorsReputation() | ||
const { mutateAsync: claimEpochKeyRepuation } = useReportEpochKeyRepuation() | ||
const { userState } = useUserState() | ||
|
||
const processReports = useCallback(async () => { | ||
if (!reports || !userState) return | ||
|
||
for (const report of reports) { | ||
if (!report.respondentEpochKey) continue | ||
if ( | ||
isMyEpochKey( | ||
userState, | ||
report.reportEpoch, | ||
report.reportorEpochKey, | ||
) | ||
) { | ||
if (!report.reportorClaimedRep) { | ||
await claimEpochKeyRepuation({ | ||
reportId: report.reportId, | ||
reportedEpochKey: BigInt(report.reportorEpochKey), | ||
reportedEpoch: report.reportEpoch, | ||
repUserType: RepUserType.REPORTER, | ||
}) | ||
} | ||
} else if ( | ||
isMyEpochKey( | ||
userState, | ||
report.object.epoch, | ||
report.respondentEpochKey, | ||
) | ||
) { | ||
if (!report.respondentClaimedRep) { | ||
await claimEpochKeyRepuation({ | ||
reportId: report.reportId, | ||
reportedEpochKey: BigInt(report.respondentEpochKey), | ||
reportedEpoch: report.object.epoch, | ||
repUserType: RepUserType.POSTER, | ||
}) | ||
} | ||
} else if ( | ||
report.adjudicatorsNullifier?.some( | ||
(adj) => | ||
isMyAdjudicateNullifier( | ||
userState, | ||
report.reportId, | ||
adj.nullifier, | ||
) && !adj.claimed, | ||
) | ||
) { | ||
await claimAdjucatorRepuation(report.reportId) | ||
} | ||
} | ||
}, [reports, userState, claimEpochKeyRepuation, claimAdjucatorRepuation]) | ||
|
||
useEffect(() => { | ||
processReports() | ||
}, [reports, processReports]) | ||
} |
16 changes: 16 additions & 0 deletions
16
...d/src/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.ts
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { QueryKeys } from '@/constants/queryKeys' | ||
import { ReportService, useEpoch, useUserState } from '@/features/core' | ||
|
||
export function useWaitForTransactionReport() { | ||
const { getGuaranteedUserState } = useUserState() | ||
const { currentEpoch } = useEpoch() | ||
return useQuery({ | ||
queryKey: [QueryKeys.ReportsWaitingForTransaction, currentEpoch], | ||
queryFn: async () => { | ||
const userState = await getGuaranteedUserState() | ||
const reportService = new ReportService(userState) | ||
return reportService.fetchWaitFotTransactionReports() | ||
}, | ||
}) | ||
} |
49 changes: 49 additions & 0 deletions
49
...c/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation.ts
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { MutationKeys, QueryKeys } from '@/constants/queryKeys' | ||
import { | ||
useActionCount, | ||
useUserState, | ||
useUserStateTransition, | ||
useWeb3Provider, | ||
} from '@/features/core' | ||
import { genReportNullifierProof } from '@/features/core/utils/genReportNullifierProof' | ||
import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' | ||
import { relayClaimReputation } from '@/utils/api' | ||
import { RepUserType } from '@/types/Report' | ||
|
||
export function useReportAdjucatorsReputation() { | ||
const { stateTransition } = useUserStateTransition() | ||
const { getGuaranteedUserState } = useUserState() | ||
const actionCount = useActionCount() | ||
const { getGuaranteedProvider } = useWeb3Provider() | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationKey: [MutationKeys.ClaimAdjudicatorReputation], | ||
mutationFn: async (reportId: string) => { | ||
await stateTransition() | ||
const userState = await getGuaranteedUserState() | ||
const nonce = getEpochKeyNonce(actionCount) | ||
const { publicSignals, proof } = await genReportNullifierProof( | ||
userState, | ||
{ reportId, nonce: nonce }, | ||
) | ||
const result = await relayClaimReputation( | ||
reportId, | ||
RepUserType.VOTER, | ||
publicSignals, | ||
proof, | ||
) | ||
const provider = getGuaranteedProvider() | ||
await provider.waitForTransaction(result.message.txHash) | ||
await userState.waitForSync() | ||
|
||
return result | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: [QueryKeys.ReputationScore], | ||
}) | ||
}, | ||
}) | ||
} |
65 changes: 65 additions & 0 deletions
65
...nd/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation.ts
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { MutationKeys, QueryKeys } from '@/constants/queryKeys' | ||
import { | ||
useActionCount, | ||
useUserState, | ||
useUserStateTransition, | ||
useWeb3Provider, | ||
} from '@/features/core' | ||
import { genReportNonNullifierProof } from '@/features/core/utils/genReportNonNullifierProof' | ||
import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' | ||
import { relayClaimReputation } from '@/utils/api' | ||
import { RepUserType } from '@/types/Report' | ||
|
||
export function useReportEpochKeyRepuation() { | ||
const { stateTransition } = useUserStateTransition() | ||
const { getGuaranteedUserState } = useUserState() | ||
const actionCount = useActionCount() | ||
const { getGuaranteedProvider } = useWeb3Provider() | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationKey: [MutationKeys.ClaimEpochKeyReputation], | ||
mutationFn: async ({ | ||
reportId, | ||
reportedEpochKey, | ||
reportedEpoch, | ||
repUserType, | ||
}: { | ||
reportId: string | ||
reportedEpochKey: bigint | ||
reportedEpoch: number | ||
repUserType: RepUserType | ||
}) => { | ||
await stateTransition() | ||
const userState = await getGuaranteedUserState() | ||
const nonce = getEpochKeyNonce(actionCount) | ||
|
||
const { publicSignals, proof } = await genReportNonNullifierProof( | ||
userState, | ||
{ | ||
reportId, | ||
reportedEpochKey, | ||
reportedEpoch, | ||
nonce, | ||
}, | ||
) | ||
const result = await relayClaimReputation( | ||
reportId, | ||
repUserType, | ||
publicSignals, | ||
proof, | ||
) | ||
const provider = getGuaranteedProvider() | ||
await provider.waitForTransaction(result.message.txHash) | ||
await userState.waitForSync() | ||
|
||
return result | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: [QueryKeys.ReputationScore], | ||
}) | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.