zksync-sdk / zksync2-go

zksync2-go is a geth library adapted to work with the zkSync Era.
Apache License 2.0
87 stars 36 forks source link

Incorrect sender address was returned #13

Closed allush closed 1 year ago

allush commented 1 year ago

There is transaction on ZkSync Era https://explorer.zksync.io/tx/0x197058ca77d906d883f75c1a720b414142092b27862f344044411b7268c7169e

Library returns incorrect sender address for this transaction -0x120982a08B06f31583c10F0383a2C3aa46f06606

Actual sender address is 0xBB4ddF60b4013B10E7D1709DED7f4B8A81398073

Code for reproduce

ctx := context.Background()
zp, err := zksync2.NewDefaultProvider("https://mainnet.era.zksync.io")
if err != nil {
    panic(err)
}

txHash := "0x197058ca77d906d883f75c1a720b414142092b27862f344044411b7268c7169e"
tx, _, err := zp.GetClient().TransactionByHash(ctx, common.HexToHash(txHash))
if err != nil {
    panic(err)
}

signer := types.LatestSignerForChainID(new(big.Int).SetUint64(324))
sender, err := signer.Sender(tx)
if err != nil {
    panic(err)
}

fmt.Printf("%s \n", sender)

How to get actual sender address?

allush commented 1 year ago

This code works fine:

func (tx *Transaction) From() common.Address {
    return tx.from.Load().(sigCache).from
}

But this is a patch candidate to go-ethereum library

tiennampham23 commented 1 year ago

@allush use l2tx, err := zp.GetTransaction(txHash) instead zp.GetClient().TransactionByHash(txHash)

And you can get

from := l2tx.From

Because when you use TransactionByHash by default, go-ethereum will replace the sender in there https://github.com/ethereum/go-ethereum/blob/699243f8ae870fa2ddbc5cb919fda85313e4684a/ethclient/ethclient.go#L238

The actual json.From is not you when sending this tx from L2 to L1

danijelTxFusion commented 1 year ago

zp.GetClient() returns instance of ethclient.Client which should be used internally because some of the RPC method are not compatible with zkSync node. That's why it will be removed in future releases because it introduces a lot of confusions among users.

allush commented 1 year ago

ok, thanks @danijelTxFusion @tiennampham23