@psavelis/enterprise-blockchain 0.1.0 → 1.1.1

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 (296) hide show
  1. package/README.md +15 -2
  2. package/dist/aid-settlement/application/reconciler.d.ts +13 -0
  3. package/dist/aid-settlement/application/reconciler.d.ts.map +1 -0
  4. package/dist/aid-settlement/application/reconciler.js +77 -0
  5. package/dist/aid-settlement/domain/entities.d.ts +24 -0
  6. package/dist/aid-settlement/domain/entities.d.ts.map +1 -0
  7. package/dist/aid-settlement/domain/entities.js +1 -0
  8. package/dist/aid-settlement/domain/ports.d.ts +10 -0
  9. package/dist/aid-settlement/domain/ports.d.ts.map +1 -0
  10. package/dist/aid-settlement/domain/ports.js +1 -0
  11. package/dist/aid-settlement/index.d.ts +19 -0
  12. package/dist/aid-settlement/index.d.ts.map +1 -0
  13. package/dist/aid-settlement/index.js +23 -0
  14. package/dist/aid-settlement/infrastructure/in-memory-store.d.ts +12 -0
  15. package/dist/aid-settlement/infrastructure/in-memory-store.d.ts.map +1 -0
  16. package/dist/aid-settlement/infrastructure/in-memory-store.js +17 -0
  17. package/dist/credentialing/application/clearance-evaluator.d.ts +10 -0
  18. package/dist/credentialing/application/clearance-evaluator.d.ts.map +1 -0
  19. package/dist/credentialing/application/clearance-evaluator.js +63 -0
  20. package/dist/credentialing/domain/entities.d.ts +28 -0
  21. package/dist/credentialing/domain/entities.d.ts.map +1 -0
  22. package/dist/credentialing/domain/entities.js +1 -0
  23. package/dist/credentialing/domain/ports.d.ts +9 -0
  24. package/dist/credentialing/domain/ports.d.ts.map +1 -0
  25. package/dist/credentialing/domain/ports.js +1 -0
  26. package/dist/credentialing/index.d.ts +19 -0
  27. package/dist/credentialing/index.d.ts.map +1 -0
  28. package/dist/credentialing/index.js +23 -0
  29. package/dist/credentialing/infrastructure/in-memory-store.d.ts +11 -0
  30. package/dist/credentialing/infrastructure/in-memory-store.d.ts.map +1 -0
  31. package/dist/credentialing/infrastructure/in-memory-store.js +14 -0
  32. package/dist/hsm/application/asymmetric-key-service.d.ts +23 -0
  33. package/dist/hsm/application/asymmetric-key-service.d.ts.map +1 -0
  34. package/dist/hsm/application/asymmetric-key-service.js +109 -0
  35. package/dist/hsm/application/envelope-encryption-service.d.ts +18 -0
  36. package/dist/hsm/application/envelope-encryption-service.d.ts.map +1 -0
  37. package/dist/hsm/application/envelope-encryption-service.js +59 -0
  38. package/dist/hsm/application/symmetric-key-service.d.ts +34 -0
  39. package/dist/hsm/application/symmetric-key-service.d.ts.map +1 -0
  40. package/dist/hsm/application/symmetric-key-service.js +107 -0
  41. package/dist/hsm/domain/entities.d.ts +104 -0
  42. package/dist/hsm/domain/entities.d.ts.map +1 -0
  43. package/dist/hsm/domain/entities.js +10 -0
  44. package/dist/hsm/domain/ports.d.ts +20 -0
  45. package/dist/hsm/domain/ports.d.ts.map +1 -0
  46. package/dist/hsm/domain/ports.js +1 -0
  47. package/dist/hsm/index.d.ts +48 -0
  48. package/dist/hsm/index.d.ts.map +1 -0
  49. package/dist/hsm/index.js +97 -0
  50. package/dist/hsm/infrastructure/audit-log-factory.d.ts +59 -0
  51. package/dist/hsm/infrastructure/audit-log-factory.d.ts.map +1 -0
  52. package/dist/hsm/infrastructure/audit-log-factory.js +95 -0
  53. package/dist/hsm/infrastructure/audit-log.d.ts +8 -0
  54. package/dist/hsm/infrastructure/audit-log.d.ts.map +1 -0
  55. package/dist/hsm/infrastructure/audit-log.js +18 -0
  56. package/dist/hsm/infrastructure/file-audit-log.d.ts +55 -0
  57. package/dist/hsm/infrastructure/file-audit-log.d.ts.map +1 -0
  58. package/dist/hsm/infrastructure/file-audit-log.js +128 -0
  59. package/dist/hsm/infrastructure/key-store.d.ts +9 -0
  60. package/dist/hsm/infrastructure/key-store.d.ts.map +1 -0
  61. package/dist/hsm/infrastructure/key-store.js +12 -0
  62. package/dist/hsm/infrastructure/syslog-audit-log.d.ts +64 -0
  63. package/dist/hsm/infrastructure/syslog-audit-log.d.ts.map +1 -0
  64. package/dist/hsm/infrastructure/syslog-audit-log.js +167 -0
  65. package/dist/index.d.ts +22 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +37 -0
  68. package/dist/integrations/besu-client/error-mapper.d.ts +9 -0
  69. package/dist/integrations/besu-client/error-mapper.d.ts.map +1 -0
  70. package/dist/integrations/besu-client/error-mapper.js +22 -0
  71. package/dist/integrations/besu-client/index.d.ts +65 -0
  72. package/dist/integrations/besu-client/index.d.ts.map +1 -0
  73. package/dist/integrations/besu-client/index.js +276 -0
  74. package/dist/integrations/besu-client/ports.d.ts +44 -0
  75. package/dist/integrations/besu-client/ports.d.ts.map +1 -0
  76. package/dist/integrations/besu-client/ports.js +1 -0
  77. package/dist/integrations/corda-gateway/index.d.ts +37 -0
  78. package/dist/integrations/corda-gateway/index.d.ts.map +1 -0
  79. package/dist/integrations/corda-gateway/index.js +234 -0
  80. package/dist/integrations/corda-gateway/ports.d.ts +33 -0
  81. package/dist/integrations/corda-gateway/ports.d.ts.map +1 -0
  82. package/dist/integrations/corda-gateway/ports.js +1 -0
  83. package/dist/integrations/fabric-gateway/index.d.ts +78 -0
  84. package/dist/integrations/fabric-gateway/index.d.ts.map +1 -0
  85. package/dist/integrations/fabric-gateway/index.js +214 -0
  86. package/dist/integrations/fabric-gateway/ports.d.ts +50 -0
  87. package/dist/integrations/fabric-gateway/ports.d.ts.map +1 -0
  88. package/dist/integrations/fabric-gateway/ports.js +1 -0
  89. package/dist/integrations/index.d.ts +19 -0
  90. package/dist/integrations/index.d.ts.map +1 -0
  91. package/dist/integrations/index.js +19 -0
  92. package/dist/integrations/shared/env.d.ts +4 -0
  93. package/dist/integrations/shared/env.d.ts.map +1 -0
  94. package/dist/integrations/shared/env.js +24 -0
  95. package/dist/integrations/shared/retry.d.ts +79 -0
  96. package/dist/integrations/shared/retry.d.ts.map +1 -0
  97. package/dist/integrations/shared/retry.js +315 -0
  98. package/dist/mpc/adapters.d.ts +36 -0
  99. package/dist/mpc/adapters.d.ts.map +1 -0
  100. package/dist/mpc/adapters.js +46 -0
  101. package/dist/mpc/crypto.d.ts +2 -0
  102. package/dist/mpc/crypto.d.ts.map +1 -0
  103. package/dist/mpc/crypto.js +2 -0
  104. package/dist/mpc/dsa.d.ts +134 -0
  105. package/dist/mpc/dsa.d.ts.map +1 -0
  106. package/dist/mpc/dsa.js +127 -0
  107. package/dist/mpc/field.d.ts +127 -0
  108. package/dist/mpc/field.d.ts.map +1 -0
  109. package/dist/mpc/field.js +209 -0
  110. package/dist/mpc/hybrid-kem.d.ts +96 -0
  111. package/dist/mpc/hybrid-kem.d.ts.map +1 -0
  112. package/dist/mpc/hybrid-kem.js +136 -0
  113. package/dist/mpc/index.d.ts +135 -0
  114. package/dist/mpc/index.d.ts.map +1 -0
  115. package/dist/mpc/index.js +348 -0
  116. package/dist/mpc/kyber.d.ts +134 -0
  117. package/dist/mpc/kyber.d.ts.map +1 -0
  118. package/dist/mpc/kyber.js +143 -0
  119. package/dist/mpc/ports.d.ts +67 -0
  120. package/dist/mpc/ports.d.ts.map +1 -0
  121. package/dist/mpc/ports.js +9 -0
  122. package/dist/mpc/quantum.d.ts +80 -0
  123. package/dist/mpc/quantum.d.ts.map +1 -0
  124. package/dist/mpc/quantum.js +180 -0
  125. package/dist/p2mr/adapters.d.ts +31 -0
  126. package/dist/p2mr/adapters.d.ts.map +1 -0
  127. package/dist/p2mr/adapters.js +35 -0
  128. package/dist/p2mr/index.d.ts +63 -0
  129. package/dist/p2mr/index.d.ts.map +1 -0
  130. package/dist/p2mr/index.js +59 -0
  131. package/dist/p2mr/merkle-tree.d.ts +109 -0
  132. package/dist/p2mr/merkle-tree.d.ts.map +1 -0
  133. package/dist/p2mr/merkle-tree.js +239 -0
  134. package/dist/p2mr/p2mr-output.d.ts +142 -0
  135. package/dist/p2mr/p2mr-output.d.ts.map +1 -0
  136. package/dist/p2mr/p2mr-output.js +150 -0
  137. package/dist/p2mr/ports.d.ts +52 -0
  138. package/dist/p2mr/ports.d.ts.map +1 -0
  139. package/dist/p2mr/ports.js +9 -0
  140. package/dist/p2mr/script-interpreter.d.ts +92 -0
  141. package/dist/p2mr/script-interpreter.d.ts.map +1 -0
  142. package/dist/p2mr/script-interpreter.js +535 -0
  143. package/dist/p2mr/script-leaf.d.ts +70 -0
  144. package/dist/p2mr/script-leaf.d.ts.map +1 -0
  145. package/dist/p2mr/script-leaf.js +203 -0
  146. package/dist/p2mr/spend-proof.d.ts +95 -0
  147. package/dist/p2mr/spend-proof.d.ts.map +1 -0
  148. package/dist/p2mr/spend-proof.js +358 -0
  149. package/dist/p2mr/types.d.ts +209 -0
  150. package/dist/p2mr/types.d.ts.map +1 -0
  151. package/dist/p2mr/types.js +9 -0
  152. package/dist/privacy/application/view-projector.d.ts +13 -0
  153. package/dist/privacy/application/view-projector.d.ts.map +1 -0
  154. package/dist/privacy/application/view-projector.js +85 -0
  155. package/dist/privacy/domain/entities.d.ts +26 -0
  156. package/dist/privacy/domain/entities.d.ts.map +1 -0
  157. package/dist/privacy/domain/entities.js +1 -0
  158. package/dist/privacy/domain/ports.d.ts +7 -0
  159. package/dist/privacy/domain/ports.d.ts.map +1 -0
  160. package/dist/privacy/domain/ports.js +1 -0
  161. package/dist/privacy/index.d.ts +21 -0
  162. package/dist/privacy/index.d.ts.map +1 -0
  163. package/dist/privacy/index.js +25 -0
  164. package/dist/privacy/infrastructure/in-memory-store.d.ts +8 -0
  165. package/dist/privacy/infrastructure/in-memory-store.d.ts.map +1 -0
  166. package/dist/privacy/infrastructure/in-memory-store.js +7 -0
  167. package/dist/protocols/besu-port.d.ts +80 -0
  168. package/dist/protocols/besu-port.d.ts.map +1 -0
  169. package/dist/protocols/besu-port.js +1 -0
  170. package/dist/protocols/corda-port.d.ts +103 -0
  171. package/dist/protocols/corda-port.d.ts.map +1 -0
  172. package/dist/protocols/corda-port.js +9 -0
  173. package/dist/protocols/credentialing-port.d.ts +11 -0
  174. package/dist/protocols/credentialing-port.d.ts.map +1 -0
  175. package/dist/protocols/credentialing-port.js +1 -0
  176. package/dist/protocols/fabric-port.d.ts +89 -0
  177. package/dist/protocols/fabric-port.d.ts.map +1 -0
  178. package/dist/protocols/fabric-port.js +9 -0
  179. package/dist/protocols/index.d.ts +14 -0
  180. package/dist/protocols/index.d.ts.map +1 -0
  181. package/dist/protocols/index.js +7 -0
  182. package/dist/protocols/p2mr-port.d.ts +159 -0
  183. package/dist/protocols/p2mr-port.d.ts.map +1 -0
  184. package/dist/protocols/p2mr-port.js +12 -0
  185. package/dist/protocols/privacy-port.d.ts +9 -0
  186. package/dist/protocols/privacy-port.d.ts.map +1 -0
  187. package/dist/protocols/privacy-port.js +1 -0
  188. package/dist/protocols/traceability-port.d.ts +12 -0
  189. package/dist/protocols/traceability-port.d.ts.map +1 -0
  190. package/dist/protocols/traceability-port.js +1 -0
  191. package/dist/shared/collection-store.d.ts +12 -0
  192. package/dist/shared/collection-store.d.ts.map +1 -0
  193. package/dist/shared/collection-store.js +26 -0
  194. package/dist/shared/commit.d.ts +24 -0
  195. package/dist/shared/commit.d.ts.map +1 -0
  196. package/dist/shared/commit.js +50 -0
  197. package/dist/shared/crypto.d.ts +2 -0
  198. package/dist/shared/crypto.d.ts.map +1 -0
  199. package/dist/shared/crypto.js +4 -0
  200. package/dist/shared/date.d.ts +2 -0
  201. package/dist/shared/date.d.ts.map +1 -0
  202. package/dist/shared/date.js +3 -0
  203. package/dist/shared/index.d.ts +9 -0
  204. package/dist/shared/index.d.ts.map +1 -0
  205. package/dist/shared/index.js +11 -0
  206. package/dist/shared/logger.d.ts +37 -0
  207. package/dist/shared/logger.d.ts.map +1 -0
  208. package/dist/shared/logger.js +45 -0
  209. package/dist/shared/store.d.ts +25 -0
  210. package/dist/shared/store.d.ts.map +1 -0
  211. package/dist/shared/store.js +18 -0
  212. package/dist/shared/telemetry-sdk.d.ts +26 -0
  213. package/dist/shared/telemetry-sdk.d.ts.map +1 -0
  214. package/dist/shared/telemetry-sdk.js +97 -0
  215. package/dist/shared/telemetry.d.ts +86 -0
  216. package/dist/shared/telemetry.d.ts.map +1 -0
  217. package/dist/shared/telemetry.js +137 -0
  218. package/dist/stark-settlement/application/aggregator-service.d.ts +112 -0
  219. package/dist/stark-settlement/application/aggregator-service.d.ts.map +1 -0
  220. package/dist/stark-settlement/application/aggregator-service.js +256 -0
  221. package/dist/stark-settlement/application/ledger-service.d.ts +114 -0
  222. package/dist/stark-settlement/application/ledger-service.d.ts.map +1 -0
  223. package/dist/stark-settlement/application/ledger-service.js +318 -0
  224. package/dist/stark-settlement/application/settlement-service.d.ts +104 -0
  225. package/dist/stark-settlement/application/settlement-service.d.ts.map +1 -0
  226. package/dist/stark-settlement/application/settlement-service.js +251 -0
  227. package/dist/stark-settlement/domain/entities.d.ts +365 -0
  228. package/dist/stark-settlement/domain/entities.d.ts.map +1 -0
  229. package/dist/stark-settlement/domain/entities.js +29 -0
  230. package/dist/stark-settlement/domain/ports.d.ts +485 -0
  231. package/dist/stark-settlement/domain/ports.d.ts.map +1 -0
  232. package/dist/stark-settlement/domain/ports.js +14 -0
  233. package/dist/stark-settlement/domain/value-objects.d.ts +268 -0
  234. package/dist/stark-settlement/domain/value-objects.d.ts.map +1 -0
  235. package/dist/stark-settlement/domain/value-objects.js +500 -0
  236. package/dist/stark-settlement/index.d.ts +172 -0
  237. package/dist/stark-settlement/index.d.ts.map +1 -0
  238. package/dist/stark-settlement/index.js +193 -0
  239. package/dist/stark-settlement/infrastructure/adapters/audit-adapter.d.ts +52 -0
  240. package/dist/stark-settlement/infrastructure/adapters/audit-adapter.d.ts.map +1 -0
  241. package/dist/stark-settlement/infrastructure/adapters/audit-adapter.js +154 -0
  242. package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.d.ts +88 -0
  243. package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.d.ts.map +1 -0
  244. package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.js +187 -0
  245. package/dist/stark-settlement/infrastructure/adapters/clock-adapter.d.ts +59 -0
  246. package/dist/stark-settlement/infrastructure/adapters/clock-adapter.d.ts.map +1 -0
  247. package/dist/stark-settlement/infrastructure/adapters/clock-adapter.js +85 -0
  248. package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.d.ts +60 -0
  249. package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.d.ts.map +1 -0
  250. package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.js +104 -0
  251. package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.d.ts +115 -0
  252. package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.d.ts.map +1 -0
  253. package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.js +191 -0
  254. package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.d.ts +65 -0
  255. package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.d.ts.map +1 -0
  256. package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.js +207 -0
  257. package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.d.ts +73 -0
  258. package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.d.ts.map +1 -0
  259. package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.js +287 -0
  260. package/dist/stark-settlement/infrastructure/adapters/solana-adapter.d.ts +78 -0
  261. package/dist/stark-settlement/infrastructure/adapters/solana-adapter.d.ts.map +1 -0
  262. package/dist/stark-settlement/infrastructure/adapters/solana-adapter.js +172 -0
  263. package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.d.ts +56 -0
  264. package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.d.ts.map +1 -0
  265. package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.js +261 -0
  266. package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.d.ts +125 -0
  267. package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.d.ts.map +1 -0
  268. package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.js +416 -0
  269. package/dist/stark-settlement/infrastructure/persistence/ledger-store.d.ts +68 -0
  270. package/dist/stark-settlement/infrastructure/persistence/ledger-store.d.ts.map +1 -0
  271. package/dist/stark-settlement/infrastructure/persistence/ledger-store.js +238 -0
  272. package/dist/stark-settlement/infrastructure/persistence/offset-store.d.ts +30 -0
  273. package/dist/stark-settlement/infrastructure/persistence/offset-store.d.ts.map +1 -0
  274. package/dist/stark-settlement/infrastructure/persistence/offset-store.js +57 -0
  275. package/dist/stark-settlement/infrastructure/persistence/outbox-store.d.ts +45 -0
  276. package/dist/stark-settlement/infrastructure/persistence/outbox-store.d.ts.map +1 -0
  277. package/dist/stark-settlement/infrastructure/persistence/outbox-store.js +171 -0
  278. package/dist/traceability/application/recall-assessor.d.ts +13 -0
  279. package/dist/traceability/application/recall-assessor.d.ts.map +1 -0
  280. package/dist/traceability/application/recall-assessor.js +74 -0
  281. package/dist/traceability/domain/entities.d.ts +23 -0
  282. package/dist/traceability/domain/entities.d.ts.map +1 -0
  283. package/dist/traceability/domain/entities.js +1 -0
  284. package/dist/traceability/domain/ports.d.ts +23 -0
  285. package/dist/traceability/domain/ports.d.ts.map +1 -0
  286. package/dist/traceability/domain/ports.js +1 -0
  287. package/dist/traceability/domain/recall.d.ts +12 -0
  288. package/dist/traceability/domain/recall.d.ts.map +1 -0
  289. package/dist/traceability/domain/recall.js +1 -0
  290. package/dist/traceability/index.d.ts +22 -0
  291. package/dist/traceability/index.d.ts.map +1 -0
  292. package/dist/traceability/index.js +26 -0
  293. package/dist/traceability/infrastructure/in-memory-store.d.ts +13 -0
  294. package/dist/traceability/infrastructure/in-memory-store.d.ts.map +1 -0
  295. package/dist/traceability/infrastructure/in-memory-store.js +24 -0
  296. package/package.json +12 -9
