forked from taikoxyz/taiko-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guardianprover.go
53 lines (44 loc) · 1.23 KB
/
guardianprover.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
package guardianproverhealthcheck
import (
"encoding/base64"
"errors"
"log/slog"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/prometheus/client_golang/prometheus"
)
type GuardianProver struct {
Address common.Address
ID *big.Int
HealthCheckCounter prometheus.Counter
SignedBlockCounter prometheus.Counter
}
func SignatureToGuardianProver(
msg []byte,
b64EncodedSig string,
guardianProvers []GuardianProver,
) (*GuardianProver, error) {
b64DecodedSig, err := base64.StdEncoding.DecodeString(b64EncodedSig)
if err != nil {
return nil, err
}
// recover the public key from the signature
r, err := crypto.SigToPub(msg, b64DecodedSig)
if err != nil {
return nil, err
}
// convert it to address type
recoveredAddr := crypto.PubkeyToAddress(*r)
// see if any of our known guardian provers have that recovered address
for _, p := range guardianProvers {
if recoveredAddr.Cmp(p.Address) == 0 {
return &p, nil
}
}
slog.Error("signature does not recover to known guardian prover",
"recovered", recoveredAddr.Hex(),
"guardianProvers", guardianProvers,
)
return nil, errors.New("signature does not recover to known guardian prover")
}