Skip to content

Commit c4a1a62

Browse files
authored
api: accept uneven length hex strings (#81)
1 parent a9e5ae2 commit c4a1a62

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

api/tree.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ func encodeProof(p [][]byte) []string {
8282
}
8383

8484
type createTreeReq struct {
85-
Leaves []hexutil.Bytes `json:"unhashedLeaves"`
86-
Ltd []string `json:"leafTypeDescriptor"`
87-
Packed bool `json:"packedEncoding"`
85+
Leaves []string `json:"unhashedLeaves"`
86+
Ltd []string `json:"leafTypeDescriptor"`
87+
Packed bool `json:"packedEncoding"`
8888
}
8989

9090
type createTreeResp struct {
@@ -98,7 +98,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
9898
)
9999
defer r.Body.Close()
100100
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
101-
s.sendJSONError(r, w, err, http.StatusBadRequest, "unhashedLeaves must be a list of hex strings")
101+
s.sendJSONError(r, w, err, http.StatusBadRequest, "invalid request body")
102102
return
103103
}
104104
switch len(req.Leaves) {
@@ -110,11 +110,13 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
110110
return
111111
}
112112

113-
//convert []hexutil.Bytes to [][]byte
114113
var leaves [][]byte
115-
for i := range req.Leaves {
116-
leaves = append(leaves, req.Leaves[i])
114+
for _, l := range req.Leaves {
115+
// use the go-ethereum HexDecode method because it is more
116+
// lenient and will allow for odd-length hex strings (by padding them)
117+
leaves = append(leaves, common.FromHex(l))
117118
}
119+
118120
tree := merkle.New(leaves)
119121

120122
type proofItem struct {
@@ -123,7 +125,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
123125
Proof []string `json:"proof"`
124126
}
125127
var proofs = []proofItem{}
126-
for _, l := range req.Leaves {
128+
for _, l := range leaves {
127129
pf := tree.Proof(l)
128130
if !merkle.Valid(tree.Root(), pf, l) {
129131
s.sendJSONError(r, w, nil, http.StatusBadRequest, "Unable to generate proof for tree")
@@ -148,7 +150,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
148150
`
149151
_, err := s.db.Exec(ctx, q,
150152
tree.Root(),
151-
req.Leaves,
153+
leaves,
152154
req.Ltd,
153155
req.Packed,
154156
proofs,

0 commit comments

Comments
 (0)