-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdomain.go
84 lines (72 loc) · 2.06 KB
/
domain.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
package consensus
import (
"encoding/hex"
"fmt"
"strings"
ssz "github.com/ferranbt/fastssz"
)
type Domain [4]byte
func (d *Domain) UnmarshalText(data []byte) error {
domainStr := string(data)
if !strings.HasPrefix(domainStr, "0x") {
return fmt.Errorf("not prefixed")
}
buf, err := hex.DecodeString(domainStr[2:])
if err != nil {
return err
}
if len(buf) != 4 {
return fmt.Errorf("bad size")
}
copy(d[:], buf)
return nil
}
func ToBytes96(b []byte) (res [96]byte) {
copy(res[:], b)
return
}
func ToBytes32(b []byte) (res [32]byte) {
copy(res[:], b)
return
}
func ComputeDomain(domain Domain, forkVersion [4]byte, genesisValidatorsRoot Root) ([32]byte, error) {
// compute_fork_data_root
// this returns the 32byte fork data root for the ``current_version`` and ``genesis_validators_root``.
// This is used primarily in signature domains to avoid collisions across forks/chains.
forkData := ForkData{
CurrentVersion: forkVersion,
GenesisValidatorsRoot: genesisValidatorsRoot,
}
forkRoot, err := forkData.HashTreeRoot()
if err != nil {
return [32]byte{}, err
}
return ToBytes32(append(domain[:], forkRoot[:28]...)), nil
}
func ComputeSigningRoot(domain [32]byte, obj ssz.HashRoot) ([32]byte, error) {
unsignedMsgRoot, err := obj.HashTreeRoot()
if err != nil {
return [32]byte{}, err
}
root, err := ssz.HashWithDefaultHasher(&SigningData{
ObjectRoot: unsignedMsgRoot,
Domain: domain,
})
if err != nil {
return [32]byte{}, err
}
return root, nil
}
type DomainType Domain
var (
DomainBeaconProposerType = Domain{0, 0, 0, 0}
DomainBeaconAttesterType = Domain{1, 0, 0, 0}
DomainRandaomType = Domain{2, 0, 0, 0}
DomainDepositType = Domain{3, 0, 0, 0}
DomainVoluntaryExitType = Domain{4, 0, 0, 0}
DomainSelectionProofType = Domain{5, 0, 0, 0}
DomainAggregateAndProofType = Domain{6, 0, 0, 0}
DomainSyncCommitteeType = Domain{7, 0, 0, 0}
DomainSyncCommitteeSelectionProof = Domain{8, 0, 0, 0}
DomainContributionAndProof = Domain{9, 0, 0, 0}
)