-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
74 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package share | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"hash" | ||
|
||
"github.com/celestiaorg/rsmt2d" | ||
) | ||
|
||
// DataHash is a representation of the Root hash. | ||
type DataHash []byte | ||
|
||
func (dh DataHash) Validate() error { | ||
if len(dh) != 32 { | ||
return fmt.Errorf("invalid hash size, expected 32, got %d", len(dh)) | ||
} | ||
return nil | ||
} | ||
|
||
func (dh DataHash) String() string { | ||
return fmt.Sprintf("%X", []byte(dh)) | ||
} | ||
|
||
// IsEmptyRoot check whether DataHash corresponds to the root of an empty block EDS. | ||
func (dh DataHash) IsEmptyRoot() bool { | ||
return bytes.Equal(EmptyRoot().Hash(), dh) | ||
} | ||
|
||
// MustDataHashFromString converts a hex string to a valid datahash. | ||
func MustDataHashFromString(datahash string) DataHash { | ||
dh, err := hex.DecodeString(datahash) | ||
if err != nil { | ||
panic(fmt.Sprintf("datahash conversion: passed string was not valid hex: %s", datahash)) | ||
} | ||
err = DataHash(dh).Validate() | ||
if err != nil { | ||
panic(fmt.Sprintf("datahash validation: passed hex string failed: %s", err)) | ||
} | ||
return dh | ||
} | ||
|
||
// NewSHA256Hasher returns a new instance of a SHA-256 hasher. | ||
func NewSHA256Hasher() hash.Hash { | ||
return sha256.New() | ||
} | ||
|
||
// RootHashForCoordinates returns the root hash for the given coordinates. | ||
func RootHashForCoordinates(r *Root, axisType rsmt2d.Axis, rowIdx, colIdx uint) []byte { | ||
if axisType == rsmt2d.Row { | ||
return r.RowRoots[rowIdx] | ||
} | ||
return r.ColumnRoots[colIdx] | ||
} | ||
|
||
// RowsWithNamespace returns indexes of rows, that might contain namespace. | ||
func RowsWithNamespace(root *Root, namespace Namespace) (idxs []int) { | ||
for i, row := range root.RowRoots { | ||
if !namespace.IsOutsideRange(row, row) { | ||
idxs = append(idxs, i) | ||
} | ||
} | ||
return idxs | ||
} |
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