forked from berachain/beacon-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for signed beacon block header and signature verification (be…
…rachain#2245) Co-authored-by: Giuseppe Cocomazzi <[email protected]>
- Loading branch information
1 parent
98d1225
commit 5246cf5
Showing
20 changed files
with
549 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ site/ | |
contracts/cache | ||
contracts/out | ||
tmp/ | ||
cscope.files | ||
|
||
########## | ||
# Golang # | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2024, Berachain Foundation. All rights reserved. | ||
// Use of this software is governed by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package types | ||
|
||
import ( | ||
"github.com/berachain/beacon-kit/primitives/common" | ||
"github.com/berachain/beacon-kit/primitives/constraints" | ||
"github.com/berachain/beacon-kit/primitives/crypto" | ||
"github.com/karalabe/ssz" | ||
) | ||
|
||
var ( | ||
_ ssz.StaticObject = (*SignedBeaconBlockHeader)(nil) | ||
_ constraints.SSZMarshallableRootable = (*SignedBeaconBlockHeader)(nil) | ||
) | ||
|
||
type SignedBeaconBlockHeader struct { | ||
Header *BeaconBlockHeader `json:"header"` | ||
Signature crypto.BLSSignature `json:"signature"` | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Constructor */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
// NewSignedBeaconBlockHeader creates a new BeaconBlockHeader. | ||
func NewSignedBeaconBlockHeader( | ||
header *BeaconBlockHeader, | ||
signature crypto.BLSSignature, | ||
) *SignedBeaconBlockHeader { | ||
return &SignedBeaconBlockHeader{ | ||
header, signature, | ||
} | ||
} | ||
|
||
// Empty creates an empty BeaconBlockHeader instance. | ||
func (*SignedBeaconBlockHeader) Empty() *SignedBeaconBlockHeader { | ||
return &SignedBeaconBlockHeader{} | ||
} | ||
|
||
// New creates a new SignedBeaconBlockHeader. | ||
func (b *SignedBeaconBlockHeader) New( | ||
header *BeaconBlockHeader, | ||
signature crypto.BLSSignature, | ||
) *SignedBeaconBlockHeader { | ||
return NewSignedBeaconBlockHeader( | ||
header, signature, | ||
) | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* SSZ */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
// SizeSSZ returns the size of the SignedBeaconBlockHeader object | ||
// in SSZ encoding. Total size: Header (112) + Signature (96). | ||
func (b *SignedBeaconBlockHeader) SizeSSZ(sizer *ssz.Sizer) uint32 { | ||
//nolint:mnd // no magic | ||
size := (*BeaconBlockHeader)(nil).SizeSSZ(sizer) + 96 | ||
return size | ||
} | ||
|
||
// DefineSSZ defines the SSZ encoding for the SignedBeaconBlockHeader object. | ||
func (b *SignedBeaconBlockHeader) DefineSSZ(codec *ssz.Codec) { | ||
ssz.DefineStaticObject(codec, &b.Header) | ||
ssz.DefineStaticBytes(codec, &b.Signature) | ||
} | ||
|
||
// MarshalSSZ marshals the SignedBeaconBlockHeader object to SSZ format. | ||
func (b *SignedBeaconBlockHeader) MarshalSSZ() ([]byte, error) { | ||
buf := make([]byte, ssz.Size(b)) | ||
return buf, ssz.EncodeToBytes(buf, b) | ||
} | ||
|
||
// UnmarshalSSZ unmarshals the SignedBeaconBlockHeader object from SSZ format. | ||
func (b *SignedBeaconBlockHeader) UnmarshalSSZ(buf []byte) error { | ||
return ssz.DecodeFromBytes(buf, b) | ||
} | ||
|
||
// HashTreeRoot computes the SSZ hash tree root of the | ||
// SignedBeaconBlockHeader object. | ||
func (b *SignedBeaconBlockHeader) HashTreeRoot() common.Root { | ||
return ssz.HashSequential(b) | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Getters and Setters */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
// Getheader retrieves the header of the SignedBeaconBlockHeader. | ||
func (b *SignedBeaconBlockHeader) GetHeader() *BeaconBlockHeader { | ||
return b.Header | ||
} | ||
|
||
// Setheader sets the header of the BeaconBlockHeader. | ||
func (b *SignedBeaconBlockHeader) SetHeader(header *BeaconBlockHeader) { | ||
b.Header = header | ||
} | ||
|
||
// GetSignature retrieves the Signature of the SignedBeaconBlockHeader. | ||
func (b *SignedBeaconBlockHeader) GetSignature() crypto.BLSSignature { | ||
return b.Signature | ||
} | ||
|
||
// SetSignature sets the Signature of the BeaconBlockHeader. | ||
func (b *SignedBeaconBlockHeader) SetSignature(signature crypto.BLSSignature) { | ||
b.Signature = signature | ||
} | ||
|
||
func (b *SignedBeaconBlockHeader) IsNil() bool { | ||
return b == nil | ||
} |
Oops, something went wrong.