@odatano/core 2.0.0-rc.4 → 2.0.0-rc.41

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 (166) hide show
  1. package/@cds-models/CardanoAgentService/index.d.ts +251 -0
  2. package/@cds-models/CardanoAgentService/index.js +36 -0
  3. package/@cds-models/CardanoBackendService/index.d.ts +357 -0
  4. package/@cds-models/CardanoBackendService/index.js +40 -0
  5. package/@cds-models/CardanoIndexerService/index.d.ts +194 -2
  6. package/@cds-models/CardanoIndexerService/index.js +25 -0
  7. package/@cds-models/CardanoODataService/index.d.ts +305 -7
  8. package/@cds-models/CardanoODataService/index.js +34 -0
  9. package/@cds-models/CardanoSignService/index.d.ts +28 -3
  10. package/@cds-models/CardanoSignService/index.js +3 -0
  11. package/@cds-models/CardanoTransactionService/index.d.ts +46 -13
  12. package/@cds-models/CardanoTransactionService/index.js +4 -0
  13. package/@cds-models/index.d.ts +72 -0
  14. package/@cds-models/index.js +12 -0
  15. package/@cds-models/odatano/cardano/index.d.ts +408 -7
  16. package/@cds-models/odatano/cardano/index.js +42 -0
  17. package/README.md +64 -5
  18. package/db/schema.cds +2297 -1633
  19. package/db/types.cds +114 -0
  20. package/package.json +27 -6
  21. package/scripts/migrate-slot-leader.mjs +104 -0
  22. package/scripts/migrate-txseq.mjs +147 -0
  23. package/src/index.d.ts +18 -11
  24. package/src/index.js +44 -14
  25. package/src/plugin.js +42 -52
  26. package/srv/blockchain/backends/blockfrost-backend.d.ts +29 -131
  27. package/srv/blockchain/backends/blockfrost-backend.js +53 -162
  28. package/srv/blockchain/backends/cardano-backend.d.ts +104 -158
  29. package/srv/blockchain/backends/cardano-backend.js +19 -5
  30. package/srv/blockchain/backends/koios-backend.d.ts +126 -60
  31. package/srv/blockchain/backends/koios-backend.js +376 -235
  32. package/srv/blockchain/backends/odatano-backend.d.ts +64 -0
  33. package/srv/blockchain/backends/odatano-backend.js +170 -0
  34. package/srv/blockchain/backends/ogmios-backend.d.ts +162 -94
  35. package/srv/blockchain/backends/ogmios-backend.js +556 -201
  36. package/srv/blockchain/backends/ogmios-frame-guard.d.ts +32 -0
  37. package/srv/blockchain/backends/ogmios-frame-guard.js +142 -0
  38. package/srv/blockchain/cardano-client.d.ts +87 -202
  39. package/srv/blockchain/cardano-client.js +210 -240
  40. package/srv/blockchain/cardano-indexer.d.ts +311 -242
  41. package/srv/blockchain/cardano-indexer.js +1238 -362
  42. package/srv/blockchain/cardano-tx-builder.d.ts +44 -81
  43. package/srv/blockchain/cardano-tx-builder.js +173 -132
  44. package/srv/blockchain/crawler/certificate-backfill.d.ts +67 -0
  45. package/srv/blockchain/crawler/certificate-backfill.js +169 -0
  46. package/srv/blockchain/crawler/crawler.d.ts +139 -54
  47. package/srv/blockchain/crawler/crawler.js +478 -96
  48. package/srv/blockchain/crawler/hooks.d.ts +3 -14
  49. package/srv/blockchain/crawler/index.d.ts +5 -0
  50. package/srv/blockchain/crawler/index.js +35 -7
  51. package/srv/blockchain/crawler/sync-state.d.ts +46 -28
  52. package/srv/blockchain/crawler/sync-state.js +128 -43
  53. package/srv/blockchain/crawler/transaction-backfill.d.ts +52 -0
  54. package/srv/blockchain/crawler/transaction-backfill.js +283 -0
  55. package/srv/blockchain/crawler/utxo-set-import.d.ts +78 -0
  56. package/srv/blockchain/crawler/utxo-set-import.js +363 -0
  57. package/srv/blockchain/ledger-state.d.ts +101 -0
  58. package/srv/blockchain/ledger-state.js +432 -0
  59. package/srv/blockchain/pending-spends.d.ts +28 -0
  60. package/srv/blockchain/pending-spends.js +89 -0
  61. package/srv/blockchain/signing/cose-verifier.d.ts +5 -12
  62. package/srv/blockchain/signing/cose-verifier.js +6 -12
  63. package/srv/blockchain/signing/external-signer.d.ts +16 -113
  64. package/srv/blockchain/signing/external-signer.js +19 -122
  65. package/srv/blockchain/signing/hsm-signer.d.ts +13 -45
  66. package/srv/blockchain/signing/hsm-signer.js +23 -63
  67. package/srv/blockchain/signing/signature-verifier.d.ts +14 -45
  68. package/srv/blockchain/signing/signature-verifier.js +73 -76
  69. package/srv/blockchain/signing/submission-finalizer.d.ts +9 -36
  70. package/srv/blockchain/signing/submission-finalizer.js +16 -49
  71. package/srv/blockchain/stored-chain.d.ts +33 -0
  72. package/srv/blockchain/stored-chain.js +234 -0
  73. package/srv/blockchain/transaction-building/buildooor-tx.d.ts +111 -173
  74. package/srv/blockchain/transaction-building/buildooor-tx.js +649 -326
  75. package/srv/blockchain/transaction-building/caller-datums.d.ts +29 -0
  76. package/srv/blockchain/transaction-building/caller-datums.js +139 -0
  77. package/srv/blockchain/transaction-building/cardano-tx.d.ts +10 -35
  78. package/srv/blockchain/transaction-rows.d.ts +12 -0
  79. package/srv/blockchain/transaction-rows.js +43 -0
  80. package/srv/blockchain/wallet-worker/build-request.d.ts +4 -17
  81. package/srv/blockchain/wallet-worker/build-request.js +18 -23
  82. package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +25 -17
  83. package/srv/blockchain/wallet-worker/confirmation-tracker.js +87 -41
  84. package/srv/blockchain/wallet-worker/index.js +2 -8
  85. package/srv/blockchain/wallet-worker/job-store.d.ts +36 -103
  86. package/srv/blockchain/wallet-worker/job-store.js +0 -0
  87. package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +4 -20
  88. package/srv/blockchain/wallet-worker/lease-heartbeat.js +4 -20
  89. package/srv/blockchain/wallet-worker/signers.d.ts +6 -19
  90. package/srv/blockchain/wallet-worker/signers.js +3 -8
  91. package/srv/blockchain/wallet-worker/wallet-worker.d.ts +15 -71
  92. package/srv/blockchain/wallet-worker/wallet-worker.js +40 -91
  93. package/srv/cardano-agent-service.cds +146 -0
  94. package/srv/cardano-agent-service.d.ts +2 -0
  95. package/srv/cardano-agent-service.js +12 -0
  96. package/srv/cardano-backend-service.cds +52 -0
  97. package/srv/cardano-backend-service.d.ts +2 -0
  98. package/srv/cardano-backend-service.js +244 -0
  99. package/srv/cardano-indexer-service.cds +201 -73
  100. package/srv/cardano-indexer-service.js +280 -11
  101. package/srv/cardano-service.cds +65 -2
  102. package/srv/cardano-service.js +170 -129
  103. package/srv/cardano-sign-service.cds +200 -180
  104. package/srv/cardano-sign-service.js +57 -184
  105. package/srv/cardano-tx-service.cds +439 -370
  106. package/srv/cardano-tx-service.js +231 -190
  107. package/srv/cardano-ui.cds +16 -16
  108. package/srv/cardano-worker-service.js +7 -15
  109. package/srv/cbor/index.d.ts +1 -1
  110. package/srv/cbor/parse.d.ts +27 -0
  111. package/srv/cbor/parse.js +63 -2
  112. package/srv/server.d.ts +28 -86
  113. package/srv/server.js +132 -135
  114. package/srv/utils/agent-grants-config.d.ts +37 -0
  115. package/srv/utils/agent-grants-config.js +138 -0
  116. package/srv/utils/agent-grants.d.ts +262 -0
  117. package/srv/utils/agent-grants.js +922 -0
  118. package/srv/utils/agent-token-auth.d.ts +16 -0
  119. package/srv/utils/agent-token-auth.js +78 -0
  120. package/srv/utils/backend-request-handler.d.ts +2 -16
  121. package/srv/utils/backend-request-handler.js +3 -18
  122. package/srv/utils/collections.d.ts +4 -8
  123. package/srv/utils/collections.js +4 -8
  124. package/srv/utils/const.d.ts +30 -67
  125. package/srv/utils/const.js +32 -72
  126. package/srv/utils/crypto.d.ts +3 -12
  127. package/srv/utils/crypto.js +5 -18
  128. package/srv/utils/db-indexes.d.ts +25 -0
  129. package/srv/utils/db-indexes.js +94 -0
  130. package/srv/utils/db-sanitize.d.ts +27 -0
  131. package/srv/utils/db-sanitize.js +85 -0
  132. package/srv/utils/epoch-slots.d.ts +20 -0
  133. package/srv/utils/epoch-slots.js +43 -0
  134. package/srv/utils/error-codes.d.ts +20 -73
  135. package/srv/utils/error-codes.js +19 -70
  136. package/srv/utils/errors.d.ts +50 -177
  137. package/srv/utils/errors.js +94 -218
  138. package/srv/utils/legacy-service-paths.d.ts +11 -0
  139. package/srv/utils/legacy-service-paths.js +58 -0
  140. package/srv/utils/liveness.d.ts +16 -0
  141. package/srv/utils/liveness.js +40 -0
  142. package/srv/utils/mappers.d.ts +128 -214
  143. package/srv/utils/mappers.js +354 -299
  144. package/srv/utils/network-context.js +2 -11
  145. package/srv/utils/pg-inline-json.d.ts +8 -0
  146. package/srv/utils/pg-inline-json.js +62 -0
  147. package/srv/utils/pg-order-nulls.d.ts +14 -0
  148. package/srv/utils/pg-order-nulls.js +56 -0
  149. package/srv/utils/plutus-placeholders.d.ts +23 -16
  150. package/srv/utils/plutus-placeholders.js +63 -27
  151. package/srv/utils/rate-limiter.d.ts +46 -0
  152. package/srv/utils/rate-limiter.js +116 -0
  153. package/srv/utils/service-events.d.ts +2 -22
  154. package/srv/utils/service-events.js +3 -23
  155. package/srv/utils/signing-helper.d.ts +6 -16
  156. package/srv/utils/signing-helper.js +36 -60
  157. package/srv/utils/tx-build-helper.d.ts +28 -93
  158. package/srv/utils/tx-build-helper.js +75 -124
  159. package/srv/utils/tx-request-parsers.d.ts +101 -28
  160. package/srv/utils/tx-request-parsers.js +410 -43
  161. package/srv/utils/tx-utils.d.ts +4 -23
  162. package/srv/utils/tx-utils.js +12 -41
  163. package/srv/utils/types.d.ts +252 -179
  164. package/srv/utils/types.js +2 -6
  165. package/srv/utils/validators.d.ts +24 -90
  166. package/srv/utils/validators.js +50 -147
