Skip to content

Commit

Permalink
fix RegisterEnclaveKey's domain
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
  • Loading branch information
bluele committed Jun 20, 2024
1 parent f6fbd71 commit 8a7233c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
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

0 comments on commit 8a7233c

Please # to comment.