@sparkysolutions/agentrail 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +249 -146
  2. package/dist/cli/mcp-stdio.d.ts +8 -0
  3. package/dist/cli/mcp-stdio.js +454 -0
  4. package/dist/cli/pipeline.d.ts +40 -0
  5. package/dist/cli/pipeline.js +114 -0
  6. package/dist/cli/sandbox.d.ts +15 -1
  7. package/dist/cli/sandbox.js +30 -5
  8. package/dist/index.d.ts +13 -0
  9. package/dist/index.js +13 -0
  10. package/dist/lib/agent-badge-issuer.d.ts +36 -0
  11. package/dist/lib/agent-badge-issuer.js +166 -0
  12. package/dist/lib/agent-registry.d.ts +136 -0
  13. package/dist/lib/agent-registry.js +124 -0
  14. package/dist/lib/agenthustle-pipeline.d.ts +68 -0
  15. package/dist/lib/agenthustle-pipeline.js +143 -0
  16. package/dist/lib/agentpass-pipeline.d.ts +44 -0
  17. package/dist/lib/agentpass-pipeline.js +84 -0
  18. package/dist/lib/audit-trail-provider.d.ts +24 -0
  19. package/dist/lib/audit-trail-provider.js +60 -0
  20. package/dist/lib/axis-clob-provider.d.ts +50 -0
  21. package/dist/lib/axis-clob-provider.js +150 -0
  22. package/dist/lib/circuit-breaker.d.ts +60 -0
  23. package/dist/lib/circuit-breaker.js +136 -0
  24. package/dist/lib/conditional-escrow.d.ts +42 -0
  25. package/dist/lib/conditional-escrow.js +126 -0
  26. package/dist/lib/mcp-server.d.ts +29 -0
  27. package/dist/lib/mcp-server.js +165 -0
  28. package/dist/lib/receipts.d.ts +153 -0
  29. package/dist/lib/receipts.js +173 -0
  30. package/dist/lib/reflector-oracle-adapter.d.ts +44 -0
  31. package/dist/lib/reflector-oracle-adapter.js +173 -0
  32. package/dist/lib/scoped-allowance.d.ts +4 -0
  33. package/dist/lib/scoped-allowance.js +22 -1
  34. package/dist/lib/universal-adapters.d.ts +42 -0
  35. package/dist/lib/universal-adapters.js +69 -0
  36. package/dist/lib/x402-gateway.d.ts +86 -0
  37. package/dist/lib/x402-gateway.js +128 -0
  38. package/dist/lib/x402-pipeline.d.ts +51 -0
  39. package/dist/lib/x402-pipeline.js +105 -0
  40. package/dist/types.d.ts +109 -0
  41. package/glama.json +12 -0
  42. package/package.json +56 -40
  43. package/smithery.json +43 -0
  44. package/smithery.yaml +29 -0
  45. package/.github/ISSUE_TEMPLATE/drips_wave_task.md +0 -33
  46. package/.github/workflows/auto-merge.yml +0 -119
  47. package/.github/workflows/ci.yml +0 -35
  48. package/BENCHMARKS.md +0 -67
  49. package/CONTRIBUTING.md +0 -40
  50. package/DRIPS_WAVE.md +0 -81
  51. package/FUNDING.json +0 -7
  52. package/ISSUES_PIPELINE.json +0 -106
  53. package/RUNBOOK.md +0 -95
  54. package/SCF_ACTIVATION_PROPOSAL.md +0 -137
  55. package/assets/sorobanagents_avatar.jpg +0 -0
  56. package/assets/sorobanagents_avatar.png +0 -0
  57. package/assets/sorobanagents_avatar_elite.png +0 -0
  58. package/contracts/agentrail/Cargo.lock +0 -1895
  59. package/contracts/agentrail/Cargo.toml +0 -25
  60. package/contracts/agentrail/src/lib.rs +0 -296
  61. package/contracts/agentrail/src/test.rs +0 -442
  62. package/contracts/agentrail/test_snapshots/test/test_circuit_breaker_pause_and_unpause.1.json +0 -195
  63. package/contracts/agentrail/test_snapshots/test/test_double_initialize_fails.1.json +0 -123
  64. package/contracts/agentrail/test_snapshots/test/test_execute_spend_deducts_and_accumulates.1.json +0 -347
  65. package/contracts/agentrail/test_snapshots/test/test_get_allowance_returns_none_for_unregistered.1.json +0 -61
  66. package/contracts/agentrail/test_snapshots/test/test_get_version_matches_cargo_toml.1.json +0 -61
  67. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_negative_amount_fails.1.json +0 -123
  68. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_success.1.json +0 -250
  69. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_zero_amount_fails.1.json +0 -123
  70. package/contracts/agentrail/test_snapshots/test/test_initialize_and_protocol_fee.1.json +0 -154
  71. package/contracts/agentrail/test_snapshots/test/test_initialize_success.1.json +0 -124
  72. package/contracts/agentrail/test_snapshots/test/test_initialize_with_excessive_fee_fails.1.json +0 -61
  73. package/contracts/agentrail/test_snapshots/test/test_max_protocol_fee_spend_boundary.1.json +0 -298
  74. package/contracts/agentrail/test_snapshots/test/test_multi_agent_allowance_isolation.1.json +0 -426
  75. package/contracts/agentrail/test_snapshots/test/test_multi_owner_isolation.1.json +0 -426
  76. package/contracts/agentrail/test_snapshots/test/test_regrant_allowance_updates_limits.1.json +0 -346
  77. package/contracts/agentrail/test_snapshots/test/test_revoke_allowance_freezes_spending.1.json +0 -292
  78. package/contracts/agentrail/test_snapshots/test/test_set_protocol_fee_by_admin.1.json +0 -165
  79. package/contracts/agentrail/test_snapshots/test/test_spend_after_expiry_fails.1.json +0 -250
  80. package/contracts/agentrail/test_snapshots/test/test_spend_exact_allowance_boundary.1.json +0 -298
  81. package/contracts/agentrail/test_snapshots/test/test_spend_exceeding_allowance_fails.1.json +0 -250
  82. package/contracts/agentrail/test_snapshots/test/test_spend_fails_when_circuit_breaker_tripped.1.json +0 -285
  83. package/contracts/agentrail/test_snapshots/test/test_spend_negative_amount_fails.1.json +0 -250
  84. package/contracts/agentrail/test_snapshots/test/test_spend_non_existent_allowance_fails.1.json +0 -123
  85. package/contracts/agentrail/test_snapshots/test/test_spend_on_revoked_allowance_fails.1.json +0 -292
  86. package/contracts/agentrail/test_snapshots/test/test_spend_with_protocol_fee.1.json +0 -286
  87. package/contracts/agentrail/test_snapshots/test/test_spend_zero_amount_fails.1.json +0 -250
  88. package/contracts/agentrail/test_snapshots/test/test_unpause_restores_spend_operations.1.json +0 -370
  89. package/contracts/agentrail/test_snapshots/test/test_zero_protocol_fee_spend.1.json +0 -298
  90. package/deployment.testnet.json +0 -15
  91. package/docs/assets/favicon.png +0 -0
  92. package/docs/assets/logo.png +0 -0
  93. package/docs/index.html +0 -840
  94. package/scripts/demo-agent.ts +0 -52
  95. package/scripts/deploy-testnet.ts +0 -228
  96. package/scripts/security-audit.mjs +0 -159
  97. package/src/cli/sandbox.ts +0 -127
  98. package/src/index.ts +0 -11
  99. package/src/lib/micro-stream.ts +0 -52
  100. package/src/lib/proof-of-task.ts +0 -39
  101. package/src/lib/scoped-allowance.ts +0 -112
  102. package/src/lib/ttl-keeper.ts +0 -44
  103. package/src/sdk/agent-client.ts +0 -66
  104. package/src/types.ts +0 -78
  105. package/tests/agent-client.test.ts +0 -62
  106. package/tests/cli.test.ts +0 -68
  107. package/tests/micro-stream.test.ts +0 -38
  108. package/tests/proof-of-task.test.ts +0 -78
  109. package/tests/scoped-allowance.test.ts +0 -135
  110. package/tests/security-audit.test.ts +0 -76
  111. package/tests/ttl-keeper.test.ts +0 -36
  112. package/tsconfig.json +0 -16
  113. package/vitest.config.ts +0 -8
