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

Fix RegisterEnclaveKey's domain #26

Merged
merged 1 commit into from
Jun 20, 2024
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
28 changes: 12 additions & 16 deletions light-clients/lcp/types/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions light-clients/lcp/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand Down
10 changes: 1 addition & 9 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Loading