@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,512 @@
1
+ /**
2
+ * @module pda
3
+ * @description Deterministic PDA derivation for every SAP v2 account.
4
+ *
5
+ * Each function returns `[PublicKey, bump]` and is pure — no network
6
+ * calls. All results are memoizable by the caller.
7
+ *
8
+ * Seeds mirror the Rust `#[account(seeds = [...])]` definitions exactly.
9
+ *
10
+ * @category PDA
11
+ * @since v0.1.0
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { deriveAgent, deriveEscrow } from "@synapse-sap/sdk/pda";
16
+ *
17
+ * const [agentPda, bump] = deriveAgent(walletPublicKey);
18
+ * const [escrowPda] = deriveEscrow(agentPda, depositor);
19
+ * ```
20
+ */
21
+
22
+ import { PublicKey } from "@solana/web3.js";
23
+ import { SAP_PROGRAM_ID, SEEDS } from "../constants";
24
+
25
+ /**
26
+ * Tuple returned by all PDA derivation functions.
27
+ *
28
+ * @name PdaResult
29
+ * @description A readonly tuple of `[pda, bump]` where `pda` is the derived
30
+ * `PublicKey` and `bump` is the canonical bump seed (`u8`).
31
+ * @category PDA
32
+ * @since v0.1.0
33
+ */
34
+ type PdaResult = readonly [pda: PublicKey, bump: number];
35
+
36
+ // ── Helpers ──────────────────────────────────────────────────────
37
+
38
+ /**
39
+ * Thin wrapper around `PublicKey.findProgramAddressSync`.
40
+ *
41
+ * @name findPda
42
+ * @description Derives a PDA from the given seed buffers and program ID.
43
+ * @param seeds - Array of seed `Buffer`s / `Uint8Array`s.
44
+ * @param programId - The program to derive against (defaults to {@link SAP_PROGRAM_ID}).
45
+ * @returns {PdaResult} The derived `[pda, bump]` tuple.
46
+ * @category PDA
47
+ * @since v0.1.0
48
+ */
49
+ const findPda = (
50
+ seeds: Array<Buffer | Uint8Array>,
51
+ programId: PublicKey = SAP_PROGRAM_ID,
52
+ ): PdaResult => PublicKey.findProgramAddressSync(seeds, programId);
53
+
54
+ /**
55
+ * Encode a seed string as a UTF-8 `Buffer`.
56
+ *
57
+ * @name toSeedBuf
58
+ * @description Converts a string seed prefix to a `Buffer` suitable for PDA derivation.
59
+ * @param s - The seed string to encode.
60
+ * @returns {Buffer} UTF-8 encoded buffer.
61
+ * @category PDA
62
+ * @since v0.1.0
63
+ */
64
+ const toSeedBuf = (s: string): Buffer => Buffer.from(s);
65
+
66
+ /**
67
+ * Encode an unsigned 32-bit integer as a little-endian `Buffer`.
68
+ *
69
+ * @name u32le
70
+ * @description Produces a 4-byte LE buffer for numeric PDA seed segments (e.g., epoch index, page index).
71
+ * @param n - The number to encode.
72
+ * @returns {Buffer} 4-byte little-endian buffer.
73
+ * @category PDA
74
+ * @since v0.1.0
75
+ */
76
+ const u32le = (n: number): Buffer => {
77
+ const buf = Buffer.alloc(4);
78
+ buf.writeUInt32LE(n, 0);
79
+ return buf;
80
+ };
81
+
82
+ // ═════════════════════════════════════════════
83
+ // Core PDAs
84
+ // ═════════════════════════════════════════════
85
+
86
+ /**
87
+ * Derive the **GlobalRegistry** PDA.
88
+ *
89
+ * Seeds: `["sap_global"]`
90
+ *
91
+ * @name deriveGlobalRegistry
92
+ * @description Returns the singleton global registry address used by the SAP program.
93
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
94
+ * @returns {PdaResult} `[pda, bump]` tuple.
95
+ * @category PDA
96
+ * @since v0.1.0
97
+ * @see GlobalRegistry account type
98
+ */
99
+ export const deriveGlobalRegistry = (
100
+ programId = SAP_PROGRAM_ID,
101
+ ): PdaResult =>
102
+ findPda([toSeedBuf(SEEDS.GLOBAL)], programId);
103
+
104
+ /**
105
+ * Derive the **AgentAccount** PDA for a given wallet.
106
+ *
107
+ * Seeds: `["sap_agent", wallet]`
108
+ *
109
+ * @name deriveAgent
110
+ * @description Computes the unique agent PDA owned by `wallet`.
111
+ * @param wallet - The agent owner’s wallet `PublicKey`.
112
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
113
+ * @returns {PdaResult} `[pda, bump]` tuple.
114
+ * @category PDA
115
+ * @since v0.1.0
116
+ * @see AgentAccount
117
+ */
118
+ export const deriveAgent = (
119
+ wallet: PublicKey,
120
+ programId = SAP_PROGRAM_ID,
121
+ ): PdaResult =>
122
+ findPda([toSeedBuf(SEEDS.AGENT), wallet.toBuffer()], programId);
123
+
124
+ /**
125
+ * Derive the **AgentStats** PDA for a given agent.
126
+ *
127
+ * Seeds: `["sap_stats", agent_pda]`
128
+ *
129
+ * @name deriveAgentStats
130
+ * @description Computes the stats account PDA associated with an agent.
131
+ * @param agentPda - The agent’s on-chain PDA.
132
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
133
+ * @returns {PdaResult} `[pda, bump]` tuple.
134
+ * @category PDA
135
+ * @since v0.1.0
136
+ * @see AgentStats
137
+ */
138
+ export const deriveAgentStats = (
139
+ agentPda: PublicKey,
140
+ programId = SAP_PROGRAM_ID,
141
+ ): PdaResult =>
142
+ findPda([toSeedBuf(SEEDS.STATS), agentPda.toBuffer()], programId);
143
+
144
+ // ═════════════════════════════════════════════
145
+ // Feedback
146
+ // ═════════════════════════════════════════════
147
+
148
+ /**
149
+ * Derive the **Feedback** PDA for a reviewer on a specific agent.
150
+ *
151
+ * Seeds: `["sap_feedback", agent_pda, reviewer_wallet]`
152
+ *
153
+ * @name deriveFeedback
154
+ * @description Computes the feedback account PDA scoped to a reviewer–agent pair.
155
+ * @param agentPda - The target agent’s PDA.
156
+ * @param reviewer - The reviewer’s wallet `PublicKey`.
157
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
158
+ * @returns {PdaResult} `[pda, bump]` tuple.
159
+ * @category PDA
160
+ * @since v0.1.0
161
+ * @see FeedbackAccount
162
+ */
163
+ export const deriveFeedback = (
164
+ agentPda: PublicKey,
165
+ reviewer: PublicKey,
166
+ programId = SAP_PROGRAM_ID,
167
+ ): PdaResult =>
168
+ findPda(
169
+ [toSeedBuf(SEEDS.FEEDBACK), agentPda.toBuffer(), reviewer.toBuffer()],
170
+ programId,
171
+ );
172
+
173
+ // ═════════════════════════════════════════════
174
+ // Indexing
175
+ // ═════════════════════════════════════════════
176
+
177
+ /**
178
+ * Derive the **CapabilityIndex** PDA for a hashed capability string.
179
+ *
180
+ * Seeds: `["sap_cap_idx", capability_hash]`
181
+ *
182
+ * @name deriveCapabilityIndex
183
+ * @description Computes the capability index PDA used to look up agents by capability.
184
+ * @param capabilityHash - SHA-256 hash of the capability string (32 bytes).
185
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
186
+ * @returns {PdaResult} `[pda, bump]` tuple.
187
+ * @category PDA
188
+ * @since v0.1.0
189
+ * @see CapabilityIndex
190
+ */
191
+ export const deriveCapabilityIndex = (
192
+ capabilityHash: Uint8Array,
193
+ programId = SAP_PROGRAM_ID,
194
+ ): PdaResult =>
195
+ findPda(
196
+ [toSeedBuf(SEEDS.CAPABILITY_INDEX), Buffer.from(capabilityHash)],
197
+ programId,
198
+ );
199
+
200
+ /**
201
+ * Derive the **ProtocolIndex** PDA for a hashed protocol string.
202
+ *
203
+ * Seeds: `["sap_proto_idx", protocol_hash]`
204
+ *
205
+ * @name deriveProtocolIndex
206
+ * @description Computes the protocol index PDA used to look up agents by protocol.
207
+ * @param protocolHash - SHA-256 hash of the protocol string (32 bytes).
208
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
209
+ * @returns {PdaResult} `[pda, bump]` tuple.
210
+ * @category PDA
211
+ * @since v0.1.0
212
+ * @see ProtocolIndex
213
+ */
214
+ export const deriveProtocolIndex = (
215
+ protocolHash: Uint8Array,
216
+ programId = SAP_PROGRAM_ID,
217
+ ): PdaResult =>
218
+ findPda(
219
+ [toSeedBuf(SEEDS.PROTOCOL_INDEX), Buffer.from(protocolHash)],
220
+ programId,
221
+ );
222
+
223
+ /**
224
+ * Derive the **ToolCategoryIndex** PDA for a tool category discriminant.
225
+ *
226
+ * Seeds: `["sap_tool_cat", category_u8]`
227
+ *
228
+ * @name deriveToolCategoryIndex
229
+ * @description Computes the tool category index PDA used to look up tools by category.
230
+ * @param category - The `u8` category discriminant (see {@link TOOL_CATEGORY_VALUES}).
231
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
232
+ * @returns {PdaResult} `[pda, bump]` tuple.
233
+ * @category PDA
234
+ * @since v0.1.0
235
+ * @see ToolCategoryIndex
236
+ */
237
+ export const deriveToolCategoryIndex = (
238
+ category: number,
239
+ programId = SAP_PROGRAM_ID,
240
+ ): PdaResult =>
241
+ findPda(
242
+ [toSeedBuf(SEEDS.TOOL_CATEGORY), Buffer.from([category])],
243
+ programId,
244
+ );
245
+
246
+ // ═════════════════════════════════════════════
247
+ // Memory Vault
248
+ // ═════════════════════════════════════════════
249
+
250
+ /**
251
+ * Derive the **MemoryVault** PDA for a given agent.
252
+ *
253
+ * Seeds: `["sap_vault", agent_pda]`
254
+ *
255
+ * @name deriveVault
256
+ * @description Computes the memory vault PDA owned by the specified agent.
257
+ * @param agentPda - The agent’s on-chain PDA.
258
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
259
+ * @returns {PdaResult} `[pda, bump]` tuple.
260
+ * @category PDA
261
+ * @since v0.1.0
262
+ * @see MemoryVault
263
+ */
264
+ export const deriveVault = (
265
+ agentPda: PublicKey,
266
+ programId = SAP_PROGRAM_ID,
267
+ ): PdaResult =>
268
+ findPda([toSeedBuf(SEEDS.VAULT), agentPda.toBuffer()], programId);
269
+
270
+ /**
271
+ * Derive the **Session** PDA for a vault and session hash.
272
+ *
273
+ * Seeds: `["sap_session", vault_pda, session_hash]`
274
+ *
275
+ * @name deriveSession
276
+ * @description Computes the session PDA scoped to a vault and unique session identifier.
277
+ * @param vaultPda - The parent vault’s PDA.
278
+ * @param sessionHash - SHA-256 hash identifying the session (32 bytes).
279
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
280
+ * @returns {PdaResult} `[pda, bump]` tuple.
281
+ * @category PDA
282
+ * @since v0.1.0
283
+ * @see SessionAccount
284
+ */
285
+ export const deriveSession = (
286
+ vaultPda: PublicKey,
287
+ sessionHash: Uint8Array,
288
+ programId = SAP_PROGRAM_ID,
289
+ ): PdaResult =>
290
+ findPda(
291
+ [
292
+ toSeedBuf(SEEDS.SESSION),
293
+ vaultPda.toBuffer(),
294
+ Buffer.from(sessionHash),
295
+ ],
296
+ programId,
297
+ );
298
+
299
+ /**
300
+ * Derive the **EpochPage** PDA for a session at a given epoch index.
301
+ *
302
+ * Seeds: `["sap_epoch", session_pda, epoch_index_u32_le]`
303
+ *
304
+ * @name deriveEpochPage
305
+ * @description Computes the epoch page PDA for paginated memory inscriptions.
306
+ * @param sessionPda - The parent session’s PDA.
307
+ * @param epochIndex - Zero-based epoch page index.
308
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
309
+ * @returns {PdaResult} `[pda, bump]` tuple.
310
+ * @category PDA
311
+ * @since v0.1.0
312
+ * @see EpochPage
313
+ */
314
+ export const deriveEpochPage = (
315
+ sessionPda: PublicKey,
316
+ epochIndex: number,
317
+ programId = SAP_PROGRAM_ID,
318
+ ): PdaResult =>
319
+ findPda(
320
+ [toSeedBuf(SEEDS.EPOCH), sessionPda.toBuffer(), u32le(epochIndex)],
321
+ programId,
322
+ );
323
+
324
+ /**
325
+ * Derive the **VaultDelegate** PDA for a delegate on a vault.
326
+ *
327
+ * Seeds: `["sap_delegate", vault_pda, delegate_pubkey]`
328
+ *
329
+ * @name deriveVaultDelegate
330
+ * @description Computes the delegate authorization PDA granting a wallet write access to a vault.
331
+ * @param vaultPda - The parent vault’s PDA.
332
+ * @param delegate - The delegate’s wallet `PublicKey`.
333
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
334
+ * @returns {PdaResult} `[pda, bump]` tuple.
335
+ * @category PDA
336
+ * @since v0.1.0
337
+ * @see VaultDelegate
338
+ */
339
+ export const deriveVaultDelegate = (
340
+ vaultPda: PublicKey,
341
+ delegate: PublicKey,
342
+ programId = SAP_PROGRAM_ID,
343
+ ): PdaResult =>
344
+ findPda(
345
+ [toSeedBuf(SEEDS.DELEGATE), vaultPda.toBuffer(), delegate.toBuffer()],
346
+ programId,
347
+ );
348
+
349
+ /**
350
+ * Derive the **Checkpoint** PDA for a session at a given checkpoint index.
351
+ *
352
+ * Seeds: `["sap_checkpoint", session_pda, checkpoint_index_u32_le]`
353
+ *
354
+ * @name deriveCheckpoint
355
+ * @description Computes the checkpoint PDA storing a Merkle snapshot of session state.
356
+ * @param sessionPda - The parent session’s PDA.
357
+ * @param checkpointIndex - Zero-based checkpoint index.
358
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
359
+ * @returns {PdaResult} `[pda, bump]` tuple.
360
+ * @category PDA
361
+ * @since v0.1.0
362
+ * @see Checkpoint
363
+ */
364
+ export const deriveCheckpoint = (
365
+ sessionPda: PublicKey,
366
+ checkpointIndex: number,
367
+ programId = SAP_PROGRAM_ID,
368
+ ): PdaResult =>
369
+ findPda(
370
+ [
371
+ toSeedBuf(SEEDS.CHECKPOINT),
372
+ sessionPda.toBuffer(),
373
+ u32le(checkpointIndex),
374
+ ],
375
+ programId,
376
+ );
377
+
378
+ // ═════════════════════════════════════════════
379
+ // Tools
380
+ // ═════════════════════════════════════════════
381
+
382
+ /**
383
+ * Derive the **Tool** PDA for an agent and tool name hash.
384
+ *
385
+ * Seeds: `["sap_tool", agent_pda, tool_name_hash]`
386
+ *
387
+ * @name deriveTool
388
+ * @description Computes the tool registration PDA scoped to an agent and tool identifier.
389
+ * @param agentPda - The owning agent’s PDA.
390
+ * @param toolNameHash - SHA-256 hash of the tool name (32 bytes).
391
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
392
+ * @returns {PdaResult} `[pda, bump]` tuple.
393
+ * @category PDA
394
+ * @since v0.1.0
395
+ * @see ToolAccount
396
+ */
397
+ export const deriveTool = (
398
+ agentPda: PublicKey,
399
+ toolNameHash: Uint8Array,
400
+ programId = SAP_PROGRAM_ID,
401
+ ): PdaResult =>
402
+ findPda(
403
+ [toSeedBuf(SEEDS.TOOL), agentPda.toBuffer(), Buffer.from(toolNameHash)],
404
+ programId,
405
+ );
406
+
407
+ // ═════════════════════════════════════════════
408
+ // Escrow
409
+ // ═════════════════════════════════════════════
410
+
411
+ /**
412
+ * Derive the **Escrow** PDA for an agent–depositor pair.
413
+ *
414
+ * Seeds: `["sap_escrow", agent_pda, depositor_wallet]`
415
+ *
416
+ * @name deriveEscrow
417
+ * @description Computes the escrow PDA holding deposited funds for service payments.
418
+ * @param agentPda - The service-providing agent’s PDA.
419
+ * @param depositor - The depositor’s wallet `PublicKey`.
420
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
421
+ * @returns {PdaResult} `[pda, bump]` tuple.
422
+ * @category PDA
423
+ * @since v0.1.0
424
+ * @see EscrowAccount
425
+ */
426
+ export const deriveEscrow = (
427
+ agentPda: PublicKey,
428
+ depositor: PublicKey,
429
+ programId = SAP_PROGRAM_ID,
430
+ ): PdaResult =>
431
+ findPda(
432
+ [toSeedBuf(SEEDS.ESCROW), agentPda.toBuffer(), depositor.toBuffer()],
433
+ programId,
434
+ );
435
+
436
+ // ═════════════════════════════════════════════
437
+ // Attestation
438
+ // ═════════════════════════════════════════════
439
+
440
+ /**
441
+ * Derive the **Attestation** PDA for an attester on a specific agent.
442
+ *
443
+ * Seeds: `["sap_attest", agent_pda, attester_wallet]`
444
+ *
445
+ * @name deriveAttestation
446
+ * @description Computes the attestation PDA recording a third-party’s trust assertion.
447
+ * @param agentPda - The attested agent’s PDA.
448
+ * @param attester - The attester’s wallet `PublicKey`.
449
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
450
+ * @returns {PdaResult} `[pda, bump]` tuple.
451
+ * @category PDA
452
+ * @since v0.1.0
453
+ * @see AttestationAccount
454
+ */
455
+ export const deriveAttestation = (
456
+ agentPda: PublicKey,
457
+ attester: PublicKey,
458
+ programId = SAP_PROGRAM_ID,
459
+ ): PdaResult =>
460
+ findPda(
461
+ [toSeedBuf(SEEDS.ATTESTATION), agentPda.toBuffer(), attester.toBuffer()],
462
+ programId,
463
+ );
464
+
465
+ // ═════════════════════════════════════════════
466
+ // Memory Ledger
467
+ // ═════════════════════════════════════════════
468
+
469
+ /**
470
+ * Derive the **MemoryLedger** PDA for a session.
471
+ *
472
+ * Seeds: `["sap_ledger", session_pda]`
473
+ *
474
+ * @name deriveLedger
475
+ * @description Computes the ledger PDA that tracks append-only entries for a session.
476
+ * @param sessionPda - The parent session’s PDA.
477
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
478
+ * @returns {PdaResult} `[pda, bump]` tuple.
479
+ * @category PDA
480
+ * @since v0.1.0
481
+ * @see MemoryLedger
482
+ */
483
+ export const deriveLedger = (
484
+ sessionPda: PublicKey,
485
+ programId = SAP_PROGRAM_ID,
486
+ ): PdaResult =>
487
+ findPda([toSeedBuf(SEEDS.LEDGER), sessionPda.toBuffer()], programId);
488
+
489
+ /**
490
+ * Derive the **LedgerPage** PDA for a ledger at a given page index.
491
+ *
492
+ * Seeds: `["sap_page", ledger_pda, page_index_u32_le]`
493
+ *
494
+ * @name deriveLedgerPage
495
+ * @description Computes the ledger page PDA for paginated ledger data storage.
496
+ * @param ledgerPda - The parent ledger’s PDA.
497
+ * @param pageIndex - Zero-based page index.
498
+ * @param programId - Override program ID (defaults to {@link SAP_PROGRAM_ID}).
499
+ * @returns {PdaResult} `[pda, bump]` tuple.
500
+ * @category PDA
501
+ * @since v0.1.0
502
+ * @see LedgerPage
503
+ */
504
+ export const deriveLedgerPage = (
505
+ ledgerPda: PublicKey,
506
+ pageIndex: number,
507
+ programId = SAP_PROGRAM_ID,
508
+ ): PdaResult =>
509
+ findPda(
510
+ [toSeedBuf(SEEDS.LEDGER_PAGE), ledgerPda.toBuffer(), u32le(pageIndex)],
511
+ programId,
512
+ );