@@ -1,21 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_EXTRA_OUTPUTS = void 0;
3
+ exports.MAX_CERTIFICATES = exports.MAX_WITHDRAWALS = exports.MAX_SCRIPT_INPUTS = exports.MAX_EXTRA_OUTPUTS = void 0;
4
+ exports.parsePlutusCbor = parsePlutusCbor;
4
5
  exports.parseUtxoRefArray = parseUtxoRefArray;
5
6
  exports.parseRequiredSigners = parseRequiredSigners;
6
7
  exports.parseAssetsArray = parseAssetsArray;
7
8
  exports.parseExtraOutputs = parseExtraOutputs;
9
+ exports.parseOutputList = parseOutputList;
8
10
  exports.parseMintActionPolicyFields = parseMintActionPolicyFields;
11
+ exports.parseScriptInputs = parseScriptInputs;
12
+ exports.parseWithdrawals = parseWithdrawals;
13
+ exports.parseCertificates = parseCertificates;
14
+ exports.parsePolicyMintActions = parsePolicyMintActions;
9
15
  const validators_1 = require("./validators");
16
+ const cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
17
+ const plutus_data_1 = require("@harmoniclabs/plutus-data");
18
+ const const_1 = require("./const");
10
19
  /**
11
- * Shared parsers for the Build*-action JSON payload fields. Used by the
12
- * synchronous CardanoTransactionService handlers AND the wallet-worker's
13
- * request transformation (srv/blockchain/wallet-worker/build-request.ts) so
14
- * both paths accept exactly the same payload shape.
15
- *
16
- * Result contract: { parsed } on success (undefined for absent/empty input →
17
- * no-op) or { error } with a caller-presentable message.
20
+ * Shared parsers for the Build*-action JSON payload fields (transaction service and wallet worker).
21
+ * Result contract: `{ parsed }` on success (undefined for absent/empty input) or `{ error }` with a client message.
18
22
  */
