rpc: refactor out reusable parts of CreateRawContractTransaction()

Signing and inputs/outputs management is common for different transactions, so
make separate functions for them.
This commit is contained in:
Roman Khimov 2019-11-20 13:14:01 +03:00
parent 3adc9150d3
commit 310104667d

View file

@ -16,10 +16,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
tx = transaction.NewContractTX() tx = transaction.NewContractTX()
toAddressHash, fromAddressHash util.Uint160 toAddressHash, fromAddressHash util.Uint160
fromAddress string fromAddress string
senderOutput, receiverOutput *transaction.Output 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 wif, assetID, address, amount, balancer = params.wif, params.assetID, params.address, params.value, params.balancer
) )
@ -39,32 +36,57 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
Data: fromAddressHash.Bytes(), Data: fromAddressHash.Bytes(),
}) })
if inputs, spent, err = balancer.CalculateInputs(fromAddress, assetID, amount); err != nil { if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil {
return nil, errs.Wrap(err, "Failed to get inputs for transaction") 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")
} }
for _, input := range inputs { for _, input := range inputs {
tx.AddInput(&input) tx.AddInput(&input)
} }
if senderUnspent := spent - amount; senderUnspent > 0 { if senderUnspent := spent - amount; senderUnspent > 0 {
senderOutput = transaction.NewOutput(assetID, senderUnspent, fromAddressHash) senderOutput := transaction.NewOutput(assetID, senderUnspent, scriptHash)
tx.AddOutput(senderOutput) tx.AddOutput(senderOutput)
} }
receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash) return nil
tx.AddOutput(receiverOutput) }
// 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
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil { if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
return nil, errs.Wrap(err, "Failed to create invocation script") return errs.Wrap(err, "failed to create invocation script")
} }
witness.VerificationScript = wif.GetVerificationScript() witness.VerificationScript = wif.GetVerificationScript()
tx.Scripts = append(tx.Scripts, &witness) tx.Scripts = append(tx.Scripts, &witness)
tx.Hash() tx.Hash()
return tx, nil return nil
} }
// GetInvocationScript returns NEO VM script containing transaction signature. // GetInvocationScript returns NEO VM script containing transaction signature.
func GetInvocationScript(tx *transaction.Transaction, wif keys.WIF) ([]byte, error) { func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) {
const ( const (
pushbytes64 = 0x40 pushbytes64 = 0x40
) )