From cdf025bf89769ce4c2200a849b8302c21c39cc02 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 12 Mar 2020 19:47:09 +0300 Subject: [PATCH] transaction: implement AddVerificationHash() method --- cli/wallet/nep5.go | 5 +---- pkg/core/transaction/transaction.go | 8 ++++++++ pkg/rpc/client/rpc.go | 6 +----- pkg/rpc/request/txBuilder.go | 6 +----- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/cli/wallet/nep5.go b/cli/wallet/nep5.go index 5640c045f..5961db31e 100644 --- a/cli/wallet/nep5.go +++ b/cli/wallet/nep5.go @@ -311,10 +311,7 @@ func transferNEP5(ctx *cli.Context) error { gas := flags.Fixed8FromContext(ctx, "gas") tx := transaction.NewInvocationTX(w.Bytes(), gas) - tx.Attributes = append(tx.Attributes, transaction.Attribute{ - Usage: transaction.Script, - Data: from.BytesBE(), - }) + tx.AddVerificationHash(from) if err := request.AddInputsAndUnspentsToTx(tx, fromFlag.String(), core.UtilityTokenID(), gas, c); err != nil { return cli.NewExitError(fmt.Errorf("can't add GAS to a tx: %v", err), 1) diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index da6704e8e..2bf7108ee 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -91,6 +91,14 @@ func (t *Transaction) AddInput(in *Input) { t.Inputs = append(t.Inputs, *in) } +// AddVerificationHash adds a script attribute for transaction verification. +func (t *Transaction) AddVerificationHash(addr util.Uint160) { + t.Attributes = append(t.Attributes, Attribute{ + Usage: Script, + Data: addr.BytesBE(), + }) +} + // DecodeBinary implements Serializable interface. func (t *Transaction) DecodeBinary(br *io.BinReader) { t.Type = TXType(br.ReadB()) diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index 893a2dddc..5da25f9af 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -515,11 +515,7 @@ func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sys if err != nil { return txHash, errors.Wrap(err, "failed to get address") } - tx.Attributes = append(tx.Attributes, - transaction.Attribute{ - Usage: transaction.Script, - Data: addr.BytesBE(), - }) + tx.AddVerificationHash(addr) } if err = acc.SignTx(tx); err != nil { diff --git a/pkg/rpc/request/txBuilder.go b/pkg/rpc/request/txBuilder.go index f841a71b3..cd7cd75fc 100644 --- a/pkg/rpc/request/txBuilder.go +++ b/pkg/rpc/request/txBuilder.go @@ -38,11 +38,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac if toAddressHash, err = address.StringToUint160(toAddress); err != nil { return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", toAddress) } - tx.Attributes = append(tx.Attributes, - transaction.Attribute{ - Usage: transaction.Script, - Data: fromAddressHash.BytesBE(), - }) + tx.AddVerificationHash(fromAddressHash) if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil { return nil, errs.Wrap(err, "failed to add inputs and unspents to transaction")