23
+ /** PlutusData as CBOR hex, checked to decode; `field` names it in the error. */
24
+ function parsePlutusCbor(value, field) {
25
+ if (typeof value !== 'string' || !(0, validators_1.isValidCbor)(value))
26
+ return { error: `${field} must be even-length CBOR hex` };
27
+ try {
28
+ (0, plutus_data_1.dataFromCbor)(value);
29
+ }
30
+ catch (err) {
31
+ return { error: `${field} is not PlutusData CBOR: ${err instanceof Error ? err.message : String(err)}` };
32
+ }
33
+ return { hex: value.toLowerCase() };
34
+ }
19
35
  function parseUtxoRefArray(json, fieldName) {
20
36
  if (!json)
21
37
  return { parsed: undefined };
@@ -44,10 +60,7 @@ function parseUtxoRefArray(json, fieldName) {
44
60
  }
45
61
  return { parsed: refs };
46
62
  }
47
- /**
48
- * Parse and validate requiredSignersJson (array of 56-hex Ed25519 key hashes).
49
- * Same result contract as the other parse* helpers.
50
- */
63
+ /** Parse and validate requiredSignersJson (array of 56-hex Ed25519 key hashes). */
51
64
  function parseRequiredSigners(requiredSignersJson) {
52
65
  if (!requiredSignersJson)
53
66
  return { parsed: undefined };
@@ -61,11 +74,7 @@ function parseRequiredSigners(requiredSignersJson) {
61
74
  return { error: err instanceof Error ? err.message : String(err) };
62
75
  }
63
76
  }
