From 8a7233c43c98c5e3143c621a3f7404230b522a7f Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Thu, 20 Jun 2024 14:26:48 +0900 Subject: [PATCH] fix `RegisterEnclaveKey`'s domain Signed-off-by: Jun Kimura --- light-clients/lcp/types/lcp.go | 28 ++++++++++++---------------- light-clients/lcp/types/update.go | 4 ++-- relay/lcp.go | 10 +--------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/light-clients/lcp/types/lcp.go b/light-clients/lcp/types/lcp.go index 78147ec..17d56ec 100644 --- a/light-clients/lcp/types/lcp.go +++ b/light-clients/lcp/types/lcp.go @@ -86,11 +86,11 @@ func LCPClientDomain(chainId int64, verifyingContract common.Address, salt commo } } -func GetRegisterEnclaveKeyTypedData(salt common.Hash, avr string) apitypes.TypedData { +func GetRegisterEnclaveKeyTypedData(avr string) apitypes.TypedData { return apitypes.TypedData{ PrimaryType: "RegisterEnclaveKey", Types: RegisterEnclaveKeyTypes, - Domain: LCPClientDomain(0, common.Address{}, salt), + Domain: LCPClientDomain(0, common.Address{}, common.Hash{}), Message: apitypes.TypedDataMessage{ "avr": avr, }, @@ -125,14 +125,22 @@ func GetUpdateOperatorsTypedData( } } -func ComputeEIP712RegisterEnclaveKeyWithSalt(salt common.Hash, report string) ([]byte, error) { - _, raw, err := apitypes.TypedDataAndHash(GetRegisterEnclaveKeyTypedData(salt, report)) +func ComputeEIP712RegisterEnclaveKey(report string) ([]byte, error) { + _, raw, err := apitypes.TypedDataAndHash(GetRegisterEnclaveKeyTypedData(report)) if err != nil { return nil, err } return []byte(raw), nil } +func ComputeEIP712RegisterEnclaveKeyHash(report string) (common.Hash, error) { + bz, err := ComputeEIP712RegisterEnclaveKey(report) + if err != nil { + return common.Hash{}, err + } + return crypto.Keccak256Hash(bz), nil +} + func ComputeEIP712UpdateOperators( chainId int64, verifyingContract common.Address, @@ -183,18 +191,6 @@ func ComputeCosmosChainSalt(chainID string, prefix []byte) common.Hash { return crypto.Keccak256Hash(msg) } -func ComputeEIP712CosmosRegisterEnclaveKey(chainID string, prefix []byte, report string) ([]byte, error) { - return ComputeEIP712RegisterEnclaveKeyWithSalt(ComputeCosmosChainSalt(chainID, prefix), report) -} - -func ComputeEIP712CosmosRegisterEnclaveKeyHash(chainID string, prefix []byte, report string) (common.Hash, error) { - bz, err := ComputeEIP712CosmosRegisterEnclaveKey(chainID, prefix, report) - if err != nil { - return common.Hash{}, err - } - return crypto.Keccak256Hash(bz), nil -} - func ComputeEIP712CosmosUpdateOperators( chainID string, prefix []byte, diff --git a/light-clients/lcp/types/update.go b/light-clients/lcp/types/update.go index 11677b4..da6d517 100644 --- a/light-clients/lcp/types/update.go +++ b/light-clients/lcp/types/update.go @@ -121,7 +121,7 @@ func (cs ClientState) verifyRegisterEnclaveKey(ctx sdk.Context, store storetypes } var operator common.Address if len(message.OperatorSignature) > 0 { - commitment, err := ComputeEIP712CosmosRegisterEnclaveKeyHash(ctx.ChainID(), []byte(exported.StoreKey), string(message.Report)) + commitment, err := ComputeEIP712RegisterEnclaveKeyHash(string(message.Report)) if err != nil { return errorsmod.Wrapf(clienttypes.ErrInvalidHeader, "failed to compute commitment: %v", err) } @@ -256,7 +256,7 @@ func (cs ClientState) registerEnclaveKey(ctx sdk.Context, clientStore storetypes } var operator common.Address if len(message.OperatorSignature) > 0 { - commitment, err := ComputeEIP712CosmosRegisterEnclaveKeyHash(ctx.ChainID(), []byte(exported.StoreKey), string(message.Report)) + commitment, err := ComputeEIP712RegisterEnclaveKeyHash(string(message.Report)) if err != nil { panic(errorsmod.Wrapf(clienttypes.ErrInvalidHeader, "failed to compute commitment: %v", err)) } diff --git a/relay/lcp.go b/relay/lcp.go index 7b942d3..430509c 100644 --- a/relay/lcp.go +++ b/relay/lcp.go @@ -382,7 +382,7 @@ func (pr *Prover) registerEnclaveKey(counterparty core.Chain, eki *enclave.Encla if expectedOperator != [20]byte{} && operator != expectedOperator { return nil, fmt.Errorf("operator mismatch: expected 0x%x, but got 0x%x", expectedOperator, operator) } - commitment, err := pr.ComputeEIP712RegisterEnclaveKeyHash(eki.Report) + commitment, err := lcptypes.ComputeEIP712RegisterEnclaveKeyHash(eki.Report) if err != nil { return nil, err } @@ -411,14 +411,6 @@ func (pr *Prover) registerEnclaveKey(counterparty core.Chain, eki *enclave.Encla return ids[0], nil } -func (pr *Prover) ComputeEIP712RegisterEnclaveKeyHash(report string) (common.Hash, error) { - bz, err := lcptypes.ComputeEIP712RegisterEnclaveKeyWithSalt(pr.computeEIP712ChainSalt(), report) - if err != nil { - return common.Hash{}, err - } - return crypto.Keccak256Hash(bz), nil -} - func (pr *Prover) ComputeEIP712UpdateOperatorsHash(nonce uint64, newOperators []common.Address, thresholdNumerator, thresholdDenominator uint64) (common.Hash, error) { params := pr.getDomainParams() bz, err := lcptypes.ComputeEIP712UpdateOperators(int64(params.ChainId), params.VerifyingContractAddr, pr.computeEIP712ChainSalt(), pr.path.ClientID, nonce, newOperators, thresholdNumerator, thresholdDenominator)