@oobe-protocol-labs/synapse-sap-sdk 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 (315) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +882 -0
  3. package/dist/cjs/constants/index.js +43 -0
  4. package/dist/cjs/constants/index.js.map +1 -0
  5. package/dist/cjs/constants/limits.js +161 -0
  6. package/dist/cjs/constants/limits.js.map +1 -0
  7. package/dist/cjs/constants/programs.js +78 -0
  8. package/dist/cjs/constants/programs.js.map +1 -0
  9. package/dist/cjs/constants/seeds.js +57 -0
  10. package/dist/cjs/constants/seeds.js.map +1 -0
  11. package/dist/cjs/core/client.js +391 -0
  12. package/dist/cjs/core/client.js.map +1 -0
  13. package/dist/cjs/core/connection.js +319 -0
  14. package/dist/cjs/core/connection.js.map +1 -0
  15. package/dist/cjs/core/index.js +24 -0
  16. package/dist/cjs/core/index.js.map +1 -0
  17. package/dist/cjs/errors/index.js +334 -0
  18. package/dist/cjs/errors/index.js.map +1 -0
  19. package/dist/cjs/events/index.js +136 -0
  20. package/dist/cjs/events/index.js.map +1 -0
  21. package/dist/cjs/idl/index.js +63 -0
  22. package/dist/cjs/idl/index.js.map +1 -0
  23. package/dist/cjs/idl/synapse_agent_sap.json +9710 -0
  24. package/dist/cjs/index.js +147 -0
  25. package/dist/cjs/index.js.map +1 -0
  26. package/dist/cjs/modules/agent.js +272 -0
  27. package/dist/cjs/modules/agent.js.map +1 -0
  28. package/dist/cjs/modules/attestation.js +147 -0
  29. package/dist/cjs/modules/attestation.js.map +1 -0
  30. package/dist/cjs/modules/base.js +128 -0
  31. package/dist/cjs/modules/base.js.map +1 -0
  32. package/dist/cjs/modules/escrow.js +246 -0
  33. package/dist/cjs/modules/escrow.js.map +1 -0
  34. package/dist/cjs/modules/feedback.js +166 -0
  35. package/dist/cjs/modules/feedback.js.map +1 -0
  36. package/dist/cjs/modules/index.js +35 -0
  37. package/dist/cjs/modules/index.js.map +1 -0
  38. package/dist/cjs/modules/indexing.js +375 -0
  39. package/dist/cjs/modules/indexing.js.map +1 -0
  40. package/dist/cjs/modules/ledger.js +234 -0
  41. package/dist/cjs/modules/ledger.js.map +1 -0
  42. package/dist/cjs/modules/tools.js +319 -0
  43. package/dist/cjs/modules/tools.js.map +1 -0
  44. package/dist/cjs/modules/vault.js +410 -0
  45. package/dist/cjs/modules/vault.js.map +1 -0
  46. package/dist/cjs/pda/index.js +377 -0
  47. package/dist/cjs/pda/index.js.map +1 -0
  48. package/dist/cjs/plugin/index.js +934 -0
  49. package/dist/cjs/plugin/index.js.map +1 -0
  50. package/dist/cjs/plugin/protocols.js +282 -0
  51. package/dist/cjs/plugin/protocols.js.map +1 -0
  52. package/dist/cjs/plugin/schemas.js +831 -0
  53. package/dist/cjs/plugin/schemas.js.map +1 -0
  54. package/dist/cjs/postgres/adapter.js +715 -0
  55. package/dist/cjs/postgres/adapter.js.map +1 -0
  56. package/dist/cjs/postgres/index.js +50 -0
  57. package/dist/cjs/postgres/index.js.map +1 -0
  58. package/dist/cjs/postgres/serializers.js +381 -0
  59. package/dist/cjs/postgres/serializers.js.map +1 -0
  60. package/dist/cjs/postgres/sync.js +221 -0
  61. package/dist/cjs/postgres/sync.js.map +1 -0
  62. package/dist/cjs/postgres/types.js +44 -0
  63. package/dist/cjs/postgres/types.js.map +1 -0
  64. package/dist/cjs/registries/builder.js +414 -0
  65. package/dist/cjs/registries/builder.js.map +1 -0
  66. package/dist/cjs/registries/discovery.js +362 -0
  67. package/dist/cjs/registries/discovery.js.map +1 -0
  68. package/dist/cjs/registries/index.js +51 -0
  69. package/dist/cjs/registries/index.js.map +1 -0
  70. package/dist/cjs/registries/session.js +433 -0
  71. package/dist/cjs/registries/session.js.map +1 -0
  72. package/dist/cjs/registries/x402.js +577 -0
  73. package/dist/cjs/registries/x402.js.map +1 -0
  74. package/dist/cjs/types/accounts.js +13 -0
  75. package/dist/cjs/types/accounts.js.map +1 -0
  76. package/dist/cjs/types/common.js +13 -0
  77. package/dist/cjs/types/common.js.map +1 -0
  78. package/dist/cjs/types/enums.js +174 -0
  79. package/dist/cjs/types/enums.js.map +1 -0
  80. package/dist/cjs/types/index.js +36 -0
  81. package/dist/cjs/types/index.js.map +1 -0
  82. package/dist/cjs/types/instructions.js +92 -0
  83. package/dist/cjs/types/instructions.js.map +1 -0
  84. package/dist/cjs/utils/hash.js +58 -0
  85. package/dist/cjs/utils/hash.js.map +1 -0
  86. package/dist/cjs/utils/index.js +27 -0
  87. package/dist/cjs/utils/index.js.map +1 -0
  88. package/dist/cjs/utils/serialization.js +105 -0
  89. package/dist/cjs/utils/serialization.js.map +1 -0
  90. package/dist/cjs/utils/validation.js +36 -0
  91. package/dist/cjs/utils/validation.js.map +1 -0
  92. package/dist/esm/constants/index.js +29 -0
  93. package/dist/esm/constants/index.js.map +1 -0
  94. package/dist/esm/constants/limits.js +158 -0
  95. package/dist/esm/constants/limits.js.map +1 -0
  96. package/dist/esm/constants/programs.js +75 -0
  97. package/dist/esm/constants/programs.js.map +1 -0
  98. package/dist/esm/constants/seeds.js +54 -0
  99. package/dist/esm/constants/seeds.js.map +1 -0
  100. package/dist/esm/core/client.js +384 -0
  101. package/dist/esm/core/client.js.map +1 -0
  102. package/dist/esm/core/connection.js +315 -0
  103. package/dist/esm/core/connection.js.map +1 -0
  104. package/dist/esm/core/index.js +19 -0
  105. package/dist/esm/core/index.js.map +1 -0
  106. package/dist/esm/errors/index.js +325 -0
  107. package/dist/esm/errors/index.js.map +1 -0
  108. package/dist/esm/events/index.js +132 -0
  109. package/dist/esm/events/index.js.map +1 -0
  110. package/dist/esm/idl/index.js +57 -0
  111. package/dist/esm/idl/index.js.map +1 -0
  112. package/dist/esm/idl/synapse_agent_sap.json +9710 -0
  113. package/dist/esm/index.js +70 -0
  114. package/dist/esm/index.js.map +1 -0
  115. package/dist/esm/modules/agent.js +268 -0
  116. package/dist/esm/modules/agent.js.map +1 -0
  117. package/dist/esm/modules/attestation.js +143 -0
  118. package/dist/esm/modules/attestation.js.map +1 -0
  119. package/dist/esm/modules/base.js +124 -0
  120. package/dist/esm/modules/base.js.map +1 -0
  121. package/dist/esm/modules/escrow.js +242 -0
  122. package/dist/esm/modules/escrow.js.map +1 -0
  123. package/dist/esm/modules/feedback.js +162 -0
  124. package/dist/esm/modules/feedback.js.map +1 -0
  125. package/dist/esm/modules/index.js +23 -0
  126. package/dist/esm/modules/index.js.map +1 -0
  127. package/dist/esm/modules/indexing.js +371 -0
  128. package/dist/esm/modules/indexing.js.map +1 -0
  129. package/dist/esm/modules/ledger.js +230 -0
  130. package/dist/esm/modules/ledger.js.map +1 -0
  131. package/dist/esm/modules/tools.js +315 -0
  132. package/dist/esm/modules/tools.js.map +1 -0
  133. package/dist/esm/modules/vault.js +406 -0
  134. package/dist/esm/modules/vault.js.map +1 -0
  135. package/dist/esm/pda/index.js +357 -0
  136. package/dist/esm/pda/index.js.map +1 -0
  137. package/dist/esm/plugin/index.js +927 -0
  138. package/dist/esm/plugin/index.js.map +1 -0
  139. package/dist/esm/plugin/protocols.js +279 -0
  140. package/dist/esm/plugin/protocols.js.map +1 -0
  141. package/dist/esm/plugin/schemas.js +828 -0
  142. package/dist/esm/plugin/schemas.js.map +1 -0
  143. package/dist/esm/postgres/adapter.js +678 -0
  144. package/dist/esm/postgres/adapter.js.map +1 -0
  145. package/dist/esm/postgres/index.js +27 -0
  146. package/dist/esm/postgres/index.js.map +1 -0
  147. package/dist/esm/postgres/serializers.js +362 -0
  148. package/dist/esm/postgres/serializers.js.map +1 -0
  149. package/dist/esm/postgres/sync.js +217 -0
  150. package/dist/esm/postgres/sync.js.map +1 -0
  151. package/dist/esm/postgres/types.js +41 -0
  152. package/dist/esm/postgres/types.js.map +1 -0
  153. package/dist/esm/registries/builder.js +410 -0
  154. package/dist/esm/registries/builder.js.map +1 -0
  155. package/dist/esm/registries/discovery.js +358 -0
  156. package/dist/esm/registries/discovery.js.map +1 -0
  157. package/dist/esm/registries/index.js +44 -0
  158. package/dist/esm/registries/index.js.map +1 -0
  159. package/dist/esm/registries/session.js +429 -0
  160. package/dist/esm/registries/session.js.map +1 -0
  161. package/dist/esm/registries/x402.js +573 -0
  162. package/dist/esm/registries/x402.js.map +1 -0
  163. package/dist/esm/types/accounts.js +12 -0
  164. package/dist/esm/types/accounts.js.map +1 -0
  165. package/dist/esm/types/common.js +12 -0
  166. package/dist/esm/types/common.js.map +1 -0
  167. package/dist/esm/types/enums.js +171 -0
  168. package/dist/esm/types/enums.js.map +1 -0
  169. package/dist/esm/types/index.js +25 -0
  170. package/dist/esm/types/index.js.map +1 -0
  171. package/dist/esm/types/instructions.js +89 -0
  172. package/dist/esm/types/instructions.js.map +1 -0
  173. package/dist/esm/utils/hash.js +53 -0
  174. package/dist/esm/utils/hash.js.map +1 -0
  175. package/dist/esm/utils/index.js +19 -0
  176. package/dist/esm/utils/index.js.map +1 -0
  177. package/dist/esm/utils/serialization.js +98 -0
  178. package/dist/esm/utils/serialization.js.map +1 -0
  179. package/dist/esm/utils/validation.js +33 -0
  180. package/dist/esm/utils/validation.js.map +1 -0
  181. package/dist/types/constants/index.d.ts +27 -0
  182. package/dist/types/constants/index.d.ts.map +1 -0
  183. package/dist/types/constants/limits.d.ts +149 -0
  184. package/dist/types/constants/limits.d.ts.map +1 -0
  185. package/dist/types/constants/programs.d.ts +69 -0
  186. package/dist/types/constants/programs.d.ts.map +1 -0
  187. package/dist/types/constants/seeds.d.ts +61 -0
  188. package/dist/types/constants/seeds.d.ts.map +1 -0
  189. package/dist/types/core/client.d.ts +323 -0
  190. package/dist/types/core/client.d.ts.map +1 -0
  191. package/dist/types/core/connection.d.ts +279 -0
  192. package/dist/types/core/connection.d.ts.map +1 -0
  193. package/dist/types/core/index.d.ts +20 -0
  194. package/dist/types/core/index.d.ts.map +1 -0
  195. package/dist/types/errors/index.d.ts +276 -0
  196. package/dist/types/errors/index.d.ts.map +1 -0
  197. package/dist/types/events/index.d.ts +248 -0
  198. package/dist/types/events/index.d.ts.map +1 -0
  199. package/dist/types/idl/index.d.ts +70 -0
  200. package/dist/types/idl/index.d.ts.map +1 -0
  201. package/dist/types/index.d.ts +68 -0
  202. package/dist/types/index.d.ts.map +1 -0
  203. package/dist/types/modules/agent.d.ts +166 -0
  204. package/dist/types/modules/agent.d.ts.map +1 -0
  205. package/dist/types/modules/attestation.d.ts +96 -0
  206. package/dist/types/modules/attestation.d.ts.map +1 -0
  207. package/dist/types/modules/base.d.ts +126 -0
  208. package/dist/types/modules/base.d.ts.map +1 -0
  209. package/dist/types/modules/escrow.d.ts +151 -0
  210. package/dist/types/modules/escrow.d.ts.map +1 -0
  211. package/dist/types/modules/feedback.d.ts +105 -0
  212. package/dist/types/modules/feedback.d.ts.map +1 -0
  213. package/dist/types/modules/index.d.ts +24 -0
  214. package/dist/types/modules/index.d.ts.map +1 -0
  215. package/dist/types/modules/indexing.d.ts +200 -0
  216. package/dist/types/modules/indexing.d.ts.map +1 -0
  217. package/dist/types/modules/ledger.d.ts +150 -0
  218. package/dist/types/modules/ledger.d.ts.map +1 -0
  219. package/dist/types/modules/tools.d.ts +182 -0
  220. package/dist/types/modules/tools.d.ts.map +1 -0
  221. package/dist/types/modules/vault.d.ts +240 -0
  222. package/dist/types/modules/vault.d.ts.map +1 -0
  223. package/dist/types/pda/index.d.ts +296 -0
  224. package/dist/types/pda/index.d.ts.map +1 -0
  225. package/dist/types/plugin/index.d.ts +171 -0
  226. package/dist/types/plugin/index.d.ts.map +1 -0
  227. package/dist/types/plugin/protocols.d.ts +152 -0
  228. package/dist/types/plugin/protocols.d.ts.map +1 -0
  229. package/dist/types/plugin/schemas.d.ts +823 -0
  230. package/dist/types/plugin/schemas.d.ts.map +1 -0
  231. package/dist/types/postgres/adapter.d.ts +355 -0
  232. package/dist/types/postgres/adapter.d.ts.map +1 -0
  233. package/dist/types/postgres/index.d.ts +24 -0
  234. package/dist/types/postgres/index.d.ts.map +1 -0
  235. package/dist/types/postgres/serializers.d.ts +30 -0
  236. package/dist/types/postgres/serializers.d.ts.map +1 -0
  237. package/dist/types/postgres/sync.d.ts +132 -0
  238. package/dist/types/postgres/sync.d.ts.map +1 -0
  239. package/dist/types/postgres/types.d.ts +167 -0
  240. package/dist/types/postgres/types.d.ts.map +1 -0
  241. package/dist/types/registries/builder.d.ts +340 -0
  242. package/dist/types/registries/builder.d.ts.map +1 -0
  243. package/dist/types/registries/discovery.d.ts +333 -0
  244. package/dist/types/registries/discovery.d.ts.map +1 -0
  245. package/dist/types/registries/index.d.ts +48 -0
  246. package/dist/types/registries/index.d.ts.map +1 -0
  247. package/dist/types/registries/session.d.ts +323 -0
  248. package/dist/types/registries/session.d.ts.map +1 -0
  249. package/dist/types/registries/x402.d.ts +463 -0
  250. package/dist/types/registries/x402.d.ts.map +1 -0
  251. package/dist/types/types/accounts.d.ts +565 -0
  252. package/dist/types/types/accounts.d.ts.map +1 -0
  253. package/dist/types/types/common.d.ts +166 -0
  254. package/dist/types/types/common.d.ts.map +1 -0
  255. package/dist/types/types/enums.d.ts +238 -0
  256. package/dist/types/types/enums.d.ts.map +1 -0
  257. package/dist/types/types/index.d.ts +28 -0
  258. package/dist/types/types/index.d.ts.map +1 -0
  259. package/dist/types/types/instructions.d.ts +366 -0
  260. package/dist/types/types/instructions.d.ts.map +1 -0
  261. package/dist/types/utils/hash.d.ts +48 -0
  262. package/dist/types/utils/hash.d.ts.map +1 -0
  263. package/dist/types/utils/index.d.ts +19 -0
  264. package/dist/types/utils/index.d.ts.map +1 -0
  265. package/dist/types/utils/serialization.d.ts +69 -0
  266. package/dist/types/utils/serialization.d.ts.map +1 -0
  267. package/dist/types/utils/validation.d.ts +29 -0
  268. package/dist/types/utils/validation.d.ts.map +1 -0
  269. package/package.json +178 -0
  270. package/src/constants/index.ts +44 -0
  271. package/src/constants/limits.ts +165 -0
  272. package/src/constants/programs.ts +83 -0
  273. package/src/constants/seeds.ts +66 -0
  274. package/src/core/client.ts +416 -0
  275. package/src/core/connection.ts +409 -0
  276. package/src/core/index.ts +20 -0
  277. package/src/errors/index.ts +346 -0
  278. package/src/events/index.ts +335 -0
  279. package/src/idl/index.ts +76 -0
  280. package/src/idl/synapse_agent_sap.json +9710 -0
  281. package/src/index.ts +253 -0
  282. package/src/modules/agent.ts +319 -0
  283. package/src/modules/attestation.ts +168 -0
  284. package/src/modules/base.ts +158 -0
  285. package/src/modules/escrow.ts +308 -0
  286. package/src/modules/feedback.ts +186 -0
  287. package/src/modules/index.ts +24 -0
  288. package/src/modules/indexing.ts +444 -0
  289. package/src/modules/ledger.ts +262 -0
  290. package/src/modules/tools.ts +411 -0
  291. package/src/modules/vault.ts +533 -0
  292. package/src/pda/index.ts +512 -0
  293. package/src/plugin/index.ts +1202 -0
  294. package/src/plugin/protocols.ts +404 -0
  295. package/src/plugin/schemas.ts +909 -0
  296. package/src/postgres/adapter.ts +904 -0
  297. package/src/postgres/index.ts +59 -0
  298. package/src/postgres/schema.sql +683 -0
  299. package/src/postgres/serializers.ts +485 -0
  300. package/src/postgres/sync.ts +254 -0
  301. package/src/postgres/types.ts +245 -0
  302. package/src/registries/builder.ts +607 -0
  303. package/src/registries/discovery.ts +572 -0
  304. package/src/registries/index.ts +77 -0
  305. package/src/registries/session.ts +613 -0
  306. package/src/registries/x402.ts +906 -0
  307. package/src/types/accounts.ts +618 -0
  308. package/src/types/common.ts +187 -0
  309. package/src/types/enums.ts +214 -0
  310. package/src/types/index.ts +92 -0
  311. package/src/types/instructions.ts +413 -0
  312. package/src/utils/hash.ts +57 -0
  313. package/src/utils/index.ts +19 -0
  314. package/src/utils/serialization.ts +98 -0
  315. package/src/utils/validation.ts +36 -0
