> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/zkp2p/zkp2p-contracts/llms.txt
> Use this file to discover all available pages before exploring further.

# Protocol Utilities

> Use protocol utility functions for hashing and calculations

The `@zkp2p/contracts-v2/utils/protocolUtils` module provides utility functions for protocol operations including hashing, currency handling, and intent calculations.

## Import Utilities

```typescript theme={null}
import { 
  getKeccak256Hash,
  calculateIntentHash,
  Currency,
  getCurrencyCodeFromHash,
  calculatePaymentMethodHash
} from '@zkp2p/contracts-v2/utils/protocolUtils';
```

## Currency Constants

The `Currency` object provides pre-computed Keccak256 hashes for all supported currencies:

```typescript theme={null}
import { Currency } from '@zkp2p/contracts-v2/utils/protocolUtils';

console.log(Currency.USD);
// "0xc4ae21aac0c6549d71dd96035b7e0bdb6c79ebdba8891b666115bc976d16a29e"

console.log(Currency.EUR);
// "0xfff16d60be267153303bbfa66e593fb8d06e24ea5ef24b6acca5224c2ca6b907"

console.log(Currency.GBP);
// "0x90832e2dc3221e4d56977c1aa8f6a6706b9ad6542fbbdaac13097d0fa5e42e67"
```

### Supported Currencies

All 33 supported currency codes:

<AccordionGroup>
  <Accordion title="Major Currencies" icon="dollar-sign">
    * `USD` - United States Dollar
    * `EUR` - Euro
    * `GBP` - British Pound Sterling
    * `JPY` - Japanese Yen
    * `CHF` - Swiss Franc
    * `CAD` - Canadian Dollar
    * `AUD` - Australian Dollar
    * `NZD` - New Zealand Dollar
  </Accordion>

  <Accordion title="Asian Currencies" icon="yen-sign">
    * `CNY` - Chinese Yuan
    * `HKD` - Hong Kong Dollar
    * `SGD` - Singapore Dollar
    * `INR` - Indian Rupee
    * `IDR` - Indonesian Rupiah
    * `MYR` - Malaysian Ringgit
    * `PHP` - Philippine Peso
    * `THB` - Thai Baht
    * `VND` - Vietnamese Dong
  </Accordion>

  <Accordion title="European Currencies" icon="euro-sign">
    * `CZK` - Czech Koruna
    * `DKK` - Danish Krone
    * `HUF` - Hungarian Forint
    * `NOK` - Norwegian Krone
    * `PLN` - Polish Zloty
    * `RON` - Romanian Leu
    * `SEK` - Swedish Krona
  </Accordion>

  <Accordion title="Other Currencies" icon="coins">
    * `AED` - UAE Dirham
    * `ARS` - Argentine Peso
    * `ILS` - Israeli Shekel
    * `KES` - Kenyan Shilling
    * `MXN` - Mexican Peso
    * `SAR` - Saudi Riyal
    * `TRY` - Turkish Lira
    * `UGX` - Ugandan Shilling
    * `ZAR` - South African Rand
  </Accordion>
</AccordionGroup>

## Hashing Functions

### getKeccak256Hash

Compute Keccak256 hash of a string value:

```typescript theme={null}
import { getKeccak256Hash } from '@zkp2p/contracts-v2/utils/protocolUtils';

const hash = getKeccak256Hash("venmo");
console.log(hash);
// "0x90262a3db0edd0be2369c6b28f9e8511ec0bac7136cefbada0880602f87e7268"

// Hash payment provider domain
const providerHash = getKeccak256Hash("venmo.com");
```

<ParamField path="value" type="string" required>
  The string value to hash
</ParamField>

<ResponseField name="return" type="string">
  Keccak256 hash as a hex string with `0x` prefix
</ResponseField>

### calculatePaymentMethodHash

Calculate payment method hash (alias for `getKeccak256Hash`):

```typescript theme={null}
import { calculatePaymentMethodHash } from '@zkp2p/contracts-v2/utils/protocolUtils';

const venmoHash = calculatePaymentMethodHash("venmo");
const revolutHash = calculatePaymentMethodHash("revolut");
const wiseHash = calculatePaymentMethodHash("wise");
```

## Intent Hash Calculation

### calculateIntentHash

Calculate the unique hash for an intent, constrained to the Circom field:

```typescript theme={null}
import { ethers } from 'ethers';
import { calculateIntentHash } from '@zkp2p/contracts-v2/utils/protocolUtils';

const orchestratorAddress = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb4";
const intentCounter = 12345;

const intentHash = calculateIntentHash(
  orchestratorAddress,
  intentCounter
);

console.log('Intent Hash:', intentHash);
// "0x..."
```

<ParamField path="orchestrator" type="string" required>
  Address of the Orchestrator contract
