Skip to content

Commit 4a6dcf6

Browse files
committed
proof: return correct error
If the meta data isn't JSON when we expect it to be, we return the correct error.
1 parent 95ea256 commit 4a6dcf6

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

proof/meta.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ func DecodeMetaJSON(jBytes []byte) (map[string]interface{}, error) {
182182
// These bytes must match our metadata size constraints.
183183
err := IsValidMetaSize(jBytes, MetaDataMaxSizeBytes)
184184
if err != nil {
185-
return nil, err
185+
return nil, fmt.Errorf("%w: %s", ErrInvalidJSON, err.Error())
186186
}
187187

188188
// Unmarshal checks internally if the JSON is valid.
189189
err = json.Unmarshal(jBytes, &jMeta)
190190
if err != nil {
191-
return nil, err
191+
return nil, fmt.Errorf("%w: %s", ErrInvalidJSON, err.Error())
192192
}
193193

194194
return jMeta, nil

proof/meta_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package proof
22

33
import (
4+
"bytes"
5+
"encoding/hex"
6+
"os"
7+
"path/filepath"
8+
"strings"
49
"testing"
510

611
"github.com/stretchr/testify/require"
712
)
813

14+
var (
15+
// proofInvalidJsonHexFileName is the name of the file that contains the
16+
// hex proof data for a proof where the meta type is declared as JSON
17+
// but the data is not valid JSON.
18+
proofInvalidJsonHexFileName = filepath.Join(
19+
testDataFileName, "proof-invalid-json-meta-reveal.hex",
20+
)
21+
)
22+
923
func TestValidateMetaReveal(t *testing.T) {
1024
t.Parallel()
1125

@@ -78,3 +92,26 @@ func TestValidateMetaReveal(t *testing.T) {
7892
})
7993
}
8094
}
95+
96+
// TestProofInvalidJsonMetaReveal tests that a proof with a meta reveal that
97+
// is declared as JSON but is not valid JSON will return the correct error when
98+
// trying to decode the decimal display.
99+
func TestProofInvalidJsonMetaReveal(t *testing.T) {
100+
proofHex, err := os.ReadFile(proofInvalidJsonHexFileName)
101+
require.NoError(t, err)
102+
103+
proofBytes, err := hex.DecodeString(
104+
strings.Trim(string(proofHex), "\n"),
105+
)
106+
require.NoError(t, err)
107+
108+
p := &Proof{}
109+
err = p.Decode(bytes.NewReader(proofBytes))
110+
require.NoError(t, err)
111+
112+
require.NotNil(t, p.MetaReveal)
113+
114+
_, decDisplay, err := p.MetaReveal.GetDecDisplay()
115+
require.ErrorIs(t, err, ErrInvalidJSON)
116+
require.Zero(t, decDisplay)
117+
}

proof/testdata/proof-invalid-json-meta-reveal.hex

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)