-
Notifications
You must be signed in to change notification settings - Fork 8
/
submitblockrequest_json.go
72 lines (61 loc) · 1.94 KB
/
submitblockrequest_json.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
package deneb
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
v1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
)
// submitBlockRequestJSON is the spec representation of the struct.
type submitBlockRequestJSON struct {
Message *v1.BidTrace `json:"message"`
ExecutionPayload *deneb.ExecutionPayload `json:"execution_payload"`
BlobsBundle *BlobsBundle `json:"blobs_bundle"`
Signature string `json:"signature"`
}
// MarshalJSON implements json.Marshaler.
func (s *SubmitBlockRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(&submitBlockRequestJSON{
Signature: fmt.Sprintf("%#x", s.Signature),
Message: s.Message,
ExecutionPayload: s.ExecutionPayload,
BlobsBundle: s.BlobsBundle,
})
}
// UnmarshalJSON implements json.Unmarshaler.
func (s *SubmitBlockRequest) UnmarshalJSON(input []byte) error {
var data submitBlockRequestJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
return s.unpack(&data)
}
func (s *SubmitBlockRequest) unpack(data *submitBlockRequestJSON) error {
if data.Message == nil {
return errors.New("message missing")
}
s.Message = data.Message
if data.Signature == "" {
return errors.New("signature missing")
}
signature, err := hex.DecodeString(strings.TrimPrefix(data.Signature, "0x"))
if err != nil {
return errors.Wrap(err, "invalid signature")
}
if len(signature) != phase0.SignatureLength {
return errors.New("incorrect length for signature")
}
copy(s.Signature[:], signature)
if data.ExecutionPayload == nil {
return errors.New("execution payload missing")
}
s.ExecutionPayload = data.ExecutionPayload
if data.BlobsBundle == nil {
return errors.New("blobs bundle missing")
}
s.BlobsBundle = data.BlobsBundle
return nil
}