-
Notifications
You must be signed in to change notification settings - Fork 42
/
register.go
95 lines (76 loc) · 2.31 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package avs
import (
"context"
"math/big"
"time"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient/ethbackend"
"github.com/omni-network/omni/lib/k1util"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func RegisterOperatorWithAVS(ctx context.Context, addr common.Address, backend *ethbackend.Backend, operator common.Address) error {
avs, err := bindings.NewOmniAVS(addr, backend)
if err != nil {
return errors.Wrap(err, "new avs")
}
if err := verifyRegisterOperator(ctx, avs, operator); err != nil {
return err
}
salt := crypto.Keccak256Hash(operator.Bytes()) // Salt can be anything, it should just be unique.
expiry := big.NewInt(time.Now().Add(time.Hour).Unix()) // Sig is 1 Hour valid
avsDirAddr, err := avs.AvsDirectory(&bind.CallOpts{})
if err != nil {
return errors.Wrap(err, "avs directory")
}
avsDir, err := bindings.NewAVSDirectory(avsDirAddr, backend)
if err != nil {
return errors.Wrap(err, "new avs directory")
}
digestHash, err := avsDir.CalculateOperatorAVSRegistrationDigestHash(&bind.CallOpts{},
operator,
addr,
salt,
expiry)
if err != nil {
return errors.Wrap(err, "calculate digest hash")
}
sig, err := backend.Sign(ctx, operator, digestHash)
if err != nil {
return errors.Wrap(err, "sign")
}
txOpts, err := backend.BindOpts(ctx, operator)
if err != nil {
return err
}
pubkey, err := backend.PublicKey(operator)
if err != nil {
return errors.Wrap(err, "public key")
}
pubkey64 := k1util.PubKeyToBytes64(pubkey)
tx, err := avs.RegisterOperator(txOpts, pubkey64, bindings.ISignatureUtilsSignatureWithSaltAndExpiry{
Signature: sig[:],
Salt: salt,
Expiry: expiry,
})
if err != nil {
return errors.Wrap(err, "register operator to avs")
}
_, err = backend.WaitMined(ctx, tx)
if err != nil {
return errors.Wrap(err, "wait mined")
}
return nil
}
func verifyRegisterOperator(ctx context.Context, avs *bindings.OmniAVS, operator common.Address) error {
canRegister, reason, err := avs.CanRegister(&bind.CallOpts{Context: ctx}, operator)
if err != nil {
return errors.Wrap(err, "can register")
}
if !canRegister {
return errors.New(reason)
}
return nil
}