Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] support pre-EIP155 unprotected transaction #3965

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions action/rlp_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func RawTxToSignedTx(rawTx *types.Transaction, signer types.Signer, sig []byte)
sc[64] -= 27
}

// TODO: currently all our web3 tx are EIP-155 protected tx
// in the future release, use proper signer for other supported tx types (EIP-1559, EIP-2930)
signedTx, err := rawTx.WithSignature(signer, sc)
if err != nil {
return nil, err
Expand Down Expand Up @@ -87,14 +85,80 @@ func DecodeRawTx(rawData string, chainID uint32) (tx *types.Transaction, sig []b

// NewEthSigner returns the proper signer for Eth-compatible tx
func NewEthSigner(txType iotextypes.Encoding, chainID uint32) (types.Signer, error) {
// TODO: use proper signer according to tx type
switch txType {
case iotextypes.Encoding_IOTEX_PROTOBUF:
// native tx use same signature format as that of Homestead
return types.HomesteadSigner{}, nil
case iotextypes.Encoding_ETHEREUM_RLP:
case iotextypes.Encoding_ETHEREUM_EIP155:
Copy link
Member

@Liuhaai Liuhaai Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iotextypes.Encoding_ETHEREUM_RLP & iotextypes.Encoding_ETHEREUM_PRE_EIP155?
Otherwise, there will be backwards compatibility issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding_ETHEREUM_UNPROTECTED is a better name/term, Ethereum call it unprotected tx, and there is Protected() bool func

Copy link
Member Author

@dustinxie dustinxie Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there won't be backwards compatibility issue, _EIP155 is an alias of _RLP

Encoding_ETHEREUM_EIP155 == Encoding_ETHEREUM_RLP == 1

the reason to create _EIP155 is because now we have 3 types (all using RLP encoding), so old _RLP is not correct now

return types.NewEIP155Signer(big.NewInt(int64(chainID))), nil
case iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return types.HomesteadSigner{}, nil
default:
return nil, ErrInvalidAct
}
}

// DecodeEtherTx decodes raw data string into eth tx
func DecodeEtherTx(rawData string) (*types.Transaction, error) {
//remove Hex prefix and decode string to byte
if strings.HasPrefix(rawData, "0x") || strings.HasPrefix(rawData, "0X") {
rawData = rawData[2:]
}
rawTxBytes, err := hex.DecodeString(rawData)
if err != nil {
return nil, err
}

// decode raw data into eth tx
tx := types.Transaction{}
if err = tx.UnmarshalBinary(rawTxBytes); err != nil {
return nil, err
}
return &tx, nil
}

// ExtractTypeSigPubkey extracts tx type, signature, and pubkey
func ExtractTypeSigPubkey(tx *types.Transaction) (iotextypes.Encoding, []byte, crypto.PublicKey, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split to 2 funcs for clarity

var (
encoding iotextypes.Encoding
signer types.Signer
V, R, S = tx.RawSignatureValues()
)
// extract correct V value
switch tx.Type() {
case types.LegacyTxType:
if tx.Protected() {
chainIDMul := tx.ChainId()
V = new(big.Int).Sub(V, chainIDMul.Lsh(chainIDMul, 1))
V.Sub(V, big.NewInt(8))
encoding = iotextypes.Encoding_ETHEREUM_EIP155
signer = types.NewEIP155Signer(tx.ChainId())
} else {
// tx has pre-EIP155 signature
encoding = iotextypes.Encoding_ETHEREUM_UNPROTECTED
signer = types.HomesteadSigner{}
}
default:
return encoding, nil, nil, ErrNotSupported
}

// construct signature
if V.BitLen() > 8 {
return encoding, nil, nil, ErrNotSupported
}

var (
r, s = R.Bytes(), S.Bytes()
sig = make([]byte, 65)
pubkey crypto.PublicKey
err error
)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = byte(V.Uint64())

// recover public key
rawHash := signer.Hash(tx)
pubkey, err = crypto.RecoverPubkey(rawHash[:], sig)
return encoding, sig, pubkey, err
}
Loading