github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neorpc/result/mpt.go (about) 1 package result 2 3 import ( 4 "bytes" 5 "encoding/base64" 6 "encoding/json" 7 8 "github.com/nspcc-dev/neo-go/pkg/io" 9 ) 10 11 // StateHeight is a result of getstateheight RPC. 12 type StateHeight struct { 13 Local uint32 `json:"localrootindex"` 14 Validated uint32 `json:"validatedrootindex"` 15 } 16 17 // ProofWithKey represens a key-proof pair. 18 type ProofWithKey struct { 19 Key []byte 20 Proof [][]byte 21 } 22 23 // VerifyProof is a result of verifyproof RPC. 24 // nil Value is considered invalid. 25 type VerifyProof struct { 26 Value []byte 27 } 28 29 // MarshalJSON implements the json.Marshaler. 30 func (p *ProofWithKey) MarshalJSON() ([]byte, error) { 31 w := io.NewBufBinWriter() 32 p.EncodeBinary(w.BinWriter) 33 if w.Err != nil { 34 return nil, w.Err 35 } 36 return []byte(`"` + base64.StdEncoding.EncodeToString(w.Bytes()) + `"`), nil 37 } 38 39 // EncodeBinary implements io.Serializable. 40 func (p *ProofWithKey) EncodeBinary(w *io.BinWriter) { 41 w.WriteVarBytes(p.Key) 42 w.WriteVarUint(uint64(len(p.Proof))) 43 for i := range p.Proof { 44 w.WriteVarBytes(p.Proof[i]) 45 } 46 } 47 48 // DecodeBinary implements io.Serializable. 49 func (p *ProofWithKey) DecodeBinary(r *io.BinReader) { 50 p.Key = r.ReadVarBytes() 51 sz := r.ReadVarUint() 52 for i := uint64(0); i < sz; i++ { 53 p.Proof = append(p.Proof, r.ReadVarBytes()) 54 } 55 } 56 57 // UnmarshalJSON implements the json.Unmarshaler. 58 func (p *ProofWithKey) UnmarshalJSON(data []byte) error { 59 var s string 60 if err := json.Unmarshal(data, &s); err != nil { 61 return err 62 } 63 return p.FromString(s) 64 } 65 66 // String implements fmt.Stringer. 67 func (p *ProofWithKey) String() string { 68 w := io.NewBufBinWriter() 69 p.EncodeBinary(w.BinWriter) 70 return base64.StdEncoding.EncodeToString(w.Bytes()) 71 } 72 73 // FromString decodes p from hex-encoded string. 74 func (p *ProofWithKey) FromString(s string) error { 75 rawProof, err := base64.StdEncoding.DecodeString(s) 76 if err != nil { 77 return err 78 } 79 r := io.NewBinReaderFromBuf(rawProof) 80 p.DecodeBinary(r) 81 return r.Err 82 } 83 84 // MarshalJSON implements the json.Marshaler. 85 func (p *VerifyProof) MarshalJSON() ([]byte, error) { 86 if p.Value == nil { 87 return []byte(`"invalid"`), nil 88 } 89 return []byte(`"` + base64.StdEncoding.EncodeToString(p.Value) + `"`), nil 90 } 91 92 // UnmarshalJSON implements the json.Unmarshaler. 93 func (p *VerifyProof) UnmarshalJSON(data []byte) error { 94 if bytes.Equal(data, []byte(`"invalid"`)) { 95 p.Value = nil 96 return nil 97 } 98 var m string 99 if err := json.Unmarshal(data, &m); err != nil { 100 return err 101 } 102 b, err := base64.StdEncoding.DecodeString(m) 103 if err != nil { 104 return err 105 } 106 p.Value = b 107 return nil 108 }