Skip to content

Commit 9b64710

Browse files
committed
fix(blob): tendermint compatible commitment proof json marshall
1 parent ace6840 commit 9b64710

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

blob/commitment_proof.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package blob
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78

@@ -11,6 +12,7 @@ import (
1112
libshare "github.com/celestiaorg/go-square/v2/share"
1213
"github.com/celestiaorg/nmt"
1314
"github.com/celestiaorg/nmt/namespace"
15+
tmjson "github.com/tendermint/tendermint/libs/json"
1416
)
1517

1618
// Commitment is a Merkle Root of the subtree built from shares of the Blob.
@@ -153,3 +155,41 @@ func (commitmentProof *CommitmentProof) Verify(root []byte, subtreeRootThreshold
153155
// verify row roots to data root proof
154156
return commitmentProof.RowProof.VerifyProof(root), nil
155157
}
158+
159+
// MarshalJSON marshals an CommitmentProof to JSON. Uses tendermint encoder for row proof for compatibility.
160+
func (commitmentProof *CommitmentProof) MarshalJSON() ([]byte, error) {
161+
type Alias CommitmentProof
162+
rowProof, err := tmjson.Marshal(commitmentProof.RowProof)
163+
if err != nil {
164+
return nil, err
165+
}
166+
return json.Marshal(&struct {
167+
RowProof json.RawMessage `json:"row_proof"`
168+
*Alias
169+
}{
170+
RowProof: rowProof,
171+
Alias: (*Alias)(commitmentProof),
172+
})
173+
}
174+
175+
// UnmarshalJSON unmarshals an CommitmentProof from JSON. Uses tendermint decoder for row proof for compatibility.
176+
func (commitmentProof *CommitmentProof) UnmarshalJSON(data []byte) error {
177+
type Alias CommitmentProof
178+
aux := &struct {
179+
RowProof json.RawMessage `json:"row_proof"`
180+
*Alias
181+
}{
182+
Alias: (*Alias)(commitmentProof),
183+
}
184+
if err := json.Unmarshal(data, &aux); err != nil {
185+
return err
186+
}
187+
rowProof := proof.RowProof{}
188+
if err := tmjson.Unmarshal(aux.RowProof, rowProof); err != nil {
189+
return err
190+
}
191+
192+
commitmentProof.RowProof = rowProof
193+
194+
return nil
195+
}

0 commit comments

Comments
 (0)