Skip to content

Commit 2e1bd01

Browse files
authored
kms support geth bind.TransactOpts (#47)
* kms support geth bind.TransactOpts
1 parent 2077aaf commit 2e1bd01

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

eth/awskms.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/aws/aws-sdk-go/aws/credentials"
1111
"github.com/aws/aws-sdk-go/aws/session"
1212
"github.com/aws/aws-sdk-go/service/kms"
13+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1314
"github.com/ethereum/go-ethereum/common"
1415
"github.com/ethereum/go-ethereum/core/types"
1516
"github.com/ethereum/go-ethereum/crypto"
@@ -66,25 +67,44 @@ func NewKmsSigner(region, keyAlias, awsKey, awsSec string, chainId *big.Int) (*K
6667
}, nil
6768
}
6869

70+
// satisfy Signer interface SignEthMessage
6971
func (s *KmsSigner) SignEthMessage(data []byte) ([]byte, error) {
7072
return s.Sign(GeneratePrefixedHash(data))
7173
}
7274

75+
// satisfy Signer interface SignEthTransaction
7376
func (s *KmsSigner) SignEthTransaction(rawTx []byte) ([]byte, error) {
7477
tx := new(types.Transaction)
75-
if err := rlp.DecodeBytes(rawTx, tx); err != nil {
78+
err := rlp.DecodeBytes(rawTx, tx)
79+
if err != nil {
7680
return nil, err
7781
}
78-
londonSigner := types.NewLondonSigner(s.chainId)
79-
signature, err := s.Sign(londonSigner.Hash(tx).Bytes())
82+
tx, err = s.SignerFn(s.Addr, tx)
8083
if err != nil {
8184
return nil, err
8285
}
83-
tx, err = tx.WithSignature(londonSigner, signature)
86+
return rlp.EncodeToBytes(tx)
87+
}
88+
89+
// return bind.TransactOpts to be used in bound contract tx
90+
func (s *KmsSigner) NewTransactOpts() *bind.TransactOpts {
91+
return &bind.TransactOpts{
92+
From: s.Addr,
93+
Signer: s.SignerFn,
94+
}
95+
}
96+
97+
// satisfy bind/base.go SignerFn
98+
func (s *KmsSigner) SignerFn(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
99+
if addr != s.Addr {
100+
return nil, bind.ErrNotAuthorized
101+
}
102+
signer := types.LatestSignerForChainID(s.chainId)
103+
signature, err := s.Sign(signer.Hash(tx).Bytes())
84104
if err != nil {
85105
return nil, err
86106
}
87-
return rlp.EncodeToBytes(tx)
107+
return tx.WithSignature(signer, signature)
88108
}
89109

90110
// input must be hash, return 65 bytes sig or error. sig[64] is 0 or 1

0 commit comments

Comments
 (0)