cred402/test/algorand_release.test.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

128 lines
4.2 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import type { PaymentRequired, PaymentRequirements } from "@x402/core/types";
import { USDC_MAINNET_ASA_ID } from "@x402/avm";
import { ALGORAND_MAINNET_NETWORK } from "../lib/x402/algorand_networks.js";
import type { AlgorandX402PublicStatus } from "../lib/x402/algorand_gateway.js";
import type { AlgorandChallengeInspection } from "../lib/x402/algorand_client.js";
import {
buildAlgorandReleaseEvidence,
verifyAlgorandReleaseEvidenceDigest,
} from "../lib/x402/algorand_release.js";
import {
assessAlgorandAccountReadiness,
type AlgorandAccountSnapshot,
} from "../lib/x402/algorand_readiness.js";
const PAYER = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ";
const RECEIVER = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
const ENDPOINT = "https://cred402.example/v1/x402/credit-score/EvidenceSellerAgent";
function snapshot(address: string): AlgorandAccountSnapshot {
return {
address,
balanceMicroAlgo: 500_000n,
minimumBalanceMicroAlgo: 200_000n,
validAsOfRound: 55_000_000n,
usdcOptedIn: true,
usdcFrozen: false,
usdcBalanceMicro: 50_000n,
};
}
function fixture() {
const selected: PaymentRequirements = {
scheme: "exact",
network: ALGORAND_MAINNET_NETWORK,
asset: USDC_MAINNET_ASA_ID,
amount: "10000",
payTo: RECEIVER,
maxTimeoutSeconds: 60,
extra: { name: "USDC", decimals: 6 },
};
const paymentRequired = {
x402Version: 2,
resource: { url: ENDPOINT, description: "Credit score", mimeType: "application/json" },
accepts: [selected],
} as PaymentRequired;
const challenge: AlgorandChallengeInspection = {
paymentRequired,
selected,
networkName: "mainnet",
amountMicroUsdc: "10000",
amountUsdc: "0.01",
resourceUrl: ENDPOINT,
};
const status: AlgorandX402PublicStatus = {
schema_version: "cred402.algorand-x402-status.v1",
configured: true,
protocol: "x402-v2",
paid_route: "/v1/x402/credit-score/:agentId",
network: ALGORAND_MAINNET_NETWORK,
network_name: "mainnet",
usdc_asset: USDC_MAINNET_ASA_ID,
price_micro_usdc: "10000",
pay_to: RECEIVER,
facilitator_url: "https://facilitator.example",
public_origin: "https://cred402.example",
discovery: { bazaar: true, challenge_tag: "x402-global-challenge" },
};
return { challenge, status };
}
test("release evidence is secret-free, serializable, and integrity checked", () => {
const { challenge, status } = fixture();
const evidence = buildAlgorandReleaseEvidence({
generatedAt: new Date("2026-08-23T12:00:00.000Z"),
endpoint: ENDPOINT,
status,
challenge,
payerReadiness: assessAlgorandAccountReadiness(snapshot(PAYER), "payer", 10_000n),
receiverReadiness: assessAlgorandAccountReadiness(snapshot(RECEIVER), "receiver", 10_000n),
releaseRef: "abc123",
});
assert.equal(evidence.result, "passed");
assert.equal(evidence.release_ref, "abc123");
assert.equal(evidence.accounts.payer.valid_as_of_round, "55000000");
assert.equal(verifyAlgorandReleaseEvidenceDigest(evidence), true);
assert.doesNotThrow(() => JSON.stringify(evidence));
assert.doesNotMatch(JSON.stringify(evidence), /private|mnemonic|secret/i);
assert.equal(
verifyAlgorandReleaseEvidenceDigest({ ...evidence, release_ref: "tampered" }),
false,
);
});
test("release evidence refuses unready accounts and Mainnet self-payment", () => {
const { challenge, status } = fixture();
const receiverReadiness = assessAlgorandAccountReadiness(snapshot(RECEIVER), "receiver", 10_000n);
assert.throws(
() => buildAlgorandReleaseEvidence({
generatedAt: new Date(),
endpoint: ENDPOINT,
status,
challenge,
payerReadiness: assessAlgorandAccountReadiness(
snapshot(PAYER),
"payer",
60_000n,
),
receiverReadiness,
}),
/payer account is not ready/,
);
assert.throws(
() => buildAlgorandReleaseEvidence({
generatedAt: new Date(),
endpoint: ENDPOINT,
status,
challenge,
payerReadiness: assessAlgorandAccountReadiness(snapshot(RECEIVER), "payer", 10_000n),
receiverReadiness,
}),
/external payer/,
);
});