Verify zkProof
The zkProofVerify method is designed to verify a zero-knowledge proof. This method confirms that the provided proof is both correctly constructed and authentic without exposing any underlying sensitive data. It returns key details such as the current rollup step, the Merkle root computed from the underlying data structure, and a boolean indicator of the proof's validity.
Method Signature
const verifyResult = (await zkdb.db(databaseName).zkProofVerify()).unwrap();
Complete Example: Insert Document and Verify Both Merkle Proof and ZK Proof
This example demonstrates a complete workflow from document insertion to proof verification, combining both Merkle proof and ZK proof verification:
import { ZkDatabase } from 'zkdb';
const zkdb = new ZkDatabase({
apiKey: 'zkdb_536aac02a1b7.c001b6b8f...da3aa4185d8d2ad07f0ae94aT',
// This URL is for test environment
url: "https://serverless.zkdatabase.org/graphql",
});
// Create new instance of 'db_test'
const dbTest = zkdb.db('db_test');
// Define book schema
const bookSchema = {
name: zkdb.Schema.string(),
author: zkdb.Schema.string(),
release: zkdb.Schema.bigInt(),
};
// Create collection
const collectionBook = dbTest.collection('books', bookSchema);
// Insert a document
const docId = await collectionBook
.insert({
name: 'Snow Crash',
author: 'Neal Stephenson',
release: 1992n,
})
.then((r) => r.unwrap().docId);
// Poll for proof generation and verify both proofs
while (true) {
const doc = await collectionBook
.findOne({
'$zkdb::docId': docId,
})
.then((r) => r.unwrap()!);
const proofStatus = await doc.proofStatus().then((r) => r.unwrap());
if (proofStatus.status === EQueueTaskStatus.Success) {
// Verify Merkle proof
const merkleProof = await doc.merkleProof().then((r) => r.unwrap());
if (doc.merkleProofVerify(bookSchema, merkleProof)) {
console.log('Merkle proof verified for document', docId);
} else {
console.error('Merkle proof verification failed for document:', docId);
}
// Verify ZK proof
if (
await dbTest
.zkProofVerify(proofStatus.sequenceNumber)
.then((r) => r.unwrap().valid)
) {
console.log('ZK proof verified for document', docId);
} else {
console.error('ZK proof verification failed for document:', docId);
}
} else if (proofStatus.status === EQueueTaskStatus.Failed) {
console.error(
'Failed to generate Merkle proof for document:',
docId,
proofStatus.error
);
} else {
// Still processing, wait and retry
await new Promise((resolve) => setTimeout(resolve, 1000));
continue;
}
break;
}
Returns
- A promise that resolves to a
TZkDbProofVerifyobject.
The TZkDbProofVerify contains the following structure:
TZkProofResponse
| Property | Type | Description |
|---|---|---|
step | bigint | Represents the rollup step or iteration number associated with the proof verification. |
merkleRoot | string | The Merkle root computed from the underlying data, ensuring data integrity. |
valid | boolean | A boolean value that indicates whether the provided proof is valid (true) or invalid (false). |
{
step: 10n,
merkleRoot: "15383398803871485586427167108754689270190633959576017615536369378087055779186",
valid: true
}