@@ -0,0 +1,262 @@
1
+ /**
2
+ * @module ledger
3
+ * @description Unified on-chain memory — init, write, seal, and close ledger.
4
+ *
5
+ * The recommended memory system: fixed-cost PDA with 4 KB ring buffer,
6
+ * automatic TX log persistence, and rolling merkle proof.
7
+ * Write cost is TX fee only (~0.000005 SOL). ZERO additional rent.
8
+ *
9
+ * @category Modules
10
+ * @since v0.1.0
11
+ * @packageDocumentation
12
+ */
13
+
14
+ import { SystemProgram, type PublicKey, type TransactionSignature } from "@solana/web3.js";
15
+ import { BaseModule } from "./base";
16
+ import { deriveLedger, deriveLedgerPage } from "../pda";
17
+ import type { MemoryLedgerData, LedgerPageData } from "../types";
18
+
19
+ /**
20
+ * @name LedgerModule
21
+ * @description Manage on-chain memory ledgers for sessions.
22
+ * Each session can have one MemoryLedger PDA with a 4KB ring buffer for recent entries.
23
+ * When the ring buffer fills, it can be sealed into a permanent LedgerPage.
24
+ * Ledgers are designed for high-frequency writes with minimal cost (~0.000005 SOL per write).
25
+ * Sealed pages are immutable and stored on-chain (~0.031 SOL per page).
26
+ */
27
+ /**
28
+ * @name LedgerModule
29
+ * @description Manages the unified on-chain memory ledger for the Solana Agent
30
+ * Protocol. Provides methods to initialise a ledger with a 4 KB ring buffer,
31
+ * write data (TX fee only), seal pages permanently, close ledgers, and
32
+ * decode ring buffer contents.
33
+ *
34
+ * @category Modules
35
+ * @since v0.1.0
36
+ * @extends BaseModule
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const sap = new SapClient(provider);
41
+ * // Init ledger, write data, seal
42
+ * await sap.ledger.init(sessionPda);
43
+ * await sap.ledger.write(sessionPda, data, contentHash);
44
+ * await sap.ledger.seal(sessionPda);
45
+ * ```
46
+ */
47
+ export class LedgerModule extends BaseModule {
48
+ // ── PDA helpers ──────────────────────────────────────
49
+
50
+ /**
51
+ * @name deriveLedger
52
+ * @description Derive the `MemoryLedger` PDA for a given session.
53
+ * @param sessionPda - The session ledger PDA.
54
+ * @returns A tuple of `[PublicKey, bump]` for the ledger PDA.
55
+ * @see {@link deriveLedger} from `pda/` module for the underlying derivation.
56
+ * @since v0.1.0
57
+ */
58
+ deriveLedger(sessionPda: PublicKey): readonly [PublicKey, number] {
59
+ return deriveLedger(sessionPda);
60
+ }
61
+
62
+ /**
63
+ * @name deriveLedgerPage
64
+ * @description Derive a `LedgerPage` PDA for a given ledger and page index.
65
+ * @param ledgerPda - The memory ledger PDA.
66
+ * @param pageIndex - The zero-based page index.
67
+ * @returns A tuple of `[PublicKey, bump]` for the page PDA.
68
+ * @see {@link deriveLedgerPage} from `pda/` module for the underlying derivation.
69
+ * @since v0.1.0
70
+ */
71
+ deriveLedgerPage(
72
+ ledgerPda: PublicKey,
73
+ pageIndex: number,
74
+ ): readonly [PublicKey, number] {
75
+ return deriveLedgerPage(ledgerPda, pageIndex);
76
+ }
77
+
78
+ // ── Instructions ─────────────────────────────────────
79
+
80
+ /**
81
+ * @name init
82
+ * @description Create a `MemoryLedger` with a 4 KB ring buffer (~0.032 SOL rent).
83
+ * @param sessionPda - The session ledger PDA to attach the ledger to.
84
+ * @returns {Promise<TransactionSignature>} The transaction signature.
85
+ * @since v0.1.0
86
+ */
87
+ async init(sessionPda: PublicKey): Promise<TransactionSignature> {
88
+ const [ledgerPda] = deriveLedger(sessionPda);
89
+
90
+ return this.methods
91
+ .initLedger()
92
+ .accounts({
93
+ wallet: this.walletPubkey,
94
+ session: sessionPda,
95
+ ledger: ledgerPda,
96
+ systemProgram: SystemProgram.programId,
97
+ })
98
+ .rpc();
99
+ }
100
+
101
+ /**
102
+ * @name write
103
+ * @description Write data to the ledger (ring buffer + TX log simultaneously).
104
+ * Cost: TX fee only (~0.000005 SOL). ZERO additional rent.
105
+ * @param sessionPda - The session ledger PDA.
106
+ * @param data - The data payload to write (Buffer or Uint8Array).
107
+ * @param contentHash - A 32-byte SHA-256 content hash for verification.
108
+ * @returns {Promise<TransactionSignature>} The transaction signature.
109
+ * @since v0.1.0
110
+ */
111
+ async write(
112
+ sessionPda: PublicKey,
113
+ data: Buffer | Uint8Array,
114
+ contentHash: number[],
115
+ ): Promise<TransactionSignature> {
116
+ const [ledgerPda] = deriveLedger(sessionPda);
117
+
118
+ return this.methods
119
+ .writeLedger(Buffer.from(data), contentHash)
120
+ .accounts({
121
+ wallet: this.walletPubkey,
122
+ session: sessionPda,
123
+ ledger: ledgerPda,
124
+ })
125
+ .rpc();
126
+ }
127
+
128
+ /**
129
+ * @name seal
130
+ * @description Seal the ring buffer into a permanent `LedgerPage`.
131
+ * Pages are WRITE-ONCE, NEVER-DELETE: ~0.031 SOL per page.
132
+ * Automatically fetches the current page index from the ledger.
133
+ * @param sessionPda - The session ledger PDA.
134
+ * @returns {Promise<TransactionSignature>} The transaction signature.
135
+ * @since v0.1.0
136
+ */
137
+ async seal(sessionPda: PublicKey): Promise<TransactionSignature> {
138
+ const [ledgerPda] = deriveLedger(sessionPda);
139
+ // Fetch ledger to get current page index
140
+ const ledger = await this.fetchLedger(sessionPda);
141
+ const [pagePda] = deriveLedgerPage(ledgerPda, ledger.numPages);
142
+
143
+ return this.methods
144
+ .sealLedger()
145
+ .accounts({
146
+ wallet: this.walletPubkey,
147
+ session: sessionPda,
148
+ ledger: ledgerPda,
149
+ page: pagePda,
150
+ systemProgram: SystemProgram.programId,
151
+ })
152
+ .rpc();
153
+ }
154
+
155
+ /**
156
+ * @name close
157
+ * @description Close a ledger PDA and reclaim ~0.032 SOL rent.
158
+ * @param sessionPda - The session ledger PDA.
159
+ * @returns {Promise<TransactionSignature>} The transaction signature.
160
+ * @since v0.1.0
161
+ */
162
+ async close(sessionPda: PublicKey): Promise<TransactionSignature> {
163
+ const [ledgerPda] = deriveLedger(sessionPda);
164
+
165
+ return this.methods
166
+ .closeLedger()
167
+ .accounts({
168
+ wallet: this.walletPubkey,
169
+ session: sessionPda,
170
+ ledger: ledgerPda,
171
+ })
172
+ .rpc();
173
+ }
174
+
175
+ // ── Fetchers ─────────────────────────────────────────
176
+
177
+ /**
178
+ * @name fetchLedger
179
+ * @description Fetch a deserialized `MemoryLedger` account.
180
+ * @param sessionPda - The session ledger PDA.
181
+ * @returns {Promise<MemoryLedgerData>} The memory ledger data.
182
+ * @throws Will throw if the ledger does not exist.
183
+ * @since v0.1.0
184
+ */
185
+ async fetchLedger(sessionPda: PublicKey): Promise<MemoryLedgerData> {
186
+ const [pda] = deriveLedger(sessionPda);
187
+ return this.fetchAccount<MemoryLedgerData>("memoryLedger", pda);
188
+ }
189
+
190
+ /**
191
+ * @name fetchLedgerNullable
192
+ * @description Fetch a deserialized `MemoryLedger` account, or `null`
193
+ * if it does not exist on-chain.
194
+ * @param sessionPda - The session ledger PDA.
195
+ * @returns {Promise<MemoryLedgerData | null>} The ledger data or `null`.
196
+ * @since v0.1.0
197
+ */
198
+ async fetchLedgerNullable(sessionPda: PublicKey): Promise<MemoryLedgerData | null> {
199
+ const [pda] = deriveLedger(sessionPda);
200
+ return this.fetchAccountNullable<MemoryLedgerData>("memoryLedger", pda);
201
+ }
202
+
203
+ /**
204
+ * @name fetchPage
205
+ * @description Fetch a deserialized sealed `LedgerPage` account.
206
+ * @param ledgerPda - The memory ledger PDA.
207
+ * @param pageIndex - The zero-based page index.
208
+ * @returns {Promise<LedgerPageData>} The ledger page data.
209
+ * @throws Will throw if the page does not exist.
210
+ * @since v0.1.0
211
+ */
212
+ async fetchPage(
213
+ ledgerPda: PublicKey,
214
+ pageIndex: number,
215
+ ): Promise<LedgerPageData> {
216
+ const [pda] = deriveLedgerPage(ledgerPda, pageIndex);
217
+ return this.fetchAccount<LedgerPageData>("ledgerPage", pda);
218
+ }
219
+
220
+ /**
221
+ * @name fetchPageNullable
222
+ * @description Fetch a deserialized sealed `LedgerPage` account, or `null`
223
+ * if it does not exist on-chain.
224
+ * @param ledgerPda - The memory ledger PDA.
225
+ * @param pageIndex - The zero-based page index.
226
+ * @returns {Promise<LedgerPageData | null>} The page data or `null`.
227
+ * @since v0.1.0
228
+ */
229
+ async fetchPageNullable(
230
+ ledgerPda: PublicKey,
231
+ pageIndex: number,
232
+ ): Promise<LedgerPageData | null> {
233
+ const [pda] = deriveLedgerPage(ledgerPda, pageIndex);
234
+ return this.fetchAccountNullable<LedgerPageData>("ledgerPage", pda);
235
+ }
236
+
237
+ /**
238
+ * @name decodeRingBuffer
239
+ * @description Decode the ring buffer into individual entries.
240
+ * Each entry is stored as `[u16 LE data_len][data bytes]`.
241
+ * An entry with `data_len === 0` acts as the empty sentinel.
242
+ * @param ring - Raw ring buffer data (byte array or Uint8Array).
243
+ * @returns {Uint8Array[]} Array of decoded data entries.
244
+ * @since v0.1.0
245
+ */
246
+ decodeRingBuffer(ring: number[] | Uint8Array): Uint8Array[] {
247
+ const buf = Buffer.from(ring);
248
+ const entries: Uint8Array[] = [];
249
+ let offset = 0;
250
+
251
+ while (offset + 2 <= buf.length) {
252
+ const len = buf.readUInt16LE(offset);
253
+ if (len === 0) break; // empty sentinel
254
+ offset += 2;
255
+ if (offset + len > buf.length) break;
256
+ entries.push(new Uint8Array(buf.subarray(offset, offset + len)));
257
+ offset += len;
258
+ }
259
+
260
+ return entries;
261
+ }
262
+ }
@@ -0,0 +1,411 @@
1
+ /**
2
+ * @module tools
3
+ * @description Tool schema registry and session checkpoints for the
4
+ * Solana Agent Protocol.
5
+ *
6
+ * Covers: publish, inscribe schema, update, deactivate/reactivate,
7
+ * close, report invocations, and session checkpoint management.
8
+ *
9
+ * @category Modules
10
+ * @since v0.1.0
11
+ * @packageDocumentation
12
+ */
13
+
14
+ import { SystemProgram, type PublicKey, type TransactionSignature } from "@solana/web3.js";
15
+ import { BaseModule } from "./base";
16
+ import {
17
+ deriveAgent,
18
+ deriveTool,
19
+ deriveCheckpoint,
20
+ deriveGlobalRegistry,
21
+ } from "../pda";
22
+ import type {
23
+ ToolDescriptorData,
24
+ SessionCheckpointData,
25
+ PublishToolArgs,
26
+ UpdateToolArgs,
27
+ InscribeToolSchemaArgs,
28
+ } from "../types";
29
+ import { sha256, hashToArray } from "../utils";
30
+
31
+ /**
32
+ * @name ToolsModule
33
+ * @description Manages tool descriptors and session checkpoints for the
34
+ * Solana Agent Protocol. Provides methods to publish, update, deactivate,
35
+ * reactivate, close, and fetch tool descriptors, as well as inscribe
36
+ * JSON schemas into TX logs and manage session checkpoints.
37
+ *
38
+ * @category Modules
39
+ * @since v0.1.0
40
+ * @extends BaseModule
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const sap = new SapClient(provider);
45
+ * // Publish a tool by name (auto-hashes)
46
+ * const sig = await sap.tools.publishByName(
47
+ * "getWeather", "mcp-v1", "Fetch weather",
48
+ * '{"type":"object"}', '{"type":"object"}',
49
+ * 0, 1, 2, 1, false,
50
+ * );
51
+ * ```
52
+ */
53
+ export class ToolsModule extends BaseModule {
54
+ // ── PDA helpers ──────────────────────────────────────
55
+
56
+ /**
57
+ * @name deriveTool
58
+ * @description Derive the `ToolDescriptor` PDA for a given agent and tool name.
59
+ * The tool name is SHA-256 hashed internally.
60
+ * @param agentPda - The agent account PDA.
61
+ * @param toolName - The human-readable tool name.
62
+ * @returns A tuple of `[PublicKey, bump]` for the tool PDA.
63
+ * @see {@link deriveTool} from `pda/` module for the underlying derivation.
64
+ * @since v0.1.0
65
+ */
66
+ deriveTool(
67
+ agentPda: PublicKey,
68
+ toolName: string,
69
+ ): readonly [PublicKey, number] {
70
+ return deriveTool(agentPda, sha256(toolName));
71
+ }
72
+
73
+ // ── Instructions ─────────────────────────────────────
74
+
75
+ /**
76
+ * @name publish
77
+ * @description Publish a new tool descriptor for an agent using pre-computed
78
+ * hashes. For auto-hashing, prefer {@link publishByName}.
79
+ * @param args - Tool publication parameters (name, hashes, HTTP method, category, params, etc.).
80
+ * @returns {Promise<TransactionSignature>} The transaction signature.
81
+ * @since v0.1.0
82
+ */
83
+ async publish(args: PublishToolArgs): Promise<TransactionSignature> {
84
+ const [agentPda] = deriveAgent(this.walletPubkey);
85
+ const [toolPda] = deriveTool(agentPda, new Uint8Array(args.toolNameHash));
86
+ const [globalPda] = deriveGlobalRegistry();
87
+
88
+ return this.methods
89
+ .publishTool(
90
+ args.toolName,
91
+ args.toolNameHash,
92
+ args.protocolHash,
93
+ args.descriptionHash,
94
+ args.inputSchemaHash,
95
+ args.outputSchemaHash,
96
+ args.httpMethod,
97
+ args.category,
98
+ args.paramsCount,
99
+ args.requiredParams,
100
+ args.isCompound,
101
+ )
102
+ .accounts({
103
+ wallet: this.walletPubkey,
104
+ agent: agentPda,
105
+ tool: toolPda,
106
+ globalRegistry: globalPda,
107
+ systemProgram: SystemProgram.programId,
108
+ })
109
+ .rpc();
110
+ }
111
+
112
+ /**
113
+ * @name publishByName
114
+ * @description Convenience method to publish a tool using string names.
115
+ * All string arguments are automatically SHA-256 hashed.
116
+ * @param toolName - Human-readable tool name.
117
+ * @param protocolId - Protocol identifier (e.g. `"mcp-v1"`).
118
+ * @param description - Tool description text.
119
+ * @param inputSchema - JSON schema string for input validation.
120
+ * @param outputSchema - JSON schema string for output validation.
121
+ * @param httpMethod - Numeric HTTP method enum value.
122
+ * @param category - Numeric tool category enum value.
123
+ * @param paramsCount - Total number of parameters.
124
+ * @param requiredParams - Number of required parameters.
125
+ * @param isCompound - Whether the tool is a compound (multi-step) tool.
126
+ * @returns {Promise<TransactionSignature>} The transaction signature.
127
+ * @since v0.1.0
128
+ */
129
+ async publishByName(
130
+ toolName: string,
131
+ protocolId: string,
132
+ description: string,
133
+ inputSchema: string,
134
+ outputSchema: string,
135
+ httpMethod: number,
136
+ category: number,
137
+ paramsCount: number,
138
+ requiredParams: number,
139
+ isCompound: boolean,
140
+ ): Promise<TransactionSignature> {
141
+ return this.publish({
142
+ toolName,
143
+ toolNameHash: hashToArray(sha256(toolName)),
144
+ protocolHash: hashToArray(sha256(protocolId)),
145
+ descriptionHash: hashToArray(sha256(description)),
146
+ inputSchemaHash: hashToArray(sha256(inputSchema)),
147
+ outputSchemaHash: hashToArray(sha256(outputSchema)),
148
+ httpMethod,
149
+ category,
150
+ paramsCount,
151
+ requiredParams,
152
+ isCompound,
153
+ });
154
+ }
155
+
156
+ /**
157
+ * @name inscribeSchema
158
+ * @description Inscribe a full JSON schema into the transaction log (zero rent).
159
+ * The schema is stored as TX log data, not as PDA account data.
160
+ * @param toolName - The human-readable tool name.
161
+ * @param args - Schema inscription parameters (type, data, hash, compression).
162
+ * @returns {Promise<TransactionSignature>} The transaction signature.
163
+ * @since v0.1.0
164
+ */
165
+ async inscribeSchema(
166
+ toolName: string,
167
+ args: InscribeToolSchemaArgs,
168
+ ): Promise<TransactionSignature> {
169
+ const [agentPda] = deriveAgent(this.walletPubkey);
170
+ const [toolPda] = this.deriveTool(agentPda, toolName);
171
+
172
+ return this.methods
173
+ .inscribeToolSchema(
174
+ args.schemaType,
175
+ args.schemaData,
176
+ args.schemaHash,
177
+ args.compression,
178
+ )
179
+ .accounts({
180
+ wallet: this.walletPubkey,
181
+ agent: agentPda,
182
+ tool: toolPda,
183
+ })
184
+ .rpc();
185
+ }
186
+
187
+ /**
188
+ * @name update
189
+ * @description Update a tool’s schema hashes and bump its version.
190
+ * All fields are optional — only non-null values are written.
191
+ * @param toolName - The human-readable tool name.
192
+ * @param args - Partial update parameters (hashes, method, category, params).
193
+ * @returns {Promise<TransactionSignature>} The transaction signature.
194
+ * @since v0.1.0
195
+ */
196
+ async update(
197
+ toolName: string,
198
+ args: UpdateToolArgs,
199
+ ): Promise<TransactionSignature> {
200
+ const [agentPda] = deriveAgent(this.walletPubkey);
201
+ const [toolPda] = this.deriveTool(agentPda, toolName);
202
+
203
+ return this.methods
204
+ .updateTool(
205
+ args.descriptionHash ?? null,
206
+ args.inputSchemaHash ?? null,
207
+ args.outputSchemaHash ?? null,
208
+ args.httpMethod ?? null,
209
+ args.category ?? null,
210
+ args.paramsCount ?? null,
211
+ args.requiredParams ?? null,
212
+ )
213
+ .accounts({
214
+ wallet: this.walletPubkey,
215
+ agent: agentPda,
216
+ tool: toolPda,
217
+ })
218
+ .rpc();
219
+ }
220
+
221
+ /**
222
+ * @name deactivate
223
+ * @description Deactivate a tool. The tool remains discoverable but is
224
+ * marked as unavailable.
225
+ * @param toolName - The human-readable tool name.
226
+ * @returns {Promise<TransactionSignature>} The transaction signature.
227
+ * @since v0.1.0
228
+ */
229
+ async deactivate(toolName: string): Promise<TransactionSignature> {
230
+ const [agentPda] = deriveAgent(this.walletPubkey);
231
+ const [toolPda] = this.deriveTool(agentPda, toolName);
232
+
233
+ return this.methods
234
+ .deactivateTool()
235
+ .accounts({
236
+ wallet: this.walletPubkey,
237
+ agent: agentPda,
238
+ tool: toolPda,
239
+ })
240
+ .rpc();
241
+ }
242
+
243
+ /**
244
+ * @name reactivate
245
+ * @description Reactivate a previously deactivated tool.
246
+ * @param toolName - The human-readable tool name.
247
+ * @returns {Promise<TransactionSignature>} The transaction signature.
248
+ * @since v0.1.0
249
+ */
250
+ async reactivate(toolName: string): Promise<TransactionSignature> {
251
+ const [agentPda] = deriveAgent(this.walletPubkey);
252
+ const [toolPda] = this.deriveTool(agentPda, toolName);
253
+
254
+ return this.methods
255
+ .reactivateTool()
256
+ .accounts({
257
+ wallet: this.walletPubkey,
258
+ agent: agentPda,
259
+ tool: toolPda,
260
+ })
261
+ .rpc();
262
+ }
263
+
264
+ /**
265
+ * @name close
266
+ * @description Close a tool PDA and reclaim rent to the owner wallet.
267
+ * @param toolName - The human-readable tool name.
268
+ * @returns {Promise<TransactionSignature>} The transaction signature.
269
+ * @since v0.1.0
270
+ */
271
+ async close(toolName: string): Promise<TransactionSignature> {
272
+ const [agentPda] = deriveAgent(this.walletPubkey);
273
+ const [toolPda] = this.deriveTool(agentPda, toolName);
274
+ const [globalPda] = deriveGlobalRegistry();
275
+
276
+ return this.methods
277
+ .closeTool()
278
+ .accounts({
279
+ wallet: this.walletPubkey,
280
+ agent: agentPda,
281
+ tool: toolPda,
282
+ globalRegistry: globalPda,
283
+ })
284
+ .rpc();
285
+ }
286
+
287
+ /**
288
+ * @name reportInvocations
289
+ * @description Report tool invocation count. Updates the on-chain counter
290
+ * for analytics and discovery ranking.
291
+ * @param toolName - The human-readable tool name.
292
+ * @param invocations - The number of invocations to report.
293
+ * @returns {Promise<TransactionSignature>} The transaction signature.
294
+ * @since v0.1.0
295
+ */
296
+ async reportInvocations(
297
+ toolName: string,
298
+ invocations: number | bigint,
299
+ ): Promise<TransactionSignature> {
300
+ const [agentPda] = deriveAgent(this.walletPubkey);
301
+ const [toolPda] = this.deriveTool(agentPda, toolName);
302
+
303
+ return this.methods
304
+ .reportToolInvocations(this.bn(invocations))
305
+ .accounts({
306
+ wallet: this.walletPubkey,
307
+ agent: agentPda,
308
+ tool: toolPda,
309
+ })
310
+ .rpc();
311
+ }
312
+
313
+ // ── Checkpoints ──────────────────────────────────────
314
+
315
+ /**
316
+ * @name createCheckpoint
317
+ * @description Create a checkpoint snapshot of the current session state.
318
+ * Checkpoints are indexed by session PDA and checkpoint index.
319
+ * @param sessionPda - The session ledger PDA.
320
+ * @param checkpointIndex - The zero-based checkpoint index.
321
+ * @returns {Promise<TransactionSignature>} The transaction signature.
322
+ * @since v0.1.0
323
+ */
324
+ async createCheckpoint(
325
+ sessionPda: PublicKey,
326
+ checkpointIndex: number,
327
+ ): Promise<TransactionSignature> {
328
+ const [checkpointPda] = deriveCheckpoint(sessionPda, checkpointIndex);
329
+
330
+ return this.methods
331
+ .createSessionCheckpoint(checkpointIndex)
332
+ .accounts({
333
+ wallet: this.walletPubkey,
334
+ session: sessionPda,
335
+ checkpoint: checkpointPda,
336
+ systemProgram: SystemProgram.programId,
337
+ })
338
+ .rpc();
339
+ }
340
+
341
+ /**
342
+ * @name closeCheckpoint
343
+ * @description Close a checkpoint PDA and reclaim rent.
344
+ * @param sessionPda - The session ledger PDA.
345
+ * @param checkpointIndex - The zero-based checkpoint index.
346
+ * @returns {Promise<TransactionSignature>} The transaction signature.
347
+ * @since v0.1.0
348
+ */
349
+ async closeCheckpoint(
350
+ sessionPda: PublicKey,
351
+ checkpointIndex: number,
352
+ ): Promise<TransactionSignature> {
353
+ const [checkpointPda] = deriveCheckpoint(sessionPda, checkpointIndex);
354
+
355
+ return this.methods
356
+ .closeCheckpoint(checkpointIndex)
357
+ .accounts({
358
+ wallet: this.walletPubkey,
359
+ session: sessionPda,
360
+ checkpoint: checkpointPda,
361
+ })
362
+ .rpc();
363
+ }
364
+
365
+ // ── Fetchers ─────────────────────────────────────────
366
+
367
+ /**
368
+ * @name fetch
369
+ * @description Fetch a deserialized `ToolDescriptor` account.
370
+ * @param agentPda - The agent account PDA.
371
+ * @param toolName - The human-readable tool name.
372
+ * @returns {Promise<ToolDescriptorData>} The tool descriptor data.
373
+ * @throws Will throw if the tool descriptor does not exist.
374
+ * @since v0.1.0
375
+ */
376
+ async fetch(agentPda: PublicKey, toolName: string): Promise<ToolDescriptorData> {
377
+ const [pda] = this.deriveTool(agentPda, toolName);
378
+ return this.fetchAccount<ToolDescriptorData>("toolDescriptor", pda);
379
+ }
380
+
381
+ /**
382
+ * @name fetchNullable
383
+ * @description Fetch a deserialized `ToolDescriptor` account, or `null`
384
+ * if it does not exist on-chain.
385
+ * @param agentPda - The agent account PDA.
386
+ * @param toolName - The human-readable tool name.
387
+ * @returns {Promise<ToolDescriptorData | null>} The tool data or `null`.
388
+ * @since v0.1.0
389
+ */
390
+ async fetchNullable(agentPda: PublicKey, toolName: string): Promise<ToolDescriptorData | null> {
391
+ const [pda] = this.deriveTool(agentPda, toolName);
392
+ return this.fetchAccountNullable<ToolDescriptorData>("toolDescriptor", pda);
393
+ }
394
+
395
+ /**
396
+ * @name fetchCheckpoint
397
+ * @description Fetch a deserialized `SessionCheckpoint` account by session PDA and index.
398
+ * @param sessionPda - The session ledger PDA.
399
+ * @param checkpointIndex - The zero-based checkpoint index.
400
+ * @returns {Promise<SessionCheckpointData>} The checkpoint data.
401
+ * @throws Will throw if the checkpoint does not exist.
402
+ * @since v0.1.0
403
+ */
404
+ async fetchCheckpoint(
405
+ sessionPda: PublicKey,
406
+ checkpointIndex: number,
407
+ ): Promise<SessionCheckpointData> {
408
+ const [pda] = deriveCheckpoint(sessionPda, checkpointIndex);
409
+ return this.fetchAccount<SessionCheckpointData>("sessionCheckpoint", pda);
410
+ }
411
+ }