native/std: add base58Check* methods, close #1974

This commit is contained in:
Evgeniy Stratonikov 2021-05-25 17:31:57 +03:00
parent 399584504e
commit 192771a58a
2 changed files with 56 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
base58neogo "github.com/nspcc-dev/neo-go/pkg/encoding/base58"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
@ -105,6 +106,16 @@ func newStd() *Std {
md = newMethodAndPrice(s.base58Decode, 1<<10, callflag.NoneFlag)
s.AddMethod(md, desc)
desc = newDescriptor("base58CheckEncode", smartcontract.StringType,
manifest.NewParameter("data", smartcontract.ByteArrayType))
md = newMethodAndPrice(s.base58CheckEncode, 1<<16, callflag.NoneFlag)
s.AddMethod(md, desc)
desc = newDescriptor("base58CheckDecode", smartcontract.ByteArrayType,
manifest.NewParameter("s", smartcontract.StringType))
md = newMethodAndPrice(s.base58CheckDecode, 1<<16, callflag.NoneFlag)
s.AddMethod(md, desc)
desc = newDescriptor("memoryCompare", smartcontract.IntegerType,
manifest.NewParameter("str1", smartcontract.ByteArrayType),
manifest.NewParameter("str2", smartcontract.ByteArrayType))
@ -319,6 +330,23 @@ func (s *Std) base58Decode(_ *interop.Context, args []stackitem.Item) stackitem.
return stackitem.NewByteArray(result)
}
func (s *Std) base58CheckEncode(_ *interop.Context, args []stackitem.Item) stackitem.Item {
src := s.toLimitedBytes(args[0])
result := base58neogo.CheckEncode(src)
return stackitem.NewByteArray([]byte(result))
}
func (s *Std) base58CheckDecode(_ *interop.Context, args []stackitem.Item) stackitem.Item {
src := s.toLimitedString(args[0])
result, err := base58neogo.CheckDecode(src)
if err != nil {
panic(err)
}
return stackitem.NewByteArray(result)
}
func (s *Std) memoryCompare(_ *interop.Context, args []stackitem.Item) stackitem.Item {
s1 := s.toLimitedBytes(args[0])
s2 := s.toLimitedBytes(args[1])

View file

@ -10,6 +10,7 @@ import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
base58neogo "github.com/nspcc-dev/neo-go/pkg/encoding/base58"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@ -163,6 +164,7 @@ func TestStdLibEncodeDecode(t *testing.T) {
original := []byte("my pretty string")
encoded64 := base64.StdEncoding.EncodeToString(original)
encoded58 := base58.Encode(original)
encoded58Check := base58neogo.CheckEncode(original)
ic := &interop.Context{VM: vm.New()}
var actual stackitem.Item
@ -188,6 +190,16 @@ func TestStdLibEncodeDecode(t *testing.T) {
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.base58Encode(ic, bigInputArgs) })
})
t.Run("CheckEncode58", func(t *testing.T) {
require.NotPanics(t, func() {
actual = s.base58CheckEncode(ic, []stackitem.Item{stackitem.Make(original)})
})
require.Equal(t, stackitem.Make(encoded58Check), actual)
})
t.Run("CheckEncode58/error", func(t *testing.T) {
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.base58CheckEncode(ic, bigInputArgs) })
})
t.Run("Decode64/positive", func(t *testing.T) {
require.NotPanics(t, func() {
actual = s.base64Decode(ic, []stackitem.Item{stackitem.Make(encoded64)})
@ -220,6 +232,22 @@ func TestStdLibEncodeDecode(t *testing.T) {
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.base58Decode(ic, bigInputArgs) })
})
t.Run("CheckDecode58/positive", func(t *testing.T) {
require.NotPanics(t, func() {
actual = s.base58CheckDecode(ic, []stackitem.Item{stackitem.Make(encoded58Check)})
})
require.Equal(t, stackitem.Make(original), actual)
})
t.Run("CheckDecode58/error", func(t *testing.T) {
require.Panics(t, func() {
_ = s.base58CheckDecode(ic, []stackitem.Item{stackitem.Make(encoded58 + "%")})
})
require.Panics(t, func() {
_ = s.base58CheckDecode(ic, []stackitem.Item{stackitem.NewInterop(nil)})
})
require.PanicsWithError(t, ErrTooBigInput.Error(),
func() { s.base58CheckDecode(ic, bigInputArgs) })
})
}
func TestStdLibSerialize(t *testing.T) {