From 78900148ee7d71640d2f10c6e90d0bd3d69d74d0 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 3 Jul 2020 18:40:45 +0300 Subject: [PATCH] rpc/request: add (*Param).GetUint160FromAddressOrHex() Allow to get address from both representations. --- pkg/rpc/request/param.go | 10 ++++++++++ pkg/rpc/request/param_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/rpc/request/param.go b/pkg/rpc/request/param.go index 205a2a93e..a6a254dd0 100644 --- a/pkg/rpc/request/param.go +++ b/pkg/rpc/request/param.go @@ -154,6 +154,16 @@ func (p *Param) GetUint160FromAddress() (util.Uint160, error) { return address.StringToUint160(s) } +// GetUint160FromAddressOrHex returns Uint160 value of the parameter that was +// supplied either as raw hex or as an address. +func (p *Param) GetUint160FromAddressOrHex() (util.Uint160, error) { + u, err := p.GetUint160FromHex() + if err == nil { + return u, err + } + return p.GetUint160FromAddress() +} + // GetFuncParam returns current parameter as a function call parameter. func (p *Param) GetFuncParam() (FuncParam, error) { if p == nil { diff --git a/pkg/rpc/request/param_test.go b/pkg/rpc/request/param_test.go index 7bf2ae22d..0f51bc1db 100644 --- a/pkg/rpc/request/param_test.go +++ b/pkg/rpc/request/param_test.go @@ -166,6 +166,25 @@ func TestParamGetUint160FromAddress(t *testing.T) { require.NotNil(t, err) } +func TestParam_GetUint160FromAddressOrHex(t *testing.T) { + in := "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y" + inHex, _ := address.StringToUint160(in) + + t.Run("Address", func(t *testing.T) { + p := Param{StringT, in} + u, err := p.GetUint160FromAddressOrHex() + require.NoError(t, err) + require.Equal(t, inHex, u) + }) + + t.Run("Hex", func(t *testing.T) { + p := Param{StringT, inHex.StringLE()} + u, err := p.GetUint160FromAddressOrHex() + require.NoError(t, err) + require.Equal(t, inHex, u) + }) +} + func TestParamGetFuncParam(t *testing.T) { fp := FuncParam{ Type: smartcontract.StringType,