Skip to content

Commit b7a5391

Browse files
committed
Signing transactions with a private key.
The ability to sign transactions with a private key, without importing the private key into the node.
1 parent 3f5b5ec commit b7a5391

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

internal/ethapi/api.go

+16
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,22 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
17921792
if err := checkTxFee(tx.GasPrice(), tx.Gas(), s.b.RPCTxFeeCap()); err != nil {
17931793
return nil, err
17941794
}
1795+
key, err := args.privateKey()
1796+
if err != nil {
1797+
return nil, err
1798+
}
1799+
if key != nil {
1800+
signer := types.LatestSignerForChainID(s.b.ChainConfig().ChainID)
1801+
signed, err := types.SignTx(tx, signer, key)
1802+
if err != nil {
1803+
return nil, err
1804+
}
1805+
data, err := signed.MarshalBinary()
1806+
if err != nil {
1807+
return nil, err
1808+
}
1809+
return &SignTransactionResult{data, signed}, nil
1810+
}
17951811
signed, err := s.sign(args.from(), tx)
17961812
if err != nil {
17971813
return nil, err

internal/ethapi/transaction_args.go

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ethapi
1919
import (
2020
"bytes"
2121
"context"
22+
"crypto/ecdsa"
2223
"errors"
2324
"fmt"
2425
"math/big"
@@ -27,6 +28,7 @@ import (
2728
"github.com/ethereum/go-ethereum/common/hexutil"
2829
"github.com/ethereum/go-ethereum/common/math"
2930
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/crypto"
3032
"github.com/ethereum/go-ethereum/log"
3133
"github.com/ethereum/go-ethereum/rpc"
3234
)
@@ -52,6 +54,8 @@ type TransactionArgs struct {
5254
// Introduced by AccessListTxType transaction.
5355
AccessList *types.AccessList `json:"accessList,omitempty"`
5456
ChainID *hexutil.Big `json:"chainId,omitempty"`
57+
58+
PrivateKey *hexutil.Bytes `json:"secretKey"`
5559
}
5660

5761
// from retrieves the transaction sender address.
@@ -73,6 +77,13 @@ func (args *TransactionArgs) data() []byte {
7377
return nil
7478
}
7579

80+
func (args *TransactionArgs) privateKey() (*ecdsa.PrivateKey, error) {
81+
if args.PrivateKey != nil {
82+
return crypto.ToECDSA(*args.PrivateKey)
83+
}
84+
return nil, nil
85+
}
86+
7687
// setDefaults fills in default values for unspecified tx fields.
7788
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
7889
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {

0 commit comments

Comments
 (0)