Skip to content

Commit

Permalink
(fix): check if all fractions exist and check if signer is owner of a…
Browse files Browse the repository at this point in the history
…ll fractions when creating order (#41)
  • Loading branch information
Jipperism authored Mar 15, 2024
1 parent ec1fba6 commit c806bbd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 33 deletions.
60 changes: 30 additions & 30 deletions hooks/marketplace/useCreateFractionalMakerAsk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ export const useCreateFractionalMakerAsk = ({
throw new Error("Fractions not found");
}

if (!provider) {
throw new Error("Provider not initialized");
}

if (!signer) {
throw new Error("Signer not initialized");
}

const [contractAddress, tokenId] = values.fractionId.split("-");

if (!contractAddress || !isAddress(contractAddress)) {
throw new Error("Invalid contract address");
}

let tokenIdBigInt: BigInt | undefined;
try {
tokenIdBigInt = BigInt(tokenId);
} catch (e) {
console.error(e);
throw new Error("Error parsing token ID");
}

if (!tokenIdBigInt) {
throw new Error("Invalid token ID");
}

if (!walletClientData) {
throw new Error("Wallet client not initialized");
}

onOpen([
{
title: "Splitting",
Expand Down Expand Up @@ -82,36 +112,6 @@ export const useCreateFractionalMakerAsk = ({
},
]);

if (!provider) {
throw new Error("Provider not initialized");
}

if (!signer) {
throw new Error("Signer not initialized");
}

const [contractAddress, tokenId] = values.fractionId.split("-");

if (!contractAddress || !isAddress(contractAddress)) {
throw new Error("Invalid contract address");
}

let tokenIdBigInt: BigInt | undefined;
try {
tokenIdBigInt = BigInt(tokenId);
} catch (e) {
console.error(e);
throw new Error("Error parsing token ID");
}

if (!tokenIdBigInt) {
throw new Error("Invalid token ID");
}

if (!walletClientData) {
throw new Error("Wallet client not initialized");
}

let signature: string | undefined;

setStep("Create");
Expand Down
51 changes: 48 additions & 3 deletions pages/api/marketplace/order.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NextApiRequest, NextApiResponse } from "next";
import { createClient } from "@supabase/supabase-js";
import {
EAS_CONTRACT_ADDRESS,
NFT_STORAGE_TOKEN,
SUPABASE_HYPERCERTS_SERVICE_ROLE_KEY,
SUPABASE_HYPERCERTS_URL,
} from "@/config";
Expand All @@ -11,6 +13,7 @@ import { verifyTypedData } from "ethers";
import { Database } from "@/types/hypercerts-database";
import NextCors from "nextjs-cors";
import { addressesByNetwork } from "@hypercerts-org/marketplace-sdk";
import { ClaimTokenByIdQuery, HypercertClient } from "@hypercerts-org/sdk";

const inputSchemaPost = z.object({
signature: z.string(),
Expand Down Expand Up @@ -139,9 +142,51 @@ export default async function handler(
console.log("[marketplace-api] Recovered address", recoveredAddress);

if (!(recoveredAddress.toLowerCase() === makerOrder.signer.toLowerCase())) {
return res
.status(401)
.json({ message: "Invalid signature", success: false, data: null });
return res.status(401).json({
message: "Recovered address is not equal to signer of order",
success: false,
data: null,
});
}

const hypercertClient = new HypercertClient({
chain: { id: chainId },
nftStorageToken: NFT_STORAGE_TOKEN,
easContractAddress: EAS_CONTRACT_ADDRESS,
});
const tokenIds = makerOrder.itemIds.map(
(id) => `${makerOrder.collection.toLowerCase()}-${id}`,
);
console.log("[marketplace-api] Token IDs", tokenIds);

const claimTokens = await Promise.all(
tokenIds.map(
(id) => hypercertClient.indexer.fractionById(id) as ClaimTokenByIdQuery,
),
);
console.log("[marketplace-api] Claim tokens", claimTokens);

// Check if all fractions exist
if (claimTokens.some((claimToken) => !claimToken.claimToken)) {
return res.status(401).json({
message: "Not all fractions in itemIds exist",
success: false,
data: null,
});
}

// Check if all fractions are owned by signer
if (
!claimTokens.every(
(claimToken) =>
claimToken.claimToken?.owner.toLowerCase() === recoveredAddress,
)
) {
return res.status(401).json({
message: "Not all fractions are owned by signer",
success: false,
data: null,
});
}

// Add to database
Expand Down

0 comments on commit c806bbd

Please sign in to comment.