diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-07-06 16:59:41 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-07-06 16:59:41 +0800 |
commit | b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d (patch) | |
tree | 6cd1903af6d8d645e6f3bf4defbaa65c35c6fd88 | |
parent | 35add89c879dd1d12e7ed702d7543f5a749a1d3e (diff) | |
parent | ec9620fb2f375379d3404a834a3350089702648a (diff) | |
download | dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar.gz dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar.bz2 dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar.lz dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar.xz dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.tar.zst dexon-b533aaa765c2a6fafed5d7e8bfe5a54c2303ad6d.zip |
Merge pull request #1409 from fjl/fix-tx-hash
core/types, xeth: separate tx hash and tx signature hash
-rw-r--r-- | core/types/transaction.go | 18 | ||||
-rw-r--r-- | core/types/transaction_test.go | 6 | ||||
-rw-r--r-- | xeth/xeth.go | 2 |
3 files changed, 17 insertions, 9 deletions
diff --git a/core/types/transaction.go b/core/types/transaction.go index 95deac36e..c381fc5f3 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -116,11 +116,21 @@ func (tx *Transaction) To() *common.Address { } } +// Hash hashes the RLP encoding of tx. +// It uniquely identifies the transaction. func (tx *Transaction) Hash() common.Hash { if hash := tx.hash.Load(); hash != nil { return hash.(common.Hash) } - v := rlpHash([]interface{}{ + v := rlpHash(tx) + tx.hash.Store(v) + return v +} + +// SigHash returns the hash to be signed by the sender. +// It does not uniquely identify the transaction. +func (tx *Transaction) SigHash() common.Hash { + return rlpHash([]interface{}{ tx.data.AccountNonce, tx.data.Price, tx.data.GasLimit, @@ -128,8 +138,6 @@ func (tx *Transaction) Hash() common.Hash { tx.data.Amount, tx.data.Payload, }) - tx.hash.Store(v) - return v } func (tx *Transaction) Size() common.StorageSize { @@ -180,7 +188,7 @@ func (tx *Transaction) publicKey() ([]byte, error) { sig[64] = tx.data.V - 27 // recover the public key from the signature - hash := tx.Hash() + hash := tx.SigHash() pub, err := crypto.Ecrecover(hash[:], sig) if err != nil { glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err) @@ -204,7 +212,7 @@ func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) { } func (tx *Transaction) SignECDSA(prv *ecdsa.PrivateKey) (*Transaction, error) { - h := tx.Hash() + h := tx.SigHash() sig, err := crypto.Sign(h[:], prv) if err != nil { return nil, err diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index dd9c5e87b..c9da4b73b 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -34,11 +34,11 @@ var ( ) ) -func TestTransactionHash(t *testing.T) { - if emptyTx.Hash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") { +func TestTransactionSigHash(t *testing.T) { + if emptyTx.SigHash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") { t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash()) } - if rightvrsTx.Hash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") { + if rightvrsTx.SigHash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") { t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash()) } } diff --git a/xeth/xeth.go b/xeth/xeth.go index 88d802820..4bd18a2f6 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -977,7 +977,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS } func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) (*types.Transaction, error) { - hash := tx.Hash() + hash := tx.SigHash() sig, err := self.doSign(from, hash, didUnlock) if err != nil { return tx, err |