Skip to content

Commit

Permalink
fix eip721 commitment calculation
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 17, 2024
1 parent 4dde693 commit 7f35df0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
22 changes: 13 additions & 9 deletions light-clients/lcp/types/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func ComputeChainSalt(chainID string, prefix []byte) common.Hash {
return crypto.Keccak256Hash(msg)
}

func LCPClientDomain(salt common.Hash) apitypes.TypedDataDomain {
func LCPClientDomain(chainId int64, verifyingContract common.Address, salt common.Hash) apitypes.TypedDataDomain {
return apitypes.TypedDataDomain{
Name: "LCPClient",
Version: "1",
ChainId: math.NewHexOrDecimal256(0),
VerifyingContract: "0x0000000000000000000000000000000000000000",
ChainId: math.NewHexOrDecimal256(chainId),
VerifyingContract: verifyingContract.Hex(),
Salt: salt.Hex(),
}
}
Expand All @@ -81,14 +81,16 @@ func GetRegisterEnclaveKeyTypedData(salt common.Hash, avr string) apitypes.Typed
return apitypes.TypedData{
PrimaryType: "RegisterEnclaveKey",
Types: RegisterEnclaveKeyTypes,
Domain: LCPClientDomain(salt),
Domain: LCPClientDomain(0, common.Address{}, salt),
Message: apitypes.TypedDataMessage{
"avr": avr,
},
}
}

func GetUpdateOperatorsTypedData(
chainId int64,
verifyingContract common.Address,
salt common.Hash,
clientID string,
nonce uint64,
Expand All @@ -103,7 +105,7 @@ func GetUpdateOperatorsTypedData(
return apitypes.TypedData{
PrimaryType: "UpdateOperators",
Types: UpdateOperatorsTypes,
Domain: LCPClientDomain(salt),
Domain: LCPClientDomain(chainId, verifyingContract, salt),
Message: apitypes.TypedDataMessage{
"clientId": clientID,
"nonce": fmt.Sprint(nonce),
Expand Down Expand Up @@ -134,7 +136,7 @@ func ComputeEIP712RegisterEnclaveKeyHash(chainID string, prefix []byte, report s
return crypto.Keccak256Hash(bz), nil
}

func ComputeEIP712UpdateOperators(
func ComputeEIP712UpdateOperatorsCosmos(
chainID string,
prefix []byte,
clientID string,
Expand All @@ -143,10 +145,12 @@ func ComputeEIP712UpdateOperators(
newOperatorThresholdNumerator uint64,
newOperatorThresholdDenominator uint64,
) ([]byte, error) {
return ComputeEIP712UpdateOperatorsWithSalt(ComputeChainSalt(chainID, prefix), clientID, nonce, newOperators, newOperatorThresholdNumerator, newOperatorThresholdDenominator)
return ComputeEIP712UpdateOperators(0, common.Address{}, ComputeChainSalt(chainID, prefix), clientID, nonce, newOperators, newOperatorThresholdNumerator, newOperatorThresholdDenominator)
}

func ComputeEIP712UpdateOperatorsWithSalt(
func ComputeEIP712UpdateOperators(
chainId int64,
verifyingContract common.Address,
salt common.Hash,
clientID string,
nonce uint64,
Expand All @@ -155,7 +159,7 @@ func ComputeEIP712UpdateOperatorsWithSalt(
newOperatorThresholdDenominator uint64,
) ([]byte, error) {
_, raw, err := apitypes.TypedDataAndHash(
GetUpdateOperatorsTypedData(salt, clientID, nonce, newOperators, newOperatorThresholdNumerator, newOperatorThresholdDenominator),
GetUpdateOperatorsTypedData(chainId, verifyingContract, salt, clientID, nonce, newOperators, newOperatorThresholdNumerator, newOperatorThresholdDenominator),
)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion light-clients/lcp/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (cs ClientState) verifyUpdateOperators(ctx sdk.Context, store storetypes.KV
return errorsmod.Wrapf(clienttypes.ErrInvalidHeader, "operator addresses must be ordered: clientID=%v op0=%v op1=%v", clientID, newOperators[i-1].String(), op.String())
}
}
signBytes, err := ComputeEIP712UpdateOperators(
signBytes, err := ComputeEIP712UpdateOperatorsCosmos(
ctx.ChainID(),
[]byte(exported.StoreKey),
clientID,
Expand Down
15 changes: 14 additions & 1 deletion relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,26 @@ func (pr *Prover) ComputeEIP712RegisterEnclaveKeyHash(report string) (common.Has
}

func (pr *Prover) ComputeEIP712UpdateOperatorsHash(nonce uint64, newOperators []common.Address, thresholdNumerator, thresholdDenominator uint64) (common.Hash, error) {
bz, err := lcptypes.ComputeEIP712UpdateOperatorsWithSalt(pr.computeEIP712ChainSalt(), pr.path.ClientID, nonce, newOperators, thresholdNumerator, thresholdDenominator)
chainId, verifyingContract := pr.getDomainParams()
bz, err := lcptypes.ComputeEIP712UpdateOperators(chainId, verifyingContract, pr.computeEIP712ChainSalt(), pr.path.ClientID, nonce, newOperators, thresholdNumerator, thresholdDenominator)
if err != nil {
return common.Hash{}, err
}
return crypto.Keccak256Hash(bz), nil
}

func (pr *Prover) getDomainParams() (int64, common.Address) {
switch pr.config.ChainType() {
case ChainTypeEVM:
salt := pr.config.GetEvmChainEip712Salt()
return int64(salt.ChainId), common.HexToAddress(salt.VerifyingContractAddress)
case ChainTypeCosmos:
return 0, common.Address{}
default:
panic(fmt.Sprintf("unsupported chain type: %v", pr.config.ChainType()))
}
}

func (pr *Prover) computeEIP712ChainSalt() common.Hash {
switch pr.config.ChainType() {
case ChainTypeEVM:
Expand Down

0 comments on commit 7f35df0

Please # to comment.