Skip to content
This repository was archived by the owner on Oct 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions components/bounty/issue-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";

import { useTranslation } from "next-i18next";

import IssueEditTag from "components/bounty/issue-edit-tag";
import Button from "components/button";
import CustomContainer from "components/custom-container";
import { IFilesProps } from "components/drag-and-drop";
Expand All @@ -16,8 +17,6 @@ import { BODY_CHARACTERES_LIMIT } from "helpers/contants";
import useApi from "x-hooks/use-api";
import { useBounty } from "x-hooks/use-bounty";

import IssueEditTag from "./issue-edit-tag";

interface issueBodyProps {
isEditIssue: boolean;
cancelEditIssue: () => void;
Expand Down
5 changes: 2 additions & 3 deletions components/bounty/issue-edit-tag.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { useEffect } from "react";
import { Col, Row } from "react-bootstrap";
import ReactSelect from "react-select";

import { useTranslation } from "next-i18next";

import { PROGRAMMING_LANGUAGES } from "assets/bounty-labels";

import BountyTags from "components/bounty/bounty-tags";
import { ContextualSpan } from "components/contextual-span";
import ReactSelect from "components/react-select";

import { useAppState } from "contexts/app-state";

import { MAX_TAGS } from "helpers/contants";

import BountyTags from "./bounty-tags";

interface IssueEditTagProps {
isEdit: boolean;
selectedTags: string[];
Expand Down
16 changes: 8 additions & 8 deletions pages/[network]/[chain]/bounty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import BountyHero from "components/bounty-hero";
import FundingSection from "components/bounty/funding-section";
import IssueBody from "components/bounty/issue-body";
import TabSections from "components/bounty/tabs-sections";
import CustomContainer from "components/custom-container";
import If from "components/If";
import IssueComments from "components/issue-comments";
import PageActions from "components/page-actions";

import {useAppState} from "contexts/app-state";
import {BountyEffectsProvider} from "contexts/bounty-effects";

import { IM_AM_CREATOR_ISSUE } from "helpers/contants";

import { useAuthentication } from "x-hooks/use-authentication";
import {useBounty} from "x-hooks/use-bounty";
import useOctokit from "x-hooks/use-octokit";
Expand All @@ -27,20 +28,19 @@ export default function PageIssue() {
const [commentsIssue, setCommentsIssue] = useState([]);
const [isRepoForked, setIsRepoForked] = useState<boolean>();
const [isEditIssue, setIsEditIssue] = useState<boolean>(false);
const { signMessageIfCreatorIssue } = useAuthentication();

const {state} = useAppState();

const { getUserRepository } = useOctokit();
const { signMessage } = useAuthentication();

const { id } = router.query;

async function handleEditIssue() {
const isCreator = await signMessageIfCreatorIssue()

if(isCreator){
setIsEditIssue(true)
}
signMessage(IM_AM_CREATOR_ISSUE)
.then(() => {
setIsEditIssue(true);
})
.catch(error => console.debug(error));
}

function handleCancelEditIssue() {
Expand Down
133 changes: 50 additions & 83 deletions x-hooks/use-authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {changeConnectingGH, changeSpinners, changeWalletSpinnerTo} from "context
import { addToast } from "contexts/reducers/change-toaster";
import {changeReAuthorizeGithub} from "contexts/reducers/update-show-prop";

import { IM_AM_CREATOR_ISSUE } from "helpers/contants";
import {IM_AN_ADMIN, NOT_AN_ADMIN} from "helpers/contants";
import decodeMessage from "helpers/decode-message";

Expand Down Expand Up @@ -274,99 +273,68 @@ export function useAuthentication() {
dispatch(changeReAuthorizeGithub(!!expirationStorage.value && new Date(expirationStorage.value) < new Date()));
}

async function signMessageIfCreatorIssue() {
return new Promise(async (resolve, reject) => {
console.log(`signMessageIfCreatorIssue()`, state.connectedChain, state.currentUser?.walletAddress)

if (
!state?.currentUser?.walletAddress ||
!state?.connectedChain?.id ||
!state?.currentBounty?.data?.creatorAddress
)
return reject("error to get data");

if (decodeMessage(state?.connectedChain?.id,
IM_AM_CREATOR_ISSUE,
state?.currentUser?.signature,
state?.currentBounty?.data?.creatorAddress))
return resolve(true)

if (
state?.currentUser?.walletAddress?.toLowerCase() ===
state?.currentBounty?.data?.creatorAddress?.toLowerCase()
)
_signMessage(IM_AM_CREATOR_ISSUE)
.then((r) => {
dispatch(changeCurrentUserSignature(r));
sessionStorage.setItem(`currentSignature`, r || '');
sessionStorage.setItem(`currentChainId`, state?.connectedChain?.id || '0');
resolve(true)
})
.catch(e => {
console.error(`ERROR`, e);
reject(e)
})
else {
sessionStorage.setItem(`currentSignature`, '');
sessionStorage.setItem(`currentChainId`, '');
return reject('error not owner of this bounty')
function signMessage(message?: string) {
return new Promise<string>(async (resolve, reject) => {
if (!state?.currentUser?.walletAddress ||
!state?.connectedChain?.id ||
state.Service?.starting ||
state.spinners?.signingMessage) {
reject("Wallet not connected, service not started or already signing a message");
return;
}
})
}

async function signMessage(message?: string) {
if (!state?.currentUser?.walletAddress ||
!state?.connectedChain?.id ||
state.Service?.starting ||
state.spinners?.signingMessage)
return undefined;
const currentWallet = state?.currentUser?.walletAddress?.toLowerCase();
const isAdminUser = currentWallet === publicRuntimeConfig?.adminWallet?.toLowerCase();

const currentWallet = state?.currentUser?.walletAddress?.toLowerCase();
const isAdminUser = currentWallet === publicRuntimeConfig?.adminWallet?.toLowerCase();

if (!isAdminUser && state.connectedChain?.name === "unknown") {
dispatch(addToast({
type: "warning",
title: "Unsupported chain",
content: "To sign a message, connect to a supported chain",
}));
if (!isAdminUser && state.connectedChain?.name === "unknown") {
dispatch(addToast({
type: "warning",
title: "Unsupported chain",
content: "To sign a message, connect to a supported chain",
}));

return undefined;
}
reject("Unsupported chain");
return;
}

const messageToSign = message || (isAdminUser ? IM_AN_ADMIN : NOT_AN_ADMIN);
const messageToSign = message || (isAdminUser ? IM_AN_ADMIN : NOT_AN_ADMIN);

const storedSignature = sessionStorage.getItem("currentSignature");
const storedSignature = sessionStorage.getItem("currentSignature");

if (decodeMessage(state?.connectedChain?.id,
messageToSign,
storedSignature || state?.currentUser?.signature,
currentWallet)) {
if (storedSignature)
dispatch(changeCurrentUserSignature(storedSignature));
else
sessionStorage.setItem("currentSignature", state?.currentUser?.signature);
if (decodeMessage(state?.connectedChain?.id,
messageToSign,
storedSignature || state?.currentUser?.signature,
currentWallet)) {
if (storedSignature)
dispatch(changeCurrentUserSignature(storedSignature));
else
sessionStorage.setItem("currentSignature", state?.currentUser?.signature);

resolve(storedSignature || state?.currentUser?.signature);
return;
}

return storedSignature || state?.currentUser?.signature;
}
dispatch(changeSpinners.update({ signingMessage: true }));

dispatch(changeSpinners.update({ signingMessage: true }));
await _signMessage(messageToSign)
.then(signature => {
dispatch(changeSpinners.update({ signingMessage: false }));

return _signMessage(messageToSign)
.then(signature => {
dispatch(changeSpinners.update({ signingMessage: false }));
if (signature) {
dispatch(changeCurrentUserSignature(signature));
sessionStorage.setItem("currentSignature", signature);

resolve(signature);
return;
}

if (signature) {
dispatch(changeCurrentUserSignature(signature));
sessionStorage.setItem("currentSignature", signature);

return signature;
}
dispatch(changeCurrentUserSignature(undefined));
sessionStorage.removeItem("currentSignature");

dispatch(changeCurrentUserSignature(undefined));
sessionStorage.removeItem("currentSignature");
throw new Error("Message not signed");
});
reject("Message not signed");
return;
});
});
}

return {
Expand All @@ -380,7 +348,6 @@ export function useAuthentication() {
listenToAccountsChanged,
updateCurrentUserLogin,
verifyReAuthorizationNeed,
signMessageIfCreatorIssue,
signMessage
}
}