go-waku/waku/v2/protocol/rln/web3.go

208 lines
5.6 KiB
Go
Raw Normal View History

2022-07-28 14:04:33 +00:00
package rln
import (
"context"
"crypto/ecdsa"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
r "github.com/status-im/go-rln/rln"
"github.com/status-im/go-waku/waku/v2/protocol/rln/contracts"
"go.uber.org/zap"
)
2022-08-04 21:39:12 +00:00
var MEMBERSHIP_FEE = big.NewInt(1000000000000000) // wei - 0.001 eth
2022-07-28 14:04:33 +00:00
func toBigInt(i []byte) *big.Int {
result := new(big.Int)
result.SetBytes(i[:])
return result
}
func register(ctx context.Context, idComm r.IDCommitment, ethAccountPrivateKey *ecdsa.PrivateKey, ethClientAddress string, membershipContractAddress common.Address, log *zap.Logger) (*r.MembershipIndex, error) {
backend, err := ethclient.Dial(ethClientAddress)
if err != nil {
return nil, err
}
defer backend.Close()
chainID, err := backend.ChainID(context.Background())
if err != nil {
return nil, err
}
auth, err := bind.NewKeyedTransactorWithChainID(ethAccountPrivateKey, chainID)
if err != nil {
return nil, err
}
auth.Value = MEMBERSHIP_FEE
auth.Context = ctx
rlnContract, err := contracts.NewRLN(membershipContractAddress, backend)
if err != nil {
return nil, err
}
log.Debug("registering an id commitment", zap.Binary("idComm", idComm[:]))
// registers the idComm into the membership contract whose address is in rlnPeer.membershipContractAddress
tx, err := rlnContract.Register(auth, toBigInt(idComm[:]))
if err != nil {
return nil, err
}
log.Info("transaction broadcasted", zap.String("transactionHash", tx.Hash().Hex()))
txReceipt, err := bind.WaitMined(ctx, backend, tx)
if err != nil {
return nil, err
}
if txReceipt.Status != types.ReceiptStatusSuccessful {
return nil, errors.New("transaction reverted")
}
// the receipt topic holds the hash of signature of the raised events
evt, err := rlnContract.ParseMemberRegistered(*txReceipt.Logs[0])
if err != nil {
return nil, err
}
var eventIdComm r.IDCommitment
copy(eventIdComm[:], evt.Pubkey.Bytes())
log.Debug("the identity commitment key extracted from tx log", zap.Binary("eventIdComm", eventIdComm[:]))
if eventIdComm != idComm {
return nil, errors.New("invalid id commitment key")
}
result := new(r.MembershipIndex)
*result = r.MembershipIndex(uint(evt.Index.Int64()))
// debug "the index of registered identity commitment key", eventIndex=eventIndex
log.Debug("the index of registered identity commitment key", zap.Uint("eventIndex", uint(*result)))
return result, nil
}
// Register registers the public key of the rlnPeer which is rlnPeer.membershipKeyPair.publicKey
// into the membership contract whose address is in rlnPeer.membershipContractAddress
func (rln *WakuRLNRelay) Register(ctx context.Context) (*r.MembershipIndex, error) {
pk := rln.membershipKeyPair.IDCommitment
return register(ctx, pk, rln.ethAccountPrivateKey, rln.ethClientAddress, rln.membershipContractAddress, rln.log)
}
// the types of inputs to this handler matches the MemberRegistered event/proc defined in the MembershipContract interface
type RegistrationEventHandler = func(pubkey r.IDCommitment, index r.MembershipIndex) error
2022-08-09 00:02:08 +00:00
func processLogs(evt *contracts.RLNMemberRegistered, handler RegistrationEventHandler) error {
2022-07-28 14:04:33 +00:00
if evt == nil {
2022-08-09 00:02:08 +00:00
return nil
2022-07-28 14:04:33 +00:00
}
var pubkey r.IDCommitment
copy(pubkey[:], evt.Pubkey.Bytes())
index := r.MembershipIndex(uint(evt.Index.Int64()))
2022-08-09 00:02:08 +00:00
return handler(pubkey, index)
2022-07-28 14:04:33 +00:00
}
// HandleGroupUpdates mounts the supplied handler for the registration events emitting from the membership contract
// It connects to the eth client, subscribes to the `MemberRegistered` event emitted from the `MembershipContract`
// and collects all the events, for every received event, it calls the `handler`
func (rln *WakuRLNRelay) HandleGroupUpdates(handler RegistrationEventHandler, errChan chan<- error) {
defer close(errChan)
2022-07-28 14:04:33 +00:00
backend, err := ethclient.Dial(rln.ethClientAddress)
if err != nil {
errChan <- err
return
2022-07-28 14:04:33 +00:00
}
2022-08-18 16:27:10 +00:00
rln.ethClient = backend
2022-07-28 14:04:33 +00:00
rlnContract, err := contracts.NewRLN(rln.membershipContractAddress, backend)
if err != nil {
errChan <- err
return
2022-07-28 14:04:33 +00:00
}
2022-08-09 00:02:08 +00:00
err = rln.loadOldEvents(rlnContract, handler)
if err != nil {
errChan <- err
return
2022-08-09 00:02:08 +00:00
}
2022-07-28 14:04:33 +00:00
doneCh := make(chan struct{})
errCh := make(chan error)
go rln.watchNewEvents(rlnContract, handler, rln.log, doneCh, errCh)
2022-07-28 14:04:33 +00:00
select {
case <-doneCh:
return
case <-errCh:
errChan <- err
return
}
2022-07-28 14:04:33 +00:00
}
func (rln *WakuRLNRelay) loadOldEvents(rlnContract *contracts.RLN, handler RegistrationEventHandler) error {
logIterator, err := rlnContract.FilterMemberRegistered(&bind.FilterOpts{Start: 0, End: nil, Context: rln.ctx})
2022-07-28 14:04:33 +00:00
if err != nil {
return err
}
for {
if !logIterator.Next() {
2022-07-28 14:04:33 +00:00
break
}
if logIterator.Error() != nil {
return logIterator.Error()
}
err = processLogs(logIterator.Event, handler)
if err != nil {
return err
}
2022-07-28 14:04:33 +00:00
}
return nil
}
func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler RegistrationEventHandler, log *zap.Logger, doneCh chan struct{}, errCh chan error) {
2022-07-28 14:04:33 +00:00
// Watch for new events
logSink := make(chan *contracts.RLNMemberRegistered)
subs, err := rlnContract.WatchMemberRegistered(&bind.WatchOpts{Context: rln.ctx, Start: nil}, logSink)
2022-07-28 14:04:33 +00:00
if err != nil {
errCh <- err
2022-07-28 14:04:33 +00:00
}
2022-08-18 16:27:10 +00:00
defer subs.Unsubscribe()
2022-07-28 14:04:33 +00:00
close(doneCh)
2022-07-28 14:04:33 +00:00
for {
select {
case evt := <-logSink:
err = processLogs(evt, handler)
if err != nil {
rln.log.Error("processing rln log", zap.Error(err))
}
2022-07-28 14:04:33 +00:00
case <-rln.ctx.Done():
subs.Unsubscribe()
close(logSink)
return
2022-07-28 14:04:33 +00:00
case err := <-subs.Err():
2022-08-18 16:27:10 +00:00
if err != nil {
rln.log.Error("watching new events", zap.Error(err))
}
2022-07-28 14:04:33 +00:00
close(logSink)
return
2022-07-28 14:04:33 +00:00
}
}
}