Skip to content

Commit 9166543

Browse files
committed
proof: add new MetaJson meta type
In this commit, we add a new meta json type. We update the validation logic to ensure we obtain a valid JSON string, add additional tests, and prep for an itest with a more descriptive error message.
1 parent 5e25af7 commit 9166543

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

proof/meta.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package proof
33
import (
44
"bytes"
55
"crypto/sha256"
6+
"encoding/json"
67
"errors"
78
"io"
89

@@ -18,6 +19,11 @@ const (
1819
// bytes without any specific interpretation.
1920
MetaOpaque MetaType = 0
2021

22+
// MetaJson signals that the meta data is a JSON object.
23+
MetaJson MetaType = 1
24+
)
25+
26+
const (
2127
// MetaDataMaxSizeBytes is the maximum length of the meta data. We limit
2228
// this to 1MiB for now. This should be of sufficient size to commit to
2329
// any JSON data or even medium resolution images. If there is need to
@@ -35,6 +41,9 @@ var (
3541

3642
// ErrMetaDataTooLarge signals that the meta data is too large.
3743
ErrMetaDataTooLarge = errors.New("meta data too large")
44+
45+
// ErrInvalidJSON signals that the meta data is not a valid JSON.
46+
ErrInvalidJSON = errors.New("invalid JSON")
3847
)
3948

4049
// MetaReveal is an optional TLV type that can be added to the proof of a
@@ -66,6 +75,13 @@ func (m *MetaReveal) Validate() error {
6675
return ErrMetaDataTooLarge
6776
}
6877

78+
// If the type is JSON, then it should be parseable as a JSON string.
79+
if m.Type == MetaJson {
80+
if !json.Valid(m.Data) {
81+
return ErrInvalidJSON
82+
}
83+
}
84+
6985
return nil
7086
}
7187

proof/meta_test.go

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,53 @@ func TestValidateMetaReveal(t *testing.T) {
1313
name string
1414
reveal *MetaReveal
1515
expectedErr error
16-
}{{
17-
name: "nil reveal",
18-
reveal: nil,
19-
expectedErr: nil,
20-
}, {
21-
name: "valid reveal",
22-
reveal: &MetaReveal{
23-
Type: MetaOpaque,
24-
Data: []byte("data"),
16+
}{
17+
{
18+
name: "nil reveal",
19+
reveal: nil,
20+
expectedErr: nil,
2521
},
26-
expectedErr: nil,
27-
}, {
28-
name: "missing data",
29-
reveal: &MetaReveal{
30-
Type: MetaOpaque,
31-
Data: nil,
22+
{
23+
name: "valid reveal",
24+
reveal: &MetaReveal{
25+
Type: MetaOpaque,
26+
Data: []byte("data"),
27+
},
28+
expectedErr: nil,
3229
},
33-
expectedErr: ErrMetaDataMissing,
34-
}, {
35-
name: "too much data",
36-
reveal: &MetaReveal{
37-
Type: MetaOpaque,
38-
Data: make([]byte, MetaDataMaxSizeBytes+1),
30+
{
31+
name: "missing data",
32+
reveal: &MetaReveal{
33+
Type: MetaOpaque,
34+
Data: nil,
35+
},
36+
expectedErr: ErrMetaDataMissing,
3937
},
40-
expectedErr: ErrMetaDataTooLarge,
41-
}}
38+
{
39+
name: "too much data",
40+
reveal: &MetaReveal{
41+
Type: MetaOpaque,
42+
Data: make([]byte, MetaDataMaxSizeBytes+1),
43+
},
44+
expectedErr: ErrMetaDataTooLarge,
45+
},
46+
{
47+
name: "invalid JSON",
48+
reveal: &MetaReveal{
49+
Type: MetaJson,
50+
Data: []byte("invalid"),
51+
},
52+
expectedErr: ErrInvalidJSON,
53+
},
54+
{
55+
name: "valid JSON",
56+
reveal: &MetaReveal{
57+
Type: MetaJson,
58+
Data: []byte(`{"key": "value"}`),
59+
},
60+
expectedErr: nil,
61+
},
62+
}
4263

4364
for _, tc := range testCases {
4465
tc := tc

rpcserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func (r *rpcServer) MintAsset(ctx context.Context,
455455
// If the asset meta field was specified, then the data inside
456456
// must be valid. Let's check that now.
457457
if err := seedling.Meta.Validate(); err != nil {
458-
return nil, err
458+
return nil, fmt.Errorf("invalid asset meta: %v", err)
459459
}
460460
}
461461

0 commit comments

Comments
 (0)