Prerequisites
- Node.js 18.0.0 or later
Quickstart
The following tutorial is based on node express backend
Install Reclaim SDK
npm i @reclaimprotocol/reclaim-sdk
Import the SDK and initialise it
import { reclaimprotocol } from '@reclaimprotocol/reclaim-sdk';
const reclaim = new reclaimprotocol.Reclaim();
Generate a request for credential from the User
Create an endpoint on your backend server to request proofs from your user.
You will need the following to generate this request
callbackId
is optional. This is used to identify the user who will be submitting the proof of their credential. We recommend you to create a new id for each user whom you are requesting a credential from. We will later use this callbackId to map the credentials uploaded to the correct user.baseCallbackUrl
is required. This is where the user will upload the credential you request along with the required proofs. The callback ID will be sent as a query parameter to this baseCallbackUrl endpoint. We recommend you to expose another endpoint as described in the next step.contextMessage
is optional. This consists of a message that you want the user to include in the credential so that you know the user generated the credential for your application, and is not using a proof generated by someone else for some other application. For example, “Acme Corp Airdrop on 1/1/2020”contextAddress
is optional. This is useful when you want the users to submit their credentials on chain. On the smart contract, you can extract this contextAddress from the credential submitted and provide onchain benefits (NFTs, Coins etc) to the extracted address. Be careful not to use address from the transaction object (e.g. msg.sender) for providing onchain benefits, because that will allow a front running attack. This context Address could be an address on EVM, Solana, Cosmos, Bitcoin.requestedProofs
is required : This is where you define the credentials you’re requesting. You can request proof of credentials from your users using Providers. Learn more about Providers here.
app.get("/request-proofs", async(req, res) => {
const request = reclaim.requestProofs({
title: "My DeFi app", // Name of your application
baseCallbackUrl: "http://<YOUR_BACKEND_SERVER_URL>/callback",
callbackId: "<UNIQUE_IDENTIFIER_GOES_HERE>", // optional
contextMessage: "Airdrop for Reclaim Users!", //optional
contextAddress: userAddress, //optional
requestedProofs: [
new reclaim.CustomProvider({
provider: 'bybit-balance',
payload: {}
}),
],
});
const { callbackId } = request;
const reclaimUrl = await request.getReclaimUrl();
// Store the callback Id and Reclaim URL in your database
// ...
res.json({ reclaimUrl });
// display this reclaimUrl as a QR code on laptop or as a link on mobile devices for users to initiate creating proofs
})
Handle proof submission
Define callback endpoint to handle proof submission
- Use
reclaim.verifyCorrectnessOfProofs(callbackId, proofs)
to verify if the proofs submitted were correct. - It returns a boolean value depending on if the proofs were correct or not
- If the proofs were correct, you can use them to perform your business logic
- If the proofs were not correct, you can handle the error accordingly
app.use(express.text({ type: "*/*" }));
app.post("/callback", async (req, res) => {
try {
// Retrieve the callback ID from the URL parameters
const callbackId = req.query.callbackId as string;
// Retrieve the proofs from the request body
const proofs = reclaimprotocol.utils.getProofsFromRequestBody(req.body)
// Verify the correctness of the proofs (optional but recommended)
const isProofsCorrect = await reclaim.verifyCorrectnessOfProofs(callbackId, proofs);
if (isProofsCorrect) {
// Proofs are correct, handle them as needed
// ... business logic goes here
// Respond with a success message
res.json({ success: true });
} else {
// Proofs are not correct or verification failed
// ... handle the error accordingly
// Respond with an error message
res.status(400).json({ error: "Proofs verification failed" });
}
} catch (error) {
console.error("Error processing callback:", error);
res.status(500).json({ error: "Failed to process callback" });
}
});