Skip to content

Commit bed1231

Browse files
authored
Merge pull request #21 from reclaimprotocol/cascading-proofs-feature
Added Support for Cascading Providers in the verifyProof method
2 parents b7fb0cb + a2ab609 commit bed1231

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ function App() {
8888
setProofs(proofs)
8989
} else if (proofs && typeof proofs !== 'string') {
9090
// When using the default callback url, we get a proof
91-
console.log('Proof received:', proofs?.claimData.context)
91+
if (Array.isArray(proofs)) {
92+
// when using the cascading providers, providers having more than one proof will return an array of proofs
93+
console.log(JSON.stringify(proofs.map(p => p.claimData.context)))
94+
} else {
95+
console.log('Proof received:', proofs?.claimData.context)
96+
}
9297
setProofs(proofs)
9398
}
9499
},

example/src/app/page.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,22 @@ export default function Home() {
3434
const proofRequest = await ReclaimProofRequest.init(
3535
process.env.NEXT_PUBLIC_RECLAIM_APP_ID!,
3636
process.env.NEXT_PUBLIC_RECLAIM_APP_SECRET!,
37-
'5eb7f8b3-cbe8-4001-848e-cb161e53fe60', // providerId
38-
// Uncomment the following line to enable logging and AI providers
39-
// { log: true, acceptAiProviders: true }
37+
process.env.NEXT_PUBLIC_RECLAIM_PROVIDER_ID!,
38+
{ log: true }
4039
)
4140
setReclaimProofRequest(proofRequest)
4241

4342
// Add context to the proof request (optional)
4443
proofRequest.addContext('0x00000000000', 'Example context message')
4544

4645
// Set parameters for the proof request (if needed)
47-
// proofRequest.setParams({ email: "test@example.com", userName: "testUser" })
46+
// proofRequest.setParams({ key: "value" })
4847

4948
// Set a redirect URL (if needed)
50-
// proofRequest.setRedirectUrl('https://example.com/redirect')
49+
// proofRequest.setRedirectUrl('your-redirect-url')
5150

5251
// Set a custom app callback URL (if needed)
53-
// proofRequest.setAppCallbackUrl('https://your-website.com/callback')
52+
// proofRequest.setAppCallbackUrl('your-callback-url')
5453

5554
// Uncomment the following line to log the proof request and to get the Json String
5655
// console.log('Proof request initialized:', proofRequest.toJsonString())
@@ -76,15 +75,19 @@ export default function Home() {
7675

7776
// Start the verification session
7877
await reclaimProofRequest.startSession({
79-
onSuccess: async (proof: Proof | string | undefined) => {
78+
onSuccess: async (proof: Proof | Proof[] | string | undefined) => {
8079
if (proof && typeof proof === 'string') {
8180
// When using a custom callback url, the proof is returned to the callback url and we get a message instead of a proof
8281
console.log('SDK Message:', proof)
8382
setExtracted(proof)
8483
} else if (proof && typeof proof !== 'string') {
8584
// When using the default callback url, we get a proof
86-
console.log('Proof received:', proof?.claimData.context)
87-
setExtracted(JSON.stringify(proof?.claimData.context))
85+
console.log('Proof received:', proof)
86+
if (Array.isArray(proof)) {
87+
setExtracted(JSON.stringify(proof.map(p => p.claimData.context)))
88+
} else {
89+
setExtracted(JSON.stringify(proof?.claimData.context))
90+
}
8891
}
8992
},
9093
onError: (error: Error) => {

src/Reclaim.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,21 @@ const logger = loggerModule.logger
4141

4242
const sdkVersion = require('../package.json').version;
4343

44+
// Implementation
45+
export async function verifyProof(proofOrProofs: Proof | Proof[]): Promise<boolean> {
46+
// If input is an array of proofs
47+
if (Array.isArray(proofOrProofs)) {
48+
for (const proof of proofOrProofs) {
49+
const isVerified = await verifyProof(proof);
50+
if (!isVerified) {
51+
return false;
52+
}
53+
}
54+
return true;
55+
}
4456

45-
export async function verifyProof(proof: Proof): Promise<boolean> {
57+
// Single proof verification logic
58+
const proof = proofOrProofs;
4659
if (!proof.signatures.length) {
4760
throw new SignatureNotFoundError('No signatures')
4861
}
@@ -492,14 +505,17 @@ export class ReclaimProofRequest {
492505

493506
if (isDefaultCallbackUrl) {
494507
if (statusUrlResponse.session.proofs && statusUrlResponse.session.proofs.length > 0) {
495-
const proof = statusUrlResponse.session.proofs[0];
496-
const verified = await verifyProof(proof);
508+
const proofs = statusUrlResponse.session.proofs;
509+
const verified = await verifyProof(proofs);
497510
if (!verified) {
498-
logger.info(`Proof not verified: ${JSON.stringify(proof)}`);
511+
logger.info(`Proofs not verified: ${JSON.stringify(proofs)}`);
499512
throw new ProofNotVerifiedError();
500513
}
501-
if (onSuccess) {
502-
onSuccess(proof);
514+
// check if the proofs array has only one proof then send the proofs in onSuccess
515+
if (proofs.length === 1) {
516+
onSuccess(proofs[0]);
517+
} else {
518+
onSuccess(proofs);
503519
}
504520
this.clearInterval();
505521
}

src/utils/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type StartSessionParams = {
2626
onError: OnError;
2727
};
2828

29-
export type OnSuccess = (proof?: Proof | string) => void;
29+
export type OnSuccess = (proof?: Proof | Proof[] | string) => void;
3030
export type OnError = (error: Error) => void;
3131

3232
export type ProofRequestOptions = {

0 commit comments

Comments
 (0)