# Phrase Trade EIP712

### Overview

The **PhraseTradeEIP712** contract implements the **EIP-712 standard**, providing a structured and secure way to sign typed data on-chain. It is a key component of the **Phrase.Trade protocol**, enabling efficient off-chain signing for operations like minting NFTs, reducing gas costs, and enhancing security.

#### Purpose

This contract is responsible for creating the **EIP-712 domain separator** and struct hash for secure and typed data signing. It allows for off-chain message signing that can be verified on-chain, ensuring gas efficiency and security.

#### Key Features

* Implements **EIP-712 domain separator**.
* Provides the ability to hash custom structs for signing.
* Facilitates the secure signing of off-chain data, which can then be submitted on-chain for verification.

***

### Contract Details

#### 1. **State Variables**

* **`domainSeparator`** (`bytes32`): The **EIP-712 domain separator** that is used for message hashing and signature verification.
* **`name`** (`string`): The name of the contract, used in the domain separator.
* **`version`** (`string`): The version of the contract, also used in the domain separator.
* **`chainId`** (`uint256`): The chain ID for the blockchain network, included in the domain separator to ensure security across different networks.

***

#### 2. **Constructor**

```solidity
constructor(string memory _name, string memory _version)
```

**Parameters:**

* **`_name`** (`string`): The name of the EIP-712 domain.
* **`_version`** (`string`): The version of the EIP-712 domain.

**Functionality:**

The constructor initializes the contract by setting the `name`, `version`, and `chainId` of the EIP-712 domain. It also calculates the **`domainSeparator`** using the `keccak256` hash of the domain details (name, version, chainId, and contract address). This separator is used for all message signing within the contract.

***

#### 3. **getStructHash**

```solidity
function getStructHash(
    string memory tokenURI,
    uint256 tillTimestamp,
    address creator
) public pure returns (bytes32)
```

**Parameters:**

* **`tokenURI`** (`string`): The URI of the token to be minted.
* **`tillTimestamp`** (`uint256`): The timestamp until the trade is valid.
* **`creator`** (`address`): The address of the token creator.

**Functionality:**

This function returns the hash of a struct, which includes the token URI, a validity timestamp, and the creator’s address. It uses `keccak256` to create a unique identifier for the specific minting operation.

**Example:**

For a minting operation with a specific token URI, this function provides the struct hash, which can then be used to generate a message hash.

***

#### 4. **getMessageHash**

```solidity
function getMessageHash(bytes32 structHash) public view returns (bytes32)
```

**Parameters:**

* **`structHash`** (`bytes32`): The hash of the struct that is to be signed.

**Functionality:**

This function returns the **message hash**, which is a combination of the domain separator and the struct hash. The message hash is what is signed by the user and can later be verified on-chain.

The encoding format used for the message is:

```
keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash))
```

This ensures that the message being signed adheres to the EIP-712 standard and that the struct data is securely encoded with the domain information.

***

### Summary

The **PhraseTradeEIP712** contract serves as a core component for **off-chain message signing** and **on-chain verification** within the Phrase.Trade ecosystem. By implementing the EIP-712 standard, it ensures that data signing is efficient and secure, reducing gas costs for repetitive operations such as minting new phrase NFTs. The contract provides:

* **Domain separation** to ensure signatures are valid only within the Phrase.Trade ecosystem.
* The ability to **hash custom structs** for specific operations.
* A secure way to create **message hashes** that can be signed off-chain and verified on-chain.
