rpc: implement verifyproof RPC
Test getproof and verifyproof together.
This commit is contained in:
parent
e38e8aa48a
commit
8e60a65b55
4 changed files with 108 additions and 0 deletions
|
@ -1,8 +1,10 @@
|
|||
package result
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
)
|
||||
|
@ -25,6 +27,12 @@ type GetProof struct {
|
|||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// VerifyProof is a result of verifyproof RPC.
|
||||
// nil Value is considered invalid.
|
||||
type VerifyProof struct {
|
||||
Value []byte
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (p *ProofWithKey) MarshalJSON() ([]byte, error) {
|
||||
w := io.NewBufBinWriter()
|
||||
|
@ -79,3 +87,36 @@ func (p *ProofWithKey) FromString(s string) error {
|
|||
p.DecodeBinary(r)
|
||||
return r.Err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (p *VerifyProof) MarshalJSON() ([]byte, error) {
|
||||
if p.Value == nil {
|
||||
return []byte(`"invalid"`), nil
|
||||
}
|
||||
return []byte(`{"value":"` + hex.EncodeToString(p.Value) + `"}`), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
func (p *VerifyProof) UnmarshalJSON(data []byte) error {
|
||||
if bytes.Equal(data, []byte(`"invalid"`)) {
|
||||
p.Value = nil
|
||||
return nil
|
||||
}
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(m) != 1 {
|
||||
return errors.New("must have single key")
|
||||
}
|
||||
v, ok := m["value"]
|
||||
if !ok {
|
||||
return errors.New("invalid json")
|
||||
}
|
||||
b, err := hex.DecodeString(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Value = b
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -55,3 +55,14 @@ func TestProofWithKey_EncodeString(t *testing.T) {
|
|||
require.NoError(t, actual.FromString(expected.String()))
|
||||
require.Equal(t, expected, &actual)
|
||||
}
|
||||
|
||||
func TestVerifyProof_MarshalJSON(t *testing.T) {
|
||||
t.Run("Good", func(t *testing.T) {
|
||||
vp := &VerifyProof{random.Bytes(100)}
|
||||
testserdes.MarshalUnmarshalJSON(t, vp, new(VerifyProof))
|
||||
})
|
||||
t.Run("NoValue", func(t *testing.T) {
|
||||
vp := new(VerifyProof)
|
||||
testserdes.MarshalUnmarshalJSON(t, vp, &VerifyProof{[]byte{1, 2, 3}})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue