UNPKG

@zkmpa/group

Version:

Group management for zkMPA (Zero-Knowledge Multi-Party Approval)

70 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupImpl = void 0; const group_1 = require("@semaphore-protocol/group"); class GroupImpl { constructor(config) { this.id = config.id; this.config = config; this.semaphoreGroup = new group_1.Group(); this.memberSet = new Set(); } addMember(commitment) { const commitmentStr = commitment.toString(); if (this.memberSet.has(commitmentStr)) { throw new Error(`Member ${commitmentStr} already exists in group ${this.id}`); } this.semaphoreGroup.addMember(commitment); this.memberSet.add(commitmentStr); } removeMember(commitment) { const index = this.indexOf(commitment); if (index === -1) { throw new Error(`Member ${commitment} not found in group ${this.id}`); } this.semaphoreGroup.removeMember(index); this.memberSet.delete(commitment.toString()); } getMerkleRoot() { return this.semaphoreGroup.root; } getMerkleProof(commitment) { const index = this.indexOf(commitment); if (index === -1) { throw new Error(`Member ${commitment} not found in group ${this.id}`); } const proof = this.semaphoreGroup.generateMerkleProof(index); // Create path indices from index const pathIndices = []; let currentIndex = index; for (let i = 0; i < proof.siblings.length; i++) { pathIndices.push(currentIndex % 2); currentIndex = Math.floor(currentIndex / 2); } return { siblings: proof.siblings.map((s) => s.toString()), pathIndices }; } getMembers() { return this.semaphoreGroup.members; } indexOf(commitment) { return this.semaphoreGroup.indexOf(commitment); } exportState() { return { id: this.id, config: this.config, merkleRoot: this.getMerkleRoot().toString(), merkleTreeDepth: this.semaphoreGroup.depth, members: this.getMembers().map(m => m.toString()), memberCount: this.getMembers().length }; } getSemaphoreGroup() { return this.semaphoreGroup; } } exports.GroupImpl = GroupImpl; //# sourceMappingURL=group.js.map