2020-01-14 12:02:38 +00:00
|
|
|
package request
|
2019-11-21 14:42:02 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-07 19:35:03 +00:00
|
|
|
"encoding/base64"
|
2019-11-26 10:13:17 +00:00
|
|
|
"encoding/hex"
|
2019-11-21 14:42:02 +00:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-10 11:45:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-11-26 10:13:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-21 14:42:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParam_UnmarshalJSON(t *testing.T) {
|
2020-06-08 13:38:44 +00:00
|
|
|
msg := `["str1", 123, null, ["str2", 3], [{"type": "String", "value": "jajaja"}],
|
2020-06-15 09:16:18 +00:00
|
|
|
{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569"},
|
2021-07-18 13:45:53 +00:00
|
|
|
{"account": "NYxb4fSZVKAz8YsgaPK2WkT3KcAE9b3Vag", "scopes": "Global"},
|
2020-06-15 09:16:18 +00:00
|
|
|
[{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569", "scopes": "Global"}]]`
|
2019-11-21 14:42:02 +00:00
|
|
|
expected := Params{
|
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: StringT,
|
2019-11-21 14:42:02 +00:00
|
|
|
Value: "str1",
|
|
|
|
},
|
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: NumberT,
|
2019-11-21 14:42:02 +00:00
|
|
|
Value: 123,
|
|
|
|
},
|
2020-06-08 13:38:44 +00:00
|
|
|
{
|
|
|
|
Type: defaultT,
|
|
|
|
},
|
2019-11-21 14:42:02 +00:00
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: ArrayT,
|
2019-11-21 14:42:02 +00:00
|
|
|
Value: []Param{
|
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: StringT,
|
2019-11-21 14:42:02 +00:00
|
|
|
Value: "str2",
|
|
|
|
},
|
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: NumberT,
|
2019-11-21 14:42:02 +00:00
|
|
|
Value: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-26 10:13:17 +00:00
|
|
|
{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: ArrayT,
|
2019-11-26 10:13:17 +00:00
|
|
|
Value: []Param{
|
|
|
|
{
|
2021-08-05 14:56:17 +00:00
|
|
|
Type: defaultT,
|
|
|
|
Value: json.RawMessage(`{"type": "String", "value": "jajaja"}`),
|
2019-11-26 10:13:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-13 10:16:42 +00:00
|
|
|
{
|
2021-08-05 14:56:17 +00:00
|
|
|
Type: defaultT,
|
|
|
|
Value: json.RawMessage(`{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569"}`),
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
2020-06-10 11:45:55 +00:00
|
|
|
{
|
2021-08-05 14:56:17 +00:00
|
|
|
Type: defaultT,
|
|
|
|
Value: json.RawMessage(`{"account": "NYxb4fSZVKAz8YsgaPK2WkT3KcAE9b3Vag", "scopes": "Global"}`),
|
2021-07-18 13:45:53 +00:00
|
|
|
},
|
2020-06-10 11:45:55 +00:00
|
|
|
{
|
|
|
|
Type: ArrayT,
|
|
|
|
Value: []Param{
|
|
|
|
{
|
2021-08-05 14:56:17 +00:00
|
|
|
Type: defaultT,
|
|
|
|
Value: json.RawMessage(`{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569", "scopes": "Global"}`),
|
2020-06-10 11:45:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-21 14:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ps Params
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(msg), &ps))
|
|
|
|
require.Equal(t, expected, ps)
|
2021-08-05 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWitness(t *testing.T) {
|
|
|
|
accountHash, err := util.Uint160DecodeStringLE("cadb3dc2faa3ef14a13b619c9a43124755aa2569")
|
|
|
|
require.NoError(t, err)
|
|
|
|
addrHash, err := address.StringToUint160("NYxb4fSZVKAz8YsgaPK2WkT3KcAE9b3Vag")
|
|
|
|
require.NoError(t, err)
|
2019-11-21 14:42:02 +00:00
|
|
|
|
2021-08-05 14:56:17 +00:00
|
|
|
testCases := []struct {
|
|
|
|
raw string
|
|
|
|
expected SignerWithWitness
|
|
|
|
}{
|
|
|
|
{`{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569"}`, SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: accountHash,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{`{"account": "NYxb4fSZVKAz8YsgaPK2WkT3KcAE9b3Vag", "scopes": "Global"}`, SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: addrHash,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{`{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569", "scopes": "Global"}`, SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: accountHash,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
p := Param{Value: json.RawMessage(tc.raw)}
|
|
|
|
actual, err := p.GetSignerWithWitness()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, actual)
|
|
|
|
require.Equal(t, tc.expected, p.Value)
|
|
|
|
|
|
|
|
actual, err = p.GetSignerWithWitness() // valid second invocation.
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, actual)
|
|
|
|
}
|
2019-11-21 14:42:02 +00:00
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
|
|
|
|
func TestParamGetString(t *testing.T) {
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{StringT, "jajaja"}
|
2019-11-26 10:13:17 +00:00
|
|
|
str, err := p.GetString()
|
|
|
|
assert.Equal(t, "jajaja", str)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, int(100500)}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetString()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetInt(t *testing.T) {
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{NumberT, int(100500)}
|
2019-11-26 10:13:17 +00:00
|
|
|
i, err := p.GetInt()
|
|
|
|
assert.Equal(t, 100500, i)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{NumberT, "jajaja"}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetInt()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetArray(t *testing.T) {
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{ArrayT, []Param{{NumberT, 42}}}
|
2019-11-26 10:13:17 +00:00
|
|
|
a, err := p.GetArray()
|
2020-01-14 12:02:38 +00:00
|
|
|
assert.Equal(t, []Param{{NumberT, 42}}, a)
|
2019-11-26 10:13:17 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{ArrayT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetArray()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetUint256(t *testing.T) {
|
|
|
|
gas := "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"
|
2019-11-27 09:23:18 +00:00
|
|
|
u256, _ := util.Uint256DecodeStringLE(gas)
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{StringT, gas}
|
2019-11-26 10:13:17 +00:00
|
|
|
u, err := p.GetUint256()
|
|
|
|
assert.Equal(t, u256, u)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-08-27 09:07:02 +00:00
|
|
|
p = Param{StringT, "0x" + gas}
|
|
|
|
u, err = p.GetUint256()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, u256, u)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint256()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, "qq2c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint256()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetUint160FromHex(t *testing.T) {
|
|
|
|
in := "50befd26fdf6e4d957c11e078b24ebce6291456f"
|
2019-12-06 16:47:58 +00:00
|
|
|
u160, _ := util.Uint160DecodeStringLE(in)
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{StringT, in}
|
2019-11-26 10:13:17 +00:00
|
|
|
u, err := p.GetUint160FromHex()
|
|
|
|
assert.Equal(t, u160, u)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint160FromHex()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, "wwbefd26fdf6e4d957c11e078b24ebce6291456f"}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint160FromHex()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetUint160FromAddress(t *testing.T) {
|
2020-06-16 10:47:29 +00:00
|
|
|
in := "NPAsqZkx9WhNd4P72uhZxBhLinSuNkxfB8"
|
2019-12-25 14:34:18 +00:00
|
|
|
u160, _ := address.StringToUint160(in)
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{StringT, in}
|
2019-11-26 10:13:17 +00:00
|
|
|
u, err := p.GetUint160FromAddress()
|
|
|
|
assert.Equal(t, u160, u)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint160FromAddress()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, "QK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetUint160FromAddress()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:40:45 +00:00
|
|
|
func TestParam_GetUint160FromAddressOrHex(t *testing.T) {
|
|
|
|
in := "NPAsqZkx9WhNd4P72uhZxBhLinSuNkxfB8"
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
func TestParamGetFuncParam(t *testing.T) {
|
|
|
|
fp := FuncParam{
|
2020-02-21 14:34:18 +00:00
|
|
|
Type: smartcontract.StringType,
|
2019-11-26 10:13:17 +00:00
|
|
|
Value: Param{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: StringT,
|
2019-11-26 10:13:17 +00:00
|
|
|
Value: "jajaja",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p := Param{
|
2020-01-14 12:02:38 +00:00
|
|
|
Type: FuncParamT,
|
2019-11-26 10:13:17 +00:00
|
|
|
Value: fp,
|
|
|
|
}
|
|
|
|
newfp, err := p.GetFuncParam()
|
|
|
|
assert.Equal(t, fp, newfp)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{FuncParamT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetFuncParam()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamGetBytesHex(t *testing.T) {
|
|
|
|
in := "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"
|
|
|
|
inb, _ := hex.DecodeString(in)
|
2020-01-14 12:02:38 +00:00
|
|
|
p := Param{StringT, in}
|
2019-11-26 10:13:17 +00:00
|
|
|
bh, err := p.GetBytesHex()
|
|
|
|
assert.Equal(t, inb, bh)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, 42}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetBytesHex()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p = Param{StringT, "qq2c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"}
|
2019-11-26 10:13:17 +00:00
|
|
|
_, err = p.GetBytesHex()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
2020-06-10 11:45:55 +00:00
|
|
|
|
2020-07-07 19:35:03 +00:00
|
|
|
func TestParamGetBytesBase64(t *testing.T) {
|
|
|
|
in := "Aj4A8DoW6HB84EXrQu6A05JFFUHuUQ3BjhyL77rFTXQm"
|
|
|
|
inb, err := base64.StdEncoding.DecodeString(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := Param{StringT, in}
|
|
|
|
bh, err := p.GetBytesBase64()
|
|
|
|
assert.Equal(t, inb, bh)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
p = Param{StringT, 42}
|
|
|
|
_, err = p.GetBytesBase64()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
p = Param{StringT, "@j4A8DoW6HB84EXrQu6A05JFFUHuUQ3BjhyL77rFTXQm"}
|
|
|
|
_, err = p.GetBytesBase64()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
func TestParamGetSigner(t *testing.T) {
|
2020-12-14 12:23:39 +00:00
|
|
|
c := SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: util.Uint160{1, 2, 3, 4},
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
Witness: transaction.Witness{
|
|
|
|
|
|
|
|
InvocationScript: []byte{1, 2, 3},
|
|
|
|
VerificationScript: []byte{1, 2, 3},
|
|
|
|
},
|
2020-06-10 11:45:55 +00:00
|
|
|
}
|
2020-12-14 12:23:39 +00:00
|
|
|
p := Param{Type: SignerWithWitnessT, Value: c}
|
|
|
|
actual, err := p.GetSignerWithWitness()
|
2020-06-10 11:45:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c, actual)
|
|
|
|
|
2020-12-14 12:23:39 +00:00
|
|
|
p = Param{Type: SignerWithWitnessT, Value: `{"account": "0xcadb3dc2faa3ef14a13b619c9a43124755aa2569", "scopes": 0}`}
|
|
|
|
_, err = p.GetSignerWithWitness()
|
2020-06-10 11:45:55 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
func TestParamGetSigners(t *testing.T) {
|
2020-06-10 11:45:55 +00:00
|
|
|
u1 := util.Uint160{1, 2, 3, 4}
|
|
|
|
u2 := util.Uint160{5, 6, 7, 8}
|
|
|
|
t.Run("from hashes", func(t *testing.T) {
|
|
|
|
p := Param{ArrayT, []Param{
|
|
|
|
{Type: StringT, Value: u1.StringLE()},
|
|
|
|
{Type: StringT, Value: u2.StringLE()},
|
|
|
|
}}
|
2020-12-14 12:23:39 +00:00
|
|
|
actual, _, err := p.GetSignersWithWitnesses()
|
2020-06-10 11:45:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(actual))
|
|
|
|
require.True(t, u1.Equals(actual[0].Account))
|
|
|
|
require.True(t, u2.Equals(actual[1].Account))
|
|
|
|
})
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
t.Run("from signers", func(t *testing.T) {
|
2020-12-14 12:23:39 +00:00
|
|
|
c1 := SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: u1,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
Witness: transaction.Witness{
|
|
|
|
InvocationScript: []byte{1, 2, 3},
|
|
|
|
VerificationScript: []byte{1, 2, 3},
|
|
|
|
},
|
2020-06-10 11:45:55 +00:00
|
|
|
}
|
2020-12-14 12:23:39 +00:00
|
|
|
c2 := SignerWithWitness{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: u2,
|
|
|
|
Scopes: transaction.CustomContracts,
|
|
|
|
AllowedContracts: []util.Uint160{
|
|
|
|
{1, 2, 3},
|
|
|
|
{4, 5, 6},
|
|
|
|
},
|
2020-06-10 11:45:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
p := Param{ArrayT, []Param{
|
2020-12-14 12:23:39 +00:00
|
|
|
{Type: SignerWithWitnessT, Value: c1},
|
|
|
|
{Type: SignerWithWitnessT, Value: c2},
|
2020-06-10 11:45:55 +00:00
|
|
|
}}
|
2020-12-14 12:23:39 +00:00
|
|
|
actualS, actualW, err := p.GetSignersWithWitnesses()
|
2020-06-10 11:45:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-14 12:23:39 +00:00
|
|
|
require.Equal(t, 2, len(actualS))
|
|
|
|
require.Equal(t, transaction.Signer{
|
|
|
|
Account: c1.Account,
|
|
|
|
Scopes: c1.Scopes,
|
|
|
|
}, actualS[0])
|
|
|
|
require.Equal(t, transaction.Signer{
|
|
|
|
Account: c2.Account,
|
|
|
|
Scopes: c2.Scopes,
|
|
|
|
AllowedContracts: c2.AllowedContracts,
|
|
|
|
}, actualS[1])
|
|
|
|
require.EqualValues(t, 2, len(actualW))
|
|
|
|
require.EqualValues(t, transaction.Witness{
|
|
|
|
InvocationScript: c1.InvocationScript,
|
|
|
|
VerificationScript: c1.VerificationScript,
|
|
|
|
}, actualW[0])
|
|
|
|
require.Equal(t, transaction.Witness{}, actualW[1])
|
2020-06-10 11:45:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad format", func(t *testing.T) {
|
|
|
|
p := Param{ArrayT, []Param{
|
|
|
|
{Type: StringT, Value: u1.StringLE()},
|
|
|
|
{Type: StringT, Value: "bla"},
|
|
|
|
}}
|
2020-12-14 12:23:39 +00:00
|
|
|
_, _, err := p.GetSignersWithWitnesses()
|
2020-06-10 11:45:55 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|