@@ -0,0 +1,535 @@
1
+ /**
2
+ * P2MR Script Interpreter
3
+ *
4
+ * Executes spending conditions by verifying witness data against script leaves.
5
+ * Each leaf type has specific verification rules:
6
+ *
7
+ * - ml-dsa-65-sig: Single ML-DSA-65 signature verification
8
+ * - timelock: ML-DSA-65 signature + timestamp >= locktime
9
+ * - multisig-ml-dsa: k-of-n threshold ML-DSA-65 signatures
10
+ * - hsm-attested-sig: ML-DSA-65 signature + HSM attestation (placeholder)
11
+ *
12
+ * The interpreter returns a detailed audit trail for compliance.
13
+ */
14
+ import { defaultSignatureVerifier, defaultHasher } from "./adapters.js";
15
+ // Module-level default instances (can be overridden via createInterpreter)
16
+ let signatureVerifier = defaultSignatureVerifier;
17
+ let hasher = defaultHasher;
18
+ /**
19
+ * Configure the script interpreter with custom implementations.
20
+ * Primarily useful for testing with mock implementations.
21
+ *
22
+ * @param options Configuration options.
23
+ */
24
+ export function configureInterpreter(options) {
25
+ if (options.signatureVerifier) {
26
+ signatureVerifier = options.signatureVerifier;
27
+ }
28
+ if (options.hasher) {
29
+ hasher = options.hasher;
30
+ }
31
+ }
32
+ /**
33
+ * Reset interpreter to default implementations.
34
+ */
35
+ export function resetInterpreter() {
36
+ signatureVerifier = defaultSignatureVerifier;
37
+ hasher = defaultHasher;
38
+ }
39
+ /**
40
+ * Interpret (execute) a P2MR script leaf against witness data.
41
+ *
42
+ * This function performs full cryptographic verification of the spending
43
+ * condition using ML-DSA-65 signatures.
44
+ *
45
+ * @param options - Interpretation options.
46
+ * @returns Verification result with audit trail.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const result = interpretScript({
51
+ * leaf: singleSigLeaf,
52
+ * witness: {
53
+ * publicKeys: [myPublicKey],
54
+ * signatures: [mySignature],
55
+ * },
56
+ * message: transactionHash,
57
+ * });
58
+ *
59
+ * if (result.valid) {
60
+ * console.log("Spending condition satisfied");
61
+ * }
62
+ * ```
63
+ */
64
+ export function interpretScript(options) {
65
+ const { leaf, witness, message, currentTime } = options;
66
+ const auditTrail = [];
67
+ switch (leaf.type) {
68
+ case "ml-dsa-65-sig":
69
+ return interpretSingleSig(leaf, witness, message, auditTrail);
70
+ case "timelock":
71
+ return interpretTimelock(leaf, witness, message, currentTime, auditTrail);
72
+ case "multisig-ml-dsa":
73
+ return interpretMultisig(leaf, witness, message, auditTrail);
74
+ case "hsm-attested-sig":
75
+ return interpretHsmAttested(leaf, witness, message, auditTrail);
76
+ default: {
77
+ // Exhaustive check - all known types handled above
78
+ const unknownType = leaf;
79
+ const typeStr = String(unknownType.type);
80
+ auditTrail.push({
81
+ step: "Script type check",
82
+ passed: false,
83
+ detail: `Unknown script type: ${typeStr}`,
84
+ });
85
+ return {
86
+ valid: false,
87
+ reason: `Unknown script type: ${typeStr}`,
88
+ auditTrail,
89
+ };
90
+ }
91
+ }
92
+ }
93
+ // ---------------------------------------------------------------------------
94
+ // Single Signature (ml-dsa-65-sig)
95
+ // ---------------------------------------------------------------------------
96
+ function interpretSingleSig(leaf, witness, message, auditTrail) {
97
+ auditTrail.push({
98
+ step: "Script type",
99
+ passed: true,
100
+ detail: "ml-dsa-65-sig (single signature)",
101
+ });
102
+ // Verify witness has exactly one public key and signature
103
+ if (witness.publicKeys.length !== 1) {
104
+ auditTrail.push({
105
+ step: "Witness count check",
106
+ passed: false,
107
+ detail: `Expected 1 public key, got ${witness.publicKeys.length}`,
108
+ });
109
+ return {
110
+ valid: false,
111
+ reason: "ml-dsa-65-sig requires exactly 1 public key",
112
+ auditTrail,
113
+ };
114
+ }
115
+ if (witness.signatures.length !== 1) {
116
+ auditTrail.push({
117
+ step: "Signature count check",
118
+ passed: false,
119
+ detail: `Expected 1 signature, got ${witness.signatures.length}`,
120
+ });
121
+ return {
122
+ valid: false,
123
+ reason: "ml-dsa-65-sig requires exactly 1 signature",
124
+ auditTrail,
125
+ };
126
+ }
127
+ auditTrail.push({
128
+ step: "Witness count check",
129
+ passed: true,
130
+ detail: "1 public key, 1 signature",
131
+ });
132
+ const publicKey = witness.publicKeys[0];
133
+ const signature = witness.signatures[0];
134
+ // Verify public key hash matches leaf
135
+ const publicKeyHash = hashPublicKey(publicKey);
136
+ if (publicKeyHash !== leaf.publicKeyHashes[0]) {
137
+ auditTrail.push({
138
+ step: "Public key hash verification",
139
+ passed: false,
140
+ detail: `Hash mismatch: witness=${publicKeyHash.substring(0, 16)}..., leaf=${leaf.publicKeyHashes[0]?.substring(0, 16)}...`,
141
+ });
142
+ return {
143
+ valid: false,
144
+ reason: "Public key hash does not match authorized key",
145
+ auditTrail,
146
+ };
147
+ }
148
+ auditTrail.push({
149
+ step: "Public key hash verification",
150
+ passed: true,
151
+ detail: `Hash: ${publicKeyHash.substring(0, 16)}...`,
152
+ });
153
+ // Verify ML-DSA-65 signature
154
+ const signatureValid = signatureVerifier.verify(message, signature, publicKey, "ml-dsa-65");
155
+ if (!signatureValid) {
156
+ auditTrail.push({
157
+ step: "ML-DSA-65 signature verification",
158
+ passed: false,
159
+ detail: "Signature verification failed",
160
+ });
161
+ return {
162
+ valid: false,
163
+ reason: "ML-DSA-65 signature verification failed",
164
+ auditTrail,
165
+ };
166
+ }
167
+ auditTrail.push({
168
+ step: "ML-DSA-65 signature verification",
169
+ passed: true,
170
+ detail: `Signature length: ${signature.length} bytes`,
171
+ });
172
+ return {
173
+ valid: true,
174
+ reason: "Single signature verified successfully",
175
+ auditTrail,
176
+ };
177
+ }
178
+ // ---------------------------------------------------------------------------
179
+ // Timelock
180
+ // ---------------------------------------------------------------------------
181
+ function interpretTimelock(leaf, witness, message, currentTime, auditTrail) {
182
+ auditTrail.push({
183
+ step: "Script type",
184
+ passed: true,
185
+ detail: "timelock (time-locked signature)",
186
+ });
187
+ // Check timelock condition first
188
+ const locktime = leaf.locktime;
189
+ if (locktime === undefined) {
190
+ auditTrail.push({
191
+ step: "Locktime check",
192
+ passed: false,
193
+ detail: "Leaf missing locktime parameter",
194
+ });
195
+ return {
196
+ valid: false,
197
+ reason: "Timelock leaf missing locktime parameter",
198
+ auditTrail,
199
+ };
200
+ }
201
+ const effectiveTime = currentTime ?? witness.timestamp;
202
+ if (effectiveTime === undefined) {
203
+ auditTrail.push({
204
+ step: "Timestamp check",
205
+ passed: false,
206
+ detail: "No timestamp provided in witness or options",
207
+ });
208
+ return {
209
+ valid: false,
210
+ reason: "Timelock requires a timestamp",
211
+ auditTrail,
212
+ };
213
+ }
214
+ if (effectiveTime < locktime) {
215
+ auditTrail.push({
216
+ step: "Timelock verification",
217
+ passed: false,
218
+ detail: `Current time ${effectiveTime} < locktime ${locktime}`,
219
+ });
220
+ return {
221
+ valid: false,
222
+ reason: `Timelock not reached: ${locktime - effectiveTime}ms remaining`,
223
+ auditTrail,
224
+ };
225
+ }
226
+ auditTrail.push({
227
+ step: "Timelock verification",
228
+ passed: true,
229
+ detail: `Current time ${effectiveTime} >= locktime ${locktime}`,
230
+ });
231
+ // Now verify signature (same as single sig)
232
+ if (witness.publicKeys.length !== 1 || witness.signatures.length !== 1) {
233
+ auditTrail.push({
234
+ step: "Witness count check",
235
+ passed: false,
236
+ detail: "Timelock requires exactly 1 public key and 1 signature",
237
+ });
238
+ return {
239
+ valid: false,
240
+ reason: "Timelock requires exactly 1 public key and 1 signature",
241
+ auditTrail,
242
+ };
243
+ }
244
+ auditTrail.push({
245
+ step: "Witness count check",
246
+ passed: true,
247
+ detail: "1 public key, 1 signature",
248
+ });
249
+ const publicKey = witness.publicKeys[0];
250
+ const signature = witness.signatures[0];
251
+ // Verify public key hash
252
+ const publicKeyHash = hashPublicKey(publicKey);
253
+ if (publicKeyHash !== leaf.publicKeyHashes[0]) {
254
+ auditTrail.push({
255
+ step: "Public key hash verification",
256
+ passed: false,
257
+ detail: "Hash mismatch",
258
+ });
259
+ return {
260
+ valid: false,
261
+ reason: "Public key hash does not match authorized key",
262
+ auditTrail,
263
+ };
264
+ }
265
+ auditTrail.push({
266
+ step: "Public key hash verification",
267
+ passed: true,
268
+ detail: `Hash: ${publicKeyHash.substring(0, 16)}...`,
269
+ });
270
+ // Verify signature
271
+ const signatureValid = signatureVerifier.verify(message, signature, publicKey, "ml-dsa-65");
272
+ if (!signatureValid) {
273
+ auditTrail.push({
274
+ step: "ML-DSA-65 signature verification",
275
+ passed: false,
276
+ detail: "Signature verification failed",
277
+ });
278
+ return {
279
+ valid: false,
280
+ reason: "ML-DSA-65 signature verification failed",
281
+ auditTrail,
282
+ };
283
+ }
284
+ auditTrail.push({
285
+ step: "ML-DSA-65 signature verification",
286
+ passed: true,
287
+ detail: `Signature length: ${signature.length} bytes`,
288
+ });
289
+ return {
290
+ valid: true,
291
+ reason: "Timelock condition and signature verified successfully",
292
+ auditTrail,
293
+ };
294
+ }
295
+ // ---------------------------------------------------------------------------
296
+ // Multisig
297
+ // ---------------------------------------------------------------------------
298
+ function interpretMultisig(leaf, witness, message, auditTrail) {
299
+ const threshold = leaf.threshold ?? leaf.publicKeyHashes.length;
300
+ const n = leaf.publicKeyHashes.length;
301
+ auditTrail.push({
302
+ step: "Script type",
303
+ passed: true,
304
+ detail: `multisig-ml-dsa (${threshold}-of-${n})`,
305
+ });
306
+ // Verify we have enough signatures
307
+ if (witness.signatures.length < threshold) {
308
+ auditTrail.push({
309
+ step: "Signature count check",
310
+ passed: false,
311
+ detail: `Expected >= ${threshold} signatures, got ${witness.signatures.length}`,
312
+ });
313
+ return {
314
+ valid: false,
315
+ reason: `Multisig requires at least ${threshold} signatures, got ${witness.signatures.length}`,
316
+ auditTrail,
317
+ };
318
+ }
319
+ if (witness.publicKeys.length !== witness.signatures.length) {
320
+ auditTrail.push({
321
+ step: "Key/signature pairing check",
322
+ passed: false,
323
+ detail: `${witness.publicKeys.length} keys, ${witness.signatures.length} signatures`,
324
+ });
325
+ return {
326
+ valid: false,
327
+ reason: "Number of public keys must match number of signatures",
328
+ auditTrail,
329
+ };
330
+ }
331
+ auditTrail.push({
332
+ step: "Witness count check",
333
+ passed: true,
334
+ detail: `${witness.signatures.length} signatures provided (threshold: ${threshold})`,
335
+ });
336
+ // Create set of authorized public key hashes for fast lookup
337
+ const authorizedHashes = new Set(leaf.publicKeyHashes);
338
+ // Verify each signature and count valid ones
339
+ let validCount = 0;
340
+ const usedHashes = new Set();
341
+ for (let i = 0; i < witness.publicKeys.length; i++) {
342
+ const publicKey = witness.publicKeys[i];
343
+ const signature = witness.signatures[i];
344
+ const keyHash = hashPublicKey(publicKey);
345
+ // Check if this key is authorized
346
+ if (!authorizedHashes.has(keyHash)) {
347
+ auditTrail.push({
348
+ step: `Signer ${i + 1} authorization`,
349
+ passed: false,
350
+ detail: `Key hash ${keyHash.substring(0, 16)}... not in authorized set`,
351
+ });
352
+ continue;
353
+ }
354
+ // Check for duplicate signers
355
+ if (usedHashes.has(keyHash)) {
356
+ auditTrail.push({
357
+ step: `Signer ${i + 1} uniqueness`,
358
+ passed: false,
359
+ detail: "Duplicate signer detected",
360
+ });
361
+ continue;
362
+ }
363
+ // Verify signature
364
+ const signatureValid = signatureVerifier.verify(message, signature, publicKey, "ml-dsa-65");
365
+ if (!signatureValid) {
366
+ auditTrail.push({
367
+ step: `Signer ${i + 1} signature`,
368
+ passed: false,
369
+ detail: "Signature verification failed",
370
+ });
371
+ continue;
372
+ }
373
+ usedHashes.add(keyHash);
374
+ validCount++;
375
+ auditTrail.push({
376
+ step: `Signer ${i + 1} verification`,
377
+ passed: true,
378
+ detail: `Key ${keyHash.substring(0, 16)}... signature valid`,
379
+ });
380
+ }
381
+ // Check threshold
382
+ if (validCount < threshold) {
383
+ auditTrail.push({
384
+ step: "Threshold check",
385
+ passed: false,
386
+ detail: `${validCount} valid signatures < threshold ${threshold}`,
387
+ });
388
+ return {
389
+ valid: false,
390
+ reason: `Only ${validCount} valid signatures, need ${threshold}`,
391
+ auditTrail,
392
+ };
393
+ }
394
+ auditTrail.push({
395
+ step: "Threshold check",
396
+ passed: true,
397
+ detail: `${validCount} valid signatures >= threshold ${threshold}`,
398
+ });
399
+ return {
400
+ valid: true,
401
+ reason: `Multisig ${threshold}-of-${n} verified: ${validCount} valid signatures`,
402
+ auditTrail,
403
+ };
404
+ }
405
+ // ---------------------------------------------------------------------------
406
+ // HSM-Attested Signature
407
+ // ---------------------------------------------------------------------------
408
+ function interpretHsmAttested(leaf, witness, message, auditTrail) {
409
+ auditTrail.push({
410
+ step: "Script type",
411
+ passed: true,
412
+ detail: "hsm-attested-sig (HSM-backed signature)",
413
+ });
414
+ // Verify HSM slot ID
415
+ const hsmSlotId = leaf.hsmSlotId;
416
+ if (!hsmSlotId) {
417
+ auditTrail.push({
418
+ step: "HSM slot check",
419
+ passed: false,
420
+ detail: "Leaf missing hsmSlotId parameter",
421
+ });
422
+ return {
423
+ valid: false,
424
+ reason: "HSM-attested leaf missing hsmSlotId",
425
+ auditTrail,
426
+ };
427
+ }
428
+ // Verify attestation is provided
429
+ if (!witness.hsmAttestation) {
430
+ auditTrail.push({
431
+ step: "HSM attestation check",
432
+ passed: false,
433
+ detail: "Witness missing hsmAttestation",
434
+ });
435
+ return {
436
+ valid: false,
437
+ reason: "HSM-attested spending requires hsmAttestation in witness",
438
+ auditTrail,
439
+ };
440
+ }
441
+ // NOTE: Full HSM attestation verification would require:
442
+ // 1. Parsing the attestation blob (format depends on HSM vendor)
443
+ // 2. Verifying the attestation signature chain to a trusted HSM root
444
+ // 3. Checking the attested key matches the signing key
445
+ // 4. Verifying the attestation timestamp and freshness
446
+ //
447
+ // For now, we verify the signature and note that HSM attestation
448
+ // verification is a protocol-level concern (requires HSM integration).
449
+ auditTrail.push({
450
+ step: "HSM attestation format",
451
+ passed: true,
452
+ detail: `Slot: ${hsmSlotId}, Attestation: ${witness.hsmAttestation.length} chars`,
453
+ });
454
+ // Standard signature verification
455
+ if (witness.publicKeys.length !== 1 || witness.signatures.length !== 1) {
456
+ auditTrail.push({
457
+ step: "Witness count check",
458
+ passed: false,
459
+ detail: "HSM-attested requires exactly 1 public key and 1 signature",
460
+ });
461
+ return {
462
+ valid: false,
463
+ reason: "HSM-attested requires exactly 1 public key and 1 signature",
464
+ auditTrail,
465
+ };
466
+ }
467
+ auditTrail.push({
468
+ step: "Witness count check",
469
+ passed: true,
470
+ detail: "1 public key, 1 signature",
471
+ });
472
+ const publicKey = witness.publicKeys[0];
473
+ const signature = witness.signatures[0];
474
+ // Verify public key hash
475
+ const publicKeyHash = hashPublicKey(publicKey);
476
+ if (publicKeyHash !== leaf.publicKeyHashes[0]) {
477
+ auditTrail.push({
478
+ step: "Public key hash verification",
479
+ passed: false,
480
+ detail: "Hash mismatch",
481
+ });
482
+ return {
483
+ valid: false,
484
+ reason: "Public key hash does not match authorized key",
485
+ auditTrail,
486
+ };
487
+ }
488
+ auditTrail.push({
489
+ step: "Public key hash verification",
490
+ passed: true,
491
+ detail: `Hash: ${publicKeyHash.substring(0, 16)}...`,
492
+ });
493
+ // Verify signature
494
+ const signatureValid = signatureVerifier.verify(message, signature, publicKey, "ml-dsa-65");
495
+ if (!signatureValid) {
496
+ auditTrail.push({
497
+ step: "ML-DSA-65 signature verification",
498
+ passed: false,
499
+ detail: "Signature verification failed",
500
+ });
501
+ return {
502
+ valid: false,
503
+ reason: "ML-DSA-65 signature verification failed",
504
+ auditTrail,
505
+ };
506
+ }
507
+ auditTrail.push({
508
+ step: "ML-DSA-65 signature verification",
509
+ passed: true,
510
+ detail: `Signature length: ${signature.length} bytes`,
511
+ });
512
+ // Note: Full HSM attestation verification deferred to protocol adapter
513
+ auditTrail.push({
514
+ step: "HSM attestation verification",
515
+ passed: true,
516
+ detail: "Attestation format valid (full verification at protocol level)",
517
+ });
518
+ return {
519
+ valid: true,
520
+ reason: "HSM-attested signature verified (attestation pending protocol verification)",
521
+ auditTrail,
522
+ };
523
+ }
524
+ // ---------------------------------------------------------------------------
525
+ // Helper Functions
526
+ // ---------------------------------------------------------------------------
527
+ /**
528
+ * Compute SHA-256 hash of a public key.
529
+ *
530
+ * The public key is converted to hex, then hashed.
531
+ * This matches the format stored in ScriptLeaf.publicKeyHashes.
532
+ */
533
+ export function hashPublicKey(publicKey) {
534
+ return hasher.sha256hex(Buffer.from(publicKey).toString("hex"));
535
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Script Leaf Creation and Validation
3
+ *
4
+ * Provides factory functions and validation for P2MR script leaves.
5
+ * Each leaf type has specific requirements for its parameters.
6
+ */
7
+ import type { ScriptLeaf } from "./types.js";
8
+ /**
9
+ * Validation result with optional error message.
10
+ */
11
+ export interface ValidationResult {
12
+ valid: boolean;
13
+ error?: string;
14
+ }
15
+ /**
16
+ * Validate a script leaf for correctness.
17
+ *
18
+ * Checks:
19
+ * - Type is a valid ScriptLeafType
20
+ * - publicKeyHashes is non-empty and contains valid hashes
21
+ * - Condition-specific parameters are valid
22
+ *
23
+ * @param leaf - Script leaf to validate.
24
+ * @returns Validation result with error message if invalid.
25
+ */
26
+ export declare function validateScriptLeaf(leaf: ScriptLeaf): ValidationResult;
27
+ /**
28
+ * Create a simple ML-DSA-65 signature leaf.
29
+ *
30
+ * Requires a single valid signature from the specified public key.
31
+ *
32
+ * @param publicKeyHash - SHA-256 hash of the authorized ML-DSA-65 public key.
33
+ * @returns A validated ScriptLeaf.
34
+ * @throws Error if the hash is invalid.
35
+ */
36
+ export declare function createSingleSigLeaf(publicKeyHash: string): ScriptLeaf;
37
+ /**
38
+ * Create a time-locked signature leaf.
39
+ *
40
+ * Requires a valid signature from the specified public key, but only after
41
+ * the locktime has passed.
42
+ *
43
+ * @param publicKeyHash - SHA-256 hash of the authorized ML-DSA-65 public key.
44
+ * @param locktime - Unix timestamp (ms) after which spending is allowed.
45
+ * @returns A validated ScriptLeaf.
46
+ * @throws Error if parameters are invalid.
47
+ */
48
+ export declare function createTimelockLeaf(publicKeyHash: string, locktime: number): ScriptLeaf;
49
+ /**
50
+ * Create a multisig leaf requiring k-of-n signatures.
51
+ *
52
+ * @param publicKeyHashes - SHA-256 hashes of all authorized ML-DSA-65 public keys.
53
+ * @param threshold - Minimum number of valid signatures required.
54
+ * @returns A validated ScriptLeaf.
55
+ * @throws Error if parameters are invalid.
56
+ */
57
+ export declare function createMultisigLeaf(publicKeyHashes: string[], threshold: number): ScriptLeaf;
58
+ /**
59
+ * Create an HSM-attested signature leaf.
60
+ *
61
+ * Requires a valid signature from the specified public key, plus attestation
62
+ * proof that the signature was produced by the specified HSM slot.
63
+ *
64
+ * @param publicKeyHash - SHA-256 hash of the authorized ML-DSA-65 public key.
65
+ * @param hsmSlotId - Identifier of the required HSM slot.
66
+ * @returns A validated ScriptLeaf.
67
+ * @throws Error if parameters are invalid.
68
+ */
69
+ export declare function createHsmAttestedLeaf(publicKeyHash: string, hsmSlotId: string): ScriptLeaf;
70
+ //# sourceMappingURL=script-leaf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-leaf.d.ts","sourceRoot":"","sources":["../../src/p2mr/script-leaf.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,YAAY,CAAC;AAgB7D;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,UAAU,GAAG,gBAAgB,CA6FrE;AAMD;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,MAAM,GAAG,UAAU,CAYrE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,MAAM,GACf,UAAU,CAaZ;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,eAAe,EAAE,MAAM,EAAE,EACzB,SAAS,EAAE,MAAM,GAChB,UAAU,CAaZ;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CACnC,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,GAChB,UAAU,CAaZ"}