From c442124248089e3f2f56904e712f5051bdfe9381 Mon Sep 17 00:00:00 2001 From: Ales Verbic Date: Fri, 11 Oct 2024 20:32:27 -0400 Subject: [PATCH] feat: add MarshalJson support for ByteString and Blake2b224 with tests (#756) Signed-off-by: Ales Verbic --- cbor/bytestring.go | 6 ++++ cbor/bytestring_test.go | 60 ++++++++++++++++++++++++++++++++++++ ledger/common/common.go | 4 +++ ledger/common/common_test.go | 36 ++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 cbor/bytestring_test.go diff --git a/cbor/bytestring.go b/cbor/bytestring.go index 7ea38db1..1b7fd501 100644 --- a/cbor/bytestring.go +++ b/cbor/bytestring.go @@ -16,6 +16,7 @@ package cbor import ( "encoding/hex" + "encoding/json" _cbor "github.com/fxamacker/cbor/v2" ) @@ -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()) +} diff --git a/cbor/bytestring_test.go b/cbor/bytestring_test.go new file mode 100644 index 00000000..8060f879 --- /dev/null +++ b/cbor/bytestring_test.go @@ -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())) + } +} diff --git a/ledger/common/common.go b/ledger/common/common.go index 98bc9fff..0d84b49a 100644 --- a/ledger/common/common.go +++ b/ledger/common/common.go @@ -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) diff --git a/ledger/common/common_test.go b/ledger/common/common_test.go index 3ca1ab64..a4eba4a8 100644 --- a/ledger/common/common_test.go +++ b/ledger/common/common_test.go @@ -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()) + } +}