fix interrep onboarding

This commit is contained in:
0xtsukino
2022-08-03 18:14:57 -07:00
parent dc4037ae51
commit bfec212b84
3 changed files with 38 additions and 1 deletions

View File

@@ -44,6 +44,20 @@ const semaphore = (sequelize: Sequelize) => {
return result?.toJSON() as SemaphoreModel;
}
const findOne = async (id_commitment: string, group_id: string): Promise<SemaphoreModel|null> => {
let result = await model.findOne({
where: {
id_commitment,
group_id,
},
order: [
['createdAt', 'DESC'],
],
});
return result?.toJSON() as SemaphoreModel;
}
const findAllByCommitment = async (id_commitment: string): Promise<SemaphoreModel[]> => {
let result = await model.findAll({
where: {
@@ -89,6 +103,7 @@ const semaphore = (sequelize: Sequelize) => {
return {
model,
findOne,
findOneByCommitment,
findAllByCommitment,
addID,

View File

@@ -31,6 +31,19 @@ export default class InterrepService extends GenericService {
this.groups = {};
}
syncOne = async (group: string) => {
const [protocol, groupType, groupName] = group.split('_');
// @ts-ignore
const resp = await fetch(`${config.interrepAPI}/api/v1/groups/${groupType}/${groupName}`);
const json = await resp.json();
const data = json.data;
const existing = await this.interepGroups!.getGroup(groupType, groupName);
if (existing?.root_hash !== data.root) {
await this.fetchMembersFromGroup(data.root, groupType, groupName);
await this.interepGroups?.addHash(data.root, groupType, groupName);
}
}
sync = async () => {
try {
if (this.timeout) {

View File

@@ -76,10 +76,19 @@ export default class MerkleService extends GenericService {
if (!row) throw new Error(`${idCommitment} is not in any groups`);
group = row.group_id;
}
const exist = await this.semaphore.findOne(idCommitment, group);
if (!exist) {
await this.call('interrep', 'syncOne', group);
}
const tree = await this.makeTree(group);
const proof = await tree.createProof(tree.indexOf(BigInt('0x' + idCommitment)));
if (!proof) throw new Error(`${idCommitment} is not in ${group}`);
if (!proof) {
throw new Error(`${idCommitment} is not in ${group}`);
}
const root = '0x' + proof.root.toString(16);