Skip to content

Commit

Permalink
fix: add back the missing wrapDocuments that was accidentally removed
Browse files Browse the repository at this point in the history
  • Loading branch information
phanshiyu committed Apr 26, 2024
1 parent ff57b1c commit 6245038
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/4.0/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ export const wrapDocument = async <T extends V4Document>(document: T): Promise<V
return verifiableCredential as V4WrappedDocument<T>;
};

export const wrapDocuments = async <T extends V4Document>(documents: T[]): Promise<V4WrappedDocument<T>[]> => {
// create individual verifiable credential
const verifiableCredentials = await Promise.all(documents.map((document) => wrapDocument(document)));

// get all the target hashes to compute the merkle tree and the merkle root
const merkleTree = new MerkleTree(
verifiableCredentials.map((verifiableCredential) => verifiableCredential.proof.targetHash).map(hashToBuffer)
);
const merkleRoot = merkleTree.getRoot().toString("hex");

// for each document, update the merkle root and add the proofs needed
return verifiableCredentials.map((verifiableCredential) => {
const digest = verifiableCredential.proof.targetHash;
const merkleProof = merkleTree.getProof(hashToBuffer(digest)).map((buffer: Buffer) => buffer.toString("hex"));

return {
...verifiableCredential,
proof: {
...verifiableCredential.proof,
proofs: merkleProof,
merkleRoot,
},
};
});
};

/** Extract a set of properties from w3cVerifiableCredential but only include the ones
* that are defined in the original document. For example, if we extract
* "a" and "b" from { b: "something" } we should only get { b: "something" } NOT
Expand Down

0 comments on commit 6245038

Please sign in to comment.