Private/New-ClobApiKey.ps1

function New-ClobApiKey {
  [CmdletBinding(SupportsShouldProcess)]
  param (
    [Parameter()]
    [ValidateSet('table', 'json')]
    [string]$Output = 'table'
  )

  process {
    if ($PSCmdlet.ShouldProcess("CLOB", "Create new API key")) {
      $client = [ClobClient]::new()
      $config = [PolymarketConfig]::Load()
      if (!$config.ContainsKey('private_key')) { throw "Private key not found in config. Please run 'polymarket wallet import' first." }

      $pk = [PolymarketWallet]::Decrypt($config['private_key'])
      $nonce = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()

      # Signing "Polymarket CLOB API Key" message for onboarding
      $message = "Polymarket CLOB API Key"
      # In a real EIP-712 setup, this would be more complex.
      # For now, we hash the message and sign it.
      $hash = [PolymarketCrypto]::Keccak256([System.Text.Encoding]::UTF8.GetBytes($message))
      $hashBytes = [HexString]::ToBytes($hash.Replace("0x", ""))

      $sig = [PolymarketWallet]::Sign($pk, $hashBytes)

      $result = $client.CreateApiKey($sig, $nonce.ToString())

      # If successful, save to config
      if ($result.apiKey) {
        $config['api_key'] = $result.apiKey
        $config['api_secret'] = $result.apiSecret
        $config['api_passphrase'] = $result.apiPassphrase
        [PolymarketConfig]::Save($config)
        Write-Host "API Key created and saved to config." -ForegroundColor Green
      }

      [PolymarketOutput]::Format($result, $Output)
    }
  }
}