64
- /**
65
- * Parse and validate an assetsJson array ({unit, quantity} entries) with the same
66
- * per-entry strictness as parseExtraOutputs — unchecked entries previously flowed
67
- * into the builder and surfaced as 500s.
68
- */
77
+ /** Parse and validate an assetsJson array of `{unit, quantity}` entries. */
69
78
  function parseAssetsArray(assetsJson, fieldName) {
70
79
  if (!assetsJson)
71
80
  return { parsed: undefined };
@@ -92,51 +101,56 @@ function parseAssetsArray(assetsJson, fieldName) {
92
101
  }
93
102
  /** Upper bound on extra outputs per transaction (defence against tx-size blow-up). */
94
103
  exports.MAX_EXTRA_OUTPUTS = 32;
104
+ /** Parse and validate extraOutputsJson (undefined for an empty array). */
105
+ function parseExtraOutputs(extraOutputsJson) {
106
+ return parseOutputList(extraOutputsJson, 'extraOutputsJson');
107
+ }
95
108
  /**
96
- * Parse and validate extraOutputsJson. Returns { parsed } on success (possibly undefined
97
- * for empty array → no-op) or { error } on validation failure.
109
+ * Parse an output list. `outputsJson` (BuildPlutusTransaction) additionally accepts `datumHash`;
110
+ * the entries keep their order, each is min-ADA checked by the builder.
98
111
  */
99
- function parseExtraOutputs(extraOutputsJson) {
100
- if (!extraOutputsJson)
112
+ function parseOutputList(json, fieldName) {
113
+ if (!json)
101
114
  return { parsed: undefined };
102
- const jsonResult = (0, validators_1.validateJsonWithLimits)(extraOutputsJson, 'extraOutputsJson');
115
+ const name = fieldName.replace(/Json$/, '');
116
+ const jsonResult = (0, validators_1.validateJsonWithLimits)(json, fieldName);
103
117
  if (!jsonResult.valid)
104
118
  return { error: jsonResult.error };
105
119
  if (!Array.isArray(jsonResult.parsed))
106
- return { error: 'extraOutputsJson must be a JSON array' };
120
+ return { error: `${fieldName} must be a JSON array` };
107
121
  if (jsonResult.parsed.length === 0)
108
122
  return { parsed: undefined };
109
123
  if (jsonResult.parsed.length > exports.MAX_EXTRA_OUTPUTS) {
110
- return { error: `extraOutputsJson exceeds maximum of ${exports.MAX_EXTRA_OUTPUTS} entries` };
124
+ return { error: `${fieldName} exceeds maximum of ${exports.MAX_EXTRA_OUTPUTS} entries` };
111
125
  }
112
126
  const out = [];
113
127
  for (let i = 0; i < jsonResult.parsed.length; i++) {
114
128
  const entry = jsonResult.parsed[i];
115
129
  if (!entry || typeof entry !== 'object') {
116
- return { error: `extraOutputs[${i}] must be an object` };
130
+ return { error: `${name}[${i}] must be an object` };
117
131
  }
118
132
  if (typeof entry.address !== 'string' || !(0, validators_1.isValidBech32Address)(entry.address)) {
119
- return { error: `extraOutputs[${i}].address is not a valid Bech32 address` };
133
+ return { error: `${name}[${i}].address is not a valid Bech32 address` };
120
134
  }
121
135
  if (typeof entry.lovelaceAmount !== 'string' || !/^\d+$/.test(entry.lovelaceAmount) || entry.lovelaceAmount === '0') {
122
- return { error: `extraOutputs[${i}].lovelaceAmount must be a positive integer string` };
136
+ return { error: `${name}[${i}].lovelaceAmount must be a positive integer string` };
123
137
  }
124
138
  let assets;
125
139
  if (entry.assets !== undefined && entry.assets !== null) {
126
140
  if (!Array.isArray(entry.assets)) {
127
- return { error: `extraOutputs[${i}].assets must be an array` };
141
+ return { error: `${name}[${i}].assets must be an array` };
128
142
  }
129
143
  assets = [];
130
144
  for (let j = 0; j < entry.assets.length; j++) {
131
145
  const a = entry.assets[j];
132
146
  if (!a || typeof a !== 'object') {
133
- return { error: `extraOutputs[${i}].assets[${j}] must be an object` };
147
+ return { error: `${name}[${i}].assets[${j}] must be an object` };
134
148
  }
135
149
  if (typeof a.unit !== 'string' || a.unit.toLowerCase() === 'lovelace' || !(0, validators_1.isAssetUnit)(a.unit)) {
136
- return { error: `extraOutputs[${i}].assets[${j}].unit must be a valid asset unit (policyId + assetName hex)` };
150
+ return { error: `${name}[${i}].assets[${j}].unit must be a valid asset unit (policyId + assetName hex)` };
137
151
  }
138
152
  if (typeof a.quantity !== 'string' || !/^\d+$/.test(a.quantity) || a.quantity === '0') {
139
- return { error: `extraOutputs[${i}].assets[${j}].quantity must be a positive integer string` };
153
+ return { error: `${name}[${i}].assets[${j}].quantity must be a positive integer string` };
140
154
  }
141
155
  assets.push({ unit: a.unit, quantity: a.quantity });
142
156
  }
@@ -144,17 +158,39 @@ function parseExtraOutputs(extraOutputsJson) {
144
158
  let inlineDatum;
145
159
  if (entry.inlineDatumJson !== undefined && entry.inlineDatumJson !== null) {
146
160
  if (typeof entry.inlineDatumJson !== 'string') {
147
- return { error: `extraOutputs[${i}].inlineDatumJson must be a JSON string` };
161
+ return { error: `${name}[${i}].inlineDatumJson must be a JSON string` };
148
162
  }
149
- const datumResult = (0, validators_1.validateJsonWithLimits)(entry.inlineDatumJson, `extraOutputs[${i}].inlineDatumJson`);
163
+ const datumResult = (0, validators_1.validatePlutusJson)(entry.inlineDatumJson, `${name}[${i}].inlineDatumJson`);
150
164
  if (!datumResult.valid)
151
165
  return { error: datumResult.error };
152
166
  inlineDatum = datumResult.parsed;
153
167
  }
168
+ let inlineDatumCbor;
169
+ if (entry.inlineDatumCbor !== undefined && entry.inlineDatumCbor !== null) {
170
+ if (fieldName !== 'outputsJson')
171
+ return { error: `${name}[${i}].inlineDatumCbor is only supported in outputsJson` };
172
+ if (inlineDatum !== undefined)
173
+ return { error: `${name}[${i}] takes inlineDatumJson or inlineDatumCbor, not both` };
174
+ const cbor = parsePlutusCbor(entry.inlineDatumCbor, `${name}[${i}].inlineDatumCbor`);
175
+ if (cbor.error)
176
+ return { error: cbor.error };
177
+ inlineDatumCbor = cbor.hex;
178
+ }
179
+ let datumHash;
180
+ if (entry.datumHash !== undefined && entry.datumHash !== null) {
181
+ if (fieldName !== 'outputsJson')
182
+ return { error: `${name}[${i}].datumHash is only supported in outputsJson` };
183
+ if (typeof entry.datumHash !== 'string' || !/^[0-9a-fA-F]{64}$/.test(entry.datumHash)) {
184
+ return { error: `${name}[${i}].datumHash must be 64 hex chars` };
185
+ }
186
+ if (inlineDatum !== undefined || inlineDatumCbor !== undefined)
187
+ return { error: `${name}[${i}] takes an inline datum or datumHash, not both` };
188
+ datumHash = entry.datumHash.toLowerCase();
189
+ }
154
190
  let referenceScript;
155
191
  if (entry.referenceScriptHex !== undefined && entry.referenceScriptHex !== null) {
156
192
  if (typeof entry.referenceScriptHex !== 'string' || !(0, validators_1.isValidCbor)(entry.referenceScriptHex)) {
157
- return { error: `extraOutputs[${i}].referenceScriptHex must be even-length hex` };
193
+ return { error: `${name}[${i}].referenceScriptHex must be even-length hex` };
158
194
  }
159
195
  referenceScript = entry.referenceScriptHex;
160
196
  }
@@ -163,20 +199,18 @@ function parseExtraOutputs(extraOutputsJson) {
163
199
  lovelaceAmount: entry.lovelaceAmount,
164
200
  assets,
165
201
  inlineDatum,
202
+ ...(datumHash ? { datumHash } : {}),
203
+ ...(inlineDatumCbor ? { inlineDatumCbor } : {}),
166
204
  referenceScript,
167
205
  });
168
206
  }
169
207
  return { parsed: out };
170
208
  }
171
209
  /**
172
- * Parse the optional PER-ACTION policy fields of a mintActionsJson entry
173
- * (multi-policy mint FR): `mintingPolicyScript` (CBOR hex, applied as-is; a
174
- * parameterized per-action script must be pre-applied by the caller) and
175
- * `redeemerJson` (a JSON-encoded string, same convention as extraOutputs'
176
- * inlineDatumJson). Absent fields fall back to the action's top-level
177
- * script/redeemer at build time.
210
+ * Parse the optional per-action policy fields of a mintActionsJson entry: `mintingPolicyScript` (CBOR hex,
211
+ * applied as-is) and `redeemerJson` (JSON string). Absent fields fall back to the top-level script/redeemer.
178
212
  */
179
- function parseMintActionPolicyFields(entry, i) {
213
+ function parseMintActionPolicyFields(entry, i, byReference = false) {
180
214
  let script;
181
215
  if (entry.mintingPolicyScript !== undefined && entry.mintingPolicyScript !== null) {
182
216
  if (typeof entry.mintingPolicyScript !== 'string' || !(0, validators_1.isValidCbor)(entry.mintingPolicyScript)) {
@@ -189,14 +223,347 @@ function parseMintActionPolicyFields(entry, i) {
189
223
  if (typeof entry.redeemerJson !== 'string') {
190
224
  return { error: `mintActions[${i}].redeemerJson must be a JSON string` };
191
225
  }
192
- const jsonResult = (0, validators_1.validateJsonWithLimits)(entry.redeemerJson, `mintActions[${i}].redeemerJson`);
226
+ const jsonResult = (0, validators_1.validatePlutusJson)(entry.redeemerJson, `mintActions[${i}].redeemerJson`);
193
227
  if (!jsonResult.valid)
194
228
  return { error: jsonResult.error };
195
229
  redeemer = jsonResult.parsed;
196
230
  }
197
- if (redeemer !== undefined && script === undefined) {
231
+ if (redeemer !== undefined && script === undefined && !byReference) {
198
232
  return { error: `mintActions[${i}].redeemerJson requires mintActions[${i}].mintingPolicyScript` };
199
233
  }
200
234
  return { script, redeemer };
201
235
  }
236
+ /** Upper bound on script inputs per BuildPlutusTransaction. */
237
+ exports.MAX_SCRIPT_INPUTS = 16;
238
+ /** Parse and validate scriptInputsJson: 0..MAX_SCRIPT_INPUTS distinct script UTxOs, each with its own redeemer. */
239
+ function parseScriptInputs(json) {
240
+ if (!json)
241
+ return { parsed: [] };
242
+ const jsonResult = (0, validators_1.validateJsonWithLimits)(json, 'scriptInputsJson');
243
+ if (!jsonResult.valid)
244
+ return { error: jsonResult.error };
245
+ if (!Array.isArray(jsonResult.parsed)) {
246
+ return { error: 'scriptInputsJson must be a JSON array' };
247
+ }
248
+ if (jsonResult.parsed.length > exports.MAX_SCRIPT_INPUTS) {
249
+ return { error: `scriptInputsJson exceeds maximum of ${exports.MAX_SCRIPT_INPUTS} entries` };
250
+ }
251
+ const parseJsonField = (value, field) => {
252
+ if (typeof value !== 'string')
253
+ return { error: `${field} must be a JSON string` };
254
+ const r = (0, validators_1.validatePlutusJson)(value, field);
255
+ return r.valid ? { parsed: r.parsed } : { error: r.error };
256
+ };
257
+ const out = [];
258
+ const seen = new Set();
259
+ for (let i = 0; i < jsonResult.parsed.length; i++) {
260
+ const entry = jsonResult.parsed[i];
261
+ const at = `scriptInputs[${i}]`;
262
+ if (!entry || typeof entry !== 'object')
263
+ return { error: `${at} must be an object` };
264
+ if (typeof entry.txHash !== 'string' || !(0, validators_1.isTxHash)(entry.txHash))
265
+ return { error: `${at}.txHash must be 64 hex chars` };
266
+ const idx = entry.outputIndex;
267
+ if (typeof idx !== 'number' || !Number.isInteger(idx) || idx < 0)
268
+ return { error: `${at}.outputIndex must be a non-negative integer` };
269
+ const key = `${entry.txHash.toLowerCase()}#${idx}`;
270
+ if (seen.has(key))
271
+ return { error: `${at} spends ${key} a second time` };
272
+ seen.add(key);
273
+ const hasInline = entry.validatorScript !== undefined && entry.validatorScript !== null;
274
+ const hasRef = entry.referenceScript !== undefined && entry.referenceScript !== null;
275
+ if (hasInline === hasRef)
276
+ return { error: `${at} needs exactly one of validatorScript or referenceScript` };
277
+ const input = { txHash: entry.txHash.toLowerCase(), outputIndex: idx, redeemer: null };
278
+ if (hasInline) {
279
+ if (typeof entry.validatorScript !== 'string' || !(0, validators_1.isValidCbor)(entry.validatorScript)) {
280
+ return { error: `${at}.validatorScript must be even-length CBOR hex` };
281
+ }
282
+ input.validatorScript = entry.validatorScript;
283
+ if (entry.scriptParamsJson !== undefined && entry.scriptParamsJson !== null) {
284
+ const params = parseJsonField(entry.scriptParamsJson, `${at}.scriptParamsJson`);
285
+ if (params.error)
286
+ return { error: params.error };
287
+ if (!Array.isArray(params.parsed))
288
+ return { error: `${at}.scriptParamsJson must be a JSON array` };
289
+ if (params.parsed.length > 0)
290
+ input.scriptParams = params.parsed;
291
+ }
292
+ }
293
+ else {
294
+ const ref = entry.referenceScript;
295
+ if (!ref || typeof ref !== 'object' || typeof ref.txHash !== 'string' || !(0, validators_1.isTxHash)(ref.txHash)
296
+ || typeof ref.outputIndex !== 'number' || !Number.isInteger(ref.outputIndex) || ref.outputIndex < 0) {
297
+ return { error: `${at}.referenceScript must be {txHash, outputIndex} of the UTxO carrying the script` };
298
+ }
299
+ if (entry.scriptParamsJson !== undefined && entry.scriptParamsJson !== null) {
300
+ return { error: `${at}.scriptParamsJson applies to an inline validatorScript only` };
301
+ }
302
+ input.referenceScript = { txHash: ref.txHash.toLowerCase(), outputIndex: ref.outputIndex };
303
+ }
304
+ const hasRedeemerJson = entry.redeemerJson !== undefined && entry.redeemerJson !== null;
305
+ const hasRedeemerCbor = entry.redeemerCbor !== undefined && entry.redeemerCbor !== null;
306
+ if (hasRedeemerJson === hasRedeemerCbor)
307
+ return { error: `${at} needs exactly one of redeemerJson or redeemerCbor` };
308
+ if (hasRedeemerCbor) {
309
+ const cbor = parsePlutusCbor(entry.redeemerCbor, `${at}.redeemerCbor`);
310
+ if (cbor.error)
311
+ return { error: cbor.error };
312
+ input.redeemerCbor = cbor.hex;
313
+ }
314
+ else {
315
+ const redeemer = parseJsonField(entry.redeemerJson, `${at}.redeemerJson`);
316
+ if (redeemer.error)
317
+ return { error: redeemer.error };
318
+ input.redeemer = redeemer.parsed;
319
+ }
320
+ const hasDatumJson = entry.datumJson !== undefined && entry.datumJson !== null;
321
+ const hasDatumCbor = entry.datumCbor !== undefined && entry.datumCbor !== null;
322
+ if (hasDatumJson && hasDatumCbor)
323
+ return { error: `${at} takes datumJson or datumCbor, not both` };
324
+ if (hasDatumCbor) {
325
+ const cbor = parsePlutusCbor(entry.datumCbor, `${at}.datumCbor`);
326
+ if (cbor.error)
327
+ return { error: cbor.error };
328
+ input.datumCbor = cbor.hex;
329
+ }
330
+ else if (hasDatumJson) {
331
+ const datum = parseJsonField(entry.datumJson, `${at}.datumJson`);
332
+ if (datum.error)
333
+ return { error: datum.error };
334
+ input.datum = datum.parsed;
335
+ }
336
+ out.push(input);
337
+ }
338
+ return { parsed: out };
339
+ }
340
+ /** Upper bound on withdrawals per BuildPlutusTransaction. */
341
+ exports.MAX_WITHDRAWALS = 16;
342
+ /**
343
+ * Parse and validate withdrawalsJson: distinct reward accounts, lovelace >= 0, optionally a staking
344
+ * script (inline or by reference) with exactly one redeemer form; without a script no redeemer.
345
+ */
346
+ function parseWithdrawals(json) {
347
+ if (!json)
348
+ return { parsed: undefined };
349
+ const jsonResult = (0, validators_1.validateJsonWithLimits)(json, 'withdrawalsJson');
350
+ if (!jsonResult.valid)
351
+ return { error: jsonResult.error };
352
+ if (!Array.isArray(jsonResult.parsed))
353
+ return { error: 'withdrawalsJson must be a JSON array' };
354
+ if (jsonResult.parsed.length === 0)
355
+ return { parsed: undefined };
356
+ if (jsonResult.parsed.length > exports.MAX_WITHDRAWALS) {
357
+ return { error: `withdrawalsJson exceeds maximum of ${exports.MAX_WITHDRAWALS} entries` };
358
+ }
359
+ const out = [];
360
+ const seen = new Set();
361
+ for (let i = 0; i < jsonResult.parsed.length; i++) {
362
+ const entry = jsonResult.parsed[i];
363
+ const at = `withdrawals[${i}]`;
364
+ if (!entry || typeof entry !== 'object')
365
+ return { error: `${at} must be an object` };
366
+ if (typeof entry.rewardAddress !== 'string' || !(0, validators_1.isValidBech32StakeAddress)(entry.rewardAddress)) {
367
+ return { error: `${at}.rewardAddress must be a Bech32 stake address` };
368
+ }
369
+ const rewardAddress = entry.rewardAddress.toLowerCase();
370
+ if (seen.has(rewardAddress))
371
+ return { error: `${at} withdraws from ${rewardAddress} a second time` };
372
+ seen.add(rewardAddress);
373
+ const lovelace = typeof entry.lovelace === 'number' && Number.isInteger(entry.lovelace) && entry.lovelace >= 0
374
+ ? String(entry.lovelace)
375
+ : typeof entry.lovelace === 'string' && /^\d+$/.test(entry.lovelace) ? entry.lovelace : undefined;
376
+ if (lovelace === undefined)
377
+ return { error: `${at}.lovelace must be a non-negative integer (0 for withdraw-zero)` };
378
+ const witness = parseStakingScriptWitness(entry, at, 'withdrawal');
379
+ if (witness.error)
380
+ return { error: witness.error };
381
+ out.push({ rewardAddress, lovelace, ...witness.parsed });
382
+ }
383
+ return { parsed: out };
384
+ }
385
+ function parseStakingScriptWitness(entry, at, kind) {
386
+ const present = (v) => v !== undefined && v !== null;
387
+ const parseJsonField = (value, field) => {
388
+ if (typeof value !== 'string')
389
+ return { error: `${field} must be a JSON string` };
390
+ const r = (0, validators_1.validatePlutusJson)(value, field);
391
+ return r.valid ? { parsed: r.parsed } : { error: r.error };
392
+ };
393
+ const w = {};
394
+ const hasInline = present(entry.stakingScript);
395
+ const hasRef = present(entry.referenceScript);
396
+ if (hasInline && hasRef)
397
+ return { error: `${at} takes stakingScript or referenceScript, not both` };
398
+ if (hasInline) {
399
+ if (typeof entry.stakingScript !== 'string' || !(0, validators_1.isValidCbor)(entry.stakingScript)) {
400
+ return { error: `${at}.stakingScript must be even-length CBOR hex` };
401
+ }
402
+ w.stakingScript = entry.stakingScript;
403
+ if (present(entry.scriptParamsJson)) {
404
+ const params = parseJsonField(entry.scriptParamsJson, `${at}.scriptParamsJson`);
405
+ if (params.error)
406
+ return { error: params.error };
407
+ if (!Array.isArray(params.parsed))
408
+ return { error: `${at}.scriptParamsJson must be a JSON array` };
409
+ if (params.parsed.length > 0)
410
+ w.scriptParams = params.parsed;
411
+ }
412
+ }
413
+ else if (hasRef) {
414
+ const ref = entry.referenceScript;
415
+ if (!ref || typeof ref !== 'object' || typeof ref.txHash !== 'string' || !(0, validators_1.isTxHash)(ref.txHash)
416
+ || typeof ref.outputIndex !== 'number' || !Number.isInteger(ref.outputIndex) || ref.outputIndex < 0) {
417
+ return { error: `${at}.referenceScript must be {txHash, outputIndex} of the UTxO carrying the script` };
418
+ }
419
+ if (present(entry.scriptParamsJson))
420
+ return { error: `${at}.scriptParamsJson applies to an inline stakingScript only` };
421
+ w.referenceScript = { txHash: ref.txHash.toLowerCase(), outputIndex: ref.outputIndex };
422
+ }
423
+ else if (present(entry.scriptParamsJson)) {
424
+ return { error: `${at}.scriptParamsJson applies to an inline stakingScript only` };
425
+ }
426
+ const hasRedeemerJson = present(entry.redeemerJson);
427
+ const hasRedeemerCbor = present(entry.redeemerCbor);
428
+ if (hasInline || hasRef) {
429
+ if (hasRedeemerJson === hasRedeemerCbor)
430
+ return { error: `${at} needs exactly one of redeemerJson or redeemerCbor` };
431
+ if (hasRedeemerCbor) {
432
+ const cbor = parsePlutusCbor(entry.redeemerCbor, `${at}.redeemerCbor`);
433
+ if (cbor.error)
434
+ return { error: cbor.error };
435
+ w.redeemerCbor = cbor.hex;
436
+ }
437
+ else {
438
+ const redeemer = parseJsonField(entry.redeemerJson, `${at}.redeemerJson`);
439
+ if (redeemer.error)
440
+ return { error: redeemer.error };
441
+ w.redeemer = redeemer.parsed;
442
+ }
443
+ }
444
+ else if (hasRedeemerJson || hasRedeemerCbor) {
445
+ return { error: `${at} has a redeemer but no staking script; a key-witnessed ${kind} takes none` };
446
+ }
447
+ return { parsed: w };
448
+ }
449
+ /** Upper bound on certificates per BuildPlutusTransaction. */
450
+ exports.MAX_CERTIFICATES = 16;
451
+ /**
452
+ * Parse and validate certificatesJson: stake registrations and deregistrations, each with an
453
+ * optional staking script (inline or by reference) and exactly one redeemer form when scripted.
454
+ */
455
+ function parseCertificates(json) {
456
+ if (!json)
457
+ return { parsed: undefined };
458
+ const jsonResult = (0, validators_1.validateJsonWithLimits)(json, 'certificatesJson');
459
+ if (!jsonResult.valid)
460
+ return { error: jsonResult.error };
461
+ if (!Array.isArray(jsonResult.parsed))
462
+ return { error: 'certificatesJson must be a JSON array' };
463
+ if (jsonResult.parsed.length === 0)
464
+ return { parsed: undefined };
465
+ if (jsonResult.parsed.length > exports.MAX_CERTIFICATES) {
466
+ return { error: `certificatesJson exceeds maximum of ${exports.MAX_CERTIFICATES} entries` };
467
+ }
468
+ const out = [];
469
+ for (let i = 0; i < jsonResult.parsed.length; i++) {
470
+ const entry = jsonResult.parsed[i];
471
+ const at = `certificates[${i}]`;
472
+ if (!entry || typeof entry !== 'object')
473
+ return { error: `${at} must be an object` };
474
+ if (entry.type !== 'registerStake' && entry.type !== 'deregisterStake') {
475
+ return { error: `${at}.type must be registerStake or deregisterStake` };
476
+ }
477
+ if (typeof entry.stakeAddress !== 'string' || !(0, validators_1.isValidBech32StakeAddress)(entry.stakeAddress)) {
478
+ return { error: `${at}.stakeAddress must be a Bech32 stake address` };
479
+ }
480
+ let deposit;
481
+ if (entry.deposit !== undefined && entry.deposit !== null) {
482
+ deposit = typeof entry.deposit === 'number' && Number.isInteger(entry.deposit) && entry.deposit > 0
483
+ ? String(entry.deposit)
484
+ : typeof entry.deposit === 'string' && /^\d+$/.test(entry.deposit) && entry.deposit !== '0' ? entry.deposit : undefined;
485
+ if (deposit === undefined)
486
+ return { error: `${at}.deposit must be a positive integer (lovelace)` };
487
+ }
488
+ const witness = parseStakingScriptWitness(entry, at, 'certificate');
489
+ if (witness.error)
490
+ return { error: witness.error };
491
+ out.push({ type: entry.type, stakeAddress: entry.stakeAddress.toLowerCase(), ...(deposit ? { deposit } : {}), ...witness.parsed });
492
+ }
493
+ return { parsed: out };
494
+ }
495
+ /**
496
+ * Parse mintActionsJson for BuildPlutusTransaction: every action carries its own `mintingPolicyScript`
497
+ * or `referenceScript` (no request-level default) and optional `redeemerJson`; `assetUnit` is
498
+ * policyId+assetName, or with an inline script a bare assetName prefixed with the script's policy id.
499
+ */
500
+ function parsePolicyMintActions(json) {
501
+ if (!json)
502
+ return { parsed: undefined };
503
+ const jsonResult = (0, validators_1.validateJsonWithLimits)(json, 'mintActionsJson');
504
+ if (!jsonResult.valid)
505
+ return { error: jsonResult.error };
506
+ if (!Array.isArray(jsonResult.parsed))
507
+ return { error: 'mintActionsJson must be a JSON array' };
508
+ if (jsonResult.parsed.length === 0)
509
+ return { parsed: undefined };
510
+ const out = [];
511
+ for (let i = 0; i < jsonResult.parsed.length; i++) {
512
+ const entry = jsonResult.parsed[i];
513
+ if (!entry || typeof entry !== 'object')
514
+ return { error: `mintActions[${i}] must be an object` };
515
+ if (typeof entry.quantity !== 'string' || !/^-?\d+$/.test(entry.quantity) || BigInt(entry.quantity) === 0n) {
516
+ return { error: `mintActions[${i}].quantity must be a non-zero integer string` };
517
+ }
518
+ const present = (v) => v !== undefined && v !== null;
519
+ const hasRef = present(entry.referenceScript);
520
+ if (present(entry.mintingPolicyScript) === hasRef) {
521
+ return { error: `mintActions[${i}] needs exactly one of mintingPolicyScript or referenceScript` };
522
+ }
523
+ const policy = parseMintActionPolicyFields(entry, i, hasRef);
524
+ if (policy.error)
525
+ return { error: policy.error };
526
+ if (typeof entry.assetUnit !== 'string' || !/^[0-9a-fA-F]*$/.test(entry.assetUnit) || entry.assetUnit.length % 2 !== 0) {
527
+ return { error: `mintActions[${i}].assetUnit must be hex (policyId+assetName, or a bare assetName)` };
528
+ }
529
+ let assetUnit = entry.assetUnit.toLowerCase();
530
+ let referenceScript;
531
+ if (hasRef) {
532
+ const ref = entry.referenceScript;
533
+ if (!ref || typeof ref !== 'object' || typeof ref.txHash !== 'string' || !(0, validators_1.isTxHash)(ref.txHash)
534
+ || typeof ref.outputIndex !== 'number' || !Number.isInteger(ref.outputIndex) || ref.outputIndex < 0) {
535
+ return { error: `mintActions[${i}].referenceScript must be {txHash, outputIndex} of the UTxO carrying the policy` };
536
+ }
537
+ referenceScript = { txHash: ref.txHash.toLowerCase(), outputIndex: ref.outputIndex };
538
+ // the policy id is known only once the builder has read the reference script
539
+ if (assetUnit.length < const_1.MIN_FULL_ASSET_UNIT_LENGTH) {
540
+ return { error: `mintActions[${i}].assetUnit must be policyId+assetName when the policy is a referenceScript` };
541
+ }
542
+ }
543
+ else {
544
+ let policyId;
545
+ try {
546
+ policyId = cardano_ledger_ts_1.Script.fromCbor(Buffer.from(policy.script, 'hex')).hash.toString();
547
+ }
548
+ catch (err) {
549
+ return { error: `mintActions[${i}].mintingPolicyScript is not a valid Plutus script: ${err instanceof Error ? err.message : String(err)}` };
550
+ }
551
+ if (assetUnit.length < const_1.MIN_FULL_ASSET_UNIT_LENGTH) {
552
+ assetUnit = policyId + assetUnit;
553
+ }
554
+ else if (!assetUnit.startsWith(policyId)) {
555
+ return { error: `mintActions[${i}].assetUnit does not start with its policy id ${policyId}` };
556
+ }
557
+ }
558
+ if (!(0, validators_1.isAssetUnit)(assetUnit))
559
+ return { error: `mintActions[${i}].assetUnit is not a valid asset unit` };
560
+ out.push({
561
+ assetUnit,
562
+ quantity: BigInt(entry.quantity),
563
+ ...(policy.script ? { mintingPolicyScript: policy.script } : { referenceScript }),
564
+ ...(policy.redeemer !== undefined ? { redeemerJson: policy.redeemer } : {}),
565
+ });
566
+ }
567
+ return { parsed: out };
568
+ }
202
569
  //# sourceMappingURL=tx-request-parsers.js.map
@@ -2,32 +2,13 @@ import cds from '@sap/cds';
2
2
  /** Default acquire budget for detached bookkeeping transactions. */
3
3
  export declare const DETACHED_TX_TIMEOUT_MS = 10000;
4
4
  /**
5
- * Run `fn` with `cds.context` cleared, so any `db.run(...)` inside it gets a
6
- * fresh short-lived transaction instead of joining a long-lived ambient one.
7
- * Under @cap-js/sqlite (pool.max=1) this is what keeps the single pooled
8
- * connection free during long awaits (NIGHTGATE lesson 2; used by the
9
- * wallet-worker job loops).
5
+ * Run `fn` with `cds.context` cleared so inner `db.run` calls get short-lived transactions instead of
6
+ * joining a long-lived ambient one; keeps the single sqlite connection free during long awaits.
10
7
  */
11
8
  export declare function runWithoutAmbientTx<T>(fn: () => Promise<T>): Promise<T>;
12
9
  /**
13
- * `cds.tx(fn)` with a deadlock guard: a NEW ROOT transaction that fails fast
14
- * instead of hanging forever when no pooled connection becomes available.
15
- *
16
- * Why: the sign-service must commit its submit bookkeeping (claim, finalize,
17
- * failure marker) independently of the caller's request transaction. When an
18
- * in-process consumer awaits the action while its own request transaction
19
- * holds the single pooled sqlite connection, the acquire can never succeed —
20
- * without this guard that is a silent, zero-CPU, infinite hang.
21
- *
22
- * Guarantees:
23
- * - On timeout, throws `BackendError` 503 `ODATANO_NESTED_TX_TIMEOUT` with a
24
- * diagnosis pointing at KNOWN_ISSUES #11.
25
- * - A late connection grant (after the timeout already fired) performs NO
26
- * work: the callback forces the acquire via an explicit `begin()` and
27
- * checks the abort flag right after it, so it rolls back immediately and
28
- * the caller never observes an error for work that then silently happened
29
- * anyway. (A flag check at callback ENTRY would run long before the lazy
30
- * acquire and miss the timeout.)
10
+ * `cds.tx(fn)` as a new root transaction with a deadlock guard: throws BackendError 503
11
+ * `ODATANO_NESTED_TX_TIMEOUT` when no pooled connection arrives in time, and a late grant does no work.
31
12
  */
32
13
  export declare function detachedTx<T>(label: string, fn: (db: cds.Transaction) => Promise<T>, timeoutMs?: number): Promise<T>;
33
14
  //# sourceMappingURL=tx-utils.d.ts.map