Skip to content

Commit

Permalink
removed second EIP-712 version option, but keep possibility for multi…
Browse files Browse the repository at this point in the history
…ple versions
  • Loading branch information
zkokelj committed Mar 13, 2024
1 parent 8350299 commit 682e698
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 51 deletions.
84 changes: 35 additions & 49 deletions go/common/viewingkey/viewing_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ import (
const SignedMsgPrefix = "vk"

const (
EIP712Domain = "EIP712Domain"
EIP712Type = "Authentication"
EIP712DomainName = "name"
EIP712DomainVersion = "version"
EIP712DomainChainID = "chainId"
EIP712EncryptionToken = "Encryption Token"
// EIP712EncryptionTokenV2 is used to support older versions of third party libraries
// that don't have the support for spaces in type names
EIP712EncryptionTokenV2 = "EncryptionToken"
EIP712Domain = "EIP712Domain"
EIP712Type = "Authentication"
EIP712DomainName = "name"
EIP712DomainVersion = "version"
EIP712DomainChainID = "chainId"
EIP712EncryptionToken = "Encryption Token"
EIP712DomainNameValue = "Ten"
EIP712DomainVersionValue = "1.0"
UserIDHexLength = 40
Expand All @@ -54,7 +51,6 @@ const (
// EIP712EncryptionTokens is a list of all possible options for Encryption token name
var EIP712EncryptionTokens = [...]string{
EIP712EncryptionToken,
EIP712EncryptionTokenV2,
}

// PersonalSignMessageSupportedVersions is a list of supported versions for the personal sign message
Expand Down Expand Up @@ -196,6 +192,7 @@ func getBytesFromTypedData(typedData apitypes.TypedData) ([]byte, error) {

// GenerateAuthenticationEIP712RawDataOptions generates all the options or raw data messages (bytes)
// for an EIP-712 message used to authenticate an address with user
// (currently only one option is supported, but function leaves room for future expansion of options)
func GenerateAuthenticationEIP712RawDataOptions(userID string, chainID int64) ([][]byte, error) {
if len(userID) != UserIDHexLength {
return nil, fmt.Errorf("userID hex length must be %d, received %d", UserIDHexLength, len(userID))
Expand All @@ -208,40 +205,35 @@ func GenerateAuthenticationEIP712RawDataOptions(userID string, chainID int64) ([
ChainId: (*math.HexOrDecimal256)(big.NewInt(chainID)),
}

typedDataList := make([]apitypes.TypedData, 0, len(EIP712EncryptionTokens))
for _, encTokenName := range EIP712EncryptionTokens {
message := map[string]interface{}{
encTokenName: encryptionToken,
}
message := map[string]interface{}{
EIP712EncryptionToken: encryptionToken,
}

types := apitypes.Types{
EIP712Domain: {
{Name: EIP712DomainName, Type: "string"},
{Name: EIP712DomainVersion, Type: "string"},
{Name: EIP712DomainChainID, Type: "uint256"},
},
EIP712Type: {
{Name: encTokenName, Type: "address"},
},
}
types := apitypes.Types{
EIP712Domain: {
{Name: EIP712DomainName, Type: "string"},
{Name: EIP712DomainVersion, Type: "string"},
{Name: EIP712DomainChainID, Type: "uint256"},
},
EIP712Type: {
{Name: EIP712EncryptionToken, Type: "address"},
},
}

newTypeElement := apitypes.TypedData{
Types: types,
PrimaryType: EIP712Type,
Domain: domain,
Message: message,
}
typedDataList = append(typedDataList, newTypeElement)
newTypeElement := apitypes.TypedData{
Types: types,
PrimaryType: EIP712Type,
Domain: domain,
Message: message,
}

rawDataOptions := make([][]byte, 0, len(typedDataList))
for _, typedDataItem := range typedDataList {
rawData, err := getBytesFromTypedData(typedDataItem)
if err != nil {
return nil, err
}
rawDataOptions = append(rawDataOptions, rawData)
rawDataOptions := make([][]byte, 0)
rawData, err := getBytesFromTypedData(newTypeElement)
if err != nil {
return nil, err
}
rawDataOptions = append(rawDataOptions, rawData)

return rawDataOptions, nil
}

Expand Down Expand Up @@ -339,18 +331,12 @@ func checkPersonalSignSignature(encryptionToken string, signature []byte, chainI
return nil, fmt.Errorf("signature verification failed")
}

// CheckSignatureWithType TODO @Ziga - Refactor and simplify this function
func CheckSignatureWithType(encryptionToken string, signature []byte, chainID int64, signatureType SignatureType) (*gethcommon.Address, error) {
// CheckSignature checks if signature is valid for provided encryptionToken and chainID and return address or nil if not valid
func CheckSignature(encryptionToken string, signature []byte, chainID int64, signatureType SignatureType) (*gethcommon.Address, error) {
if signatureType == PersonalSign {
addr, err := checkPersonalSignSignature(encryptionToken, signature, chainID)
if err == nil {
return addr, nil
}
return checkPersonalSignSignature(encryptionToken, signature, chainID)
} else if signatureType == EIP712Signature {
addr, err := checkEIP712Signature(encryptionToken, signature, chainID)
if err == nil {
return addr, nil
}
return checkEIP712Signature(encryptionToken, signature, chainID)
} else if signatureType == Legacy {
// todo - this part is only for the legacy format and will be removed once the legacy format is no longer supported
publicKey := []byte(encryptionToken)
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/vkhandler/vk_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func checkViewingKeyAndRecoverAddress(vk *AuthenticatedViewingKey, chainID int64
}

// check the signature and recover the address assuming the message was signed with EIP712
recoveredSignerAddress, err := viewingkey.CheckSignatureWithType(userID, vk.rpcVK.SignatureWithAccountKey, chainID, vk.rpcVK.SignatureType)
recoveredSignerAddress, err := viewingkey.CheckSignature(userID, vk.rpcVK.SignatureWithAccountKey, chainID, vk.rpcVK.SignatureType)
if err != nil {
return nil, fmt.Errorf("signature verification failed %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (w *WalletExtension) AddAddressToUser(hexUserID string, address string, sig
requestStartTime := time.Now()
addressFromMessage := gethcommon.HexToAddress(address)
// check if a message was signed by the correct address and if the signature is valid
_, err := viewingkey.CheckSignatureWithType(hexUserID, signature, int64(w.config.TenChainID), viewingkey.IntToSignatureType(signatureType))
_, err := viewingkey.CheckSignature(hexUserID, signature, int64(w.config.TenChainID), viewingkey.IntToSignatureType(signatureType))
if err != nil {
return fmt.Errorf("signature is not valid: %w", err)
}
Expand Down

0 comments on commit 682e698

Please sign in to comment.