2022-07-07 17:41:01 +03:00
|
|
|
package params
|
2018-12-21 12:32:18 +03:00
|
|
|
|
|
|
|
import (
|
2019-11-26 13:13:17 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-20 16:07:43 +03:00
|
|
|
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 13:44:07 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-12-21 12:32:18 +03:00
|
|
|
)
|
|
|
|
|
2021-03-10 17:43:52 +03:00
|
|
|
// ExpandArrayIntoScript pushes all FuncParam parameters from the given array
|
2022-04-20 21:30:09 +03:00
|
|
|
// into the given buffer in the reverse order.
|
2021-03-10 17:43:52 +03:00
|
|
|
func ExpandArrayIntoScript(script *io.BinWriter, slice []Param) error {
|
2019-11-28 19:08:31 +03:00
|
|
|
for j := len(slice) - 1; j >= 0; j-- {
|
|
|
|
fp, err := slice[j].GetFuncParam()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch fp.Type {
|
2020-07-07 22:35:03 +03:00
|
|
|
case smartcontract.ByteArrayType:
|
|
|
|
str, err := fp.Value.GetBytesBase64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Bytes(script, str)
|
|
|
|
case smartcontract.SignatureType:
|
rpc: fix Signature parameter unmarshalling
It is based64 encoded and decoded, see
https://github.com/nspcc-dev/neo-go/blob/5108d1c2c748ad00de2c0b37c05f41e841d770c9/pkg/smartcontract/parameter.go#L78
and
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/SmartContract/ContractParameter.cs#L150
and
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/SmartContract/ContractParameter.cs#L79.
Also, TestInvocationScriptCreationGood is extended and compatibility
is tested with the following C# node requests:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [{"type": "Signature", "value": "4edf5005771de04619235d5a4c7a9a11bb78e008541f1da7725f654c33380a3c87e2959a025da706d7255cb3a3fa07ebe9c6559d0d9e6213c68049168eb1056f"}]] }' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 665 0 381 100 284 197 147 0:00:01 0:00:01 --:--:-- 344
{
"jsonrpc" : "2.0",
"id" : 1,
"result" : {
"notifications" : [],
"stack" : [],
"script" : "DGDh51/nTTnvvV17TjrX3bfl3lrhztr1rXVtvvx7TTznjV/V1rvvbl/rnhzfffzRrdzzt7b3n1rTbl1rvTp3vbnlxvdrd9rTt5t71zrnn13R317rbXdzrzTj3Xrx5vXTnp8RwB8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f",
"state" : "FAULT",
"gasconsumed" : "104526"
}
}
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [{"type": "Signature", "value": "Tt9QBXcd4EYZI11aTHqaEbt44AhUHx2ncl9lTDM4CjyH4pWaAl2nBtclXLOj+gfr6cZVnQ2eYhPGgEkWjrEFbw=="}]] }' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 591 0 347 100 244 794 558 --:--:-- --:--:-- --:--:-- 1352
{
"jsonrpc" : "2.0",
"result" : {
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f",
"gasconsumed" : "104526",
"notifications" : [],
"stack" : [],
"script" : "DEBO31AFdx3gRhkjXVpMepoRu3jgCFQfHadyX2VMMzgKPIfilZoCXacG1yVcs6P6B+vpxlWdDZ5iE8aASRaOsQVvEcAfDAFhDBRvRZFizuskiwcewVfZ5Pb9Jv2+UEFifVtS"
},
"id" : 1
}
```
2022-06-22 12:57:19 +03:00
|
|
|
str, err := fp.Value.GetBytesBase64()
|
2019-11-28 19:08:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Bytes(script, str)
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.StringType:
|
2019-11-28 19:08:31 +03:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.String(script, str)
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.Hash160Type:
|
2019-11-28 19:08:31 +03:00
|
|
|
hash, err := fp.Value.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.Hash256Type:
|
2019-11-28 19:08:31 +03:00
|
|
|
hash, err := fp.Value.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.PublicKeyType:
|
2019-11-28 19:08:31 +03:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := keys.NewPublicKeyFromString(string(str))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Bytes(script, key.Bytes())
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.IntegerType:
|
2022-02-09 15:13:15 +03:00
|
|
|
bi, err := fp.Value.GetBigInt()
|
2019-11-28 19:08:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 15:13:15 +03:00
|
|
|
emit.BigInt(script, bi)
|
2020-02-21 17:34:18 +03:00
|
|
|
case smartcontract.BoolType:
|
2021-10-28 14:10:18 +03:00
|
|
|
val, err := fp.Value.GetBoolean() // not GetBooleanStrict(), because that's the way C# code works
|
|
|
|
if err != nil {
|
2021-05-25 11:24:28 +03:00
|
|
|
return errors.New("not a bool")
|
2019-11-28 19:08:31 +03:00
|
|
|
}
|
2021-05-25 11:24:28 +03:00
|
|
|
if val {
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Int(script, 1)
|
2021-05-25 11:24:28 +03:00
|
|
|
} else {
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Int(script, 0)
|
2019-11-28 19:08:31 +03:00
|
|
|
}
|
2020-08-27 11:44:40 +03:00
|
|
|
case smartcontract.ArrayType:
|
|
|
|
val, err := fp.Value.GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 13:23:55 +03:00
|
|
|
err = ExpandArrayIntoScriptAndPack(script, val)
|
2020-08-27 11:44:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-09 10:37:22 +03:00
|
|
|
case smartcontract.AnyType:
|
|
|
|
if fp.Value.IsNull() {
|
|
|
|
emit.Opcodes(script, opcode.PUSHNULL)
|
|
|
|
}
|
2019-11-28 19:08:31 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 15:38:22 +03:00
|
|
|
return script.Err
|
2019-11-28 19:08:31 +03:00
|
|
|
}
|
|
|
|
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 13:23:55 +03:00
|
|
|
// ExpandArrayIntoScriptAndPack expands provided array into script and packs the
|
|
|
|
// resulting items in the array.
|
|
|
|
func ExpandArrayIntoScriptAndPack(script *io.BinWriter, slice []Param) error {
|
|
|
|
if len(slice) == 0 {
|
|
|
|
emit.Opcodes(script, opcode.NEWARRAY0)
|
|
|
|
return script.Err
|
|
|
|
}
|
|
|
|
err := ExpandArrayIntoScript(script, slice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Int(script, int64(len(slice)))
|
|
|
|
emit.Opcodes(script, opcode.PACK)
|
|
|
|
return script.Err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// CreateFunctionInvocationScript creates a script to invoke the given contract with
|
|
|
|
// the given parameters.
|
2021-11-20 19:25:42 +03:00
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, method string, param *Param) ([]byte, error) {
|
2020-02-03 17:46:51 +03:00
|
|
|
script := io.NewBufBinWriter()
|
2021-11-20 19:25:42 +03:00
|
|
|
if param == nil {
|
2021-10-16 21:09:29 +03:00
|
|
|
emit.Opcodes(script.BinWriter, opcode.NEWARRAY0)
|
2021-11-20 19:25:42 +03:00
|
|
|
} else if slice, err := param.GetArray(); err == nil {
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 13:23:55 +03:00
|
|
|
err = ExpandArrayIntoScriptAndPack(script.BinWriter, slice)
|
2021-11-20 19:25:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("failed to convert %s to script parameter", param)
|
2021-10-16 21:09:29 +03:00
|
|
|
}
|
2019-11-26 13:13:17 +03:00
|
|
|
|
2020-12-29 13:44:07 +03:00
|
|
|
emit.AppCallNoArgs(script.BinWriter, contract, method, callflag.All)
|
2019-11-26 13:13:17 +03:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|