cred402/lib/x402/algorand_networks.ts
caelum0x 334432dd02 fix: use the full Algorand genesis hash as the network id
The paid Mainnet endpoint returned 502 x402_facilitator_unavailable and
could never have sold anything:

  Facilitator does not support scheme "exact" on network
  "algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k"

@x402/avm exports ALGORAND_*_CAIP2 truncated to 32 chars, because CAIP-2
caps a chain reference at 32. The GoPlausible facilitator's /supported and
every catalogued Bazaar resource use the untruncated 44-char genesis hash,
and the resource server matches facilitator capabilities by exact string,
so registration failed outright. Latest @x402/avm (2.26.0) truncates too,
so this is an interop mismatch rather than a stale dependency.

The truncated id was also wrong on the wire: it is what we advertised in
PAYMENT-REQUIRED and what the Bazaar would have recorded, so buyers keyed
on the full hash could not have matched our offer.

Safe in both directions -- the SDK's own normalizeAlgorandNetwork accepts
algorand:<full genesis hash> and maps it back internally, so the AVM
scheme still resolves the right genesis hash when signing and verifying.
Config still accepts the friendly name and either id spelling.

Verified against the live facilitator with a real Mainnet configuration:
the endpoint now returns 402 with network
algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=, USDC ASA 31566704,
extra.tag=x402-global-challenge, and a facilitator-injected feePayer.

Also adds the two secrets CRED402_ENV=mainnet requires. Without both,
loadConfig throws at import and the service crash-loops before binding a
port rather than degrading to a 503.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 16:14:32 +03:00

63 lines
2.5 KiB
TypeScript

import {
ALGORAND_MAINNET_CAIP2,
ALGORAND_MAINNET_GENESIS_HASH,
ALGORAND_TESTNET_CAIP2,
ALGORAND_TESTNET_GENESIS_HASH,
} from "@x402/avm";
/**
* Algorand network identifiers as the GoPlausible facilitator and the Bazaar use them:
* `algorand:` followed by the **full** 44-character genesis hash.
*
* `@x402/avm` exports `ALGORAND_*_CAIP2` constants truncated to 32 characters, because
* CAIP-2 caps a chain reference at 32. The facilitator's `GET /supported` and every
* catalogued Bazaar resource use the untruncated hash instead, and the resource server
* matches facilitator capabilities by exact string. Registering a route with the
* truncated form therefore fails capability matching outright:
*
* 502 x402_facilitator_unavailable — Facilitator does not support scheme "exact"
* on network "algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k"
*
* The truncated form would also be wrong on the wire: it is what we would advertise in
* `PAYMENT-REQUIRED` and what the Bazaar would record, so buyers keyed on the full hash
* could not match our offer.
*
* Using the full form is safe in both directions. The SDK's own
* `normalizeAlgorandNetwork` accepts `algorand:<full genesis hash>` and maps it back to
* the truncated CAIP-2 constant internally, so the AVM scheme still resolves the right
* genesis hash when it builds and verifies transactions.
*/
export const ALGORAND_MAINNET_NETWORK = `algorand:${ALGORAND_MAINNET_GENESIS_HASH}` as const;
export const ALGORAND_TESTNET_NETWORK = `algorand:${ALGORAND_TESTNET_GENESIS_HASH}` as const;
export type AlgorandNetworkId =
| typeof ALGORAND_MAINNET_NETWORK
| typeof ALGORAND_TESTNET_NETWORK;
/** Every spelling of a network we accept in configuration, per network. */
const MAINNET_ALIASES: readonly string[] = [
"mainnet",
ALGORAND_MAINNET_NETWORK,
ALGORAND_MAINNET_CAIP2,
];
const TESTNET_ALIASES: readonly string[] = [
"testnet",
ALGORAND_TESTNET_NETWORK,
ALGORAND_TESTNET_CAIP2,
];
/**
* Resolve a configured network string to a network name, accepting the friendly name,
* the full genesis-hash id, and the truncated CAIP-2 id. Returns undefined for anything
* else so callers can fail closed.
*/
export function algorandNetworkNameFor(raw: string): "mainnet" | "testnet" | undefined {
if (MAINNET_ALIASES.includes(raw)) return "mainnet";
if (TESTNET_ALIASES.includes(raw)) return "testnet";
return undefined;
}
export function algorandNetworkIdFor(name: "mainnet" | "testnet"): AlgorandNetworkId {
return name === "mainnet" ? ALGORAND_MAINNET_NETWORK : ALGORAND_TESTNET_NETWORK;
}