</ParamField>

<ParamField path="intentCounter" type="BigNumber | number" required>
  The intent counter value (can be ethers BigNumber or number)
</ParamField>

<ResponseField name="return" type="string">
  Intent hash constrained to Circom field, zero-padded to 32 bytes
</ResponseField>

<Info>
  The intent hash is computed as `keccak256(orchestrator, intentCounter)` modulo the Circom field prime:
  `21888242871839275222246405745257275088548364400416034343698204186575808495617`
</Info>

## Currency Helpers

### getCurrencyCodeFromHash

Reverse lookup to get currency code from its hash:

```typescript theme={null}
import { Currency, getCurrencyCodeFromHash } from '@zkp2p/contracts-v2/utils/protocolUtils';

const usdHash = Currency.USD;
const code = getCurrencyCodeFromHash(usdHash);
console.log(code); // "USD"

// Unknown hash returns empty string
const unknown = getCurrencyCodeFromHash("0xinvalidhash");
console.log(unknown); // ""
```

<ParamField path="hash" type="string" required>
  The currency hash to look up
</ParamField>

<ResponseField name="return" type="string">
  Currency code (e.g., "USD", "EUR") or empty string if not found
</ResponseField>

## Complete Example

### Building Intent Parameters

```typescript theme={null}
import { ethers } from 'ethers';
import { base } from '@zkp2p/contracts-v2/addresses';
import { Orchestrator } from '@zkp2p/contracts-v2/abis/base';
import { 
  Currency,
  calculateIntentHash,
  calculatePaymentMethodHash 
} from '@zkp2p/contracts-v2/utils/protocolUtils';

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();

const orchestrator = new ethers.Contract(
  base.Orchestrator,
  Orchestrator,
  signer
);

// Get current intent counter for this address
const userAddress = await signer.getAddress();
const intentCounter = await orchestrator.intentCounter(userAddress);

// Calculate intent hash
const intentHash = calculateIntentHash(
  base.Orchestrator,
  intentCounter
);

console.log('Intent Hash:', intentHash);

// Prepare intent parameters
const depositId = 1;
const amount = ethers.utils.parseUnits('100', 6); // 100 USDC
const to = await signer.getAddress();
const paymentMethodHash = calculatePaymentMethodHash('venmo');
const currencyHash = Currency.USD;
const priceLimit = ethers.utils.parseUnits('1.01', 6); // 1.01 price limit
const paymentIdentifier = "user@venmo.com";
const encryptedPaymentDetails = "0x..."; // Encrypted details

// Signal intent
const tx = await orchestrator.signalIntent(
  depositId,
  amount,
  to,
  paymentMethodHash,
  currencyHash,
  priceLimit,
  paymentIdentifier,
  encryptedPaymentDetails
);

await tx.wait();
console.log('Intent signaled with hash:', intentHash);
```

### Currency Validation

```typescript theme={null}
import { Currency, getCurrencyCodeFromHash } from '@zkp2p/contracts-v2/utils/protocolUtils';
import { base as paymentMethods } from '@zkp2p/contracts-v2/paymentMethods';

function validateCurrencyForPaymentMethod(
  paymentMethod: string,
  currencyHash: string
): boolean {
  const config = paymentMethods.methods[paymentMethod];
  
  if (!config) {
    throw new Error(`Unknown payment method: ${paymentMethod}`);
  }
  
  const isSupported = config.currencies.includes(currencyHash);
  
  if (!isSupported) {
    const currencyCode = getCurrencyCodeFromHash(currencyHash);
    console.error(
      `${paymentMethod} does not support ${currencyCode || 'this currency'}`
    );
  }
  
  return isSupported;
}

// Validate Venmo supports USD
const valid = validateCurrencyForPaymentMethod('venmo', Currency.USD);
console.log('Valid:', valid); // true

// Validate Venmo supports EUR (will fail)
const invalid = validateCurrencyForPaymentMethod('venmo', Currency.EUR);
// Error: venmo does not support EUR
```

## Type Definitions

```typescript theme={null}
// Currency object type
const Currency: Readonly<{
  AED: string;
  ARS: string;
  AUD: string;
  // ... all 33 currencies
  ZAR: string;
}>;

// Function signatures
function getKeccak256Hash(value: string): string;
function calculatePaymentMethodHash(paymentMethod: string): string;
function calculateIntentHash(
  orchestrator: string,
  intentCounter: BigNumber | number
): string;
function getCurrencyCodeFromHash(hash: string): string;
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Payment Methods" icon="credit-card" href="/sdk/payment-methods">
    Work with payment method configurations
  </Card>

  <Card title="Constants" icon="hashtag" href="/sdk/constants">
    Access protocol constants
  </Card>
</CardGroup>
