Skip to content

Commit

Permalink
core: add System.Binary.Base64Decode interop
Browse files Browse the repository at this point in the history
Part of #1055
  • Loading branch information
AnnaShaleva committed Jul 16, 2020
1 parent 28cab40 commit 172b026
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/core/interop_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,14 @@ func runtimeEncode(_ *interop.Context, v *vm.VM) error {
v.Estack().PushVal([]byte(result))
return nil
}

// runtimeDecode decodes top stack item from base64 string to byte array.
func runtimeDecode(_ *interop.Context, v *vm.VM) error {
src := string(v.Estack().Pop().Bytes())
result, err := base64.StdEncoding.DecodeString(src)
if err != nil {
return err
}
v.Estack().PushVal(result)
return nil
}
20 changes: 20 additions & 0 deletions pkg/core/interop_neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ func TestRuntimeEncode(t *testing.T) {
require.Equal(t, expected, actual)
}

func TestRuntimeDecode(t *testing.T) {
expected := []byte("my pretty string")
str := base64.StdEncoding.EncodeToString(expected)
v, ic, bc := createVM(t)
defer bc.Close()

t.Run("positive", func(t *testing.T) {
v.Estack().PushVal(str)
require.NoError(t, runtimeDecode(ic, v))

actual := v.Estack().Pop().Bytes()
require.Equal(t, expected, actual)
})

t.Run("error", func(t *testing.T) {
v.Estack().PushVal(str + "%")
require.Error(t, runtimeDecode(ic, v))
})
}

// Helper functions to create VM, InteropContext, TX, Account, Contract.

func createVM(t *testing.T) (*vm.VM, *interop.Context, *Blockchain) {
Expand Down
1 change: 1 addition & 0 deletions pkg/core/interops.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var systemInterops = []interop.Function{
{Name: "System.Binary.Deserialize", Func: runtimeDeserialize, Price: 500000},
{Name: "System.Binary.Serialize", Func: runtimeSerialize, Price: 100000},
{Name: "System.Binary.Base64Encode", Func: runtimeEncode, Price: 100000},
{Name: "System.Binary.Base64Decode", Func: runtimeDecode, Price: 100000},
{Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 2500000,
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
{Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 1000000,
Expand Down

0 comments on commit 172b026

Please sign in to comment.