-
Notifications
You must be signed in to change notification settings - Fork 5
/
ssz.go
76 lines (65 loc) · 2.06 KB
/
ssz.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
package eth2deposit
import (
"errors"
"fmt"
ssz "github.com/ferranbt/fastssz"
)
func ComputeDepositDomain(fork_version [4]byte) ([]byte, error) {
domain_type := domainDeposit
fork_data_root, err := ComputeDepositForkDataRoot(fork_version)
if err != nil {
return nil, err
}
return append(domain_type[:], fork_data_root[:28]...), nil
}
func ComputeDepositForkDataRoot(current_version [4]byte) ([]byte, error) {
forkData := new(ForkData)
forkData.CurrentVersion = current_version
forkData.GenesisValidatorRoot = zeroBytes32
err := forkData.HashTreeRootWith(ssz.DefaultHasherPool.Get())
if err != nil {
return nil, err
}
root, err := forkData.HashTreeRoot()
if err != nil {
return nil, err
}
return root[:], nil
}
func ComputeSigningRoot(ssz_object ssz.HashRoot, domain []byte) ([]byte, error) {
if len(domain) != 32 {
return nil, errors.New(fmt.Sprintf("Domain should be in 32 bytes. Got %v.", len(domain)))
}
signingRoot := new(SigningData)
copy(signingRoot.Domain[:], domain)
objectRoot, err := ssz_object.HashTreeRoot()
if err != nil {
return nil, err
}
signingRoot.ObjectRoot = objectRoot
messageToSign, err := signingRoot.HashTreeRoot()
if err != nil {
return nil, err
}
return messageToSign[:], nil
}
type SigningData struct {
ObjectRoot [32]byte `json:"current_version" ssz-size:"32"`
Domain [32]byte `json:"domain" ssz-size:"32"`
}
type ForkData struct {
CurrentVersion [4]byte `json:"current_version" ssz-size:"4"`
GenesisValidatorRoot [32]byte `json:"genesis_validators_root" ssz-size:"32"`
}
type DepositMessage struct {
Pubkey [48]byte `json:"pubkey" ssz-size:"48"`
WithdrawalCredentials [32]byte `json:"withdrawal_credentials" ssz-size:"32"`
Amount uint64 `json:"amount"`
}
type DepositData struct {
Pubkey [48]byte `json:"pubkey" ssz-size:"48"`
WithdrawalCredentials [32]byte `json:"withdrawal_credentials" ssz-size:"32"`
Amount uint64 `json:"amount"`
Signature [96]byte `json:"signature" ssz-size:"96"`
Root [32]byte `ssz:"-"`
}