@@ -1,112 +0,0 @@
1
- import { ScopedAllowance, SpendRequest, SpendResult } from "../types.js";
2
-
3
- export const DEFAULT_PROTOCOL_FEE_BPS = 20; // 0.20%
4
- export const DEFAULT_TREASURY_ADDRESS = "GDDJH64X5VEXI7UWZMJA3YUG7ANYHKRRRFHLD2SF5OWEETNFBVYMRXZT";
5
-
6
- /**
7
- * Validate and process an agent spend attempt against a scoped allowance.
8
- */
9
- export function verifyAndDeductSpend(
10
- allowance: ScopedAllowance,
11
- request: SpendRequest
12
- ): SpendResult {
13
- if (!allowance.active) {
14
- return {
15
- ok: false,
16
- code: "ALLOWANCE_PAUSED",
17
- message: "The specified allowance is currently paused or inactive.",
18
- };
19
- }
20
-
21
- if (request.agentPublicKey !== allowance.agentPublicKey) {
22
- return {
23
- ok: false,
24
- code: "UNAUTHORIZED_AGENT",
25
- message: "Agent public key does not match allowance beneficiary.",
26
- };
27
- }
28
-
29
- if (request.timestamp < allowance.validFrom) {
30
- return {
31
- ok: false,
32
- code: "ALLOWANCE_NOT_YET_VALID",
33
- message: `Allowance window opens at timestamp ${allowance.validFrom}.`,
34
- };
35
- }
36
-
37
- if (request.timestamp > allowance.validUntil) {
38
- return {
39
- ok: false,
40
- code: "ALLOWANCE_EXPIRED",
41
- message: `Allowance window expired at timestamp ${allowance.validUntil}.`,
42
- };
43
- }
44
-
45
- if (request.amountStroops <= 0n) {
46
- return {
47
- ok: false,
48
- code: "INVALID_AMOUNT",
49
- message: "Spend amount must be strictly greater than zero.",
50
- };
51
- }
52
-
53
- if (
54
- allowance.allowedDestinations &&
55
- allowance.allowedDestinations.length > 0 &&
56
- !allowance.allowedDestinations.includes(request.destination)
57
- ) {
58
- return {
59
- ok: false,
60
- code: "UNAPPROVED_DESTINATION",
61
- message: `Destination ${request.destination} is not in the approved routing list.`,
62
- };
63
- }
64
-
65
- const remaining = allowance.maxStroops - allowance.spentStroops;
66
- if (request.amountStroops > remaining) {
67
- return {
68
- ok: false,
69
- code: "ALLOWANCE_EXCEEDED",
70
- message: `Requested ${request.amountStroops} stroops exceeds remaining allowance of ${remaining} stroops.`,
71
- remainingStroops: remaining,
72
- };
73
- }
74
-
75
- // Calculate protocol monetization fee
76
- const feeBps = allowance.protocolFeeBps !== undefined ? allowance.protocolFeeBps : DEFAULT_PROTOCOL_FEE_BPS;
77
- const treasury = allowance.treasuryPublicKey || DEFAULT_TREASURY_ADDRESS;
78
- const feeStroops = (request.amountStroops * BigInt(feeBps)) / 10000n;
79
- const netStroops = request.amountStroops - feeStroops;
80
-
81
- // Deduct
82
- allowance.spentStroops += request.amountStroops;
83
- const newRemaining = allowance.maxStroops - allowance.spentStroops;
84
-
85
- return {
86
- ok: true,
87
- code: "SPEND_AUTHORIZED",
88
- message: "Spend authorized successfully within scoped allowance.",
89
- remainingStroops: newRemaining,
90
- spentStroops: allowance.spentStroops,
91
- feeStroops,
92
- netStroops,
93
- treasuryPublicKey: treasury,
94
- };
95
- }
96
-
97
- /**
98
- * Reset spent balance for a recurring epoch if timestamp has rolled over.
99
- */
100
- export function renewEpochAllowance(
101
- allowance: ScopedAllowance,
102
- newValidFrom: number,
103
- newValidUntil: number
104
- ): ScopedAllowance {
105
- return {
106
- ...allowance,
107
- spentStroops: 0n,
108
- validFrom: newValidFrom,
109
- validUntil: newValidUntil,
110
- active: true,
111
- };
112
- }
@@ -1,44 +0,0 @@
1
- import { SorobanTtlStatus } from "../types.js";
2
-
3
- /** Default ledger threshold (approx 1 day at ~5 sec/ledger = 17,280 ledgers). */
4
- export const DEFAULT_TTL_THRESHOLD_LEDGERS = 17_280;
5
-
6
- /** Default ledger extension size (approx 30 days = 518,400 ledgers). */
7
- export const DEFAULT_EXTEND_TO_LEDGERS = 518_400;
8
-
9
- /**
10
- * Evaluate the Soroban contract state Time-To-Live (TTL) and determine
11
- * if an automated `extend_ttl` transaction is required to prevent state archival.
12
- */
13
- export function evaluateContractTtl(
14
- contractId: string,
15
- currentLedger: number,
16
- liveUntilLedger: number,
17
- thresholdLedgers: number = DEFAULT_TTL_THRESHOLD_LEDGERS,
18
- extendToLedgers: number = DEFAULT_EXTEND_TO_LEDGERS
19
- ): SorobanTtlStatus {
20
- if (liveUntilLedger < currentLedger) {
21
- return {
22
- contractId,
23
- currentLedger,
24
- liveUntilLedger,
25
- remainingLedgers: 0,
26
- thresholdLedgers,
27
- needsBump: true,
28
- recommendedExtendLedgers: extendToLedgers,
29
- };
30
- }
31
-
32
- const remaining = liveUntilLedger - currentLedger;
33
- const needsBump = remaining <= thresholdLedgers;
34
-
35
- return {
36
- contractId,
37
- currentLedger,
38
- liveUntilLedger,
39
- remainingLedgers: remaining,
40
- thresholdLedgers,
41
- needsBump,
42
- recommendedExtendLedgers: needsBump ? extendToLedgers - remaining : 0,
43
- };
44
- }
@@ -1,66 +0,0 @@
1
- import { ScopedAllowance, SpendRequest, SpendResult } from "../types.js";
2
- import { verifyAndDeductSpend } from "../lib/scoped-allowance.js";
3
- import { calculateTokenCost, TokenPricingRate, usdcToStroops, stroopsToUsdc } from "../lib/micro-stream.js";
4
- import { evaluateContractTtl } from "../lib/ttl-keeper.js";
5
- import { computeTaskCommitment, verifyTaskProof } from "../lib/proof-of-task.js";
6
-
7
- export class AgentRailClient {
8
- private allowances: Map<string, ScopedAllowance> = new Map();
9
-
10
- /**
11
- * Register a newly provisioned scoped allowance for an autonomous agent session.
12
- */
13
- public registerAllowance(allowance: ScopedAllowance): void {
14
- this.allowances.set(allowance.id, { ...allowance });
15
- }
16
-
17
- /**
18
- * Authorize a micropayment spend from an active agent allowance.
19
- */
20
- public spend(request: SpendRequest): SpendResult {
21
- const allowance = this.allowances.get(request.allowanceId);
22
- if (!allowance) {
23
- return {
24
- ok: false,
25
- code: "ALLOWANCE_NOT_FOUND",
26
- message: `Allowance with ID ${request.allowanceId} was not found.`,
27
- };
28
- }
29
-
30
- const result = verifyAndDeductSpend(allowance, request);
31
- return result;
32
- }
33
-
34
- /**
35
- * Helper to compute inference micropayment cost in stroops.
36
- */
37
- public computeTokenFee(
38
- promptTokens: number,
39
- completionTokens: number,
40
- rates: TokenPricingRate
41
- ): bigint {
42
- return calculateTokenCost(promptTokens, completionTokens, rates);
43
- }
44
-
45
- /**
46
- * Helper to inspect Soroban contract TTL health.
47
- */
48
- public checkContractTtl(
49
- contractId: string,
50
- currentLedger: number,
51
- liveUntilLedger: number
52
- ) {
53
- return evaluateContractTtl(contractId, currentLedger, liveUntilLedger);
54
- }
55
-
56
- /**
57
- * Helper for proof of task commitment computation.
58
- */
59
- public makeProofCommitment(outputPayload: string, nonce: string): string {
60
- return computeTaskCommitment(outputPayload, nonce);
61
- }
62
-
63
- public getAllowance(id: string): ScopedAllowance | undefined {
64
- return this.allowances.get(id);
65
- }
66
- }
package/src/types.ts DELETED
@@ -1,78 +0,0 @@
1
- /**
2
- * Core type definitions for AgentRail Protocol.
3
- */
4
-
5
- export interface ScopedAllowance {
6
- /** Unique allowance identifier. */
7
- id: string;
8
- /** Human or organization master account public key (G...). */
9
- ownerPublicKey: string;
10
- /** Autonomous AI Agent session public key (G...). */
11
- agentPublicKey: string;
12
- /** Maximum allowance amount in stroops (1 USDC = 10^7 stroops). */
13
- maxStroops: bigint;
14
- /** Currently spent stroops in the active epoch. */
15
- spentStroops: bigint;
16
- /** Unix timestamp in seconds when the allowance window opens. */
17
- validFrom: number;
18
- /** Unix timestamp in seconds when the allowance window expires. */
19
- validUntil: number;
20
- /** Array of approved destination public keys or contract IDs. */
21
- allowedDestinations?: string[];
22
- /** Whether the allowance is currently active or paused. */
23
- active: boolean;
24
- /** Protocol fee basis points (e.g. 20 bps = 0.20%). Defaults to 20 if omitted. */
25
- protocolFeeBps?: number;
26
- /** Protocol treasury wallet address that collects transaction fees. */
27
- treasuryPublicKey?: string;
28
- }
29
-
30
- export interface SpendRequest {
31
- allowanceId: string;
32
- agentPublicKey: string;
33
- destination: string;
34
- amountStroops: bigint;
35
- timestamp: number;
36
- memo?: string;
37
- }
38
-
39
- export interface SpendResult {
40
- ok: boolean;
41
- code: string;
42
- message: string;
43
- remainingStroops?: bigint;
44
- spentStroops?: bigint;
45
- /** Protocol monetization fee in stroops routed to treasury. */
46
- feeStroops?: bigint;
47
- /** Net amount in stroops routed to recipient. */
48
- netStroops?: bigint;
49
- /** Treasury address where fee was routed. */
50
- treasuryPublicKey?: string;
51
- }
52
-
53
- export interface EscrowTask {
54
- taskId: string;
55
- payerPublicKey: string;
56
- agentWorkerPublicKey: string;
57
- rewardStroops: bigint;
58
- taskCommitmentHash: string;
59
- expiresAt: number;
60
- status: "pending" | "fulfilled" | "refunded" | "disputed";
61
- }
62
-
63
- export interface ProofOfTaskSubmission {
64
- taskId: string;
65
- agentWorkerPublicKey: string;
66
- outputPayload: string;
67
- nonce: string;
68
- }
69
-
70
- export interface SorobanTtlStatus {
71
- contractId: string;
72
- currentLedger: number;
73
- liveUntilLedger: number;
74
- remainingLedgers: number;
75
- thresholdLedgers: number;
76
- needsBump: boolean;
77
- recommendedExtendLedgers: number;
78
- }
@@ -1,62 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { AgentRailClient } from "../src/sdk/agent-client.js";
3
- import { ScopedAllowance, SpendRequest } from "../src/types.js";
4
-
5
- describe("AgentRailClient Core SDK Wrapper", () => {
6
- const now = Math.floor(Date.now() / 1000);
7
- const client = new AgentRailClient();
8
-
9
- const mockAllowance: ScopedAllowance = {
10
- id: "session-allowance-abc",
11
- ownerPublicKey: "GOWNER111111111111111111111111111111111111111111111",
12
- agentPublicKey: "GAGENT222222222222222222222222222222222222222222222",
13
- maxStroops: 20_000_000n, // 2 USDC
14
- spentStroops: 0n,
15
- validFrom: now - 100,
16
- validUntil: now + 3600,
17
- active: true,
18
- };
19
-
20
- it("registers allowance and authorizes spend through client instance", () => {
21
- client.registerAllowance(mockAllowance);
22
- expect(client.getAllowance(mockAllowance.id)).toBeDefined();
23
-
24
- const spendReq: SpendRequest = {
25
- allowanceId: mockAllowance.id,
26
- agentPublicKey: mockAllowance.agentPublicKey,
27
- destination: "GDEST333333333333333333333333333333333333333333333",
28
- amountStroops: 5_000_000n,
29
- timestamp: now,
30
- };
31
-
32
- const result = client.spend(spendReq);
33
- expect(result.ok).toBe(true);
34
- expect(result.spentStroops).toBe(5_000_000n);
35
- expect(result.remainingStroops).toBe(15_000_000n);
36
- expect(result.feeStroops).toBe(10_000n);
37
- expect(result.netStroops).toBe(4_990_000n);
38
- expect(result.treasuryPublicKey).toBe("GDDJH64X5VEXI7UWZMJA3YUG7ANYHKRRRFHLD2SF5OWEETNFBVYMRXZT");
39
- });
40
-
41
- it("handles unknown allowance gracefully", () => {
42
- const spendReq: SpendRequest = {
43
- allowanceId: "non-existent-allowance",
44
- agentPublicKey: "GAGENT222222222222222222222222222222222222222222222",
45
- destination: "GDEST333333333333333333333333333333333333333333333",
46
- amountStroops: 1000n,
47
- timestamp: now,
48
- };
49
-
50
- const result = client.spend(spendReq);
51
- expect(result.ok).toBe(false);
52
- expect(result.code).toBe("ALLOWANCE_NOT_FOUND");
53
- });
54
-
55
- it("computes token fee through SDK helper", () => {
56
- const fee = client.computeTokenFee(1000, 500, {
57
- promptStroopsPer1k: 2000n,
58
- completionStroopsPer1k: 8000n,
59
- });
60
- expect(fee).toBe(6000n);
61
- });
62
- });
package/tests/cli.test.ts DELETED
@@ -1,68 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { provisionSandbox, inspectBalance, executeSandboxSpend } from "../src/cli/sandbox.js";
3
-
4
- describe("AgentRail CLI & Sandbox Provisioner", () => {
5
- it("should provision a sandbox session with active allowance and proper defaults", () => {
6
- const env = provisionSandbox("Test-Agent-X", "100.00", 25);
7
- expect(env.agent.name).toBe("Test-Agent-X");
8
- expect(env.ownerAddress).toBe("GDDJH64X5VEXI7UWZMJA3YUG7ANYHKRRRFHLD2SF5OWEETNFBVYMRXZT");
9
- expect(env.treasuryAddress).toBe("GDDJH64X5VEXI7UWZMJA3YUG7ANYHKRRRFHLD2SF5OWEETNFBVYMRXZT");
10
- expect(env.protocolFeeBps).toBe(25);
11
- expect(env.allowance.active).toBe(true);
12
- expect(env.allowance.maxStroops).toBe(1000000000n);
13
- expect(env.allowance.spentStroops).toBe(0n);
14
- });
15
-
16
- it("should inspect initial balance accurately", () => {
17
- const env = provisionSandbox("Test-Agent-Y", "50.00", 20);
18
- const balance = inspectBalance(env, env.allowance.id);
19
-
20
- expect(balance.allowanceId).toBe(env.allowance.id);
21
- expect(balance.totalLimitStroops).toBe("500000000");
22
- expect(balance.spentStroops).toBe("0");
23
- expect(balance.remainingStroops).toBe("500000000");
24
- expect(balance.remainingUsdc).toBe("50.0000000");
25
- expect(balance.active).toBe(true);
26
- expect(balance.isExpired).toBe(false);
27
- });
28
-
29
- it("should execute micro-spend, deduct balance, and route protocol fee accurately", () => {
30
- const env = provisionSandbox("Test-Agent-Z", "10.00", 20); // 20 bps = 0.20%
31
- const { result, feeStroops, netStroops } = executeSandboxSpend(env, "2.00", "test:inference");
32
-
33
- expect(result.ok).toBe(true);
34
- expect(result.code).toBe("SPEND_AUTHORIZED");
35
-
36
- // 2 USDC = 20,000,000 stroops
37
- // 20 bps of 20,000,000 = 40,000 stroops
38
- // Net = 19,960,000 stroops
39
- expect(feeStroops).toBe(40000n);
40
- expect(netStroops).toBe(19960000n);
41
- expect(result.feeStroops).toBe(40000n);
42
- expect(result.netStroops).toBe(19960000n);
43
-
44
- // Verify post-spend inspection
45
- const postBalance = inspectBalance(env, env.allowance.id);
46
- expect(postBalance.spentStroops).toBe("20000000");
47
- expect(postBalance.remainingStroops).toBe("80000000");
48
- expect(postBalance.remainingUsdc).toBe("8.0000000");
49
- });
50
-
51
- it("should reject spend when exceeding remaining allowance balance", () => {
52
- const env = provisionSandbox("Test-Agent-Limited", "1.00", 20);
53
- const { result } = executeSandboxSpend(env, "5.00");
54
-
55
- expect(result.ok).toBe(false);
56
- expect(result.code).toBe("ALLOWANCE_EXCEEDED");
57
- });
58
-
59
- it("should identify expired allowance during balance inspection", () => {
60
- const env = provisionSandbox("Test-Agent-Expiring", "10.00", 20);
61
- // Inspect with future timestamp past validUntil
62
- const futureTime = env.allowance.validUntil + 1000;
63
- const balance = inspectBalance(env, env.allowance.id, futureTime);
64
-
65
- expect(balance.isExpired).toBe(true);
66
- expect(balance.active).toBe(false);
67
- });
68
- });
@@ -1,38 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import {
3
- calculateTokenCost,
4
- usdcToStroops,
5
- stroopsToUsdc,
6
- TokenPricingRate,
7
- } from "../src/lib/micro-stream.js";
8
-
9
- const mockRates: TokenPricingRate = {
10
- promptStroopsPer1k: 1500n, // 0.00015 USDC per 1k prompt tokens
11
- completionStroopsPer1k: 6000n, // 0.00060 USDC per 1k completion tokens
12
- };
13
-
14
- describe("Micro-Stream Payment Calculator", () => {
15
- it("calculates exact stroop fees for prompt and completion tokens", () => {
16
- // 2,000 prompt tokens = 3,000 stroops
17
- // 1,000 completion tokens = 6,000 stroops
18
- // Total = 9,000 stroops ($0.0009 USDC)
19
- const cost = calculateTokenCost(2000, 1000, mockRates);
20
- expect(cost).toBe(9000n);
21
- });
22
-
23
- it("handles zero token counts gracefully", () => {
24
- expect(calculateTokenCost(0, 0, mockRates)).toBe(0n);
25
- });
26
-
27
- it("converts decimal USDC to integer stroops accurately", () => {
28
- expect(usdcToStroops("1.5000000")).toBe(15_000_000n);
29
- expect(usdcToStroops("0.0000001")).toBe(1n);
30
- expect(usdcToStroops("10")).toBe(100_000_000n);
31
- });
32
-
33
- it("converts integer stroops to 7-decimal USDC string", () => {
34
- expect(stroopsToUsdc(15_000_000n)).toBe("1.5000000");
35
- expect(stroopsToUsdc(1n)).toBe("0.0000001");
36
- expect(stroopsToUsdc(100_000_000n)).toBe("10.0000000");
37
- });
38
- });
@@ -1,78 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { computeTaskCommitment, verifyTaskProof } from "../src/lib/proof-of-task.js";
3
- import { EscrowTask, ProofOfTaskSubmission } from "../src/types.js";
4
-
5
- describe("Proof-of-Task Cryptographic Verification", () => {
6
- const now = Math.floor(Date.now() / 1000);
7
- const outputPayload = JSON.stringify({ summary: "Scraped 500 leads successfully", count: 500 });
8
- const nonce = "secure-random-nonce-987654";
9
- const commitmentHash = computeTaskCommitment(outputPayload, nonce);
10
-
11
- function createMockTask(overrides: Partial<EscrowTask> = {}): EscrowTask {
12
- return {
13
- taskId: "task-escrow-42",
14
- payerPublicKey: "GPAYER1111111111111111111111111111111111111111111111",
15
- agentWorkerPublicKey: "GWORKER222222222222222222222222222222222222222222222",
16
- rewardStroops: 50_000_000n, // 5 USDC
17
- taskCommitmentHash: commitmentHash,
18
- expiresAt: now + 3600,
19
- status: "pending",
20
- ...overrides,
21
- };
22
- }
23
-
24
- it("successfully verifies valid task proof matching commitment hash", () => {
25
- const task = createMockTask();
26
- const submission: ProofOfTaskSubmission = {
27
- taskId: task.taskId,
28
- agentWorkerPublicKey: task.agentWorkerPublicKey,
29
- outputPayload,
30
- nonce,
31
- };
32
-
33
- const verification = verifyTaskProof(task, submission, now);
34
- expect(verification.ok).toBe(true);
35
- });
36
-
37
- it("rejects proof with tampered output payload", () => {
38
- const task = createMockTask();
39
- const submission: ProofOfTaskSubmission = {
40
- taskId: task.taskId,
41
- agentWorkerPublicKey: task.agentWorkerPublicKey,
42
- outputPayload: "tampered payload data",
43
- nonce,
44
- };
45
-
46
- const verification = verifyTaskProof(task, submission, now);
47
- expect(verification.ok).toBe(false);
48
- expect(verification.reason).toContain("mismatch");
49
- });
50
-
51
- it("rejects proof submitted after expiration deadline", () => {
52
- const task = createMockTask({ expiresAt: now - 100 });
53
- const submission: ProofOfTaskSubmission = {
54
- taskId: task.taskId,
55
- agentWorkerPublicKey: task.agentWorkerPublicKey,
56
- outputPayload,
57
- nonce,
58
- };
59
-
60
- const verification = verifyTaskProof(task, submission, now);
61
- expect(verification.ok).toBe(false);
62
- expect(verification.reason).toContain("expired");
63
- });
64
-
65
- it("rejects proof submitted by unauthorized worker key", () => {
66
- const task = createMockTask();
67
- const submission: ProofOfTaskSubmission = {
68
- taskId: task.taskId,
69
- agentWorkerPublicKey: "GUNAUTHORIZED3333333333333333333333333333333333333",
70
- outputPayload,
71
- nonce,
72
- };
73
-
74
- const verification = verifyTaskProof(task, submission, now);
75
- expect(verification.ok).toBe(false);
76
- expect(verification.reason).toContain("designated worker");
77
- });
78
- });
@@ -1,135 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { verifyAndDeductSpend, renewEpochAllowance } from "../src/lib/scoped-allowance.js";
3
- import { ScopedAllowance, SpendRequest } from "../src/types.js";
4
-
5
- const now = Math.floor(Date.now() / 1000);
6
-
7
- function createMockAllowance(overrides: Partial<ScopedAllowance> = {}): ScopedAllowance {
8
- return {
9
- id: "allowance-001",
10
- ownerPublicKey: "GOWNER1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
11
- agentPublicKey: "GAGENT1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
12
- maxStroops: 10_000_000n, // 1 USDC
13
- spentStroops: 0n,
14
- validFrom: now - 3600,
15
- validUntil: now + 3600,
16
- allowedDestinations: ["GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"],
17
- active: true,
18
- ...overrides,
19
- };
20
- }
21
-
22
- describe("Scoped Allowance Module", () => {
23
- it("authorizes valid spend and deducts from allowance", () => {
24
- const allowance = createMockAllowance();
25
- const req: SpendRequest = {
26
- allowanceId: allowance.id,
27
- agentPublicKey: allowance.agentPublicKey,
28
- destination: "GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
29
- amountStroops: 2_000_000n,
30
- timestamp: now,
31
- };
32
-
33
- const res = verifyAndDeductSpend(allowance, req);
34
- expect(res.ok).toBe(true);
35
- expect(res.code).toBe("SPEND_AUTHORIZED");
36
- expect(res.spentStroops).toBe(2_000_000n);
37
- expect(res.remainingStroops).toBe(8_000_000n);
38
- // 20 bps = 0.20% of 2,000,000 = 4,000 stroops fee
39
- expect(res.feeStroops).toBe(4_000n);
40
- expect(res.netStroops).toBe(1_996_000n);
41
- expect(res.treasuryPublicKey).toBe("GDDJH64X5VEXI7UWZMJA3YUG7ANYHKRRRFHLD2SF5OWEETNFBVYMRXZT");
42
- });
43
-
44
- it("calculates custom protocol fee bps when specified", () => {
45
- const allowance = createMockAllowance({
46
- protocolFeeBps: 50, // 0.50%
47
- treasuryPublicKey: "GTREASURYCUSTOM11111111111111111111111111111111111111",
48
- });
49
- const req: SpendRequest = {
50
- allowanceId: allowance.id,
51
- agentPublicKey: allowance.agentPublicKey,
52
- destination: "GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
53
- amountStroops: 10_000_000n,
54
- timestamp: now,
55
- };
56
-
57
- const res = verifyAndDeductSpend(allowance, req);
58
- expect(res.ok).toBe(true);
59
- // 50 bps = 0.50% of 10,000,000 = 50,000 stroops
60
- expect(res.feeStroops).toBe(50_000n);
61
- expect(res.netStroops).toBe(9_950_000n);
62
- expect(res.treasuryPublicKey).toBe("GTREASURYCUSTOM11111111111111111111111111111111111111");
63
- });
64
-
65
- it("rejects spend when allowance is inactive/paused", () => {
66
- const allowance = createMockAllowance({ active: false });
67
- const req: SpendRequest = {
68
- allowanceId: allowance.id,
69
- agentPublicKey: allowance.agentPublicKey,
70
- destination: "GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
71
- amountStroops: 1000n,
72
- timestamp: now,
73
- };
74
-
75
- const res = verifyAndDeductSpend(allowance, req);
76
- expect(res.ok).toBe(false);
77
- expect(res.code).toBe("ALLOWANCE_PAUSED");
78
- });
79
-
80
- it("rejects unauthorized agent public key", () => {
81
- const allowance = createMockAllowance();
82
- const req: SpendRequest = {
83
- allowanceId: allowance.id,
84
- agentPublicKey: "GWRONGAGENT99999999999999999999999999999999999999999",
85
- destination: "GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
86
- amountStroops: 1000n,
87
- timestamp: now,
88
- };
89
-
90
- const res = verifyAndDeductSpend(allowance, req);
91
- expect(res.ok).toBe(false);
92
- expect(res.code).toBe("UNAUTHORIZED_AGENT");
93
- });
94
-
95
- it("rejects spend exceeding remaining allowance", () => {
96
- const allowance = createMockAllowance({ maxStroops: 5_000_000n });
97
- const req: SpendRequest = {
98
- allowanceId: allowance.id,
99
- agentPublicKey: allowance.agentPublicKey,
100
- destination: "GDEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
101
- amountStroops: 6_000_000n,
102
- timestamp: now,
103
- };
104
-
105
- const res = verifyAndDeductSpend(allowance, req);
106
- expect(res.ok).toBe(false);
107
- expect(res.code).toBe("ALLOWANCE_EXCEEDED");
108
- });
109
-
110
- it("rejects unapproved destination when destinations filter is set", () => {
111
- const allowance = createMockAllowance({
112
- allowedDestinations: ["GAPPROVED111111111111111111111111111111111111111111"],
113
- });
114
- const req: SpendRequest = {
115
- allowanceId: allowance.id,
116
- agentPublicKey: allowance.agentPublicKey,
117
- destination: "GUNAPPROVED2222222222222222222222222222222222222222",
118
- amountStroops: 1000n,
119
- timestamp: now,
120
- };
121
-
122
- const res = verifyAndDeductSpend(allowance, req);
123
- expect(res.ok).toBe(false);
124
- expect(res.code).toBe("UNAPPROVED_DESTINATION");
125
- });
126
-
127
- it("renews epoch allowance cleanly", () => {
128
- const allowance = createMockAllowance({ spentStroops: 5_000_000n });
129
- const renewed = renewEpochAllowance(allowance, now + 10, now + 86400);
130
-
131
- expect(renewed.spentStroops).toBe(0n);
132
- expect(renewed.validFrom).toBe(now + 10);
133
- expect(renewed.validUntil).toBe(now + 86400);
134
- });
135
- });