diff --git a/types/block.go b/types/block.go index 858969852f..662478b7c0 100644 --- a/types/block.go +++ b/types/block.go @@ -1054,6 +1054,11 @@ type Blob struct { ShareVersion uint8 } +// Namespace returns the namespace of this blob encoded as a byte slice. +func (b Blob) Namespace() []byte { + return append([]byte{b.NamespaceVersion}, b.NamespaceID...) +} + // StringIndented returns an indented string representation of the transactions. func (data *Data) StringIndented(indent string) string { if data == nil { diff --git a/types/block_test.go b/types/block_test.go index b20986137b..0190e47628 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -856,3 +856,27 @@ func TestBlockIDEquals(t *testing.T) { assert.True(t, blockIDEmpty.Equals(blockIDEmpty)) assert.False(t, blockIDEmpty.Equals(blockIDDifferent)) } + +func TestBlob(t *testing.T) { + namespaceVersion := uint8(0) + namespaceID := stdbytes.Repeat([]byte{0x01}, 28) + data := []byte("data") + shareVersion := uint8(0) + + blob := Blob{ + NamespaceVersion: namespaceVersion, + NamespaceID: namespaceID, + Data: data, + ShareVersion: shareVersion, + } + + t.Run("blob.Namespace() returns encoded namespace", func(t *testing.T) { + got := blob.Namespace() + want := []byte{ + 0, // namespace version + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // namespace ID + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // namespace ID + } + assert.Equal(t, want, got) + }) +}