diff --git a/nodebuilder/share/share.go b/nodebuilder/share/share.go index 8a3efcc757..3d64e2ec4f 100644 --- a/nodebuilder/share/share.go +++ b/nodebuilder/share/share.go @@ -2,7 +2,9 @@ package share import ( "context" + "encoding/json" + tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/types" libshare "github.com/celestiaorg/go-square/v2/share" @@ -23,6 +25,44 @@ type GetRangeResult struct { Proof *types.ShareProof } +// MarshalJSON marshals an GetRangeResult to JSON. Uses tendermint encoder for proof for compatibility. +func (getRangeResult *GetRangeResult) MarshalJSON() ([]byte, error) { + type Alias GetRangeResult + proof, err := tmjson.Marshal(getRangeResult.Proof) + if err != nil { + return nil, err + } + return json.Marshal(&struct { + Proof json.RawMessage `json:"Proof"` + *Alias + }{ + Proof: proof, + Alias: (*Alias)(getRangeResult), + }) +} + +// UnmarshalJSON unmarshals an GetRangeResult from JSON. Uses tendermint decoder for proof for compatibility. +func (getRangeResult *GetRangeResult) UnmarshalJSON(data []byte) error { + type Alias GetRangeResult + aux := &struct { + Proof json.RawMessage `json:"Proof"` + *Alias + }{ + Alias: (*Alias)(getRangeResult), + } + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + proof := new(types.ShareProof) + if err := tmjson.Unmarshal(aux.Proof, proof); err != nil { + return err + } + + getRangeResult.Proof = proof + + return nil +} + // Module provides access to any data square or block share on the network. // // All Get methods provided on Module follow the following flow: