@odatano/core 2.0.0-rc.1 → 2.0.0-rc.11
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.
- package/@cds-models/CardanoAgentService/index.d.ts +255 -0
- package/@cds-models/CardanoAgentService/index.js +36 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +37 -2
- package/@cds-models/CardanoIndexerService/index.js +4 -0
- package/@cds-models/CardanoSignService/index.d.ts +5 -2
- package/@cds-models/CardanoTransactionService/index.d.ts +9 -9
- package/@cds-models/odatano/cardano/index.d.ts +86 -0
- package/@cds-models/odatano/cardano/index.js +6 -0
- package/README.md +62 -4
- package/db/schema.cds +105 -0
- package/package.json +9 -3
- package/src/index.d.ts +6 -0
- package/src/index.js +21 -1
- package/src/plugin.js +33 -30
- package/srv/blockchain/backends/blockfrost-backend.js +1 -10
- package/srv/blockchain/backends/koios-backend.js +1 -10
- package/srv/blockchain/backends/ogmios-backend.d.ts +29 -4
- package/srv/blockchain/backends/ogmios-backend.js +96 -7
- package/srv/blockchain/backends/ogmios-frame-guard.d.ts +77 -0
- package/srv/blockchain/backends/ogmios-frame-guard.js +181 -0
- package/srv/blockchain/cardano-indexer.d.ts +18 -0
- package/srv/blockchain/cardano-indexer.js +58 -3
- package/srv/blockchain/crawler/crawler.d.ts +55 -0
- package/srv/blockchain/crawler/crawler.js +153 -3
- package/srv/blockchain/crawler/index.d.ts +9 -0
- package/srv/blockchain/crawler/index.js +37 -4
- package/srv/blockchain/crawler/sync-state.d.ts +13 -1
- package/srv/blockchain/crawler/sync-state.js +24 -2
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +20 -0
- package/srv/blockchain/transaction-building/buildooor-tx.js +104 -20
- package/srv/blockchain/wallet-worker/build-request.js +35 -6
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +32 -1
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +91 -29
- package/srv/blockchain/wallet-worker/job-store.d.ts +11 -0
- package/srv/blockchain/wallet-worker/job-store.js +42 -9
- package/srv/cardano-agent-service.cds +150 -0
- package/srv/cardano-agent-service.d.ts +2 -0
- package/srv/cardano-agent-service.js +16 -0
- package/srv/cardano-indexer-service.cds +36 -3
- package/srv/cardano-indexer-service.js +3 -0
- package/srv/cardano-service.js +57 -3
- package/srv/cardano-sign-service.cds +200 -180
- package/srv/cardano-tx-service.cds +32 -26
- package/srv/cardano-tx-service.js +96 -38
- package/srv/cardano-worker-service.cds +125 -125
- package/srv/server.d.ts +2 -1
- package/srv/server.js +18 -34
- package/srv/utils/agent-grants-config.d.ts +65 -0
- package/srv/utils/agent-grants-config.js +148 -0
- package/srv/utils/agent-grants.d.ts +314 -0
- package/srv/utils/agent-grants.js +821 -0
- package/srv/utils/agent-token-auth.d.ts +19 -0
- package/srv/utils/agent-token-auth.js +81 -0
- package/srv/utils/db-sanitize.d.ts +40 -0
- package/srv/utils/db-sanitize.js +98 -0
- package/srv/utils/errors.d.ts +18 -0
- package/srv/utils/errors.js +24 -1
- package/srv/utils/liveness.d.ts +23 -0
- package/srv/utils/liveness.js +49 -0
- package/srv/utils/mappers.d.ts +27 -0
- package/srv/utils/mappers.js +67 -7
- package/srv/utils/rate-limiter.d.ts +57 -0
- package/srv/utils/rate-limiter.js +126 -0
- package/srv/utils/tx-request-parsers.d.ts +13 -0
- package/srv/utils/tx-request-parsers.js +32 -0
- package/srv/utils/types.d.ts +34 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* In-memory sliding-window rate limiter, ported from NIGHTGATE
|
|
4
|
+
* (srv/utils/rate-limiter.ts). Used for grant administration and for failed
|
|
5
|
+
* agent-token attempts; nothing here touches the database.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.RateLimiter = void 0;
|
|
9
|
+
exports.principalRateKey = principalRateKey;
|
|
10
|
+
/** The group a key belongs to: everything before the first ':' (the principal). */
|
|
11
|
+
function groupOf(key) {
|
|
12
|
+
const i = key.indexOf(':');
|
|
13
|
+
return i < 0 ? key : key.slice(0, i);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Rate-limit key for a CAP request: the principal (user, else client address)
|
|
17
|
+
* plus the scope. Batch parts carry no address and a proxy hides the real one,
|
|
18
|
+
* so the address is the last resort only. The principal is the limiter's GROUP
|
|
19
|
+
* (see maxKeysPerGroup), hence no ':' in it.
|
|
20
|
+
*/
|
|
21
|
+
function principalRateKey(req, scope) {
|
|
22
|
+
const r = req;
|
|
23
|
+
const user = r?.user?.id;
|
|
24
|
+
const ip = r?._?.req?.ip ?? r?.http?.req?.ip ?? r?.ip;
|
|
25
|
+
const principal = user ? `user=${String(user)}`
|
|
26
|
+
: ip ? `ip=${String(ip).replace(/:/g, '.')}`
|
|
27
|
+
: 'anonymous';
|
|
28
|
+
return `${principal}:${scope}`;
|
|
29
|
+
}
|
|
30
|
+
class RateLimiter {
|
|
31
|
+
windowMs;
|
|
32
|
+
maxRequests;
|
|
33
|
+
maxKeys;
|
|
34
|
+
maxKeysPerGroup;
|
|
35
|
+
groupCounts = new Map();
|
|
36
|
+
hits = new Map();
|
|
37
|
+
sweepTimer;
|
|
38
|
+
constructor(opts) {
|
|
39
|
+
this.windowMs = opts.windowMs;
|
|
40
|
+
this.maxRequests = opts.maxRequests;
|
|
41
|
+
this.maxKeys = opts.maxKeys || 10_000;
|
|
42
|
+
this.maxKeysPerGroup = opts.maxKeysPerGroup || 64;
|
|
43
|
+
this.sweepTimer = setInterval(() => this.sweep(), opts.sweepIntervalMs || 60_000);
|
|
44
|
+
if (typeof this.sweepTimer.unref === 'function')
|
|
45
|
+
this.sweepTimer.unref();
|
|
46
|
+
}
|
|
47
|
+
check(key) {
|
|
48
|
+
return this.checkMany(key, 1);
|
|
49
|
+
}
|
|
50
|
+
/** Would ONE more hit fit? Records nothing. */
|
|
51
|
+
peek(key) {
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
const inWindow = (this.hits.get(key) ?? []).filter((t) => t > now - this.windowMs);
|
|
54
|
+
if (inWindow.length < this.maxRequests)
|
|
55
|
+
return { allowed: true, retryAfterMs: 0 };
|
|
56
|
+
const oldest = Math.min(...inWindow);
|
|
57
|
+
return { allowed: false, retryAfterMs: Math.max(oldest + this.windowMs - now, 0) };
|
|
58
|
+
}
|
|
59
|
+
/** Forget every key (tests). */
|
|
60
|
+
reset() {
|
|
61
|
+
this.hits.clear();
|
|
62
|
+
this.groupCounts.clear();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Consume `count` slots atomically: either ALL fit into the window and are
|
|
66
|
+
* recorded, or NONE are (a rejected caller has consumed nothing).
|
|
67
|
+
*/
|
|
68
|
+
checkMany(key, count) {
|
|
69
|
+
if (count <= 0)
|
|
70
|
+
return { allowed: true, retryAfterMs: 0 };
|
|
71
|
+
const now = Date.now();
|
|
72
|
+
const windowStart = now - this.windowMs;
|
|
73
|
+
if (!this.hits.has(key)) {
|
|
74
|
+
const group = groupOf(key);
|
|
75
|
+
if ((this.groupCounts.get(group) ?? 0) >= this.maxKeysPerGroup) {
|
|
76
|
+
return { allowed: false, retryAfterMs: this.windowMs };
|
|
77
|
+
}
|
|
78
|
+
// At capacity a NEW key evicts the least recently used one (the map is
|
|
79
|
+
// insertion ordered and `set` below re-inserts on every hit).
|
|
80
|
+
if (this.hits.size >= this.maxKeys) {
|
|
81
|
+
const oldest = this.hits.keys().next().value;
|
|
82
|
+
if (oldest !== undefined)
|
|
83
|
+
this.dropKey(oldest);
|
|
84
|
+
}
|
|
85
|
+
this.groupCounts.set(group, (this.groupCounts.get(group) ?? 0) + 1);
|
|
86
|
+
}
|
|
87
|
+
let timestamps = this.hits.get(key) || [];
|
|
88
|
+
timestamps = timestamps.filter((t) => t > windowStart);
|
|
89
|
+
this.hits.delete(key); // re-insert below = most recently used
|
|
90
|
+
if (timestamps.length + count > this.maxRequests) {
|
|
91
|
+
timestamps.sort((a, b) => a - b);
|
|
92
|
+
const oldestInWindow = timestamps[0];
|
|
93
|
+
const retryAfterMs = oldestInWindow === undefined ? this.windowMs : oldestInWindow + this.windowMs - now;
|
|
94
|
+
this.hits.set(key, timestamps);
|
|
95
|
+
return { allowed: false, retryAfterMs: Math.max(retryAfterMs, 0) };
|
|
96
|
+
}
|
|
97
|
+
for (let i = 0; i < count; i++)
|
|
98
|
+
timestamps.push(now);
|
|
99
|
+
this.hits.set(key, timestamps);
|
|
100
|
+
return { allowed: true, retryAfterMs: 0 };
|
|
101
|
+
}
|
|
102
|
+
dropKey(key) {
|
|
103
|
+
if (!this.hits.delete(key))
|
|
104
|
+
return;
|
|
105
|
+
const group = groupOf(key);
|
|
106
|
+
const n = (this.groupCounts.get(group) ?? 1) - 1;
|
|
107
|
+
if (n <= 0)
|
|
108
|
+
this.groupCounts.delete(group);
|
|
109
|
+
else
|
|
110
|
+
this.groupCounts.set(group, n);
|
|
111
|
+
}
|
|
112
|
+
/** Remove keys with no hits within the current window. */
|
|
113
|
+
sweep() {
|
|
114
|
+
const windowStart = Date.now() - this.windowMs;
|
|
115
|
+
for (const [key, timestamps] of this.hits) {
|
|
116
|
+
if (!timestamps.some((t) => t > windowStart))
|
|
117
|
+
this.dropKey(key);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** Stop the background sweep timer. */
|
|
121
|
+
destroy() {
|
|
122
|
+
clearInterval(this.sweepTimer);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.RateLimiter = RateLimiter;
|
|
126
|
+
//# sourceMappingURL=rate-limiter.js.map
|
|
@@ -55,4 +55,17 @@ export declare function parseExtraOutputs(extraOutputsJson: string | undefined):
|
|
|
55
55
|
parsed?: ParsedExtraOutput[];
|
|
56
56
|
error?: string;
|
|
57
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* Parse the optional PER-ACTION policy fields of a mintActionsJson entry
|
|
60
|
+
* (multi-policy mint FR): `mintingPolicyScript` (CBOR hex, applied as-is; a
|
|
61
|
+
* parameterized per-action script must be pre-applied by the caller) and
|
|
62
|
+
* `redeemerJson` (a JSON-encoded string, same convention as extraOutputs'
|
|
63
|
+
* inlineDatumJson). Absent fields fall back to the action's top-level
|
|
64
|
+
* script/redeemer at build time.
|
|
65
|
+
*/
|
|
66
|
+
export declare function parseMintActionPolicyFields(entry: Record<string, unknown>, i: number): {
|
|
67
|
+
script?: string;
|
|
68
|
+
redeemer?: JSONValue;
|
|
69
|
+
error?: string;
|
|
70
|
+
};
|
|
58
71
|
//# sourceMappingURL=tx-request-parsers.d.ts.map
|
|
@@ -5,6 +5,7 @@ exports.parseUtxoRefArray = parseUtxoRefArray;
|
|
|
5
5
|
exports.parseRequiredSigners = parseRequiredSigners;
|
|
6
6
|
exports.parseAssetsArray = parseAssetsArray;
|
|
7
7
|
exports.parseExtraOutputs = parseExtraOutputs;
|
|
8
|
+
exports.parseMintActionPolicyFields = parseMintActionPolicyFields;
|
|
8
9
|
const validators_1 = require("./validators");
|
|
9
10
|
/**
|
|
10
11
|
* Shared parsers for the Build*-action JSON payload fields. Used by the
|
|
@@ -167,4 +168,35 @@ function parseExtraOutputs(extraOutputsJson) {
|
|
|
167
168
|
}
|
|
168
169
|
return { parsed: out };
|
|
169
170
|
}
|
|
171
|
+
/**
|
|
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.
|
|
178
|
+
*/
|
|
179
|
+
function parseMintActionPolicyFields(entry, i) {
|
|
180
|
+
let script;
|
|
181
|
+
if (entry.mintingPolicyScript !== undefined && entry.mintingPolicyScript !== null) {
|
|
182
|
+
if (typeof entry.mintingPolicyScript !== 'string' || !(0, validators_1.isValidCbor)(entry.mintingPolicyScript)) {
|
|
183
|
+
return { error: `mintActions[${i}].mintingPolicyScript must be even-length CBOR hex` };
|
|
184
|
+
}
|
|
185
|
+
script = entry.mintingPolicyScript;
|
|
186
|
+
}
|
|
187
|
+
let redeemer;
|
|
188
|
+
if (entry.redeemerJson !== undefined && entry.redeemerJson !== null) {
|
|
189
|
+
if (typeof entry.redeemerJson !== 'string') {
|
|
190
|
+
return { error: `mintActions[${i}].redeemerJson must be a JSON string` };
|
|
191
|
+
}
|
|
192
|
+
const jsonResult = (0, validators_1.validateJsonWithLimits)(entry.redeemerJson, `mintActions[${i}].redeemerJson`);
|
|
193
|
+
if (!jsonResult.valid)
|
|
194
|
+
return { error: jsonResult.error };
|
|
195
|
+
redeemer = jsonResult.parsed;
|
|
196
|
+
}
|
|
197
|
+
if (redeemer !== undefined && script === undefined) {
|
|
198
|
+
return { error: `mintActions[${i}].redeemerJson requires mintActions[${i}].mintingPolicyScript` };
|
|
199
|
+
}
|
|
200
|
+
return { script, redeemer };
|
|
201
|
+
}
|
|
170
202
|
//# sourceMappingURL=tx-request-parsers.js.map
|
package/srv/utils/types.d.ts
CHANGED
|
@@ -58,8 +58,27 @@ export interface Transaction {
|
|
|
58
58
|
blockHeight: number;
|
|
59
59
|
slot: number;
|
|
60
60
|
index: number;
|
|
61
|
+
/**
|
|
62
|
+
* What the ledger charged. For an ordinary transaction that is the fee declared in the
|
|
63
|
+
* body; for a phase-2 failure (`spendsCollaterals`) it is the collateral consumed, which
|
|
64
|
+
* is what the sender actually paid — the declared fee was never collected.
|
|
65
|
+
*/
|
|
61
66
|
fee: Lovelace | string;
|
|
62
67
|
deposit: Lovelace | string;
|
|
68
|
+
/**
|
|
69
|
+
* Phase-2 validity as the ledger applied it. True when the script phase failed: the
|
|
70
|
+
* declared inputs and outputs were not applied, the collateral inputs were consumed and
|
|
71
|
+
* only the collateral return was produced. Only the Ogmios chain-sync path reports it;
|
|
72
|
+
* the historical backends leave it undefined.
|
|
73
|
+
*/
|
|
74
|
+
spendsCollaterals?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* `total_collateral` from the transaction body, when it declares one. The ledger requires
|
|
77
|
+
* it to equal the collateral actually consumed, so it is the exact value for `fee` on a
|
|
78
|
+
* phase-2 failure without resolving a single input. Null when the body omits it — the
|
|
79
|
+
* charge then has to be derived from the resolved collateral inputs minus the return.
|
|
80
|
+
*/
|
|
81
|
+
totalCollateral?: Lovelace | string | null;
|
|
63
82
|
/** Tx size in bytes; null when the source cannot provide it (Ogmios chain-sync). */
|
|
64
83
|
size: number | null;
|
|
65
84
|
blockTime: number;
|
|
@@ -288,6 +307,21 @@ export type MintAction = {
|
|
|
288
307
|
quantity: bigint;
|
|
289
308
|
/** Optional redeemer data (integer). Defaults to 0 if not specified. */
|
|
290
309
|
redeemer?: number;
|
|
310
|
+
/**
|
|
311
|
+
* Optional PER-ACTION minting policy (CBOR hex). When set, this action
|
|
312
|
+
* mints under THIS script instead of the request's top-level
|
|
313
|
+
* mintingPolicyScript, enabling multiple policies in one transaction.
|
|
314
|
+
* Passed as-is (a parameterized script must be pre-applied); the action's
|
|
315
|
+
* assetUnit must carry this script's policyId.
|
|
316
|
+
*/
|
|
317
|
+
mintingPolicyScript?: string;
|
|
318
|
+
/**
|
|
319
|
+
* Optional PER-ACTION redeemer (parsed PlutusData JSON). Only valid
|
|
320
|
+
* together with mintingPolicyScript; falls back to the request's
|
|
321
|
+
* mintRedeemer when absent. Actions resolving to the SAME policy must
|
|
322
|
+
* agree on their redeemer (the ledger carries one redeemer per policy).
|
|
323
|
+
*/
|
|
324
|
+
redeemerJson?: JSONValue;
|
|
291
325
|
};
|
|
292
326
|
/**
|
|
293
327
|
* Plutus Script Execution Type - For spending from script addresses
|