Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blob)!: tendermint compatible commitment proof json marshall #3929

Open
wants to merge 8 commits into
base: feature/api-breaks
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading