2018-12-21 09:32:18 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
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-09-03 15:11:13 +00:00
|
|
|
wif, assetID, address, 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
|
|
|
|
|
|
|
if fromAddressHash, err = crypto.Uint160DecodeAddress(fromAddress); err != nil {
|
|
|
|
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
if toAddressHash, err = crypto.Uint160DecodeAddress(address); err != nil {
|
|
|
|
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", address)
|
|
|
|
}
|
|
|
|
tx.Attributes = append(tx.Attributes,
|
|
|
|
&transaction.Attribute{
|
|
|
|
Usage: transaction.Script,
|
|
|
|
Data: fromAddressHash.Bytes(),
|
|
|
|
})
|
|
|
|
|
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.
|
|
|
|
func AddInputsAndUnspentsToTx(tx *transaction.Transaction, address string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error {
|
|
|
|
scriptHash, err := crypto.Uint160DecodeAddress(address)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrapf(err, "failed to take script hash from address: %v", address)
|
|
|
|
}
|
|
|
|
inputs, spent, err := balancer.CalculateInputs(address, assetID, amount)
|
|
|
|
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-09-05 06:35:02 +00:00
|
|
|
witness.VerificationScript = wif.GetVerificationScript()
|
2018-12-21 09:32:18 +00:00
|
|
|
tx.Scripts = append(tx.Scripts, &witness)
|
|
|
|
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
|
|
|
const (
|
|
|
|
pushbytes64 = 0x40
|
|
|
|
)
|
|
|
|
var (
|
|
|
|
err error
|
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()
|
|
|
|
signature, err = wif.PrivateKey.Sign(data[:(len(data) - 1)])
|
2018-12-21 09:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed ti sign transaction with private key")
|
|
|
|
}
|
|
|
|
return append([]byte{pushbytes64}, signature...), nil
|
|
|
|
}
|