fix address recovery from pubkey.

This commit is contained in:
Raúl Kripalani 2022-10-22 17:29:12 +01:00 committed by vyzo
parent 9017e5de52
commit 6d5ec6aff8
3 changed files with 19 additions and 4 deletions

View File

@ -22,6 +22,8 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)
const Eip1559TxType = 2
type EthTx struct {
ChainID EthUint64 `json:"chainId"`
Nonce EthUint64 `json:"nonce"`
@ -279,12 +281,18 @@ func (tx *EthTxArgs) Sender() (address.Address, error) {
return address.Undef, err
}
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubk[0] == 0x04 {
pubk = pubk[1:]
}
// Calculate the f4 address based on the keccak hash of the pubkey.
hasher.Reset()
hasher.Write(pubk)
addrHash := hasher.Sum(nil)
ethAddr := hasher.Sum(nil)[12:]
return address.NewDelegatedAddress(builtintypes.EthereumAddressManagerActorID, addrHash[len(addrHash)-20:])
return address.NewDelegatedAddress(builtintypes.EthereumAddressManagerActorID, ethAddr)
}
func parseEip1559Tx(data []byte) (*EthTxArgs, error) {
@ -392,7 +400,7 @@ func ParseEthTxArgs(data []byte) (*EthTxArgs, error) {
} else if data[0] == 1 {
// EIP-2930
return nil, fmt.Errorf("EIP-2930 transaction is not supported")
} else if data[0] == 2 {
} else if data[0] == Eip1559TxType {
// EIP-1559
return parseEip1559Tx(data)
}

View File

@ -775,6 +775,7 @@ var StateGetActorCmd = &cli.Command{
fmt.Printf("Nonce:\t\t%d\n", a.Nonce)
fmt.Printf("Code:\t\t%s (%s)\n", a.Code, strtype)
fmt.Printf("Head:\t\t%s\n", a.Head)
fmt.Printf("Predictable address:\t\t%s\n", a.Address)
return nil
},

View File

@ -41,11 +41,17 @@ func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {
return err
}
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubk[0] == 0x04 {
pubk = pubk[1:]
}
hasher.Reset()
hasher.Write(pubk)
addrHash := hasher.Sum(nil)
maybeaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, addrHash[len(addrHash)-20:])
maybeaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, addrHash[12:])
if err != nil {
return err
}