Readme
This commit is contained in:
@@ -19,7 +19,12 @@ import { prisma } from "@/lib/db/prisma";
|
||||
import { BlockchainService } from "@/lib/services/blockchain.service";
|
||||
import { NotificationService } from "@/lib/services/notification.service";
|
||||
import { ContractService } from "@/lib/services/contract.service";
|
||||
import type { BlockchainTransactionView, BlockchainStats } from "@/lib/services/blockchain.types";
|
||||
import { CertificateService } from "@/lib/services/certificate.service";
|
||||
import type {
|
||||
BlockchainTransactionView,
|
||||
BlockchainStats,
|
||||
CryptographicCertificate,
|
||||
} from "@/lib/services/blockchain.types";
|
||||
|
||||
/**
|
||||
* Register a contract's document on the blockchain.
|
||||
@@ -44,7 +49,8 @@ export async function registerContractOnBlockchain(contractId: string) {
|
||||
if (!BlockchainService.isConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Blockchain not configured. Start a Hardhat node and check your .env.",
|
||||
error:
|
||||
"Blockchain not configured. Start a Hardhat node and check your .env.",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,7 +80,7 @@ export async function registerContractOnBlockchain(contractId: string) {
|
||||
// Hash the document and register on-chain
|
||||
const proof = await BlockchainService.hashAndRegister(
|
||||
contract.fileUrl,
|
||||
contract.fileName
|
||||
contract.fileName,
|
||||
);
|
||||
|
||||
// Save proof data to the Contract record
|
||||
@@ -137,7 +143,8 @@ export async function registerContractOnBlockchain(contractId: string) {
|
||||
console.error("❌ Blockchain registration error:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : "Unknown blockchain error",
|
||||
error:
|
||||
error instanceof Error ? error.message : "Unknown blockchain error",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -151,14 +158,18 @@ export async function verifyContractOnBlockchain(contractId: string) {
|
||||
if (!clerkId) return { success: false, error: "Unauthorized" };
|
||||
|
||||
if (!BlockchainService.isReadConfigured()) {
|
||||
return { success: false, error: "Blockchain not configured for verification" };
|
||||
return {
|
||||
success: false,
|
||||
error: "Blockchain not configured for verification",
|
||||
};
|
||||
}
|
||||
|
||||
const user = await ContractService.getUserByClerkId(clerkId);
|
||||
if (!user) return { success: false, error: "User not found" };
|
||||
|
||||
const contract = await ContractService.getById(contractId);
|
||||
if (contract.userId !== user.id) return { success: false, error: "Unauthorized" };
|
||||
if (contract.userId !== user.id)
|
||||
return { success: false, error: "Unauthorized" };
|
||||
|
||||
if (!contract.documentHash) {
|
||||
return {
|
||||
@@ -167,7 +178,9 @@ export async function verifyContractOnBlockchain(contractId: string) {
|
||||
};
|
||||
}
|
||||
|
||||
const verification = await BlockchainService.verifyOnChain(contract.documentHash);
|
||||
const verification = await BlockchainService.verifyOnChain(
|
||||
contract.documentHash,
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@@ -199,7 +212,10 @@ export async function verifyDocumentHashOnBlockchain(documentHash: string) {
|
||||
if (!clerkId) return { success: false, error: "Unauthorized" };
|
||||
|
||||
if (!BlockchainService.isReadConfigured()) {
|
||||
return { success: false, error: "Blockchain not configured for verification" };
|
||||
return {
|
||||
success: false,
|
||||
error: "Blockchain not configured for verification",
|
||||
};
|
||||
}
|
||||
|
||||
// Ensure proper format
|
||||
@@ -313,3 +329,97 @@ export async function getBlockchainStats(): Promise<{
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a cryptographic certificate for a blockchain-registered contract.
|
||||
*
|
||||
* The certificate contains:
|
||||
* - Contract metadata
|
||||
* - Blockchain proof (txHash, block, timestamp)
|
||||
* - A digital signature created by the server wallet
|
||||
*
|
||||
* @param contractId - The contract ID
|
||||
* @returns Certificate data (JSON) or error
|
||||
*/
|
||||
export async function generateContractCertificate(contractId: string): Promise<{
|
||||
success: boolean;
|
||||
certificate?: CryptographicCertificate;
|
||||
certificateJson?: string;
|
||||
certificatePdfBase64?: string;
|
||||
certificateFileName?: string;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
const { userId: clerkId } = await auth();
|
||||
if (!clerkId) return { success: false, error: "Unauthorized" };
|
||||
|
||||
if (!BlockchainService.isReadConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Blockchain not configured for certificate generation",
|
||||
};
|
||||
}
|
||||
|
||||
const user = await ContractService.getUserByClerkId(clerkId);
|
||||
if (!user) return { success: false, error: "User not found" };
|
||||
|
||||
const contract = await ContractService.getById(contractId);
|
||||
if (contract.userId !== user.id) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Unauthorized: Contract does not belong to you",
|
||||
};
|
||||
}
|
||||
|
||||
// Check if contract is registered on blockchain
|
||||
if (!contract.documentHash || !contract.txHash) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
"Contract is not registered on blockchain. Register it first to generate a certificate.",
|
||||
};
|
||||
}
|
||||
|
||||
// Generate the certificate
|
||||
const certificate = await CertificateService.generateCertificate(
|
||||
contract.id,
|
||||
contract.title,
|
||||
contract.fileName,
|
||||
contract.documentHash,
|
||||
contract.txHash,
|
||||
contract.blockNumber || 0,
|
||||
contract.blockTimestamp?.toISOString() || new Date().toISOString(),
|
||||
contract.blockchainNetwork || "hardhat",
|
||||
contract.contractAddress || "",
|
||||
);
|
||||
|
||||
// Generate a professional PDF for download
|
||||
const pdfBuffer =
|
||||
await CertificateService.generateCertificatePdf(certificate);
|
||||
const certificatePdfBase64 = pdfBuffer.toString("base64");
|
||||
|
||||
const baseName = (contract.title || contract.fileName || contract.id)
|
||||
.replace(/[^a-zA-Z0-9-_]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 64);
|
||||
const dateStamp = new Date().toISOString().slice(0, 10);
|
||||
const certificateFileName = `certificate-${baseName || contract.id}-${dateStamp}.pdf`;
|
||||
|
||||
// Keep JSON available for internal use or auditing if needed
|
||||
const certificateJson = JSON.stringify(certificate, null, 2);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
certificate,
|
||||
certificateJson,
|
||||
certificatePdfBase64,
|
||||
certificateFileName,
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
console.error("❌ Certificate generation error:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user