# AddrResolver

### Overview

AddrResolver is an abstract contract responsible for managing address records associated with Payment ID tokens, supporting both direct updates and signature-based authorization.

### Key Concepts

| Concept                             | Description                                                                                                                     |
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| **Address Resolution**              | Stores address mappings for Payment ID tokens, allowing structured lookup and updates.                                          |
| **EIP-712 Signature-Based Updates** | Enables gasless transactions using off-chain signatures, ensuring security through nonce management and signature verification. |
| **Public Key Registry Integration** | Ensures ownership validation by linking public keys to addresses.                                                               |

### Data Structures

#### Immutable Components

| Name                | Type                 | Description                                               |
| ------------------- | -------------------- | --------------------------------------------------------- |
| `publicKeyRegistry` | `IPublicKeyRegistry` | Stores the reference to the public key registry contract. |

#### State Variables

| Name     | Type                                                                | Description                                                                        |
| -------- | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| `_addrs` | `mapping(uint256 => mapping(uint256 => mapping(uint256 => bytes)))` | Nested mapping storing addresses for different source and address types per token. |
| `nonces` | `mapping(address => uint256)`                                       | Tracks the nonce for each address to prevent replay attacks.                       |

#### Type Hashes

| Name                      | Type      | Description                                             |
| ------------------------- | --------- | ------------------------------------------------------- |
| `SET_ADDR_TYPEHASH`       | `bytes32` | Type hash for setting a single address via signature.   |
| `BATCH_SET_ADDR_TYPEHASH` | `bytes32` | Type hash for setting multiple addresses via signature. |

### Functions

#### Initialization

**Constructor**

```solidity
constructor(address _publicKeyRegistry)
```

| Input Parameter      | Type      | Description                                  |
| -------------------- | --------- | -------------------------------------------- |
| `_publicKeyRegistry` | `address` | Address of the public key registry contract. |

#### Address Management

**addr**

```solidity
function addr(uint256 tokenId, uint256 sourceType, uint256 addrType) external view returns (bytes memory)
```

| Input Parameter | Type           | Description                     |
| --------------- | -------------- | ------------------------------- |
| `tokenId`       | `uint256`      | ID of the token.                |
| `sourceType`    | `uint256`      | Type of source for the address. |
| `addrType`      | `uint256`      | Type of address to retrieve.    |
| **Returns**     | `bytes memory` | The stored address record.      |

**setAddr**

```solidity
function setAddr(uint256 tokenId, uint256 sourceType, uint256 addrType, bytes memory newAddr) external
```

| Input Parameter | Type           | Description                     |
| --------------- | -------------- | ------------------------------- |
| `tokenId`       | `uint256`      | ID of the token.                |
| `sourceType`    | `uint256`      | Type of source for the address. |
| `addrType`      | `uint256`      | Type of address to set.         |
| `newAddr`       | `bytes memory` | The new address value.          |

#### Signature-Based Address Management

**setAddrWithSig**

```solidity
function setAddrWithSig(uint256 tokenId, uint256 sourceType, uint256 addrType, bytes memory newAddr, address owner, bytes calldata signature) external
```

| Input Parameter | Type             | Description                       |
| --------------- | ---------------- | --------------------------------- |
| `tokenId`       | `uint256`        | ID of the token.                  |
| `sourceType`    | `uint256`        | Type of source for the address.   |
| `addrType`      | `uint256`        | Type of address to set.           |
| `newAddr`       | `bytes memory`   | The new address value.            |
| `owner`         | `address`        | The owner authorizing the change. |
| `signature`     | `bytes calldata` | The EIP-712 signature.            |

**batchSetAddrWithSig**

```solidity
function batchSetAddrWithSig(uint256 tokenId, uint256[] calldata sourceTypes, uint256[] calldata addrTypes, bytes[] calldata newAddrs, address owner, bytes calldata signature) external
```

| Input Parameter | Type             | Description                       |
| --------------- | ---------------- | --------------------------------- |
| `tokenId`       | `uint256`        | ID of the token.                  |
| `sourceTypes`   | `uint256[]`      | Array of source types.            |
| `addrTypes`     | `uint256[]`      | Array of address types.           |
| `newAddrs`      | `bytes[]`        | Array of new address values.      |
| `owner`         | `address`        | The owner authorizing the change. |
| `signature`     | `bytes calldata` | The EIP-712 signature.            |

#### Nonce Management

**getNonce**

```solidity
function getNonce(address owner) external view returns (uint256)
```

| Input Parameter | Type      | Description                       |
| --------------- | --------- | --------------------------------- |
| `owner`         | `address` | The address to get the nonce for. |
| **Returns**     | `uint256` | The current nonce value.          |

### Events

| Event                        | Parameters                                                                              | Description                                                    |
| ---------------------------- | --------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| `AddressChanged`             | `(uint256 tokenId, uint256 sourceType, uint256 addrType, bytes newAddr)`                | Emitted when an address record is updated.                     |
| `AddressChangedWithSig`      | `(uint256 tokenId, uint256 sourceType, uint256 addrType, bytes newAddr, uint256 nonce)` | Emitted when an address is updated using a signature.          |
| `BatchAddressChangedWithSig` | `(uint256 tokenId, uint256 nonce)`                                                      | Emitted when multiple addresses are updated using a signature. |

### Errors

| Error                               | Description                                                           |
| ----------------------------------- | --------------------------------------------------------------------- |
| `AddrResolver__InvalidSignature`    | Thrown when a signature is invalid.                                   |
| `AddrResolver__InvalidPublicKey`    | Thrown when the recovered public key is not associated with an owner. |
| `AddrResolver__ArrayLengthMismatch` | Thrown when input arrays have different lengths.                      |
