Skip to content

Commit c442124

Browse files
authored
feat: add MarshalJson support for ByteString and Blake2b224 with tests (#756)
Signed-off-by: Ales Verbic <[email protected]>
1 parent 69fdfa5 commit c442124

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

cbor/bytestring.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cbor
1616

1717
import (
1818
"encoding/hex"
19+
"encoding/json"
1920

2021
_cbor "github.com/fxamacker/cbor/v2"
2122
)
@@ -36,3 +37,8 @@ func NewByteString(data []byte) ByteString {
3637
func (bs ByteString) String() string {
3738
return hex.EncodeToString(bs.Bytes())
3839
}
40+
41+
// MarshalJSON converts ByteString to a JSON-compatible string
42+
func (bs ByteString) MarshalJSON() ([]byte, error) {
43+
return json.Marshal(bs.String())
44+
}

cbor/bytestring_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2023 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package cbor
15+
16+
import (
17+
"encoding/json"
18+
"testing"
19+
)
20+
21+
// Test the String method to ensure it properly converts ByteString to hex.
22+
func TestByteString_String(t *testing.T) {
23+
data := []byte("blinklabs") // "blinklabs" as bytes
24+
bs := NewByteString(data)
25+
26+
expected := "626c696e6b6c616273" // "blinklabs" in hex
27+
actual := bs.String()
28+
29+
if actual != expected {
30+
t.Errorf("expected %s but got %s", expected, actual)
31+
}
32+
}
33+
34+
// Test the MarshalJSON method to ensure it properly marshals ByteString to JSON as hex.
35+
func TestByteString_MarshalJSON(t *testing.T) {
36+
data := []byte("blinklabs") // "blinklabs" as bytes
37+
bs := NewByteString(data)
38+
39+
jsonData, err := json.Marshal(bs)
40+
if err != nil {
41+
t.Fatalf("failed to marshal ByteString: %v", err)
42+
}
43+
44+
// Expected JSON result, hex-encoded string
45+
expectedJSON := `"626c696e6b6c616273"` // "blinklabs" in hex
46+
47+
if string(jsonData) != expectedJSON {
48+
t.Errorf("expected %s but got %s", expectedJSON, string(jsonData))
49+
}
50+
}
51+
52+
// Test NewByteString to ensure it properly wraps the byte slice
53+
func TestNewByteString(t *testing.T) {
54+
data := []byte{0x41, 0x42, 0x43} // "ABC" in hex
55+
bs := NewByteString(data)
56+
57+
if string(bs.Bytes()) != "ABC" {
58+
t.Errorf("expected ABC but got %s", string(bs.Bytes()))
59+
}
60+
}

ledger/common/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (b Blake2b224) Bytes() []byte {
7878
return b[:]
7979
}
8080

81+
func (b Blake2b224) MarshalJSON() ([]byte, error) {
82+
return json.Marshal(b.String())
83+
}
84+
8185
// Blake2b224Hash generates a Blake2b-224 hash from the provided data
8286
func Blake2b224Hash(data []byte) Blake2b224 {
8387
tmpHash, err := blake2b.New(Blake2b224Size, nil)

ledger/common/common_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,39 @@ func TestMultiAssetJson(t *testing.T) {
125125
}
126126
}
127127
}
128+
129+
// Test the MarshalJSON method for Blake2b224 to ensure it properly converts to JSON.
130+
func TestBlake2b224_MarshalJSON(t *testing.T) {
131+
// Example data to represent Blake2b224 hash
132+
data := []byte("blinklabs")
133+
hash := NewBlake2b224(data)
134+
135+
// Blake2b224 always produces 28 bytes (224 bits) as its output.
136+
// Expected JSON value: the hex-encoded string of "blinklabs" padded to fit 28 bytes.
137+
// JSON marshalling adds quotes around the string, so include quotes in expected value.
138+
expected := `"626c696e6b6c61627300000000000000000000000000000000000000"`
139+
140+
// Marshal the Blake2b224 object to JSON
141+
jsonData, err := json.Marshal(hash)
142+
if err != nil {
143+
t.Fatalf("failed to marshal Blake2b224: %v", err)
144+
}
145+
146+
// Compare the result with the expected output
147+
if string(jsonData) != expected {
148+
t.Errorf("expected %s but got %s", expected, string(jsonData))
149+
}
150+
}
151+
152+
func TestBlake2b224_String(t *testing.T) {
153+
data := []byte("blinklabs") // Less than 28 bytes
154+
hash := NewBlake2b224(data)
155+
156+
// Expected hex string for "blinklabs" padded/truncated to fit 28 bytes
157+
expected := "626c696e6b6c61627300000000000000000000000000000000000000"
158+
159+
// Verify if String() gives the correct hex-encoded string
160+
if hash.String() != expected {
161+
t.Errorf("expected %s but got %s", expected, hash.String())
162+
}
163+
}

0 commit comments

Comments
 (0)