Skip to content

Commit

Permalink
fix(blob): tendermint compatible commitment proof json marshall
Browse files Browse the repository at this point in the history
  • Loading branch information
zvolin committed Nov 11, 2024
1 parent ace6840 commit 9b64710
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions blob/commitment_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blob

import (
"bytes"
"encoding/json"
"errors"
"fmt"

Expand All @@ -11,6 +12,7 @@ import (
libshare "github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
tmjson "github.com/tendermint/tendermint/libs/json"
)

// Commitment is a Merkle Root of the subtree built from shares of the Blob.
Expand Down Expand Up @@ -153,3 +155,41 @@ func (commitmentProof *CommitmentProof) Verify(root []byte, subtreeRootThreshold
// verify row roots to data root proof
return commitmentProof.RowProof.VerifyProof(root), nil
}

// MarshalJSON marshals an CommitmentProof to JSON. Uses tendermint encoder for row proof for compatibility.
func (commitmentProof *CommitmentProof) MarshalJSON() ([]byte, error) {
type Alias CommitmentProof
rowProof, err := tmjson.Marshal(commitmentProof.RowProof)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
RowProof json.RawMessage `json:"row_proof"`
*Alias
}{
RowProof: rowProof,
Alias: (*Alias)(commitmentProof),
})
}

// UnmarshalJSON unmarshals an CommitmentProof from JSON. Uses tendermint decoder for row proof for compatibility.
func (commitmentProof *CommitmentProof) UnmarshalJSON(data []byte) error {
type Alias CommitmentProof
aux := &struct {
RowProof json.RawMessage `json:"row_proof"`
*Alias
}{
Alias: (*Alias)(commitmentProof),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
rowProof := proof.RowProof{}
if err := tmjson.Unmarshal(aux.RowProof, rowProof); err != nil {
return err
}

commitmentProof.RowProof = rowProof

return nil
}

0 comments on commit 9b64710

Please sign in to comment.