forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BLADE-97] Refactor tx signer (#113)
* Refactoring the tx singer logic * Build fix * Fix tests
- Loading branch information
1 parent
1bcd34a
commit 49c1d69
Showing
18 changed files
with
950 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/0xPolygon/polygon-edge/helper/keccak" | ||
"github.com/0xPolygon/polygon-edge/types" | ||
) | ||
|
||
// BerlinSigner may be used for signing legacy (pre-EIP-155 and EIP-155) and EIP-2930 transactions | ||
type BerlinSigner struct { | ||
EIP155Signer | ||
} | ||
|
||
// NewBerlinSigner returns new BerlinSinger object (constructor) | ||
// | ||
// BerlinSigner accepts the following types of transactions: | ||
// - EIP-2930 access list transactions, | ||
// - EIP-155 replay protected transactions, and | ||
// - pre-EIP-155 legacy transactions | ||
func NewBerlinSigner(chainID uint64) *BerlinSigner { | ||
return &BerlinSigner{ | ||
EIP155Signer: EIP155Signer{ | ||
chainID: chainID, | ||
HomesteadSigner: HomesteadSigner{}, | ||
}, | ||
} | ||
} | ||
|
||
// Hash returns the keccak256 hash of the transaction | ||
// | ||
// The EIP-2930 transaction hash preimage is as follows: | ||
// (0x01 || RLP(chainId, nonce, gasPrice, gas, to, value, input, accessList) | ||
// | ||
// Specification: https://eips.ethereum.org/EIPS/eip-2930#specification | ||
func (signer *BerlinSigner) Hash(tx *types.Transaction) types.Hash { | ||
if tx.Type() != types.AccessListTx { | ||
return signer.EIP155Signer.Hash(tx) | ||
} | ||
|
||
var hash []byte | ||
|
||
RLP := arenaPool.Get() | ||
|
||
// RLP(-, -, -, -, -, -, -, -) | ||
hashPreimage := RLP.NewArray() | ||
|
||
// RLP(chainId, -, -, -, -, -, -, -) | ||
hashPreimage.Set(RLP.NewUint(signer.chainID)) | ||
|
||
// RLP(chainId, nonce, -, -, -, -, -, -) | ||
hashPreimage.Set(RLP.NewUint(tx.Nonce())) | ||
|
||
// RLP(chainId, nonce, gasPrice, -, -, -, -, -) | ||
hashPreimage.Set(RLP.NewBigInt(tx.GasPrice())) | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, -, -, -, -) | ||
hashPreimage.Set(RLP.NewUint(tx.Gas())) | ||
|
||
// Checking whether the transaction is a smart contract deployment | ||
if tx.To() == nil { | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, to, -, -, -) | ||
hashPreimage.Set(RLP.NewNull()) | ||
} else { | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, to, -, -, -) | ||
hashPreimage.Set(RLP.NewCopyBytes((*(tx.To())).Bytes())) | ||
} | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, to, value, -, -) | ||
hashPreimage.Set(RLP.NewBigInt(tx.Value())) | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, to, value, input, -) | ||
hashPreimage.Set(RLP.NewCopyBytes(tx.Input())) | ||
|
||
// Serialization format of the access list: [[{20-bytes address}, [{32-bytes key}, ...]], ...] where `...` denotes zero or more items | ||
accessList := RLP.NewArray() | ||
|
||
if tx.AccessList() != nil { | ||
|
||
// accessTuple contains (address, storageKeys[]) | ||
for _, accessTuple := range tx.AccessList() { | ||
|
||
accessTupleSerFormat := RLP.NewArray() | ||
accessTupleSerFormat.Set(RLP.NewCopyBytes(accessTuple.Address.Bytes())) | ||
|
||
storageKeysSerFormat := RLP.NewArray() | ||
|
||
for _, storageKey := range accessTuple.StorageKeys { | ||
storageKeysSerFormat.Set(RLP.NewCopyBytes(storageKey.Bytes())) | ||
} | ||
|
||
accessTupleSerFormat.Set(storageKeysSerFormat) | ||
accessList.Set(accessTupleSerFormat) | ||
} | ||
} | ||
|
||
// RLP(chainId, nonce, gasPrice, gas, to, value, input, accessList) | ||
hashPreimage.Set(accessList) | ||
|
||
// keccak256(0x01 || RLP(chainId, nonce, gasPrice, gas, to, value, input, accessList) | ||
hash = keccak.PrefixedKeccak256Rlp([]byte{byte(tx.Type())}, nil, hashPreimage) | ||
|
||
arenaPool.Put(RLP) | ||
|
||
return types.BytesToHash(hash) | ||
} | ||
|
||
// Sender returns the sender of the transaction | ||
func (signer *BerlinSigner) Sender(tx *types.Transaction) (types.Address, error) { | ||
if tx.Type() != types.AccessListTx { | ||
return signer.EIP155Signer.Sender(tx) | ||
} | ||
|
||
v, r, s := tx.RawSignatureValues() | ||
|
||
return recoverAddress(signer.Hash(tx), r, s, v, true) | ||
} | ||
|
||
// SingTx takes the original transaction as input and returns its signed version | ||
func (signer *BerlinSigner) SignTx(tx *types.Transaction, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) { | ||
if tx.Type() != types.AccessListTx { | ||
return signer.EIP155Signer.SignTx(tx, privateKey) | ||
} | ||
|
||
tx = tx.Copy() | ||
|
||
h := signer.Hash(tx) | ||
|
||
sig, err := Sign(privateKey, h[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r := new(big.Int).SetBytes(sig[:32]) | ||
s := new(big.Int).SetBytes(sig[32:64]) | ||
|
||
if s.Cmp(secp256k1NHalf) > 0 { | ||
return nil, errors.New("SignTx method: S must be inclusively lower than secp256k1n/2") | ||
} | ||
|
||
v := new(big.Int).SetBytes(signer.calculateV(sig[64])) | ||
|
||
tx.SetSignatureValues(v, r, s) | ||
|
||
return tx, nil | ||
} | ||
|
||
// Private method calculateV returns the V value for the EIP-2930 transactions | ||
// | ||
// V represents the parity of the Y coordinate | ||
func (e *BerlinSigner) calculateV(parity byte) []byte { | ||
return big.NewInt(int64(parity)).Bytes() | ||
} |
Oops, something went wrong.