Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions be/src/adsense/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function auditRevenueProof(proof: RevenueProof, expect: AuditExpectations
ok(
"parameters.responseMatches",
`${matches.length} regexes`,
"What the response had to contain: USD, this exact window, a total. Patterns only, no response data.",
"What the response had to contain: USD and a total; the window is fixed by the URL. Patterns only, no response data.",
);
} else fail("parameters.responseMatches differ from the provider's");

Expand All @@ -254,7 +254,7 @@ export function auditRevenueProof(proof: RevenueProof, expect: AuditExpectations
ok(
"parameters.responseRedactions",
`${redactions.length} regexes`,
"Which response spans were revealed to the attestor: the same four fragments the matches need. Patterns only.",
"Which response spans were revealed to the attestor: the same two fragments the matches need. Patterns only.",
);
} else fail("parameters.responseRedactions differ from the provider's");

Expand Down
20 changes: 18 additions & 2 deletions be/src/adsense/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* AdSense response, and the OAuth token can only end up in the secret headers.
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { test } from "node:test";

import {
Expand Down Expand Up @@ -120,9 +122,23 @@ test("a response in another currency does not match", () => {
assert.equal(matchesAll(adsenseResponse({ currency: "IDR" })).ok, false);
});

test("a response for a different window does not match", () => {
test("the window lives only in the signed URL; the matches are the same for every window (#6)", () => {
// Google computes the total for the URL's window, and the URL is signed, so the
// response's echo of the dates proves nothing more. Keeping the matches constant is
// what lets the contract's pinned template fill in only the URL's values (#61).
const other = { start: { year: 2026, month: 6, day: 1 }, end: { year: 2026, month: 8, day: 31 } };
assert.equal(matchesAll(adsenseResponse({ window: other })).ok, false);
const q3 = buildRevenueRequest({ publisherId: PUB, stellarAddress: ALICE, window: Q3 });
const q2 = buildRevenueRequest({ publisherId: PUB, stellarAddress: ALICE, window: other });
assert.deepEqual(q2.secretOptions, q3.secretOptions);
assert.notEqual(q2.url, q3.url);
assert.ok(!JSON.stringify(q3.secretOptions).includes("2026"));
});

test("the context message is the contract's constant, byte for byte", () => {
const claimRs = readFileSync(join(import.meta.dirname, "../../../sc/advance/src/claim.rs"), "utf8");
const m = /pub const CONTEXT_MESSAGE: &\[u8\] = b"([^"]*)";/.exec(claimRs);
assert.ok(m, "CONTEXT_MESSAGE not found in claim.rs");
assert.equal(CONTEXT_MESSAGE, m[1]);
});

test("a response without a numeric total does not match", () => {
Expand Down
31 changes: 13 additions & 18 deletions be/src/adsense/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*
* - `parameters.url`: host, account and window. Signed, so the contract can check
* the figure came from `adsense.googleapis.com` for one account over one window.
* Google computes the total for the window in this URL, so the URL alone fixes
* the window; the response's echo of the dates is not matched (#6).
* A claim whose URL is not exactly `buildRevenueUrl(...)` proves nothing: anyone
* can point zkFetch at their own server and have the attestor sign whatever it
* returns. That check belongs to the consumer of the claim (see #6, #9).
* - `parameters.responseMatches`: the response must say USD, cover exactly this
* window, and carry a total. The figure is captured from the total.
* - `parameters.responseMatches`: the response must say USD and carry a total. The
* figure is captured from the total. Nothing in them varies per proof, so the
* contract's pinned template (#61) only has to fill in the URL's values.
* - `context.contextAddress`: the borrower's Stellar address, inside the signed
* bytes, so a proof bound to A cannot be spent by B (#17, #18).
* - `context.extractedParameters.revenueUsd`: the figure, as a decimal string.
Expand All @@ -35,10 +38,12 @@ export const ADSENSE_API = "https://adsense.googleapis.com/v2";
export const ADSENSE_SCOPE = "https://www.googleapis.com/auth/adsense.readonly";

/**
* Fixed, versioned purpose string. Signed with the claim, so a consumer can refuse a
* proof that was produced for something else, even one carrying the same address.
* Fixed purpose string. Signed with the claim, so a consumer can refuse a proof that
* was produced for something else, even one carrying the same address. Byte for byte
* the contract's `claim::CONTEXT_MESSAGE` (sc/advance/src/claim.rs), which rebuilds
* the context with it (#6).
*/
export const CONTEXT_MESSAGE = "forepay:adsense-trailing-revenue:v1";
export const CONTEXT_MESSAGE = "Forepay advance request";

/** Complete calendar months the figure covers. Proposed for the #6 freeze. */
export const DEFAULT_TRAILING_MONTHS = 3;
Expand Down Expand Up @@ -146,11 +151,6 @@ export function buildRevenueUrl(publisherId: string, window: RevenueWindow): str
return `${ADSENSE_API}/accounts/${publisherId}/reports:generate?${q}`;
}

/** `"startDate": { "year": 2026, "month": 7, "day": 1 }`, any whitespace. */
function dateRegex(field: "startDate" | "endDate", d: CivilDate): string {
return `"${field}":\\s*\\{\\s*"year":\\s*${d.year},\\s*"month":\\s*${d.month},\\s*"day":\\s*${d.day}\\s*\\}`;
}

/** The header of the single metric column must be USD. */
export const CURRENCY_REGEX = `"currencyCode":\\s*"USD"`;

Expand All @@ -165,14 +165,9 @@ export function buildRevenueRequest(input: RevenueRequestInput): RevenueRequest
const url = buildRevenueUrl(input.publisherId, input.window);

// Each match asserts one property of the response. Each redaction reveals exactly
// the span its match needs, so the attestor sees these four fragments and nothing
// else: not the account name, not the rows, not the averages.
const reveal = [
CURRENCY_REGEX,
dateRegex("startDate", input.window.start),
dateRegex("endDate", input.window.end),
TOTAL_REGEX,
];
// the span its match needs, so the attestor sees these two fragments and nothing
// else: not the account name, not the rows, not the averages, not the dates.
const reveal = [CURRENCY_REGEX, TOTAL_REGEX];
return {
url,
options: {
Expand Down