Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

署名の検証を追加 #551

Open
wants to merge 1 commit into
base: feature/mint-point
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions frontend/src/pages/api/points/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { Stripe } from "stripe";
import { ethers } from "ethers";

const UNIT_AMOUNT_STD = 5; // 1ポイントあたりの価格(単位:円)

Expand All @@ -23,13 +24,14 @@ export default async function handler(
// todo: 購入ポイント数の価格計算
const unit_amount = Number(amount) * UNIT_AMOUNT_STD;

// todo: signatureからウォレットアドレスを複合
const walletAddress = ethers.utils.verifyMessage(amount, signature);

// ここのmetadataがwebhookで飛んでくるので、それでどのアドレスにいくらポイント付与するかがわかる
const walletAddress = "0x019281ce34F8b8739991713D5E09D0C290B53886";
const metadata = {
walletAddress,
amount, // number型で送信してもwebhook経由で取得するとstring型に変換されてしまうのでstring型のままで送信
};
console.log("metadata:", metadata);

const session = await stripeClient.checkout.sessions.create({
mode: "payment",
Expand Down
105 changes: 61 additions & 44 deletions frontend/src/pages/purchase-points/purchase.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Container, Button, useDisclosure, Input, Text, Flex } from "@chakra-ui/react";
import {
Container,
Button,
useDisclosure,
Input,
Text,
Flex,
} from "@chakra-ui/react";
import { useAddress } from "@thirdweb-dev/react";
import React, { FC, useCallback, useState } from "react";
import React, { FC, useCallback, useState } from "react";
import { useLocale } from "src/hooks/useLocale";
import { ConnectWalletModal } from "./../../components/molecules/web3/ConnectWalletModal";

Expand All @@ -14,9 +21,9 @@ const User: FC = () => {
onClose: onConnectWalletClose,
} = useDisclosure();
const [connecting, setConnecting] = useState<boolean>(false);
const [value, setValue] = useState('');
const [value, setValue] = useState("");
const [isValid, setIsValid] = useState(true);
const [valueURL, setValueURL] = useState('');
const [valueURL, setValueURL] = useState("");

const handleOpenConnectWallet = useCallback(() => {
onConnectWalletOpen();
Expand All @@ -26,61 +33,71 @@ const User: FC = () => {
const handlePayment = async () => {
const isNumeric = /^-?\d*\.?\d+$/.test(value);
setIsValid(isNumeric);
if(isNumeric){
if (isNumeric) {
try {
const response = await fetch(`/api/points/checkout?amount=${value}&signature=${address}`);
// todo: 署名をダミーデータから変更
const response = await fetch(
`/api/points/checkout?amount=${value}&signature=0x681c1341f66238f9e2c44a9e98a3c8eceb0d27ca031dee628490d7772f13a19f31ce708d13be3a662fd43ebd12ddaf0e208869af422a20070ca254da629fe0a61c`
);
const data = await response.json();
// データを処理する
console.log(data.sessionURL);
setValueURL(data.sessionURL);
window.location.href = data.sessionURL;
} catch (error) {
console.error('Error:', error);
console.error("Error:", error);
}
}
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => setValue(event.target.value);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) =>
setValue(event.target.value);

return (
<Container maxWidth={1000} mt={{ base: 5, md: 10 }}>
{address ?
<>


<Text pb={5}>{t.PURCHASE}</Text>
<Flex mb={5}>
<Input w={500} value={value} onChange={handleChange} placeholder={t.POINTS_TO_BE_PURCHASED}/>
<Text pl={1}>pt</Text>
</Flex>
{!isValid && <Text pb={5} color="red">{t.INCORRECT_INPUT}</Text>}
<Button
backgroundColor="yellow.900"
color="white"
onClick={handlePayment}
>
{t.PAYMENT}
</Button>

</>
:
<Button
backgroundColor="yellow.900"
color="white"
onClick={handleOpenConnectWallet}
>
{t.SIGN_IN}
</Button>
}
{(!address || connecting) && (
<ConnectWalletModal
setConnecting={setConnecting}
onClose={onConnectWalletClose}
isOpen={isConnectWalletOpen}
{address ? (
<>
<Text pb={5}>{t.PURCHASE}</Text>
<Flex mb={5}>
<Input
w={500}
value={value}
onChange={handleChange}
placeholder={t.POINTS_TO_BE_PURCHASED}
/>
)}
<Text pl={1}>pt</Text>
</Flex>
{!isValid && (
<Text pb={5} color="red">
{t.INCORRECT_INPUT}
</Text>
)}
<Button
backgroundColor="yellow.900"
color="white"
onClick={handlePayment}
>
{t.PAYMENT}
</Button>
</>
) : (
<Button
backgroundColor="yellow.900"
color="white"
onClick={handleOpenConnectWallet}
>
{t.SIGN_IN}
</Button>
)}
{(!address || connecting) && (
<ConnectWalletModal
setConnecting={setConnecting}
onClose={onConnectWalletClose}
isOpen={isConnectWalletOpen}
/>
)}
</Container>
);
};

export default User;
export default User;