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
93 changes: 93 additions & 0 deletions packages/core/src/tasks/terms-hash-parity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expect, it } from "vitest";

import { acceptedV2TermsHash } from "./v2-plan-guard.js";
import { TRUSTED_TASKS_V2_DEPLOYMENTS } from "./v2-trust.js";
import { declaredTermsFields, taskTermsFields } from "./v2.js";
import type { GetOrderResponse } from "./types.js";

const BASE = TRUSTED_TASKS_V2_DEPLOYMENTS[8453]!;

/**
* A pool posted on Base mainnet on 2026-12-02 (order 0eb0adc1…). vAPI froze its terms with the
* declared `share` field, and the pinned pool factory predicts vAPI's funding address
* 0x96d5Bd72E199feE7F9e198694bf08642C9aEe021 only for this terms hash.
*/
const mainnetPool = {
workOrder: {
id: "0eb0adc1-5cf8-4b66-aa8d-21e6f0a90488",
title: "Pilot test: submit a test username",
description:
"Pilot test pool (vAPI team). Claim with your vAPI ID and submit a test username as text. Reward 0.01 USDC per claim, one claim per wallet.",
clientAddress: "0x39733bd6670b1d0ea1ec081f8b2137663bc2d510",
terms: {
shape: "pool",
proofSpec: { items: [{ preset: "text" }], anyOf: false },
checkin: "none",
pool: {
rewardPerClaimBaseUnits: "10000",
budgetBaseUnits: "20000",
gate: "open",
verifier: "poster",
openCheck: "vapi-id",
},
liveness: { nextDueAt: null, stalledAt: null, cadence: "none", canCancelRemainderAt: null },
share: { public: false },
},
market: {
budget: { network: "eip155:8453", asset: "USDC", amountBaseUnits: "20000" },
deadlineAt: "2026-12-02T17:45:54.632Z",
},
},
} as unknown as GetOrderResponse;

function withTerms(view: GetOrderResponse, terms: Record<string, unknown>): GetOrderResponse {
return { ...view, workOrder: { ...view.workOrder, terms } } as unknown as GetOrderResponse;
}

const task = {
workOrder: {
...mainnetPool.workOrder,
terms: {
shape: "task",
checkin: "none",
milestones: [
{
index: 1,
amountBaseUnits: "1000000",
acceptance: "Deliver the page",
deadlineAt: "2026-12-09T12:00:00.000Z",
},
],
},
market: {
budget: { network: "eip155:8453", asset: "USDC", amountBaseUnits: "1000000" },
deadlineAt: "2026-12-09T12:00:00.000Z",
},
},
} as unknown as GetOrderResponse;

describe("terms hash parity with vAPI's declared order terms", () => {
it("hashes a mainnet pool exactly as vAPI froze it, including share", () => {
expect(acceptedV2TermsHash(mainnetPool, BASE, 0n)).toBe(
"0x37e5666efa834f366bef6bc7910c3a073a590fb938832f88cf76b5b4a9a54f2c",
);
});

it.each([
["share", { share: { public: true } }],
["skills", { skills: ["design"] }],
["location", { location: { lat: 52.37, lng: 4.9 } }],
])("binds the declared %s field into a task's terms hash", (_field, extra) => {
const terms = task.workOrder.terms as Record<string, unknown>;
expect(acceptedV2TermsHash(withTerms(task, { ...terms, ...extra }), BASE, 604800n)).not.toBe(
acceptedV2TermsHash(task, BASE, 604800n),
);
});

it("hashes vAPI's declared fields without offering them as post input", () => {
for (const field of ["share", "skills", "location"]) {
expect(Object.keys(declaredTermsFields)).toContain(field);
expect(Object.keys(taskTermsFields)).not.toContain(field);
}
});
});
8 changes: 4 additions & 4 deletions packages/core/src/tasks/v2-plan-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { canonicalJson } from "./canonical-json.js";
import { getTrustedTasksDurations } from "./trusted-deployments.js";
import { DEFAULT_ACCEPTANCE_WINDOW_SECONDS } from "./scope-terms.js";
import { freezeScopeTerms } from "./scope-terms.js";
import { taskTermsFields } from "./v2.js";
import { declaredTermsFields } from "./v2.js";
import { TaskOperationError } from "./operation-support.js";
import {
escrowFactoryV2Abi,
Expand Down Expand Up @@ -217,7 +217,7 @@ export function acceptedV2TermsHash(
reviewWindow: bigint,
) {
const order = view.workOrder;
const terms = z.object(taskTermsFields).parse(order.terms);
const terms = z.object(declaredTermsFields).parse(order.terms);
const market = z
.object({ budget: z.object({ network: z.string(), amountBaseUnits: z.string() }) })
.parse(order.market);
Expand Down Expand Up @@ -282,7 +282,7 @@ export function acceptedV2TermsHash(

/** The worker bond the accepted order declares (EscrowV2 accepts it only on a one-milestone task). */
function acceptedWorkerBondBp(view: GetOrderResponse) {
const terms = z.object(taskTermsFields).parse(view.workOrder.terms);
const terms = z.object(declaredTermsFields).parse(view.workOrder.terms);
return terms.bounty ? 0 : (terms.workerBondBp ?? 0);
}

Expand All @@ -300,7 +300,7 @@ function acceptedCreationArgs(
onchainVerifier?: Address,
): readonly unknown[] {
const order = view.workOrder;
const terms = z.object(taskTermsFields).parse(order.terms);
const terms = z.object(declaredTermsFields).parse(order.terms);
const poster = address.parse(order.clientAddress);
const total = acceptedV2Total(view);
const salt = keccak256(toHex(order.id));
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/tasks/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ export const taskTermsFields = {
bounty: bountyTermsSchema.optional(),
pool: poolTermsSchema.optional(),
};
/**
* Every declared term vAPI freezes into an order's on-chain terms hash (vapi-app
* shared/order-terms.ts orderTermsSchema). Hashing reads a hosted order with these fields; a
* missing one makes the client hash other terms than vAPI and refuse vAPI's escrow. `share`,
* `skills` and `location` are set on vAPI, not by this client's post input.
*/
export const declaredTermsFields = {
...taskTermsFields,
share: z.strictObject({ public: z.boolean() }).optional(),
skills: z.array(z.string()).max(10).optional(),
location: z
.strictObject({ lat: z.number().min(-90).max(90), lng: z.number().min(-180).max(180) })
.optional(),
};
export const taskTermsSchema = z.looseObject({
...taskTermsFields,
shape: taskShapeSchema,
Expand Down
Loading