2018-12-21 09:32:18 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-11-20 13:07:43 +00:00
|
|
|
"bytes"
|
2019-11-26 10:13:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-12-25 12:22:02 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/encoding/address"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-11-20 13:07:43 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-11-20 13:07:43 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
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
|
|
|
|
2019-12-25 12:22:02 +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
|
|
|
}
|
|
|
|
tx.Attributes = append(tx.Attributes,
|
2019-12-09 14:14:10 +00:00
|
|
|
transaction.Attribute{
|
2018-12-21 09:32:18 +00:00
|
|
|
Usage: transaction.Script,
|
2019-11-27 09:20:31 +00:00
|
|
|
Data: fromAddressHash.BytesBE(),
|
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)
|
|
|
|
if err = SignTx(tx, &wif); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTx signs given transaction in-place using given key.
|
|
|
|
func SignTx(tx *transaction.Transaction, wif *keys.WIF) error {
|
|
|
|
var witness transaction.Witness
|
|
|
|
var err error
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2019-01-22 12:14:40 +00:00
|
|
|
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
2019-11-20 10:14:01 +00:00
|
|
|
return errs.Wrap(err, "failed to create invocation script")
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
2019-12-03 14:25:25 +00:00
|
|
|
witness.VerificationScript = wif.PrivateKey.PublicKey().GetVerificationScript()
|
2019-12-09 14:14:10 +00:00
|
|
|
tx.Scripts = append(tx.Scripts, witness)
|
2018-12-21 09:32:18 +00:00
|
|
|
tx.Hash()
|
|
|
|
|
2019-11-20 10:14:01 +00:00
|
|
|
return nil
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// GetInvocationScript returns NEO VM script containing transaction signature.
|
2019-11-20 10:14:01 +00:00
|
|
|
func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) {
|
2018-12-21 09:32:18 +00:00
|
|
|
var (
|
2019-09-16 09:18:13 +00:00
|
|
|
buf = io.NewBufBinWriter()
|
2018-12-21 09:32:18 +00:00
|
|
|
signature []byte
|
|
|
|
)
|
2019-09-16 16:31:49 +00:00
|
|
|
tx.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary")
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
2019-01-25 11:20:35 +00:00
|
|
|
data := buf.Bytes()
|
2020-01-16 15:30:16 +00:00
|
|
|
signature = wif.PrivateKey.Sign(data[:(len(data) - 1)])
|
2019-12-03 14:27:35 +00:00
|
|
|
return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
2019-11-20 13:07:43 +00:00
|
|
|
|
|
|
|
// CreateDeploymentScript returns a script that deploys given smart contract
|
|
|
|
// with its metadata.
|
|
|
|
func CreateDeploymentScript(avm []byte, contract *ContractDetails) ([]byte, error) {
|
|
|
|
var props smartcontract.PropertyState
|
|
|
|
|
|
|
|
script := new(bytes.Buffer)
|
|
|
|
if err := vm.EmitBytes(script, []byte(contract.Description)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, []byte(contract.Email)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, []byte(contract.Author)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, []byte(contract.Version)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, []byte(contract.ProjectName)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if contract.HasStorage {
|
|
|
|
props |= smartcontract.HasStorage
|
|
|
|
}
|
|
|
|
if contract.HasDynamicInvocation {
|
|
|
|
props |= smartcontract.HasDynamicInvoke
|
|
|
|
}
|
|
|
|
if contract.IsPayable {
|
|
|
|
props |= smartcontract.IsPayable
|
|
|
|
}
|
|
|
|
if err := vm.EmitInt(script, int64(props)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitInt(script, int64(contract.ReturnType)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
params := make([]byte, len(contract.Parameters))
|
|
|
|
for k := range contract.Parameters {
|
|
|
|
params[k] = byte(contract.Parameters[k])
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, avm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitSyscall(script, "Neo.Contract.Create"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
|
|
|
for j := len(slice) - 1; j >= 0; j-- {
|
|
|
|
fp, err := slice[j].GetFuncParam()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch fp.Type {
|
|
|
|
case ByteArray, Signature:
|
|
|
|
str, err := fp.Value.GetBytesHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case String:
|
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vm.EmitString(script, str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Hash160:
|
|
|
|
hash, err := fp.Value.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
if err := vm.EmitBytes(script, hash.BytesBE()); err != nil {
|
2019-11-28 16:08:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Hash256:
|
|
|
|
hash, err := fp.Value.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
if err := vm.EmitBytes(script, hash.BytesBE()); err != nil {
|
2019-11-28 16:08:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case PublicKey:
|
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := keys.NewPublicKeyFromString(string(str))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vm.EmitBytes(script, key.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Integer:
|
|
|
|
val, err := fp.Value.GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vm.EmitInt(script, int64(val)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Boolean:
|
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch str {
|
|
|
|
case "true":
|
|
|
|
err = vm.EmitInt(script, 1)
|
|
|
|
case "false":
|
|
|
|
err = vm.EmitInt(script, 0)
|
|
|
|
default:
|
|
|
|
err = errors.New("wrong boolean value")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byte, error) {
|
|
|
|
script := new(bytes.Buffer)
|
|
|
|
for i := len(params) - 1; i >= 0; i-- {
|
|
|
|
switch params[i].Type {
|
|
|
|
case stringT:
|
|
|
|
if err := vm.EmitString(script, params[i].String()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case numberT:
|
|
|
|
num, err := params[i].GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := vm.EmitString(script, strconv.Itoa(num)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case arrayT:
|
|
|
|
slice, err := params[i].GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-28 16:08:31 +00:00
|
|
|
err = expandArrayIntoScript(script, slice)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
err = vm.EmitInt(script, int64(len(slice)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
err = vm.EmitOpcode(script, opcode.PACK)
|
2019-11-26 10:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vm.EmitAppCall(script, contract, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
func CreateInvocationScript(contract util.Uint160, funcParams []Param) ([]byte, error) {
|
|
|
|
script := new(bytes.Buffer)
|
|
|
|
err := expandArrayIntoScript(script, funcParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = vm.EmitAppCall(script, contract, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|