import { fromHex } from '@lucid-evolution/lucid';
import { ParsedFeedPayload } from '@pythnetwork/pyth-lazer-sdk';
import {
  encodePriceUpdate,
  encodePythMessage,
  type PythMessageParts,
} from '../../src/utils/pyth';
import {
  TEST_TRUSTED_SIGNER_PRIV_KEY,
  TEST_TRUSTED_SIGNER_PUB_KEY,
} from './endpoints';

/**
 * Creates a signed Pyth message from a list of price feeds.
 * Uses the test trusted signer keys for signing.
 */
export async function createPythMessage(
  feeds: ParsedFeedPayload[],
  timestampUs: bigint,
  channelId = 0,
): Promise<Uint8Array> {
  const noble = await import('@noble/ed25519');
  const payload = encodePriceUpdate({
    timestampUs: timestampUs.toString(),
    channelId,
    priceFeeds: feeds,
  });
  const parts: PythMessageParts = {
    signature: await noble.signAsync(
      payload,
      fromHex(TEST_TRUSTED_SIGNER_PRIV_KEY),
    ),
    publicKey: fromHex(TEST_TRUSTED_SIGNER_PUB_KEY),
    payload,
  };
  return encodePythMessage(parts);
}
