2020-02-17 11:54:53 +00:00
|
|
|
package request
|
2018-12-21 09:32:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-26 10:13:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2018-12-21 09:32:18 +00:00
|
|
|
errs "github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// CreateRawContractTransaction returns contract-type Transaction built from specified parameters.
|
2018-12-21 09:32:18 +00:00
|
|
|
func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transaction, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tx = transaction.NewContractTX()
|
|
|
|
toAddressHash, fromAddressHash util.Uint160
|
|
|
|
fromAddress string
|
2019-11-20 10:14:01 +00:00
|
|
|
receiverOutput *transaction.Output
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2020-02-17 11:54:53 +00:00
|
|
|
wif, assetID, toAddress, amount, balancer = params.WIF, params.AssetID, params.Address, params.Value, params.Balancer
|
2018-12-21 09:32:18 +00:00
|
|
|
)
|
|
|
|
|
2019-09-05 06:35:02 +00:00
|
|
|
fromAddress = wif.PrivateKey.Address()
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2019-12-25 14:34:18 +00:00
|
|
|
if fromAddressHash, err = address.StringToUint160(fromAddress); err != nil {
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
|
|
|
|
}
|
|
|
|
|
2019-12-25 14:34:18 +00:00
|
|
|
if toAddressHash, err = address.StringToUint160(toAddress); err != nil {
|
2019-12-25 12:22:02 +00:00
|
|
|
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", toAddress)
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
2020-03-12 16:47:09 +00:00
|
|
|
tx.AddVerificationHash(fromAddressHash)
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2019-11-20 10:14:01 +00:00
|
|
|
if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "failed to add inputs and unspents to transaction")
|
|
|
|
}
|
|
|
|
receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash)
|
|
|
|
tx.AddOutput(receiverOutput)
|
2020-02-28 16:01:07 +00:00
|
|
|
if acc, err := wallet.NewAccountFromWIF(wif.S); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if err = acc.SignTx(tx); err != nil {
|
2019-11-20 10:14:01 +00:00
|
|
|
return nil, errs.Wrap(err, "failed to sign tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddInputsAndUnspentsToTx adds inputs needed to transaction and one output
|
|
|
|
// with change.
|
2019-12-25 12:22:02 +00:00
|
|
|
func AddInputsAndUnspentsToTx(tx *transaction.Transaction, addr string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error {
|
2019-12-25 14:34:18 +00:00
|
|
|
scriptHash, err := address.StringToUint160(addr)
|
2019-11-20 10:14:01 +00:00
|
|
|
if err != nil {
|
2019-12-25 12:22:02 +00:00
|
|
|
return errs.Wrapf(err, "failed to take script hash from address: %v", addr)
|
2019-11-20 10:14:01 +00:00
|
|
|
}
|
2019-12-25 12:22:02 +00:00
|
|
|
inputs, spent, err := balancer.CalculateInputs(addr, assetID, amount)
|
2019-11-20 10:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err, "failed to get inputs")
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
|
|
|
tx.AddInput(&input)
|
|
|
|
}
|
|
|
|
|
2019-02-07 11:44:51 +00:00
|
|
|
if senderUnspent := spent - amount; senderUnspent > 0 {
|
2019-11-20 10:14:01 +00:00
|
|
|
senderOutput := transaction.NewOutput(assetID, senderUnspent, scriptHash)
|
2019-02-07 11:44:51 +00:00
|
|
|
tx.AddOutput(senderOutput)
|
|
|
|
}
|
2019-11-20 10:14:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:32:06 +00:00
|
|
|
// DetailsToSCProperties extract the fields needed from ContractDetails
|
|
|
|
// and converts them to smartcontract.PropertyState.
|
2020-04-29 10:45:01 +00:00
|
|
|
func DetailsToSCProperties(contract *smartcontract.ContractDetails) smartcontract.PropertyState {
|
2019-11-20 13:07:43 +00:00
|
|
|
var props smartcontract.PropertyState
|
|
|
|
if contract.HasStorage {
|
|
|
|
props |= smartcontract.HasStorage
|
|
|
|
}
|
|
|
|
if contract.HasDynamicInvocation {
|
|
|
|
props |= smartcontract.HasDynamicInvoke
|
|
|
|
}
|
|
|
|
if contract.IsPayable {
|
|
|
|
props |= smartcontract.IsPayable
|
|
|
|
}
|
2020-03-11 17:32:06 +00:00
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDeploymentScript returns a script that deploys given smart contract
|
|
|
|
// with its metadata.
|
2020-04-29 10:45:01 +00:00
|
|
|
func CreateDeploymentScript(avm []byte, contract *smartcontract.ContractDetails) ([]byte, error) {
|
2020-03-11 17:32:06 +00:00
|
|
|
script := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(script.BinWriter, []byte(contract.Description))
|
|
|
|
emit.Bytes(script.BinWriter, []byte(contract.Email))
|
|
|
|
emit.Bytes(script.BinWriter, []byte(contract.Author))
|
|
|
|
emit.Bytes(script.BinWriter, []byte(contract.Version))
|
|
|
|
emit.Bytes(script.BinWriter, []byte(contract.ProjectName))
|
|
|
|
emit.Int(script.BinWriter, int64(DetailsToSCProperties(contract)))
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script.BinWriter, int64(contract.ReturnType))
|
2019-11-20 13:07:43 +00:00
|
|
|
params := make([]byte, len(contract.Parameters))
|
|
|
|
for k := range contract.Parameters {
|
|
|
|
params[k] = byte(contract.Parameters[k])
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script.BinWriter, params)
|
|
|
|
emit.Bytes(script.BinWriter, avm)
|
|
|
|
emit.Syscall(script.BinWriter, "Neo.Contract.Create")
|
2019-11-20 13:07:43 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
|
2019-11-28 16:08:31 +00:00
|
|
|
// expandArrayIntoScript pushes all FuncParam parameters from the given array
|
|
|
|
// into the given buffer in reverse order.
|
2020-02-17 11:54:53 +00:00
|
|
|
func expandArrayIntoScript(script *io.BinWriter, slice []Param) error {
|
2019-11-28 16:08:31 +00:00
|
|
|
for j := len(slice) - 1; j >= 0; j-- {
|
|
|
|
fp, err := slice[j].GetFuncParam()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch fp.Type {
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.ByteArrayType, smartcontract.SignatureType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetBytesHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.StringType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash160Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash256Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.PublicKeyType:
|
2019-11-28 16:08:31 +00: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 14:46:51 +00:00
|
|
|
emit.Bytes(script, key.Bytes())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.IntegerType:
|
2019-11-28 16:08:31 +00:00
|
|
|
val, err := fp.Value.GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, int64(val))
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.BoolType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch str {
|
|
|
|
case "true":
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 1)
|
2019-11-28 16:08:31 +00:00
|
|
|
case "false":
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 0)
|
2019-11-28 16:08:31 +00:00
|
|
|
default:
|
2020-02-03 14:46:51 +00:00
|
|
|
return errors.New("wrong boolean value")
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
// CreateFunctionInvocationScript creates a script to invoke given contract with
|
|
|
|
// given parameters.
|
2020-02-17 11:54:53 +00:00
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byte, error) {
|
2020-02-03 14:46:51 +00:00
|
|
|
script := io.NewBufBinWriter()
|
2019-11-26 10:13:17 +00:00
|
|
|
for i := len(params) - 1; i >= 0; i-- {
|
|
|
|
switch params[i].Type {
|
2020-02-17 11:54:53 +00:00
|
|
|
case StringT:
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script.BinWriter, params[i].String())
|
2020-02-17 11:54:53 +00:00
|
|
|
case NumberT:
|
2019-11-26 10:13:17 +00:00
|
|
|
num, err := params[i].GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script.BinWriter, strconv.Itoa(num))
|
2020-02-17 11:54:53 +00:00
|
|
|
case ArrayT:
|
2019-11-26 10:13:17 +00:00
|
|
|
slice, err := params[i].GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
err = expandArrayIntoScript(script.BinWriter, slice)
|
2019-11-26 10:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script.BinWriter, int64(len(slice)))
|
|
|
|
emit.Opcode(script.BinWriter, opcode.PACK)
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.AppCall(script.BinWriter, contract, false)
|
2019-11-26 10:13:17 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|
2019-11-28 16:08:31 +00:00
|
|
|
|
|
|
|
// CreateInvocationScript creates a script to invoke given contract with
|
|
|
|
// given parameters. It differs from CreateFunctionInvocationScript in that it
|
|
|
|
// expects one array of FuncParams and expands it onto the stack as independent
|
|
|
|
// elements.
|
2020-02-17 11:54:53 +00:00
|
|
|
func CreateInvocationScript(contract util.Uint160, funcParams []Param) ([]byte, error) {
|
2020-02-03 14:46:51 +00:00
|
|
|
script := io.NewBufBinWriter()
|
|
|
|
err := expandArrayIntoScript(script.BinWriter, funcParams)
|
2019-11-28 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.AppCall(script.BinWriter, contract, false)
|
2019-11-28 16:08:31 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|