From 750612f2bc6a348bbaeb009435803926f00e7527 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Fri, 22 Sep 2017 17:16:22 +0200 Subject: [PATCH] fix SendTransaction signature in go-ethereum (#355) I created a separate method SendTransactionWithPassphrase which accepts passphrase as a second argument. It's an exact copy of SendTransaction except for calling wallet.SignTxWithPassphrase. --- geth/api/backend_test.go | 2 +- .../go-ethereum/internal/ethapi/api.go | 46 ++++++++++++++++++- .../internal/ethapi/status_backend.go | 4 +- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/geth/api/backend_test.go b/geth/api/backend_test.go index 836d30d30..f1a034c92 100644 --- a/geth/api/backend_test.go +++ b/geth/api/backend_test.go @@ -206,7 +206,7 @@ func (s *BackendTestSuite) TestCallRPC() { "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],"id":1}`, func(resultJSON string) { log.Info("eth_sendTransaction") - s.T().Log("GOT: ", resultJSON) + s.NotContains(resultJSON, "error") progress <- struct{}{} }, }, diff --git a/vendor/github.com/ethereum/go-ethereum/internal/ethapi/api.go b/vendor/github.com/ethereum/go-ethereum/internal/ethapi/api.go index a234d82e6..47957dd14 100644 --- a/vendor/github.com/ethereum/go-ethereum/internal/ethapi/api.go +++ b/vendor/github.com/ethereum/go-ethereum/internal/ethapi/api.go @@ -1180,9 +1180,10 @@ func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c return tx.Hash(), nil } -// SendTransaction creates a transaction by unpacking queued transaction, signs it and submits to the +// SendTransactionWithPassphrase creates a transaction by unpacking queued transaction, signs it and submits to the // transaction pool. -func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs, passphrase string) (common.Hash, error) { +// @Status +func (s *PublicTransactionPoolAPI) SendTransactionWithPassphrase(ctx context.Context, args SendTxArgs, passphrase string) (common.Hash, error) { // Set some sanity defaults and terminate on failure if err := args.setDefaults(ctx, s.b); err != nil { return common.Hash{}, err @@ -1221,6 +1222,47 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen return submitTransaction(ctx, s.b, signed) } +// SendTransaction creates a transaction by unpacking queued transaction, signs it and submits to the +// transaction pool. +func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) { + // Set some sanity defaults and terminate on failure + if err := args.setDefaults(ctx, s.b); err != nil { + return common.Hash{}, err + } + + // Look up the wallet containing the requested signer + account := accounts.Account{Address: args.From} + + wallet, err := s.b.AccountManager().Find(account) + if err != nil { + return common.Hash{}, err + } + + if args.Nonce == nil { + // Hold the addresse's mutex around signing to prevent concurrent assignment of + // the same nonce to multiple accounts. + s.nonceLock.LockAddr(args.From) + defer s.nonceLock.UnlockAddr(args.From) + } + + // Set some sanity defaults and terminate on failure + if err := args.setDefaults(ctx, s.b); err != nil { + return common.Hash{}, err + } + // Assemble the transaction and sign with the wallet + tx := args.toTransaction() + + var chainID *big.Int + if config := s.b.ChainConfig(); config.IsEIP155(s.b.CurrentBlock().Number()) { + chainID = config.ChainId + } + signed, err := wallet.SignTx(account, tx, chainID) + if err != nil { + return common.Hash{}, err + } + return submitTransaction(ctx, s.b, signed) +} + // SendRawTransaction will add the signed transaction to the transaction pool. // The sender is responsible for signing the transaction and using the correct nonce. func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (string, error) { diff --git a/vendor/github.com/ethereum/go-ethereum/internal/ethapi/status_backend.go b/vendor/github.com/ethereum/go-ethereum/internal/ethapi/status_backend.go index 763d0e27e..77535d92c 100644 --- a/vendor/github.com/ethereum/go-ethereum/internal/ethapi/status_backend.go +++ b/vendor/github.com/ethereum/go-ethereum/internal/ethapi/status_backend.go @@ -45,7 +45,7 @@ func (b *StatusBackend) AccountManager() *status.AccountManager { return b.am } -// SendTransaction wraps call to PublicTransactionPoolAPI.SendTransaction +// SendTransaction wraps call to PublicTransactionPoolAPI.SendTransactionWithPassphrase func (b *StatusBackend) SendTransaction(ctx context.Context, args status.SendTxArgs, passphrase string) (common.Hash, error) { if ctx == nil { ctx = context.Background() @@ -57,7 +57,7 @@ func (b *StatusBackend) SendTransaction(ctx context.Context, args status.SendTxA } } - return b.txapi.SendTransaction(ctx, SendTxArgs(args), passphrase) + return b.txapi.SendTransactionWithPassphrase(ctx, SendTxArgs(args), passphrase) } // EstimateGas uses underlying blockchain API to obtain gas for a given tx arguments