Skip to content

Commit

Permalink
feat: add MarshalJson support for ByteString and Blake2b224 with tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Oct 12, 2024
1 parent 69fdfa5 commit 6c36292
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cbor/bytestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cbor

import (
"encoding/hex"
"encoding/json"

_cbor "github.com/fxamacker/cbor/v2"
)
Expand All @@ -36,3 +37,8 @@ func NewByteString(data []byte) ByteString {
func (bs ByteString) String() string {
return hex.EncodeToString(bs.Bytes())
}

// MarshalJSON converts ByteString to a JSON-compatible string
func (bs ByteString) MarshalJSON() ([]byte, error) {
return json.Marshal(bs.String())
}
60 changes: 60 additions & 0 deletions cbor/bytestring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cbor

import (
"encoding/json"
"testing"
)

// Test the String method to ensure it properly converts ByteString to hex.
func TestByteString_String(t *testing.T) {
data := []byte("blinklabs") // "blinklabs" as bytes
bs := NewByteString(data)

expected := "626c696e6b6c616273" // "blinklabs" in hex
actual := bs.String()

if actual != expected {
t.Errorf("expected %s but got %s", expected, actual)
}
}

// Test the MarshalJSON method to ensure it properly marshals ByteString to JSON as hex.
func TestByteString_MarshalJSON(t *testing.T) {
data := []byte("blinklabs") // "blinklabs" as bytes
bs := NewByteString(data)

jsonData, err := json.Marshal(bs)
if err != nil {
t.Fatalf("failed to marshal ByteString: %v", err)
}

// Expected JSON result, hex-encoded string
expectedJSON := `"626c696e6b6c616273"` // "blinklabs" in hex

if string(jsonData) != expectedJSON {
t.Errorf("expected %s but got %s", expectedJSON, string(jsonData))
}
}

// Test NewByteString to ensure it properly wraps the byte slice
func TestNewByteString(t *testing.T) {
data := []byte{0x41, 0x42, 0x43} // "ABC" in hex
bs := NewByteString(data)

if string(bs.Bytes()) != "ABC" {
t.Errorf("expected ABC but got %s", string(bs.Bytes()))
}
}
4 changes: 4 additions & 0 deletions ledger/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (b Blake2b224) Bytes() []byte {
return b[:]
}

func (b Blake2b224) MarshalJSON() ([]byte, error) {
return json.Marshal(b.String())
}

// Blake2b224Hash generates a Blake2b-224 hash from the provided data
func Blake2b224Hash(data []byte) Blake2b224 {
tmpHash, err := blake2b.New(Blake2b224Size, nil)
Expand Down
36 changes: 36 additions & 0 deletions ledger/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,39 @@ func TestMultiAssetJson(t *testing.T) {
}
}
}

// Test the MarshalJSON method for Blake2b224 to ensure it properly converts to JSON.
func TestBlake2b224_MarshalJSON(t *testing.T) {
// Example data to represent Blake2b224 hash
data := []byte("blinklabs")
hash := NewBlake2b224(data)

// Blake2b224 always produces 28 bytes (224 bits) as its output.
// Expected JSON value: the hex-encoded string of "blinklabs" padded to fit 28 bytes.
// JSON marshalling adds quotes around the string, so include quotes in expected value.
expected := `"626c696e6b6c61627300000000000000000000000000000000000000"`

// Marshal the Blake2b224 object to JSON
jsonData, err := json.Marshal(hash)
if err != nil {
t.Fatalf("failed to marshal Blake2b224: %v", err)
}

// Compare the result with the expected output
if string(jsonData) != expected {
t.Errorf("expected %s but got %s", expected, string(jsonData))
}
}

func TestBlake2b224_String(t *testing.T) {
data := []byte("blinklabs") // Less than 28 bytes
hash := NewBlake2b224(data)

// Expected hex string for "blinklabs" padded/truncated to fit 28 bytes
expected := "626c696e6b6c61627300000000000000000000000000000000000000"

// Verify if String() gives the correct hex-encoded string
if hash.String() != expected {
t.Errorf("expected %s but got %s", expected, hash.String())
}
}

0 comments on commit 6c36292

Please sign in to comment.