2023-04-03 17:45:19 -04:00
|
|
|
package static
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
|
2023-04-03 17:45:19 -04:00
|
|
|
"github.com/waku-org/go-zerokit-rln/rln"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StaticGroupManager struct {
|
|
|
|
rln *rln.RLN
|
|
|
|
log *zap.Logger
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
identityCredential *rln.IdentityCredential
|
|
|
|
membershipIndex *rln.MembershipIndex
|
|
|
|
|
|
|
|
group []rln.IDCommitment
|
|
|
|
rootTracker *group_manager.MerkleRootTracker
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStaticGroupManager(
|
|
|
|
group []rln.IDCommitment,
|
2023-04-04 11:44:28 -04:00
|
|
|
identityCredential rln.IdentityCredential,
|
|
|
|
index rln.MembershipIndex,
|
2023-04-03 17:45:19 -04:00
|
|
|
log *zap.Logger,
|
|
|
|
) (*StaticGroupManager, error) {
|
|
|
|
// check the peer's index and the inclusion of user's identity commitment in the group
|
2023-04-04 11:44:28 -04:00
|
|
|
if identityCredential.IDCommitment != group[int(index)] {
|
2023-04-03 17:45:19 -04:00
|
|
|
return nil, errors.New("peer's IDCommitment does not match commitment in group")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StaticGroupManager{
|
2023-04-04 11:44:28 -04:00
|
|
|
log: log.Named("rln-static"),
|
|
|
|
group: group,
|
|
|
|
identityCredential: &identityCredential,
|
|
|
|
membershipIndex: &index,
|
2023-04-03 17:45:19 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
func (gm *StaticGroupManager) Start(ctx context.Context, rlnInstance *rln.RLN, rootTracker *group_manager.MerkleRootTracker) error {
|
2023-04-03 17:45:19 -04:00
|
|
|
gm.log.Info("mounting rln-relay in off-chain/static mode")
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
gm.rln = rlnInstance
|
|
|
|
gm.rootTracker = rootTracker
|
|
|
|
|
|
|
|
err := rootTracker.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 17:45:19 -04:00
|
|
|
|
|
|
|
// add members to the Merkle tree
|
|
|
|
for _, member := range gm.group {
|
2023-04-04 11:44:28 -04:00
|
|
|
if err := rlnInstance.InsertMember(member); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rootTracker.Sync()
|
|
|
|
if err != nil {
|
2023-04-03 17:45:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gm.group = nil // Deleting group to release memory
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
func (gm *StaticGroupManager) InsertMember(pubkey rln.IDCommitment) error {
|
|
|
|
gm.log.Debug("a new key is added", zap.Binary("pubkey", pubkey[:]))
|
|
|
|
// assuming all the members arrive in order
|
|
|
|
err := gm.rln.InsertMember(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
gm.log.Error("inserting member into merkletree", zap.Error(err))
|
|
|
|
return err
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
err = gm.rootTracker.Sync()
|
2023-04-03 17:45:19 -04:00
|
|
|
if err != nil {
|
2023-04-04 11:44:28 -04:00
|
|
|
return err
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gm *StaticGroupManager) IdentityCredentials() (rln.IdentityCredential, error) {
|
|
|
|
if gm.identityCredential == nil {
|
|
|
|
return rln.IdentityCredential{}, errors.New("identity credential has not been setup")
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
return *gm.identityCredential, nil
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
func (gm *StaticGroupManager) MembershipIndex() (rln.MembershipIndex, error) {
|
|
|
|
if gm.membershipIndex == nil {
|
|
|
|
return 0, errors.New("membership index has not been setup")
|
|
|
|
}
|
2023-04-03 17:45:19 -04:00
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
return *gm.membershipIndex, nil
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:44:28 -04:00
|
|
|
func (gm *StaticGroupManager) Stop() {
|
|
|
|
// Do nothing
|
2023-04-03 17:45:19 -04:00
|
|
|
}
|