-
Notifications
You must be signed in to change notification settings - Fork 57
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 #212 from reborn-sama/main
Feature: modify wallet address
- Loading branch information
Showing
10 changed files
with
401 additions
and
12 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
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,26 @@ | ||
import React, { SVGProps } from "react"; | ||
|
||
const HeroiconsCog6Tooth = (props: SVGProps<SVGSVGElement>) => { | ||
return ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="1em" | ||
height="1em" | ||
viewBox="0 0 24 24" | ||
{...props} | ||
> | ||
<g | ||
fill="none" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="1.5" | ||
> | ||
<path d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87q.11.06.22.127c.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a8 8 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a7 7 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a7 7 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a7 7 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124q.108-.066.22-.128c.332-.183.582-.495.644-.869z"></path> | ||
<path d="M15 12a3 3 0 1 1-6 0a3 3 0 0 1 6 0"></path> | ||
</g> | ||
</svg> | ||
); | ||
}; | ||
|
||
export default HeroiconsCog6Tooth; |
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
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,102 @@ | ||
import React, { useEffect, useMemo } from "react"; | ||
import StepCard from "@site/src/components/StepCard"; | ||
import { ArrowRightCircleIcon } from "lucide-react"; | ||
import { useAccount, useSigner } from "wagmi"; | ||
import { useMutation } from "react-query"; | ||
import { get } from "lodash-es"; | ||
import useAuth from "@site/src/hooks/useAuth"; | ||
import { changeWallet } from "@site/src/api/wallet-auth"; | ||
import { useHistory } from "@docusaurus/router"; | ||
import Spinner from "@site/src/components/ui/Spinner"; | ||
import Translate from "@docusaurus/Translate"; | ||
import { toast } from "react-hot-toast"; | ||
|
||
const formatChainError = (message: string) => { | ||
if (message.includes("user rejected signing")) { | ||
return "User rejected signing"; | ||
} | ||
}; | ||
|
||
const StepChangeWallet = () => { | ||
const { data: user, isGithubLogin } = useAuth(); | ||
const { address } = useAccount(); | ||
const { data: signer } = useSigner(); | ||
const userWallet = JSON.parse(localStorage.getItem("WTF_USER")).wallet; | ||
|
||
const githubName = get(user, "user_metadata.user_name"); // TODO(chong) 待使用统一格式USER数据 | ||
const history = useHistory(); | ||
|
||
const { | ||
isSuccess, | ||
isError, | ||
isLoading, | ||
error, | ||
mutateAsync: bindWalletMutate, | ||
} = useMutation(async () => { | ||
// 添加一个对signer的检查 | ||
if (!signer) { | ||
throw new Error("No signer found"); | ||
} | ||
const nonce = await signer.getTransactionCount(); | ||
const message = `You are binding the wallet address to your github ID in WTF Academy. \n\nThis binding can not be changed later. \nPlease confirm the binding operation. \n\nGithub ID: ${githubName}\n\nWallet Address: ${address}\n\nNonce: ${nonce}`; | ||
const signData = await signer.signMessage(message); | ||
return changeWallet({ mesData: message, signData, wallet: address }); | ||
}); | ||
|
||
useEffect(() => { | ||
if (isSuccess) { | ||
history.push("/"); | ||
} | ||
}, [isSuccess]); | ||
|
||
const errorMessage = useMemo(() => { | ||
return ( | ||
formatChainError((error as any)?.message || "") || | ||
(error as any)?.msg || | ||
"Unknown error" | ||
); | ||
}, [error]); | ||
|
||
const handleOnClick = () => { | ||
if (isGithubLogin) { | ||
if (address != userWallet) { | ||
bindWalletMutate(); | ||
} else { | ||
toast.error("当前钱包与Github绑定的钱包一致,无需修改"); | ||
} | ||
} else { | ||
toast.error("请使用Github登录"); | ||
} | ||
}; | ||
|
||
return ( | ||
<StepCard | ||
error={isError} | ||
errorMessage={errorMessage} | ||
onClick={handleOnClick} | ||
className={"cursor-pointer"} | ||
> | ||
<div className="flex justify-between w-full"> | ||
<div className="flex flex-col"> | ||
<span> | ||
<Translate id="login.StepBindWallet.intro"> | ||
签名消息+绑定钱包 | ||
</Translate> | ||
</span> | ||
{isError && ( | ||
<span className="text-xs text-start text-destructive-foreground"> | ||
{errorMessage} | ||
</span> | ||
)} | ||
</div> | ||
{!isLoading ? ( | ||
<ArrowRightCircleIcon className="w-6 h-6 text-white" /> | ||
) : ( | ||
<Spinner loading className="w-6 h-6" /> | ||
)} | ||
</div> | ||
</StepCard> | ||
); | ||
}; | ||
|
||
export default StepChangeWallet; |
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,18 @@ | ||
import React, { useState } from "react"; | ||
import Stepper from "@site/src/components/ui/Stepper"; | ||
import Step from "@site/src/components/ui/Stepper/Step"; | ||
import StepChangeWallet from "@site/src/pages/wallet/_ChangeWallet/_StepChangeWallet"; | ||
|
||
function ChangeWallet() { | ||
const [activeStep] = useState(0); | ||
|
||
return ( | ||
<Stepper activeStep={activeStep}> | ||
<Step placeholderWidth={342}> | ||
<StepChangeWallet /> | ||
</Step> | ||
</Stepper> | ||
); | ||
} | ||
|
||
export default ChangeWallet; |
Oops, something went wrong.