Skip to content

Commit f1f353d

Browse files
authored
Merge pull request #626 from semaphore-protocol/feat/export-group
New Group `export`/`import` methods Former-commit-id: f44b0b1
2 parents 5c3c655 + 12557ac commit f1f353d

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

packages/group/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,32 @@ console.log(proof)
169169
}
170170
*/
171171
```
172+
173+
\# **export**(): _string_
174+
175+
```typescript
176+
import { Group } from "@semaphore-protocol/group"
177+
178+
const group = new Group([1, 2, 3])
179+
180+
const exportedGroup = group.export()
181+
182+
console.log(exportedGroup)
183+
/*
184+
[["1","2","3"],["7853200120776062878684798364095072458815029376092732009249414926327459813530","3"],["13816780880028945690020260331303642730075999758909899334839547418969502592169"]]
185+
*/
186+
```
187+
188+
\# **import**(exportedGroup: _string_): _Group_
189+
190+
```typescript
191+
import { Group } from "@semaphore-protocol/group"
192+
193+
const group1 = new Group([1, 2, 3])
194+
195+
const exportedGroup = group.export()
196+
197+
const group2 = Group.import(exportedGroup)
198+
199+
assert(group1.root === group2.root)
200+
```

packages/group/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"rollup-plugin-typescript2": "^0.36.0"
3838
},
3939
"dependencies": {
40-
"@zk-kit/imt": "^2.0.0-beta"
40+
"@zk-kit/imt": "^2.0.0-beta.1"
4141
}
4242
}

packages/group/src/group.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,29 @@ export default class Group {
102102
siblings: siblings.map(String)
103103
}
104104
}
105+
106+
/**
107+
* It enables the conversion of the group into a JSON string that
108+
* can be re-used for future imports. This approach is beneficial for
109+
* large groups, as it avoids re-calculating the tree hashes.
110+
* @returns The stringified JSON of the group.
111+
*/
112+
export(): string {
113+
return this.leanIMT.export()
114+
}
115+
116+
/**
117+
* It imports an entire group by initializing the tree without calculating
118+
* any hashes. Note that it is crucial to ensure the integrity of the
119+
* exported group.
120+
* @param nodes The stringified JSON of the group.
121+
* @returns The group instance.
122+
*/
123+
static import(exportedGroup: string): Group {
124+
const group = new Group()
125+
126+
group.leanIMT.import(exportedGroup)
127+
128+
return group
129+
}
105130
}

packages/group/tests/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,34 @@ describe("Group", () => {
9191
expect(proof.leaf).toBe("1")
9292
})
9393
})
94+
95+
describe("# export", () => {
96+
it("Should export a group", () => {
97+
const group = new Group([1, 2, 3])
98+
99+
const exportedGroup = group.export()
100+
101+
console.log(exportedGroup)
102+
103+
expect(typeof exportedGroup).toBe("string")
104+
expect(JSON.parse(exportedGroup)).toHaveLength(3)
105+
expect(JSON.parse(exportedGroup)[0]).toHaveLength(3)
106+
})
107+
})
108+
109+
describe("# import", () => {
110+
it("Should import a group", () => {
111+
const group1 = new Group([1, 2, 3])
112+
const exportedGroup = group1.export()
113+
114+
const group2 = Group.import(exportedGroup)
115+
116+
group1.addMember(4)
117+
group2.addMember(4)
118+
119+
expect(group2.depth).toBe(group1.depth)
120+
expect(group2.size).toBe(group1.size)
121+
expect(group2.root).toBe(group1.root)
122+
})
123+
})
94124
})

yarn.lock.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
41216b543a37ea32df4cb98d913cca8daee1ca63
1+
905709848cfc19dff98c61c4b975512626b90b89

0 commit comments

Comments
 (0)