2018-12-21 09:32:18 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"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"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
errs "github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transaction, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tx = transaction.NewContractTX()
|
|
|
|
toAddressHash, fromAddressHash util.Uint160
|
|
|
|
fromAddress string
|
|
|
|
senderOutput, receiverOutput *transaction.Output
|
|
|
|
inputs []transaction.Input
|
|
|
|
spent util.Fixed8
|
|
|
|
witness transaction.Witness
|
|
|
|
|
|
|
|
wif, assetID, address, amount, balancer = params.wif, params.assetId, params.address, params.value, params.balancer
|
|
|
|
)
|
|
|
|
|
|
|
|
if fromAddress, err = wif.PrivateKey.Address(); err != nil {
|
|
|
|
return nil, errs.Wrapf(err, "Failed to take address from WIF: %v", wif.S)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if inputs, spent, err = balancer.CalculateInputs(fromAddress, assetID, amount); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed to get inputs for transaction")
|
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
|
|
|
tx.AddInput(&input)
|
|
|
|
}
|
|
|
|
|
2019-02-07 11:44:51 +00:00
|
|
|
if senderUnspent := spent - amount; senderUnspent > 0 {
|
|
|
|
senderOutput = transaction.NewOutput(assetID, senderUnspent, fromAddressHash)
|
|
|
|
tx.AddOutput(senderOutput)
|
|
|
|
}
|
2018-12-21 09:32:18 +00:00
|
|
|
receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash)
|
|
|
|
tx.AddOutput(receiverOutput)
|
|
|
|
|
2019-01-22 12:14:40 +00:00
|
|
|
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil, errs.Wrap(err, "Failed to create invocation script")
|
|
|
|
}
|
|
|
|
if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed to create verification script")
|
|
|
|
}
|
|
|
|
tx.Scripts = append(tx.Scripts, &witness)
|
|
|
|
tx.Hash()
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:29:42 +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
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
signature []byte
|
|
|
|
)
|
|
|
|
if err = tx.EncodeBinary(buf); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed to encode transaction to binary")
|
|
|
|
}
|
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
|
|
|
|
}
|