Skip to content

Commit b533aaa

Browse files
committed
Merge pull request bnb-chain#1409 from fjl/fix-tx-hash
core/types, xeth: separate tx hash and tx signature hash
2 parents 35add89 + ec9620f commit b533aaa

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

‎core/types/transaction.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,28 @@ func (tx *Transaction) To() *common.Address {
116116
}
117117
}
118118

119+
// Hash hashes the RLP encoding of tx.
120+
// It uniquely identifies the transaction.
119121
func (tx *Transaction) Hash() common.Hash {
120122
if hash := tx.hash.Load(); hash != nil {
121123
return hash.(common.Hash)
122124
}
123-
v := rlpHash([]interface{}{
125+
v := rlpHash(tx)
126+
tx.hash.Store(v)
127+
return v
128+
}
129+
130+
// SigHash returns the hash to be signed by the sender.
131+
// It does not uniquely identify the transaction.
132+
func (tx *Transaction) SigHash() common.Hash {
133+
return rlpHash([]interface{}{
124134
tx.data.AccountNonce,
125135
tx.data.Price,
126136
tx.data.GasLimit,
127137
tx.data.Recipient,
128138
tx.data.Amount,
129139
tx.data.Payload,
130140
})
131-
tx.hash.Store(v)
132-
return v
133141
}
134142

135143
func (tx *Transaction) Size() common.StorageSize {
@@ -180,7 +188,7 @@ func (tx *Transaction) publicKey() ([]byte, error) {
180188
sig[64] = tx.data.V - 27
181189

182190
// recover the public key from the signature
183-
hash := tx.Hash()
191+
hash := tx.SigHash()
184192
pub, err := crypto.Ecrecover(hash[:], sig)
185193
if err != nil {
186194
glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
@@ -204,7 +212,7 @@ func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) {
204212
}
205213

206214
func (tx *Transaction) SignECDSA(prv *ecdsa.PrivateKey) (*Transaction, error) {
207-
h := tx.Hash()
215+
h := tx.SigHash()
208216
sig, err := crypto.Sign(h[:], prv)
209217
if err != nil {
210218
return nil, err

‎core/types/transaction_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ var (
3434
)
3535
)
3636

37-
func TestTransactionHash(t *testing.T) {
38-
if emptyTx.Hash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
37+
func TestTransactionSigHash(t *testing.T) {
38+
if emptyTx.SigHash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
3939
t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
4040
}
41-
if rightvrsTx.Hash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
41+
if rightvrsTx.SigHash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
4242
t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
4343
}
4444
}

‎xeth/xeth.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
977977
}
978978

979979
func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) (*types.Transaction, error) {
980-
hash := tx.Hash()
980+
hash := tx.SigHash()
981981
sig, err := self.doSign(from, hash, didUnlock)
982982
if err != nil {
983983
return tx, err

0 commit comments

Comments
 (0)