@odatano/core 2.0.0-rc.5 → 2.0.0-rc.50
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 +99 -51
- package/@cds-models/CardanoAgentService/index.js +15 -0
- package/@cds-models/CardanoBackendService/index.d.ts +347 -0
- package/@cds-models/CardanoBackendService/index.js +40 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +496 -11
- package/@cds-models/CardanoIndexerService/index.js +64 -0
- package/@cds-models/CardanoODataService/index.d.ts +414 -57
- package/@cds-models/CardanoODataService/index.js +42 -0
- package/@cds-models/CardanoSignService/index.d.ts +32 -19
- package/@cds-models/CardanoSignService/index.js +3 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +58 -44
- package/@cds-models/CardanoTransactionService/index.js +4 -0
- package/@cds-models/CardanoWorkerService/index.d.ts +7 -22
- package/@cds-models/index.d.ts +101 -1
- package/@cds-models/index.js +16 -1
- package/@cds-models/odatano/cardano/index.d.ts +437 -32
- package/@cds-models/odatano/cardano/index.js +48 -0
- package/README.md +207 -167
- package/db/schema.cds +2402 -1705
- package/db/types.cds +446 -295
- package/package.json +41 -16
- package/scripts/migrate-schema.mjs +334 -0
- package/scripts/migrate-slot-leader.mjs +96 -0
- package/src/index.d.ts +20 -21
- package/src/index.js +45 -31
- package/src/plugin.js +17 -35
- package/srv/blockchain/asset-decimals.d.ts +7 -0
- package/srv/blockchain/asset-decimals.js +91 -0
- package/srv/blockchain/backends/blockfrost-backend.d.ts +25 -135
- package/srv/blockchain/backends/blockfrost-backend.js +66 -172
- package/srv/blockchain/backends/cardano-backend.d.ts +94 -186
- package/srv/blockchain/backends/cardano-backend.js +29 -5
- package/srv/blockchain/backends/koios-backend.d.ts +103 -168
- package/srv/blockchain/backends/koios-backend.js +428 -360
- package/srv/blockchain/backends/odatano-backend.d.ts +54 -0
- package/srv/blockchain/backends/odatano-backend.js +169 -0
- package/srv/blockchain/backends/ogmios-backend.d.ts +123 -190
- package/srv/blockchain/backends/ogmios-backend.js +637 -315
- package/srv/blockchain/backends/ogmios-frame-guard.d.ts +14 -0
- package/srv/blockchain/backends/ogmios-frame-guard.js +89 -0
- package/srv/blockchain/cardano-client.d.ts +61 -241
- package/srv/blockchain/cardano-client.js +253 -293
- package/srv/blockchain/cardano-indexer.d.ts +282 -281
- package/srv/blockchain/cardano-indexer.js +1551 -484
- package/srv/blockchain/cardano-tx-builder.d.ts +25 -89
- package/srv/blockchain/cardano-tx-builder.js +164 -140
- package/srv/blockchain/circuit-breaker.d.ts +1 -13
- package/srv/blockchain/circuit-breaker.js +5 -15
- package/srv/blockchain/crawler/account-snapshots.d.ts +29 -0
- package/srv/blockchain/crawler/account-snapshots.js +153 -0
- package/srv/blockchain/crawler/asset-decimals-backfill.d.ts +15 -0
- package/srv/blockchain/crawler/asset-decimals-backfill.js +56 -0
- package/srv/blockchain/crawler/asset-registry-backfill.d.ts +43 -0
- package/srv/blockchain/crawler/asset-registry-backfill.js +153 -0
- package/srv/blockchain/crawler/certificate-backfill.d.ts +52 -0
- package/srv/blockchain/crawler/certificate-backfill.js +162 -0
- package/srv/blockchain/crawler/crawler.d.ts +104 -81
- package/srv/blockchain/crawler/crawler.js +532 -160
- package/srv/blockchain/crawler/credential-backfill.d.ts +28 -0
- package/srv/blockchain/crawler/credential-backfill.js +124 -0
- package/srv/blockchain/crawler/hooks.d.ts +2 -17
- package/srv/blockchain/crawler/index.d.ts +1 -8
- package/srv/blockchain/crawler/index.js +31 -17
- package/srv/blockchain/crawler/input-asset-copies.d.ts +34 -0
- package/srv/blockchain/crawler/input-asset-copies.js +171 -0
- package/srv/blockchain/crawler/metadata-position-backfill.d.ts +16 -0
- package/srv/blockchain/crawler/metadata-position-backfill.js +55 -0
- package/srv/blockchain/crawler/output-datum-backfill.d.ts +39 -0
- package/srv/blockchain/crawler/output-datum-backfill.js +110 -0
- package/srv/blockchain/crawler/sync-state.d.ts +27 -47
- package/srv/blockchain/crawler/sync-state.js +113 -63
- package/srv/blockchain/crawler/transaction-backfill.d.ts +44 -0
- package/srv/blockchain/crawler/transaction-backfill.js +278 -0
- package/srv/blockchain/crawler/utxo-set-import.d.ts +66 -0
- package/srv/blockchain/crawler/utxo-set-import.js +399 -0
- package/srv/blockchain/ledger-state.d.ts +62 -0
- package/srv/blockchain/ledger-state.js +419 -0
- package/srv/blockchain/pending-spends.d.ts +16 -0
- package/srv/blockchain/pending-spends.js +81 -0
- package/srv/blockchain/pool-metadata.d.ts +15 -0
- package/srv/blockchain/pool-metadata.js +132 -0
- package/srv/blockchain/request-coalescer.d.ts +2 -16
- package/srv/blockchain/request-coalescer.js +2 -16
- package/srv/blockchain/signing/cose-verifier.d.ts +3 -17
- package/srv/blockchain/signing/cose-verifier.js +146 -70
- package/srv/blockchain/signing/external-signer.d.ts +15 -115
- package/srv/blockchain/signing/external-signer.js +17 -126
- package/srv/blockchain/signing/hsm-signer.d.ts +10 -54
- package/srv/blockchain/signing/hsm-signer.js +28 -79
- package/srv/blockchain/signing/signature-verifier.d.ts +9 -47
- package/srv/blockchain/signing/signature-verifier.js +70 -83
- package/srv/blockchain/signing/submission-finalizer.d.ts +23 -45
- package/srv/blockchain/signing/submission-finalizer.js +219 -86
- package/srv/blockchain/stored-chain.d.ts +20 -0
- package/srv/blockchain/stored-chain.js +237 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +31 -229
- package/srv/blockchain/transaction-building/buildooor-tx.js +652 -416
- package/srv/blockchain/transaction-building/cardano-tx.d.ts +9 -37
- package/srv/blockchain/transaction-presence.d.ts +5 -0
- package/srv/blockchain/transaction-presence.js +29 -0
- package/srv/blockchain/transaction-rows.d.ts +12 -0
- package/srv/blockchain/transaction-rows.js +44 -0
- package/srv/blockchain/wallet-worker/build-request.d.ts +5 -20
- package/srv/blockchain/wallet-worker/build-request.js +47 -30
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +5 -46
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +25 -45
- package/srv/blockchain/wallet-worker/index.d.ts +1 -1
- package/srv/blockchain/wallet-worker/index.js +3 -11
- package/srv/blockchain/wallet-worker/job-store.d.ts +30 -148
- package/srv/blockchain/wallet-worker/job-store.js +101 -201
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +2 -26
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +6 -28
- package/srv/blockchain/wallet-worker/signers.d.ts +5 -24
- package/srv/blockchain/wallet-worker/signers.js +4 -12
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +8 -80
- package/srv/blockchain/wallet-worker/wallet-worker.js +107 -155
- package/srv/cardano-agent-service.cds +122 -84
- package/srv/cardano-agent-service.js +4 -9
- package/srv/cardano-backend-service.cds +78 -0
- package/srv/cardano-backend-service.d.ts +2 -0
- package/srv/cardano-backend-service.js +242 -0
- package/srv/cardano-indexer-service.cds +387 -73
- package/srv/cardano-indexer-service.d.ts +5 -1
- package/srv/cardano-indexer-service.js +532 -16
- package/srv/cardano-service.cds +424 -269
- package/srv/cardano-service.js +289 -201
- package/srv/cardano-sign-service.cds +201 -180
- package/srv/cardano-sign-service.js +71 -201
- package/srv/cardano-tx-service.cds +451 -370
- package/srv/cardano-tx-service.js +271 -289
- package/srv/cardano-ui.cds +16 -16
- package/srv/cardano-worker-service.cds +118 -125
- package/srv/cardano-worker-service.js +52 -61
- package/srv/cbor/cip68.d.ts +13 -0
- package/srv/cbor/cip68.js +56 -0
- package/srv/cbor/index.d.ts +3 -1
- package/srv/cbor/index.js +9 -1
- package/srv/cbor/parse.d.ts +30 -18
- package/srv/cbor/parse.js +44 -9
- package/srv/cbor/plutus-data-json.d.ts +20 -0
- package/srv/cbor/plutus-data-json.js +32 -0
- package/srv/config.d.ts +113 -0
- package/srv/config.js +409 -0
- package/srv/server.d.ts +15 -110
- package/srv/server.js +84 -404
- package/srv/utils/agent-access.d.ts +9 -0
- package/srv/utils/agent-access.js +67 -0
- package/srv/utils/agent-grants-config.d.ts +0 -46
- package/srv/utils/agent-grants-config.js +17 -85
- package/srv/utils/agent-grants.d.ts +106 -122
- package/srv/utils/agent-grants.js +538 -236
- package/srv/utils/agent-token-auth.d.ts +4 -99
- package/srv/utils/agent-token-auth.js +18 -182
- package/srv/utils/backend-request-handler.d.ts +3 -17
- package/srv/utils/backend-request-handler.js +3 -18
- package/srv/utils/collections.d.ts +3 -10
- package/srv/utils/collections.js +3 -10
- package/srv/utils/const.d.ts +23 -74
- package/srv/utils/const.js +32 -79
- package/srv/utils/crypto.d.ts +3 -12
- package/srv/utils/crypto.js +6 -21
- package/srv/utils/db-indexes.d.ts +27 -0
- package/srv/utils/db-indexes.js +120 -0
- package/srv/utils/db-sanitize.d.ts +20 -0
- package/srv/utils/db-sanitize.js +80 -0
- package/srv/utils/deep-json.d.ts +6 -0
- package/srv/utils/deep-json.js +240 -0
- package/srv/utils/epoch-slots.d.ts +13 -0
- package/srv/utils/epoch-slots.js +38 -0
- package/srv/utils/error-codes.d.ts +22 -75
- package/srv/utils/error-codes.js +23 -72
- package/srv/utils/errors.d.ts +46 -187
- package/srv/utils/errors.js +114 -230
- package/srv/utils/liveness.d.ts +12 -0
- package/srv/utils/liveness.js +35 -0
- package/srv/utils/mappers.d.ts +102 -223
- package/srv/utils/mappers.js +387 -316
- package/srv/utils/network-context.js +2 -13
- package/srv/utils/paging.d.ts +31 -0
- package/srv/utils/paging.js +43 -0
- package/srv/utils/pg-inline-json.d.ts +8 -0
- package/srv/utils/pg-inline-json.js +58 -0
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +54 -0
- package/srv/utils/plutus-placeholders.d.ts +16 -22
- package/srv/utils/plutus-placeholders.js +62 -33
- package/srv/utils/rate-limiter.d.ts +2 -21
- package/srv/utils/rate-limiter.js +5 -17
- package/srv/utils/readiness.d.ts +12 -0
- package/srv/utils/readiness.js +29 -0
- package/srv/utils/reference-script.d.ts +19 -0
- package/srv/utils/reference-script.js +84 -0
- package/srv/utils/result-numbers.d.ts +15 -0
- package/srv/utils/result-numbers.js +61 -0
- package/srv/utils/service-events.d.ts +0 -24
- package/srv/utils/service-events.js +3 -25
- package/srv/utils/signing-helper.d.ts +3 -20
- package/srv/utils/signing-helper.js +35 -64
- package/srv/utils/tx-build-helper.d.ts +10 -105
- package/srv/utils/tx-build-helper.js +81 -144
- package/srv/utils/tx-request-parsers.d.ts +82 -33
- package/srv/utils/tx-request-parsers.js +401 -49
- package/srv/utils/tx-utils.d.ts +3 -27
- package/srv/utils/tx-utils.js +32 -50
- package/srv/utils/types.d.ts +261 -191
- package/srv/utils/types.js +2 -6
- package/srv/utils/validators.d.ts +22 -92
- package/srv/utils/validators.js +50 -149
|
@@ -12,47 +12,59 @@ const tx_build_helper_1 = require("./utils/tx-build-helper");
|
|
|
12
12
|
const cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
|
|
13
13
|
const mappers_1 = require("./utils/mappers");
|
|
14
14
|
const server_1 = require("./server");
|
|
15
|
+
const signing_helper_1 = require("./utils/signing-helper");
|
|
16
|
+
const tx_utils_1 = require("./utils/tx-utils");
|
|
17
|
+
const paging_1 = require("./utils/paging");
|
|
18
|
+
const submission_finalizer_1 = require("./blockchain/signing/submission-finalizer");
|
|
19
|
+
const CardanoTransactionService_1 = require("#cds-models/CardanoTransactionService");
|
|
15
20
|
const const_1 = require("./utils/const");
|
|
21
|
+
const result_numbers_1 = require("./utils/result-numbers");
|
|
16
22
|
const tx_request_parsers_1 = require("./utils/tx-request-parsers");
|
|
17
|
-
const VALID_DERIVE_NETWORKS = ['mainnet', 'preview', 'preprod'];
|
|
18
23
|
const { SELECT, UPDATE } = cds_1.default.ql;
|
|
24
|
+
/** Blocks on top of a submitted transaction, its own included, before CheckSubmissionStatus confirms it. */
|
|
25
|
+
const SUBMISSION_CONFIRMATION_DEPTH = 3;
|
|
19
26
|
const logger = cds_1.default.log('CardanoTxService');
|
|
20
|
-
/**
|
|
21
|
-
* Cardano Transaction Service Implementation
|
|
22
|
-
* Handles transaction building and submission operations & some additional data queries.
|
|
23
|
-
*/
|
|
27
|
+
/** CardanoTransactionService handlers: transaction building, submission and script helpers. */
|
|
24
28
|
module.exports = (srv) => {
|
|
29
|
+
(0, result_numbers_1.answerNumbersAsText)(srv);
|
|
25
30
|
logger.debug('Module loaded - registering handlers');
|
|
26
31
|
const { TransactionBuilds, TransactionSubmissions, AddressTransactionBuilds } = require('#cds-models/CardanoTransactionService');
|
|
27
|
-
|
|
28
|
-
* Build a simple ADA-only transaction
|
|
29
|
-
* @param req - CDS request object (with senderAddress, recipientAddress, lovelaceAmount, changeAddress)
|
|
30
|
-
* @returns {TransactionBuild} Transaction build details
|
|
31
|
-
*/
|
|
32
|
+
// BuildSimpleAdaTransaction: ADA (optionally with assets / datum) to one recipient or a script.
|
|
32
33
|
srv.on('BuildSimpleAdaTransaction', async (req) => {
|
|
33
34
|
const { senderAddress, recipientAddress, lovelaceAmount, outputDatumJson, assetsJson, forceInputsJson, validatorScript, scriptParamsJson, lockOnScript, referenceScriptHex, validityStartMs, validityEndMs } = req.data;
|
|
34
|
-
// validate inputs
|
|
35
35
|
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, recipientAddress, lovelaceAmount, referenceScriptHex, validityStartMs, validityEndMs }, ['senderAddress', 'recipientAddress', 'lovelaceAmount']);
|
|
36
36
|
(0, errors_1.throwIfValidationErrors)(req, 'BuildSimpleAdaTransaction', errors);
|
|
37
37
|
if (req.data.changeAddress && !(0, validators_1.isValidBech32Address)(req.data.changeAddress)) {
|
|
38
38
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', 'Invalid changeAddress format', 'changeAddress');
|
|
39
39
|
}
|
|
40
|
-
// parse optional output datum
|
|
41
40
|
const cleanData = { ...req.data };
|
|
42
|
-
// parse optional forceInputsJson
|
|
43
41
|
const forceInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(forceInputsJson, 'forceInputsJson');
|
|
44
42
|
if (forceInputsResult.error)
|
|
45
43
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', forceInputsResult.error, 'forceInputsJson');
|
|
46
44
|
cleanData.forceInputs = forceInputsResult.parsed;
|
|
47
45
|
delete cleanData.forceInputsJson;
|
|
46
|
+
const protectInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(req.data.protectInputsJson, 'protectInputsJson');
|
|
47
|
+
if (protectInputsResult.error)
|
|
48
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', protectInputsResult.error, 'protectInputsJson');
|
|
49
|
+
cleanData.protectInputs = protectInputsResult.parsed;
|
|
50
|
+
delete cleanData.protectInputsJson;
|
|
48
51
|
if (outputDatumJson) {
|
|
49
|
-
const jsonResult = (0, validators_1.
|
|
52
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(outputDatumJson, 'outputDatumJson');
|
|
50
53
|
if (!jsonResult.valid)
|
|
51
54
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', jsonResult.error, 'outputDatumJson');
|
|
52
55
|
cleanData.outputDatum = jsonResult.parsed;
|
|
53
56
|
delete cleanData.outputDatumJson;
|
|
54
57
|
}
|
|
55
|
-
|
|
58
|
+
delete cleanData.outputDatumCbor;
|
|
59
|
+
if (req.data.outputDatumCbor != null) {
|
|
60
|
+
if (outputDatumJson)
|
|
61
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', 'outputDatumJson and outputDatumCbor exclude each other; pass one', 'outputDatumCbor');
|
|
62
|
+
const cbor = (0, tx_request_parsers_1.parsePlutusCbor)(req.data.outputDatumCbor, 'outputDatumCbor');
|
|
63
|
+
if (cbor.error)
|
|
64
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', cbor.error, 'outputDatumCbor');
|
|
65
|
+
cleanData.outputDatumCbor = cbor.hex;
|
|
66
|
+
}
|
|
67
|
+
// Native assets to lock at a script address
|
|
56
68
|
const assetsResult = (0, tx_request_parsers_1.parseAssetsArray)(assetsJson, 'assetsJson');
|
|
57
69
|
if (assetsResult.error)
|
|
58
70
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', assetsResult.error, 'assetsJson');
|
|
@@ -60,10 +72,10 @@ module.exports = (srv) => {
|
|
|
60
72
|
cleanData.assets = assetsResult.parsed;
|
|
61
73
|
delete cleanData.assetsJson;
|
|
62
74
|
}
|
|
63
|
-
//
|
|
75
|
+
// Parameters for a parameterized validator
|
|
64
76
|
let scriptParams;
|
|
65
77
|
if (scriptParamsJson) {
|
|
66
|
-
const jsonResult = (0, validators_1.
|
|
78
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(scriptParamsJson, 'scriptParamsJson');
|
|
67
79
|
if (!jsonResult.valid)
|
|
68
80
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', jsonResult.error, 'scriptParamsJson');
|
|
69
81
|
scriptParams = jsonResult.parsed;
|
|
@@ -71,11 +83,10 @@ module.exports = (srv) => {
|
|
|
71
83
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', 'scriptParamsJson must be a JSON array', 'scriptParamsJson');
|
|
72
84
|
}
|
|
73
85
|
}
|
|
74
|
-
// lockOnScript requires a validatorScript to derive the target address from
|
|
75
86
|
if (lockOnScript && !validatorScript) {
|
|
76
87
|
return (0, errors_1.rejectInvalid)(req, 'BuildSimpleAdaTransaction', 'lockOnScript requires validatorScript to derive the script address', 'validatorScript');
|
|
77
88
|
}
|
|
78
|
-
//
|
|
89
|
+
// lockOnScript: derive script hash + address and override the recipient before the build.
|
|
79
90
|
let derivedScriptHash;
|
|
80
91
|
let derivedScriptAddress;
|
|
81
92
|
if (lockOnScript && validatorScript) {
|
|
@@ -99,11 +110,10 @@ module.exports = (srv) => {
|
|
|
99
110
|
cleanData.referenceScript = referenceScriptHex;
|
|
100
111
|
}
|
|
101
112
|
delete cleanData.referenceScriptHex;
|
|
102
|
-
// handle the request / building the transaction / indexing the build result / returning build details
|
|
103
113
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
104
114
|
logger.debug({ senderAddress, recipientAddress: cleanData.recipientAddress, lovelaceAmount, hasDatum: !!outputDatumJson, hasAssets: !!assetsJson, lockOnScript: !!lockOnScript }, 'Building simple ADA transaction');
|
|
105
115
|
const buildResult = await (0, server_1.getCardanoIndexer)().indexSimpleBuildResult(db, cleanData);
|
|
106
|
-
// Persist derived script address + hash on the build record
|
|
116
|
+
// Persist the derived script address + hash on the build record
|
|
107
117
|
if (lockOnScript && derivedScriptHash && derivedScriptAddress && buildResult.id) {
|
|
108
118
|
buildResult.scriptHash = derivedScriptHash;
|
|
109
119
|
buildResult.scriptAddress = derivedScriptAddress;
|
|
@@ -115,41 +125,29 @@ module.exports = (srv) => {
|
|
|
115
125
|
return buildResult;
|
|
116
126
|
});
|
|
117
127
|
});
|
|
118
|
-
|
|
119
|
-
* Build a transaction with metadata
|
|
120
|
-
* @param req - CDS request object
|
|
121
|
-
* @returns {TransactionBuild} Transaction build details
|
|
122
|
-
*/
|
|
128
|
+
// BuildTransactionWithMetadata: ADA transfer with auxiliary metadata.
|
|
123
129
|
srv.on('BuildTransactionWithMetadata', async (req) => {
|
|
124
130
|
const { senderAddress, recipientAddress, lovelaceAmount, metadataJson } = req.data;
|
|
125
|
-
// validate inputs (includes JSON parsing validation)
|
|
126
131
|
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, recipientAddress, lovelaceAmount, metadataJson }, ['senderAddress', 'recipientAddress', 'lovelaceAmount', 'metadataJson']);
|
|
127
132
|
(0, errors_1.throwIfValidationErrors)(req, 'BuildTransactionWithMetadata', errors);
|
|
128
133
|
if (req.data.changeAddress && !(0, validators_1.isValidBech32Address)(req.data.changeAddress)) {
|
|
129
134
|
return (0, errors_1.rejectInvalid)(req, 'BuildTransactionWithMetadata', 'Invalid changeAddress format', 'changeAddress');
|
|
130
135
|
}
|
|
131
|
-
//
|
|
136
|
+
// Already validated as JSON above
|
|
132
137
|
const parsedMetadata = JSON.parse(metadataJson);
|
|
133
|
-
// handle the request / building the transaction / indexing the build result / returning build details
|
|
134
138
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
135
139
|
logger.debug({ senderAddress, recipientAddress, lovelaceAmount, metadataKeyCount: Object.keys(parsedMetadata).length }, 'Building transaction with metadata');
|
|
136
140
|
return await (0, server_1.getCardanoIndexer)().indexMetadataBuildResult(db, { ...req.data, metadataJson: parsedMetadata });
|
|
137
141
|
});
|
|
138
142
|
});
|
|
139
|
-
|
|
140
|
-
* Build a multi-asset transaction
|
|
141
|
-
* @param req CDS request object
|
|
142
|
-
* @returns {TransactionBuild} Transaction build details
|
|
143
|
-
*/
|
|
143
|
+
// BuildMultiAssetTransaction: ADA plus native assets to one recipient.
|
|
144
144
|
srv.on('BuildMultiAssetTransaction', async (req) => {
|
|
145
145
|
const { senderAddress, recipientAddress, lovelaceAmount, assetsJson, outputDatumJson, referenceScriptHex, validityStartMs, validityEndMs } = req.data;
|
|
146
|
-
// validate inputs (includes JSON parsing validation)
|
|
147
146
|
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, recipientAddress, lovelaceAmount, assetsJson, referenceScriptHex, validityStartMs, validityEndMs }, ['senderAddress', 'recipientAddress', 'lovelaceAmount', 'assetsJson']);
|
|
148
147
|
(0, errors_1.throwIfValidationErrors)(req, 'BuildMultiAssetTransaction', errors);
|
|
149
148
|
if (req.data.changeAddress && !(0, validators_1.isValidBech32Address)(req.data.changeAddress)) {
|
|
150
149
|
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', 'Invalid changeAddress format', 'changeAddress');
|
|
151
150
|
}
|
|
152
|
-
// Parse and validate assetsJson entries (unit + quantity, like parseExtraOutputs)
|
|
153
151
|
const assetsResult = (0, tx_request_parsers_1.parseAssetsArray)(assetsJson, 'assetsJson');
|
|
154
152
|
if (assetsResult.error)
|
|
155
153
|
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', assetsResult.error, 'assetsJson');
|
|
@@ -157,22 +155,28 @@ module.exports = (srv) => {
|
|
|
157
155
|
if (!parsedAssets || parsedAssets.length === 0) {
|
|
158
156
|
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', 'assetsJson must contain at least one asset', 'assetsJson');
|
|
159
157
|
}
|
|
160
|
-
// Validation BEFORE handleRequest
|
|
161
158
|
const cleanData = { ...req.data };
|
|
162
159
|
delete cleanData.assetsJson;
|
|
163
|
-
// parse optional output datum
|
|
164
160
|
if (outputDatumJson) {
|
|
165
|
-
const jsonResult = (0, validators_1.
|
|
161
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(outputDatumJson, 'outputDatumJson');
|
|
166
162
|
if (!jsonResult.valid)
|
|
167
163
|
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', jsonResult.error, 'outputDatumJson');
|
|
168
164
|
cleanData.outputDatum = jsonResult.parsed;
|
|
169
165
|
delete cleanData.outputDatumJson;
|
|
170
166
|
}
|
|
167
|
+
delete cleanData.outputDatumCbor;
|
|
168
|
+
if (req.data.outputDatumCbor != null) {
|
|
169
|
+
if (outputDatumJson)
|
|
170
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', 'outputDatumJson and outputDatumCbor exclude each other; pass one', 'outputDatumCbor');
|
|
171
|
+
const cbor = (0, tx_request_parsers_1.parsePlutusCbor)(req.data.outputDatumCbor, 'outputDatumCbor');
|
|
172
|
+
if (cbor.error)
|
|
173
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMultiAssetTransaction', cbor.error, 'outputDatumCbor');
|
|
174
|
+
cleanData.outputDatumCbor = cbor.hex;
|
|
175
|
+
}
|
|
171
176
|
if (referenceScriptHex) {
|
|
172
177
|
cleanData.referenceScript = referenceScriptHex;
|
|
173
178
|
}
|
|
174
179
|
delete cleanData.referenceScriptHex;
|
|
175
|
-
// handle the request / building the transaction / indexing the build result / returning build details
|
|
176
180
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
177
181
|
logger.debug({ senderAddress, recipientAddress, lovelaceAmount, assets: parsedAssets, hasDatum: !!outputDatumJson }, 'Building multi-asset transaction');
|
|
178
182
|
const result = await (0, server_1.getCardanoIndexer)().indexMultiAssetBuildResult(db, { ...cleanData, assets: parsedAssets });
|
|
@@ -180,20 +184,15 @@ module.exports = (srv) => {
|
|
|
180
184
|
return result;
|
|
181
185
|
});
|
|
182
186
|
});
|
|
183
|
-
|
|
184
|
-
* Build a minting transaction
|
|
185
|
-
* @param req CDS request object
|
|
186
|
-
* @returns {TransactionBuild} Transaction build details
|
|
187
|
-
*/
|
|
187
|
+
// BuildMintTransaction: mint/burn under one or more Plutus policies.
|
|
188
188
|
srv.on('BuildMintTransaction', async (req) => {
|
|
189
189
|
const { senderAddress, recipientAddress, lovelaceAmount, mintActionsJson, mintingPolicyScript, requiredSignersJson, scriptParamsJson, inlineDatumJson, mintRedeemerJson, lockOnScript, forceInputsJson, referenceInputsJson, referenceScriptHex, extraOutputsJson, metadataJson, validityStartMs, validityEndMs } = req.data;
|
|
190
|
-
// validate inputs (includes JSON and CBOR validation)
|
|
191
190
|
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, recipientAddress, lovelaceAmount, mintActionsJson, mintingPolicyScript, referenceScriptHex, metadataJson, validityStartMs, validityEndMs }, ['senderAddress', 'recipientAddress', 'lovelaceAmount', 'mintActionsJson', 'mintingPolicyScript']);
|
|
192
191
|
(0, errors_1.throwIfValidationErrors)(req, 'BuildMintTransaction', errors);
|
|
193
192
|
if (req.data.changeAddress && !(0, validators_1.isValidBech32Address)(req.data.changeAddress)) {
|
|
194
193
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'Invalid changeAddress format', 'changeAddress');
|
|
195
194
|
}
|
|
196
|
-
//
|
|
195
|
+
// Mint actions: quantities become bigint; bare asset names are expanded with the policy id.
|
|
197
196
|
const parsedMintActionsRaw = JSON.parse(mintActionsJson);
|
|
198
197
|
if (!Array.isArray(parsedMintActionsRaw)) {
|
|
199
198
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'mintActionsJson must be a JSON array', 'mintActionsJson');
|
|
@@ -202,7 +201,7 @@ module.exports = (srv) => {
|
|
|
202
201
|
if (!action || typeof action !== 'object') {
|
|
203
202
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'Each mint action must be an object with assetUnit and quantity', 'mintActionsJson');
|
|
204
203
|
}
|
|
205
|
-
//
|
|
204
|
+
// Optional per-action mintingPolicyScript + redeemerJson
|
|
206
205
|
const policyFields = (0, tx_request_parsers_1.parseMintActionPolicyFields)(action, actionIndex);
|
|
207
206
|
if (policyFields.error) {
|
|
208
207
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', policyFields.error, 'mintActionsJson');
|
|
@@ -223,7 +222,7 @@ module.exports = (srv) => {
|
|
|
223
222
|
action.assetUnit.length % 2 === 0 &&
|
|
224
223
|
/^[0-9a-fA-F]*$/.test(action.assetUnit);
|
|
225
224
|
if (typeof action.assetUnit !== 'string' || (!(0, validators_1.isAssetUnit)(action.assetUnit) && !bareAssetNameForExpansion)) {
|
|
226
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid assetUnit: "${action.assetUnit}"
|
|
225
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid assetUnit: "${action.assetUnit}": must be policyId+assetName hex (or a bare assetName hex when scriptParamsJson or a per-action mintingPolicyScript is set)`, 'mintActionsJson');
|
|
227
226
|
}
|
|
228
227
|
let assetUnit = action.assetUnit;
|
|
229
228
|
if (actionPolicyId) {
|
|
@@ -238,7 +237,7 @@ module.exports = (srv) => {
|
|
|
238
237
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'Each mint action must have a string quantity', 'mintActionsJson');
|
|
239
238
|
}
|
|
240
239
|
if (!/^-?\d+$/.test(action.quantity)) {
|
|
241
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid quantity format: "${action.quantity}"
|
|
240
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid quantity format: "${action.quantity}": must be an integer string`, 'mintActionsJson');
|
|
242
241
|
}
|
|
243
242
|
try {
|
|
244
243
|
return {
|
|
@@ -250,18 +249,16 @@ module.exports = (srv) => {
|
|
|
250
249
|
};
|
|
251
250
|
}
|
|
252
251
|
catch {
|
|
253
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid quantity: "${action.quantity}"
|
|
252
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `Invalid quantity: "${action.quantity}": cannot parse as integer`, 'mintActionsJson');
|
|
254
253
|
}
|
|
255
254
|
});
|
|
256
|
-
// Parse and validate optional requiredSignersJson
|
|
257
255
|
const requiredSignersResult = (0, tx_request_parsers_1.parseRequiredSigners)(requiredSignersJson);
|
|
258
256
|
if (requiredSignersResult.error)
|
|
259
257
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', requiredSignersResult.error, 'requiredSignersJson');
|
|
260
258
|
const requiredSigners = requiredSignersResult.parsed;
|
|
261
|
-
// Parse and validate optional scriptParamsJson
|
|
262
259
|
let scriptParams;
|
|
263
260
|
if (scriptParamsJson) {
|
|
264
|
-
const jsonResult = (0, validators_1.
|
|
261
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(scriptParamsJson, 'scriptParamsJson');
|
|
265
262
|
if (!jsonResult.valid)
|
|
266
263
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', jsonResult.error, 'scriptParamsJson');
|
|
267
264
|
scriptParams = jsonResult.parsed;
|
|
@@ -269,43 +266,41 @@ module.exports = (srv) => {
|
|
|
269
266
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'scriptParamsJson must be a JSON array', 'scriptParamsJson');
|
|
270
267
|
}
|
|
271
268
|
}
|
|
272
|
-
// Parse and validate optional inlineDatumJson
|
|
273
269
|
let inlineDatum;
|
|
274
270
|
if (inlineDatumJson) {
|
|
275
|
-
const jsonResult = (0, validators_1.
|
|
271
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(inlineDatumJson, 'inlineDatumJson');
|
|
276
272
|
if (!jsonResult.valid)
|
|
277
273
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', jsonResult.error, 'inlineDatumJson');
|
|
278
274
|
inlineDatum = jsonResult.parsed;
|
|
279
275
|
}
|
|
280
|
-
// Parse and validate optional mintRedeemerJson
|
|
281
276
|
let mintRedeemer;
|
|
282
277
|
if (mintRedeemerJson) {
|
|
283
|
-
const jsonResult = (0, validators_1.
|
|
278
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(mintRedeemerJson, 'mintRedeemerJson');
|
|
284
279
|
if (!jsonResult.valid)
|
|
285
280
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', jsonResult.error, 'mintRedeemerJson');
|
|
286
281
|
mintRedeemer = jsonResult.parsed;
|
|
287
282
|
}
|
|
288
|
-
// Validate lockOnScript requires scriptParamsJson
|
|
289
283
|
if (lockOnScript && (!scriptParams || scriptParams.length === 0)) {
|
|
290
284
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'lockOnScript requires scriptParamsJson to derive script address', 'lockOnScript');
|
|
291
285
|
}
|
|
292
|
-
//
|
|
293
|
-
// token can sit on its own output with its own inline datum)
|
|
286
|
+
// extraOutputsJson: each minted token can sit on its own output with its own inline datum
|
|
294
287
|
const extraOutputsResult = (0, tx_request_parsers_1.parseExtraOutputs)(extraOutputsJson);
|
|
295
288
|
if (extraOutputsResult.error)
|
|
296
289
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', extraOutputsResult.error, 'extraOutputsJson');
|
|
297
290
|
const extraOutputs = extraOutputsResult.parsed;
|
|
298
|
-
// Parse and validate optional forceInputsJson
|
|
299
291
|
const forceInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(forceInputsJson, 'forceInputsJson');
|
|
300
292
|
if (forceInputsResult.error)
|
|
301
293
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', forceInputsResult.error, 'forceInputsJson');
|
|
302
294
|
const forceInputs = forceInputsResult.parsed;
|
|
303
|
-
|
|
295
|
+
const protectInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(req.data.protectInputsJson, 'protectInputsJson');
|
|
296
|
+
if (protectInputsResult.error)
|
|
297
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', protectInputsResult.error, 'protectInputsJson');
|
|
298
|
+
const protectInputs = protectInputsResult.parsed;
|
|
299
|
+
// CIP-31 reference inputs
|
|
304
300
|
const refInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(referenceInputsJson, 'referenceInputsJson');
|
|
305
301
|
if (refInputsResult.error)
|
|
306
302
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', refInputsResult.error, 'referenceInputsJson');
|
|
307
303
|
const referenceInputs = refInputsResult.parsed;
|
|
308
|
-
// Parse and validate optional metadataJson
|
|
309
304
|
let parsedMetadata;
|
|
310
305
|
if (metadataJson) {
|
|
311
306
|
try {
|
|
@@ -319,10 +314,8 @@ module.exports = (srv) => {
|
|
|
319
314
|
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', 'metadataJson must be an object with numeric label keys', 'metadataJson');
|
|
320
315
|
}
|
|
321
316
|
}
|
|
322
|
-
// handle the request / building the transaction / indexing the build result / returning build details
|
|
323
317
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
324
318
|
logger.debug({ senderAddress, recipientAddress, lovelaceAmount, mintActions: parsedMintActions, forceInputs: forceInputs?.length ?? 0, referenceInputs: referenceInputs?.length ?? 0, hasMetadata: !!parsedMetadata }, 'Building minting transaction');
|
|
325
|
-
// Create clean request object with parsed mintActions (remove mintActionsJson, add mintActions)
|
|
326
319
|
const cleanData = { ...req.data };
|
|
327
320
|
delete cleanData.mintActionsJson;
|
|
328
321
|
delete cleanData.requiredSignersJson;
|
|
@@ -331,6 +324,7 @@ module.exports = (srv) => {
|
|
|
331
324
|
delete cleanData.mintRedeemerJson;
|
|
332
325
|
delete cleanData.lockOnScript;
|
|
333
326
|
delete cleanData.forceInputsJson;
|
|
327
|
+
delete cleanData.protectInputsJson;
|
|
334
328
|
delete cleanData.referenceInputsJson;
|
|
335
329
|
delete cleanData.extraOutputsJson;
|
|
336
330
|
if (parsedMetadata) {
|
|
@@ -343,13 +337,12 @@ module.exports = (srv) => {
|
|
|
343
337
|
cleanData.referenceScript = referenceScriptHex;
|
|
344
338
|
}
|
|
345
339
|
delete cleanData.referenceScriptHex;
|
|
346
|
-
//
|
|
340
|
+
// Parameterized policy: apply params, then expand bare asset names with the applied hash.
|
|
347
341
|
let finalMintingPolicyScript = mintingPolicyScript;
|
|
348
342
|
let effectivePolicyId;
|
|
349
343
|
if (scriptParams && scriptParams.length > 0) {
|
|
350
344
|
try {
|
|
351
345
|
finalMintingPolicyScript = (0, tx_build_helper_1.applyScriptParameters)(mintingPolicyScript, scriptParams);
|
|
352
|
-
// BUG 7 fix: expand assetName-only entries to full assetUnit using the applied script's policyId.
|
|
353
346
|
const appliedScript = cardano_ledger_ts_1.Script.fromCbor(Buffer.from(finalMintingPolicyScript, 'hex'));
|
|
354
347
|
effectivePolicyId = appliedScript.hash.toString();
|
|
355
348
|
}
|
|
@@ -362,7 +355,7 @@ module.exports = (srv) => {
|
|
|
362
355
|
action.assetUnit = effectivePolicyId + action.assetUnit;
|
|
363
356
|
}
|
|
364
357
|
}
|
|
365
|
-
// lockOnScript: route output to the enterprise
|
|
358
|
+
// lockOnScript: route the output to the enterprise address of the applied script
|
|
366
359
|
if (lockOnScript) {
|
|
367
360
|
const scriptAddr = (0, mappers_1.scriptHashToEnterpriseAddress)(effectivePolicyId, (0, server_1.getCardanoClient)().network);
|
|
368
361
|
cleanData.recipientAddress = scriptAddr;
|
|
@@ -374,14 +367,15 @@ module.exports = (srv) => {
|
|
|
374
367
|
effectivePolicyId = cardano_ledger_ts_1.Script.fromCbor(Buffer.from(mintingPolicyScript, 'hex')).hash.toString();
|
|
375
368
|
}
|
|
376
369
|
catch {
|
|
377
|
-
//
|
|
370
|
+
// Invalid script CBOR: skip the prefix check, the builder rejects it with its own message.
|
|
378
371
|
}
|
|
379
372
|
}
|
|
373
|
+
// Every action without its own policy must carry the top-level policy id prefix.
|
|
380
374
|
if (effectivePolicyId) {
|
|
381
375
|
const policyId = effectivePolicyId;
|
|
382
376
|
const mismatch = parsedMintActions.find((action) => !action.mintingPolicyScript && !action.assetUnit.toLowerCase().startsWith(policyId));
|
|
383
377
|
if (mismatch) {
|
|
384
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `assetUnit "${mismatch.assetUnit}" does not start with the minting policy id ${policyId}
|
|
378
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildMintTransaction', `assetUnit "${mismatch.assetUnit}" does not start with the minting policy id ${policyId}: pass the full unit as policyId+assetName (asset names longer than 28 bytes cannot be passed bare)`, 'mintActionsJson');
|
|
385
379
|
}
|
|
386
380
|
}
|
|
387
381
|
const buildResult = await (0, server_1.getCardanoIndexer)().indexMintBuildResult(db, {
|
|
@@ -392,19 +386,16 @@ module.exports = (srv) => {
|
|
|
392
386
|
inlineDatum,
|
|
393
387
|
mintRedeemer,
|
|
394
388
|
forceInputs,
|
|
389
|
+
protectInputs,
|
|
395
390
|
referenceInputs,
|
|
396
391
|
extraOutputs
|
|
397
392
|
});
|
|
398
|
-
// Post-build:
|
|
393
|
+
// Post-build: CIP-14 fingerprint and scriptAddress
|
|
399
394
|
if (buildResult.scriptHash) {
|
|
400
395
|
const policyId = buildResult.scriptHash;
|
|
401
396
|
const updates = {};
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
// the prefix checks rejected everything else (a 56-hex unit is an empty
|
|
405
|
-
// asset name, not a bare 28-byte name). The policy id comes from the UNIT,
|
|
406
|
-
// not from buildResult.scriptHash: with a per-action mintingPolicyScript the
|
|
407
|
-
// first action may mint under a different policy than the top-level script.
|
|
397
|
+
// Fingerprint of the first minted asset. The policy id is taken from the unit, not from
|
|
398
|
+
// buildResult.scriptHash: a per-action policy may differ from the top-level script.
|
|
408
399
|
if (parsedMintActions.length > 0) {
|
|
409
400
|
const firstAssetUnit = parsedMintActions[0].assetUnit;
|
|
410
401
|
const firstPolicyId = firstAssetUnit.slice(0, const_1.POLICY_ID_HEX_LENGTH);
|
|
@@ -412,7 +403,7 @@ module.exports = (srv) => {
|
|
|
412
403
|
buildResult.fingerprint = (0, mappers_1.computeCip14Fingerprint)(firstPolicyId, assetNameHex);
|
|
413
404
|
updates.fingerprint = buildResult.fingerprint;
|
|
414
405
|
}
|
|
415
|
-
// lockOnScript: persist the derived script address on the build record
|
|
406
|
+
// lockOnScript: persist the derived script address on the build record
|
|
416
407
|
if (lockOnScript && scriptParams && scriptParams.length > 0) {
|
|
417
408
|
buildResult.scriptAddress = (0, mappers_1.scriptHashToEnterpriseAddress)(policyId, (0, server_1.getCardanoClient)().network);
|
|
418
409
|
updates.scriptAddress = buildResult.scriptAddress;
|
|
@@ -425,36 +416,27 @@ module.exports = (srv) => {
|
|
|
425
416
|
return buildResult;
|
|
426
417
|
});
|
|
427
418
|
});
|
|
428
|
-
|
|
429
|
-
* Build a Plutus spending transaction (consume UTxO at script address)
|
|
430
|
-
* @param req - CDS request object (with senderAddress, recipientAddress, lovelaceAmount, validatorScript, scriptTxHash, scriptOutputIndex, redeemerJson, datumJson, changeAddress)
|
|
431
|
-
* @returns {TransactionBuild} Transaction build details
|
|
432
|
-
*/
|
|
419
|
+
// BuildPlutusSpendTransaction: consume a UTxO at a script address, optionally minting too.
|
|
433
420
|
srv.on('BuildPlutusSpendTransaction', async (req) => {
|
|
434
421
|
const { senderAddress, recipientAddress, lovelaceAmount, validatorScript, scriptTxHash, scriptOutputIndex, redeemerJson, datumJson, requiredSignersJson, scriptParamsJson, inlineDatumJson, lockOnScript, forceInputsJson, extraOutputsJson, mintActionsJson, mintingPolicyScript, mintRedeemerJson, referenceInputsJson, referenceScriptHex, validityStartMs, validityEndMs } = req.data;
|
|
435
|
-
// Validate inputs
|
|
436
422
|
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, recipientAddress, lovelaceAmount, validatorScript, scriptTxHash, scriptOutputIndex, redeemerJson, datumJson, referenceScriptHex, validityStartMs, validityEndMs }, ['senderAddress', 'recipientAddress', 'lovelaceAmount', 'validatorScript', 'scriptTxHash', 'redeemerJson']);
|
|
437
423
|
(0, errors_1.throwIfValidationErrors)(req, 'BuildPlutusSpendTransaction', errors);
|
|
438
424
|
if (req.data.changeAddress && !(0, validators_1.isValidBech32Address)(req.data.changeAddress)) {
|
|
439
425
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', 'Invalid changeAddress format', 'changeAddress');
|
|
440
426
|
}
|
|
441
|
-
//
|
|
427
|
+
// A number: not covered by the required-fields check
|
|
442
428
|
if (scriptOutputIndex === undefined || scriptOutputIndex === null) {
|
|
443
429
|
return (0, errors_1.rejectMissing)(req, 'BuildPlutusSpendTransaction', 'scriptOutputIndex');
|
|
444
430
|
}
|
|
445
|
-
// Parse redeemer JSON
|
|
446
431
|
const parsedRedeemer = JSON.parse(redeemerJson);
|
|
447
|
-
// Parse optional datum JSON
|
|
448
432
|
const parsedDatum = datumJson ? JSON.parse(datumJson) : undefined;
|
|
449
|
-
// Parse and validate optional requiredSignersJson
|
|
450
433
|
const requiredSignersResult = (0, tx_request_parsers_1.parseRequiredSigners)(requiredSignersJson);
|
|
451
434
|
if (requiredSignersResult.error)
|
|
452
435
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', requiredSignersResult.error, 'requiredSignersJson');
|
|
453
436
|
const requiredSigners = requiredSignersResult.parsed;
|
|
454
|
-
// Parse and validate optional scriptParamsJson
|
|
455
437
|
let scriptParams;
|
|
456
438
|
if (scriptParamsJson) {
|
|
457
|
-
const jsonResult = (0, validators_1.
|
|
439
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(scriptParamsJson, 'scriptParamsJson');
|
|
458
440
|
if (!jsonResult.valid)
|
|
459
441
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', jsonResult.error, 'scriptParamsJson');
|
|
460
442
|
scriptParams = jsonResult.parsed;
|
|
@@ -462,29 +444,29 @@ module.exports = (srv) => {
|
|
|
462
444
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', 'scriptParamsJson must be a JSON array', 'scriptParamsJson');
|
|
463
445
|
}
|
|
464
446
|
}
|
|
465
|
-
// Parse and validate optional inlineDatumJson
|
|
466
447
|
let inlineDatum;
|
|
467
448
|
if (inlineDatumJson) {
|
|
468
|
-
const jsonResult = (0, validators_1.
|
|
449
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(inlineDatumJson, 'inlineDatumJson');
|
|
469
450
|
if (!jsonResult.valid)
|
|
470
451
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', jsonResult.error, 'inlineDatumJson');
|
|
471
452
|
inlineDatum = jsonResult.parsed;
|
|
472
453
|
}
|
|
473
|
-
// Validate lockOnScript requires scriptParamsJson
|
|
474
454
|
if (lockOnScript && (!scriptParams || scriptParams.length === 0)) {
|
|
475
455
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', 'lockOnScript requires scriptParamsJson to derive script address', 'lockOnScript');
|
|
476
456
|
}
|
|
477
|
-
// Parse and validate optional forceInputsJson
|
|
478
457
|
const forceInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(forceInputsJson, 'forceInputsJson');
|
|
479
458
|
if (forceInputsResult.error)
|
|
480
459
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', forceInputsResult.error, 'forceInputsJson');
|
|
481
460
|
const forceInputs = forceInputsResult.parsed;
|
|
482
|
-
|
|
461
|
+
const protectInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(req.data.protectInputsJson, 'protectInputsJson');
|
|
462
|
+
if (protectInputsResult.error)
|
|
463
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', protectInputsResult.error, 'protectInputsJson');
|
|
464
|
+
const protectInputs = protectInputsResult.parsed;
|
|
465
|
+
// CIP-31 reference inputs
|
|
483
466
|
const refInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(referenceInputsJson, 'referenceInputsJson');
|
|
484
467
|
if (refInputsResult.error)
|
|
485
468
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', refInputsResult.error, 'referenceInputsJson');
|
|
486
469
|
const referenceInputs = refInputsResult.parsed;
|
|
487
|
-
// Parse and validate optional extraOutputsJson
|
|
488
470
|
const extraOutputsResult = (0, tx_request_parsers_1.parseExtraOutputs)(extraOutputsJson);
|
|
489
471
|
if (extraOutputsResult.error)
|
|
490
472
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', extraOutputsResult.error, 'extraOutputsJson');
|
|
@@ -508,13 +490,12 @@ module.exports = (srv) => {
|
|
|
508
490
|
}
|
|
509
491
|
const action = rawAction;
|
|
510
492
|
if (typeof action.quantity !== 'string' || !/^-?\d+$/.test(action.quantity)) {
|
|
511
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `Invalid quantity: "${action.quantity}"
|
|
493
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `Invalid quantity: "${action.quantity}": must be an integer string`, 'mintActionsJson');
|
|
512
494
|
}
|
|
513
495
|
if (typeof action.assetUnit !== 'string') {
|
|
514
496
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', 'Each mint action must have assetUnit string', 'mintActionsJson');
|
|
515
497
|
}
|
|
516
|
-
//
|
|
517
|
-
// (same rules as BuildMintTransaction).
|
|
498
|
+
// Optional per-action mintingPolicyScript + redeemerJson (same rules as BuildMintTransaction)
|
|
518
499
|
const policyFields = (0, tx_request_parsers_1.parseMintActionPolicyFields)(action, actionIndex);
|
|
519
500
|
if (policyFields.error) {
|
|
520
501
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', policyFields.error, 'mintActionsJson');
|
|
@@ -534,7 +515,7 @@ module.exports = (srv) => {
|
|
|
534
515
|
action.assetUnit.length % 2 === 0 &&
|
|
535
516
|
/^[0-9a-fA-F]*$/.test(action.assetUnit);
|
|
536
517
|
if (!(0, validators_1.isAssetUnit)(action.assetUnit) && !bareAssetNameForExpansion) {
|
|
537
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `Invalid assetUnit: "${action.assetUnit}"
|
|
518
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `Invalid assetUnit: "${action.assetUnit}": must be policyId+assetName hex (or a bare assetName hex when a per-action mintingPolicyScript is set, or scriptParamsJson is set and the policy equals the validator)`, 'mintActionsJson');
|
|
538
519
|
}
|
|
539
520
|
let assetUnit = action.assetUnit;
|
|
540
521
|
if (actionPolicyId) {
|
|
@@ -554,7 +535,7 @@ module.exports = (srv) => {
|
|
|
554
535
|
};
|
|
555
536
|
});
|
|
556
537
|
if (mintRedeemerJson) {
|
|
557
|
-
const mrJson = (0, validators_1.
|
|
538
|
+
const mrJson = (0, validators_1.validatePlutusJson)(mintRedeemerJson, 'mintRedeemerJson');
|
|
558
539
|
if (!mrJson.valid)
|
|
559
540
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', mrJson.error, 'mintRedeemerJson');
|
|
560
541
|
parsedMintRedeemer = mrJson.parsed;
|
|
@@ -565,7 +546,6 @@ module.exports = (srv) => {
|
|
|
565
546
|
}
|
|
566
547
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
567
548
|
logger.debug({ senderAddress, recipientAddress, lovelaceAmount, scriptTxHash, scriptOutputIndex }, 'Building Plutus spending transaction');
|
|
568
|
-
// Apply script parameters if provided (for parameterized validators)
|
|
569
549
|
let finalValidatorScript = validatorScript;
|
|
570
550
|
if (scriptParams && scriptParams.length > 0) {
|
|
571
551
|
try {
|
|
@@ -576,8 +556,8 @@ module.exports = (srv) => {
|
|
|
576
556
|
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `Failed to apply script parameters: ${errMsg}`, 'scriptParamsJson');
|
|
577
557
|
}
|
|
578
558
|
}
|
|
579
|
-
//
|
|
580
|
-
//
|
|
559
|
+
// A mint policy byte-equal to the validator (multi-purpose script) reuses the
|
|
560
|
+
// params-applied validator hex; any other policy passes through unchanged.
|
|
581
561
|
let finalMintingPolicyScript;
|
|
582
562
|
if (parsedMintActions) {
|
|
583
563
|
finalMintingPolicyScript = (mintingPolicyScript === validatorScript)
|
|
@@ -598,25 +578,22 @@ module.exports = (srv) => {
|
|
|
598
578
|
}
|
|
599
579
|
}
|
|
600
580
|
}
|
|
601
|
-
//
|
|
602
|
-
//
|
|
603
|
-
// discards the unit's first 56 hex chars, so a mismatched prefix would mint a
|
|
604
|
-
// truncated asset name.
|
|
581
|
+
// Policy-id prefix check as in BuildMintTransaction: the builder discards the unit's
|
|
582
|
+
// first 56 hex chars, so a mismatched prefix would mint a truncated asset name.
|
|
605
583
|
if (finalMintingPolicyScript) {
|
|
606
584
|
let mintPolicyId;
|
|
607
585
|
try {
|
|
608
586
|
mintPolicyId = cardano_ledger_ts_1.Script.fromCbor(Buffer.from(finalMintingPolicyScript, 'hex')).hash.toString();
|
|
609
587
|
}
|
|
610
588
|
catch {
|
|
611
|
-
//
|
|
589
|
+
// Invalid script CBOR: skip the prefix check, the builder rejects it with its own message.
|
|
612
590
|
}
|
|
613
591
|
if (mintPolicyId) {
|
|
614
592
|
const policyId = mintPolicyId;
|
|
615
|
-
// Actions with
|
|
616
|
-
// against THAT script's policy id during parsing.
|
|
593
|
+
// Actions with a per-action script were already checked against that policy id.
|
|
617
594
|
const mismatch = parsedMintActions.find((action) => !action.mintingPolicyScript && !action.assetUnit.toLowerCase().startsWith(policyId));
|
|
618
595
|
if (mismatch) {
|
|
619
|
-
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `assetUnit "${mismatch.assetUnit}" does not start with the minting policy id ${policyId}
|
|
596
|
+
return (0, errors_1.rejectInvalid)(req, 'BuildPlutusSpendTransaction', `assetUnit "${mismatch.assetUnit}" does not start with the minting policy id ${policyId}: pass the full unit as policyId+assetName (asset names longer than 28 bytes cannot be passed bare)`, 'mintActionsJson');
|
|
620
597
|
}
|
|
621
598
|
}
|
|
622
599
|
}
|
|
@@ -635,6 +612,7 @@ module.exports = (srv) => {
|
|
|
635
612
|
requiredSigners,
|
|
636
613
|
inlineDatum,
|
|
637
614
|
forceInputs,
|
|
615
|
+
protectInputs,
|
|
638
616
|
referenceInputs,
|
|
639
617
|
extraOutputs,
|
|
640
618
|
mintActions: parsedMintActions,
|
|
@@ -649,12 +627,13 @@ module.exports = (srv) => {
|
|
|
649
627
|
delete cleanData.inlineDatumJson;
|
|
650
628
|
delete cleanData.lockOnScript;
|
|
651
629
|
delete cleanData.forceInputsJson;
|
|
630
|
+
delete cleanData.protectInputsJson;
|
|
652
631
|
delete cleanData.referenceInputsJson;
|
|
653
632
|
delete cleanData.extraOutputsJson;
|
|
654
633
|
delete cleanData.mintActionsJson;
|
|
655
634
|
delete cleanData.mintRedeemerJson;
|
|
656
635
|
delete cleanData.referenceScriptHex;
|
|
657
|
-
// lockOnScript: route continuing output to enterprise script address
|
|
636
|
+
// lockOnScript: route the continuing output to the enterprise script address
|
|
658
637
|
if (lockOnScript && scriptParams && scriptParams.length > 0) {
|
|
659
638
|
try {
|
|
660
639
|
const appliedScript = cardano_ledger_ts_1.Script.fromCbor(Buffer.from(finalValidatorScript, 'hex'));
|
|
@@ -669,7 +648,7 @@ module.exports = (srv) => {
|
|
|
669
648
|
}
|
|
670
649
|
}
|
|
671
650
|
const buildResult = await (0, server_1.getCardanoIndexer)().indexPlutusSpendBuildResult(db, cleanData);
|
|
672
|
-
// lockOnScript: persist the derived script address on the build record
|
|
651
|
+
// lockOnScript: persist the derived script address on the build record
|
|
673
652
|
if (lockOnScript && scriptParams && scriptParams.length > 0 && buildResult.scriptHash && buildResult.id) {
|
|
674
653
|
const scriptAddr = (0, mappers_1.scriptHashToEnterpriseAddress)(buildResult.scriptHash, (0, server_1.getCardanoClient)().network);
|
|
675
654
|
buildResult.scriptAddress = scriptAddr;
|
|
@@ -679,17 +658,130 @@ module.exports = (srv) => {
|
|
|
679
658
|
return buildResult;
|
|
680
659
|
});
|
|
681
660
|
});
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
661
|
+
// BuildPlutusTransaction: several script UTxOs in one transaction, each with its own redeemer.
|
|
662
|
+
srv.on('BuildPlutusTransaction', async (req) => {
|
|
663
|
+
const { senderAddress, changeAddress, scriptInputsJson, outputsJson, referenceInputsJson, forceInputsJson, protectInputsJson, withdrawalsJson, certificatesJson, requiredSignersJson, mintActionsJson, validityStartMs, validityEndMs } = req.data;
|
|
664
|
+
const action = 'BuildPlutusTransaction';
|
|
665
|
+
const errors = (0, validators_1.validateTransactionInputs)({ senderAddress, validityStartMs, validityEndMs }, ['senderAddress']);
|
|
666
|
+
(0, errors_1.throwIfValidationErrors)(req, action, errors);
|
|
667
|
+
if (changeAddress && !(0, validators_1.isValidBech32Address)(changeAddress)) {
|
|
668
|
+
return (0, errors_1.rejectInvalid)(req, action, 'Invalid changeAddress format', 'changeAddress');
|
|
669
|
+
}
|
|
670
|
+
const scriptInputsResult = (0, tx_request_parsers_1.parseScriptInputs)(scriptInputsJson);
|
|
671
|
+
if (scriptInputsResult.error)
|
|
672
|
+
return (0, errors_1.rejectInvalid)(req, action, scriptInputsResult.error, 'scriptInputsJson');
|
|
673
|
+
if (!outputsJson)
|
|
674
|
+
return (0, errors_1.rejectMissing)(req, action, 'outputsJson');
|
|
675
|
+
const outputsResult = (0, tx_request_parsers_1.parseOutputList)(outputsJson, 'outputsJson');
|
|
676
|
+
if (outputsResult.error)
|
|
677
|
+
return (0, errors_1.rejectInvalid)(req, action, outputsResult.error, 'outputsJson');
|
|
678
|
+
if (!outputsResult.parsed)
|
|
679
|
+
return (0, errors_1.rejectInvalid)(req, action, 'outputsJson must list at least one output', 'outputsJson');
|
|
680
|
+
const refInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(referenceInputsJson, 'referenceInputsJson');
|
|
681
|
+
if (refInputsResult.error)
|
|
682
|
+
return (0, errors_1.rejectInvalid)(req, action, refInputsResult.error, 'referenceInputsJson');
|
|
683
|
+
const forceInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(forceInputsJson, 'forceInputsJson');
|
|
684
|
+
if (forceInputsResult.error)
|
|
685
|
+
return (0, errors_1.rejectInvalid)(req, action, forceInputsResult.error, 'forceInputsJson');
|
|
686
|
+
const protectInputsResult = (0, tx_request_parsers_1.parseUtxoRefArray)(protectInputsJson, 'protectInputsJson');
|
|
687
|
+
if (protectInputsResult.error)
|
|
688
|
+
return (0, errors_1.rejectInvalid)(req, action, protectInputsResult.error, 'protectInputsJson');
|
|
689
|
+
const withdrawalsResult = (0, tx_request_parsers_1.parseWithdrawals)(withdrawalsJson);
|
|
690
|
+
if (withdrawalsResult.error)
|
|
691
|
+
return (0, errors_1.rejectInvalid)(req, action, withdrawalsResult.error, 'withdrawalsJson');
|
|
692
|
+
const certificatesResult = (0, tx_request_parsers_1.parseCertificates)(certificatesJson);
|
|
693
|
+
if (certificatesResult.error)
|
|
694
|
+
return (0, errors_1.rejectInvalid)(req, action, certificatesResult.error, 'certificatesJson');
|
|
695
|
+
const signersResult = (0, tx_request_parsers_1.parseRequiredSigners)(requiredSignersJson);
|
|
696
|
+
if (signersResult.error)
|
|
697
|
+
return (0, errors_1.rejectInvalid)(req, action, signersResult.error, 'requiredSignersJson');
|
|
698
|
+
const mintResult = (0, tx_request_parsers_1.parsePolicyMintActions)(mintActionsJson);
|
|
699
|
+
if (mintResult.error)
|
|
700
|
+
return (0, errors_1.rejectInvalid)(req, action, mintResult.error, 'mintActionsJson');
|
|
701
|
+
// Script params are applied here, so the builder sees the final validators
|
|
702
|
+
const scriptInputs = [];
|
|
703
|
+
for (const [i, si] of scriptInputsResult.parsed.entries()) {
|
|
704
|
+
let validatorScript = si.validatorScript;
|
|
705
|
+
if (validatorScript && si.scriptParams) {
|
|
706
|
+
try {
|
|
707
|
+
validatorScript = (0, tx_build_helper_1.applyScriptParameters)(validatorScript, si.scriptParams);
|
|
708
|
+
}
|
|
709
|
+
catch (err) {
|
|
710
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
711
|
+
return (0, errors_1.rejectInvalid)(req, action, `scriptInputs[${i}]: failed to apply script parameters: ${errMsg}`, 'scriptInputsJson');
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
scriptInputs.push({
|
|
715
|
+
txHash: si.txHash,
|
|
716
|
+
outputIndex: si.outputIndex,
|
|
717
|
+
...(validatorScript ? { validatorScript } : { referenceScript: si.referenceScript }),
|
|
718
|
+
redeemer: si.redeemer,
|
|
719
|
+
...(si.redeemerCbor ? { redeemerCbor: si.redeemerCbor } : {}),
|
|
720
|
+
...(si.datum !== undefined ? { datum: si.datum } : {}),
|
|
721
|
+
...(si.datumCbor ? { datumCbor: si.datumCbor } : {}),
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
// Same for the staking scripts of withdrawals and certificates
|
|
725
|
+
const stakingWitness = (w, at, field) => {
|
|
726
|
+
let stakingScript = w.stakingScript;
|
|
727
|
+
if (stakingScript && w.scriptParams) {
|
|
728
|
+
try {
|
|
729
|
+
stakingScript = (0, tx_build_helper_1.applyScriptParameters)(stakingScript, w.scriptParams);
|
|
730
|
+
}
|
|
731
|
+
catch (err) {
|
|
732
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
733
|
+
return { error: (0, errors_1.rejectInvalid)(req, action, `${at}: failed to apply script parameters: ${errMsg}`, field) };
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return {
|
|
737
|
+
parsed: {
|
|
738
|
+
...(stakingScript ? { stakingScript } : {}),
|
|
739
|
+
...(w.referenceScript ? { referenceScript: w.referenceScript } : {}),
|
|
740
|
+
...(w.redeemer !== undefined ? { redeemer: w.redeemer } : {}),
|
|
741
|
+
...(w.redeemerCbor ? { redeemerCbor: w.redeemerCbor } : {}),
|
|
742
|
+
},
|
|
743
|
+
};
|
|
744
|
+
};
|
|
745
|
+
const withdrawals = [];
|
|
746
|
+
for (const [i, w] of (withdrawalsResult.parsed ?? []).entries()) {
|
|
747
|
+
const witness = stakingWitness(w, `withdrawals[${i}]`, 'withdrawalsJson');
|
|
748
|
+
if ('error' in witness)
|
|
749
|
+
return witness.error;
|
|
750
|
+
withdrawals.push({ rewardAddress: w.rewardAddress, lovelace: w.lovelace, ...witness.parsed });
|
|
751
|
+
}
|
|
752
|
+
const certificates = [];
|
|
753
|
+
for (const [i, c] of (certificatesResult.parsed ?? []).entries()) {
|
|
754
|
+
const witness = stakingWitness(c, `certificates[${i}]`, 'certificatesJson');
|
|
755
|
+
if ('error' in witness)
|
|
756
|
+
return witness.error;
|
|
757
|
+
certificates.push({ type: c.type, stakeAddress: c.stakeAddress, ...(c.deposit ? { deposit: c.deposit } : {}), ...witness.parsed });
|
|
758
|
+
}
|
|
759
|
+
const buildReq = {
|
|
760
|
+
network: (0, server_1.getCardanoClient)().network,
|
|
761
|
+
senderAddress,
|
|
762
|
+
...(changeAddress ? { changeAddress } : {}),
|
|
763
|
+
scriptInputs,
|
|
764
|
+
outputs: outputsResult.parsed,
|
|
765
|
+
referenceInputs: refInputsResult.parsed,
|
|
766
|
+
forceInputs: forceInputsResult.parsed,
|
|
767
|
+
protectInputs: protectInputsResult.parsed,
|
|
768
|
+
...(withdrawals.length > 0 ? { withdrawals } : {}),
|
|
769
|
+
...(certificates.length > 0 ? { certificates } : {}),
|
|
770
|
+
requiredSigners: signersResult.parsed,
|
|
771
|
+
mintActions: mintResult.parsed,
|
|
772
|
+
validityStartMs,
|
|
773
|
+
validityEndMs,
|
|
774
|
+
};
|
|
775
|
+
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
776
|
+
logger.debug({ senderAddress, scriptInputs: scriptInputs.length, outputs: buildReq.outputs.length }, 'Building multi-script Plutus transaction');
|
|
777
|
+
return (0, server_1.getCardanoIndexer)().indexPlutusBuildResult(db, buildReq);
|
|
778
|
+
});
|
|
779
|
+
});
|
|
780
|
+
// GetBuildDetails: an existing transaction build by id.
|
|
687
781
|
srv.on('GetBuildDetails', async (req) => {
|
|
688
782
|
const { buildId } = req.data;
|
|
689
|
-
// Validate inputs
|
|
690
783
|
const errors = (0, validators_1.validateTransactionInputs)({ buildId }, ['buildId']);
|
|
691
784
|
(0, errors_1.throwIfValidationErrors)(req, 'GetBuildDetails', errors);
|
|
692
|
-
// handle the request / fetching the build details
|
|
693
785
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
694
786
|
const existing = await db.run(SELECT.one.from(TransactionBuilds).where({ id: buildId }));
|
|
695
787
|
if (!existing)
|
|
@@ -697,13 +789,8 @@ module.exports = (srv) => {
|
|
|
697
789
|
return existing;
|
|
698
790
|
});
|
|
699
791
|
});
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
* Checks if the address already has >= 2 UTxOs with >= 5 ADA each.
|
|
703
|
-
* If not, builds a self-send transaction to create a 5 ADA collateral UTxO.
|
|
704
|
-
* @param req - CDS request object (with address)
|
|
705
|
-
* @returns {TransactionBuild} Transaction build details for the collateral setup
|
|
706
|
-
*/
|
|
792
|
+
// SetCollateral: collateral is available when the builders would find it: an ADA-only UTxO of
|
|
793
|
+
// >= 5 ADA without a reference script, plus another UTxO to fund with; otherwise a self-send creates one.
|
|
707
794
|
srv.on('SetCollateral', async (req) => {
|
|
708
795
|
const { address } = req.data;
|
|
709
796
|
if (!address)
|
|
@@ -712,17 +799,15 @@ module.exports = (srv) => {
|
|
|
712
799
|
return (0, errors_1.rejectInvalid)(req, 'SetCollateral', 'Invalid Bech32 address format', 'address');
|
|
713
800
|
}
|
|
714
801
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
715
|
-
// Inside handleRequest
|
|
716
|
-
//
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
// mapError sees a typed BackendError and preserves the 400 status.
|
|
720
|
-
const utxos = await (0, server_1.getCardanoClient)().getAddressUtxos(address);
|
|
802
|
+
// Inside handleRequest use rejectInvalid (typed BackendError 400), not req.reject,
|
|
803
|
+
// so mapError preserves the status.
|
|
804
|
+
const ledger = await (0, server_1.getCardanoIndexer)().resolveAddressUtxos(db, address);
|
|
805
|
+
const utxos = (0, server_1.getCardanoClient)().pendingSpends?.apply(address, ledger) ?? ledger;
|
|
721
806
|
if (utxos.length === 0) {
|
|
722
807
|
return (0, errors_1.rejectInvalid)(req, 'SetCollateral', 'No UTxOs found at address', 'address');
|
|
723
808
|
}
|
|
724
|
-
const qualifyingUtxos = utxos.filter(u => (0, tx_build_helper_1.getLovelace)(u) >= const_1.COLLATERAL_LOVELACE);
|
|
725
|
-
if (qualifyingUtxos.length >= 2) {
|
|
809
|
+
const qualifyingUtxos = utxos.filter(u => (0, tx_build_helper_1.isCollateralCandidate)(u) && (0, tx_build_helper_1.getLovelace)(u) >= const_1.COLLATERAL_LOVELACE);
|
|
810
|
+
if (qualifyingUtxos.length >= 1 && utxos.length >= 2) {
|
|
726
811
|
return {
|
|
727
812
|
id: cds_1.default.utils.uuid(),
|
|
728
813
|
network: (0, server_1.getCardanoClient)().network,
|
|
@@ -732,7 +817,7 @@ module.exports = (srv) => {
|
|
|
732
817
|
}
|
|
733
818
|
const totalLovelace = utxos.reduce((sum, u) => sum + (0, tx_build_helper_1.getLovelace)(u), 0n);
|
|
734
819
|
if (totalLovelace < const_1.COLLATERAL_LOVELACE + const_1.FEE_BUFFER_LOVELACE) {
|
|
735
|
-
return (0, errors_1.rejectInvalid)(req, 'SetCollateral', `Insufficient funds
|
|
820
|
+
return (0, errors_1.rejectInvalid)(req, 'SetCollateral', `Insufficient funds: need at least ${Number(const_1.COLLATERAL_LOVELACE + const_1.FEE_BUFFER_LOVELACE) / 1_000_000} ADA, have ${Number(totalLovelace) / 1_000_000} ADA`, 'address');
|
|
736
821
|
}
|
|
737
822
|
logger.debug({ address, existingQualifying: qualifyingUtxos.length }, 'Building collateral setup transaction');
|
|
738
823
|
const result = await (0, server_1.getCardanoIndexer)().indexSimpleBuildResult(db, {
|
|
@@ -745,175 +830,82 @@ module.exports = (srv) => {
|
|
|
745
830
|
return { ...result, collateralAvailable: false };
|
|
746
831
|
});
|
|
747
832
|
});
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
* Handler validates, checks build exists, submits to blockchain, delegates persistence to indexer
|
|
751
|
-
* @param req - CDS request object (with buildId, signedTxCbor)
|
|
752
|
-
* @returns {TransactionSubmission} Transaction submission details
|
|
753
|
-
*/
|
|
754
|
-
srv.on('SubmitTransaction', async (req) => {
|
|
833
|
+
// SubmitTransaction: submit the signed CBOR of a previous build.
|
|
834
|
+
srv.on(CardanoTransactionService_1.SubmitTransaction, async (req) => {
|
|
755
835
|
logger.debug('SubmitTransaction Action handler called');
|
|
756
|
-
const { buildId, signedTxCbor } = req.data;
|
|
757
|
-
|
|
758
|
-
|
|
836
|
+
const { buildId, signedTxCbor: signedInput } = req.data;
|
|
837
|
+
if (!buildId)
|
|
838
|
+
return (0, errors_1.rejectMissing)(req, 'SubmitTransaction', 'buildId');
|
|
839
|
+
if (!signedInput)
|
|
840
|
+
return (0, errors_1.rejectMissing)(req, 'SubmitTransaction', 'signedTxCbor');
|
|
841
|
+
const errors = (0, validators_1.validateTransactionInputs)({ buildId, signedTxCbor: signedInput }, ['buildId', 'signedTxCbor']);
|
|
759
842
|
(0, errors_1.throwIfValidationErrors)(req, 'SubmitTransaction', errors);
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
const existing = await db.run(SELECT.one.from(TransactionBuilds).where({ id: buildId }));
|
|
764
|
-
if (!existing)
|
|
843
|
+
try {
|
|
844
|
+
const build = await (0, tx_utils_1.detachedTx)('load build for submit', (db) => db.run(SELECT.one.from(TransactionBuilds).columns('unsignedTxCbor', 'txBodyHash').where({ id: buildId })));
|
|
845
|
+
if (!build)
|
|
765
846
|
throw new errors_1.NotFoundError(`Build '${buildId}'`);
|
|
766
|
-
//
|
|
847
|
+
// A CIP-30 wallet returns only the witness set; cardano-cli a full signed tx.
|
|
848
|
+
const signedTxCbor = (0, signing_helper_1.toSignedTransaction)(build.unsignedTxCbor, signedInput);
|
|
849
|
+
// The signed CBOR must belong to this build (audit-trail integrity)
|
|
767
850
|
const signedTxHash = (0, tx_build_helper_1.getTxHashFromCbor)(signedTxCbor);
|
|
768
|
-
if (signedTxHash !==
|
|
769
|
-
|
|
770
|
-
}
|
|
771
|
-
// Use txBodyHash from build
|
|
772
|
-
const txHash = existing.txBodyHash;
|
|
773
|
-
// Two-phase submit: persist with 'pending', then submit to blockchain
|
|
774
|
-
const submissionRecord = await (0, server_1.getCardanoIndexer)().persistTransactionSubmission(db, {
|
|
775
|
-
signedTxCbor,
|
|
776
|
-
txHash,
|
|
777
|
-
buildId,
|
|
778
|
-
});
|
|
779
|
-
try {
|
|
780
|
-
await (0, server_1.getCardanoClient)().submitTransaction(signedTxCbor);
|
|
781
|
-
logger.info({ txHash }, 'Transaction submitted to blockchain');
|
|
782
|
-
await (0, server_1.getCardanoIndexer)().updateSubmissionStatus(db, submissionRecord.id, 'submitted');
|
|
783
|
-
submissionRecord.status = 'submitted';
|
|
784
|
-
// Invalidate stale UTxO cache: spent input refs + output addresses from
|
|
785
|
-
// the signed CBOR, plus the build's sender address. Best-effort — a
|
|
786
|
-
// failure here must not fail the already-successful submit.
|
|
787
|
-
try {
|
|
788
|
-
const addrBuild = await db.run(SELECT.one.from(AddressTransactionBuilds).where({ txBuild_id: buildId }));
|
|
789
|
-
await (0, server_1.getCardanoIndexer)().invalidateUtxoCacheForTx(db, (0, tx_build_helper_1.extractTxCacheTargets)(signedTxCbor), addrBuild?.address_address ? [addrBuild.address_address] : []);
|
|
790
|
-
}
|
|
791
|
-
catch (invalidateErr) {
|
|
792
|
-
logger.warn(`UTxO cache invalidation failed (submit unaffected): ${invalidateErr instanceof Error ? invalidateErr.message : String(invalidateErr)}`);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
catch (err) {
|
|
796
|
-
const errMsg = err instanceof Error ? err.message : String(err);
|
|
797
|
-
logger.error({ txHash, error: errMsg }, 'Transaction submission failed');
|
|
798
|
-
await (0, server_1.getCardanoIndexer)().updateSubmissionStatus(db, submissionRecord.id, 'failed', errMsg);
|
|
799
|
-
throw err;
|
|
851
|
+
if (signedTxHash !== build.txBodyHash) {
|
|
852
|
+
(0, errors_1.rejectInvalid)(req, 'SubmitTransaction', `signedTxCbor hash '${signedTxHash}' does not match build txBodyHash '${build.txBodyHash}'`, 'signedTxCbor');
|
|
800
853
|
}
|
|
801
|
-
return
|
|
802
|
-
}
|
|
854
|
+
return await (0, submission_finalizer_1.submitRecorded)({ signedTxCbor, txHash: build.txBodyHash, buildId });
|
|
855
|
+
}
|
|
856
|
+
catch (e) {
|
|
857
|
+
return (0, mappers_1.mapError)(req, e, 'SubmitTransaction');
|
|
858
|
+
}
|
|
803
859
|
});
|
|
804
|
-
|
|
805
|
-
* Submit signed transaction without prior build
|
|
806
|
-
* Handler validates, submits to blockchain, delegates persistence to indexer
|
|
807
|
-
* Two-phase: persist with 'pending' first, then update to 'submitted' or 'failed'
|
|
808
|
-
* @param req - CDS request object (with signedTxCbor, network)
|
|
809
|
-
* @returns {TransactionSubmission} Transaction submission details
|
|
810
|
-
*/
|
|
860
|
+
// SubmitSignedTransaction: submit externally built signed CBOR (no prior build).
|
|
811
861
|
srv.on('SubmitSignedTransaction', async (req) => {
|
|
812
862
|
logger.debug('SubmitSignedTransaction Action handler called');
|
|
813
863
|
const { signedTxCbor, network } = req.data;
|
|
814
|
-
// Validate inputs (includes CBOR format validation)
|
|
815
864
|
const errors = (0, validators_1.validateTransactionInputs)({ signedTxCbor }, ['signedTxCbor']);
|
|
816
865
|
(0, errors_1.throwIfValidationErrors)(req, 'SubmitSignedTransaction', errors);
|
|
817
|
-
|
|
818
|
-
//
|
|
819
|
-
// configured network so a caller targeting the wrong deployment gets a clear 400.
|
|
866
|
+
try {
|
|
867
|
+
// A caller targeting the wrong deployment gets a clear 400
|
|
820
868
|
const configuredNetwork = (0, server_1.getCardanoClient)().network;
|
|
821
869
|
if (network && network !== configuredNetwork) {
|
|
822
|
-
|
|
823
|
-
}
|
|
824
|
-
// Extract txHash from signed CBOR
|
|
825
|
-
const txHash = (0, tx_build_helper_1.getTxHashFromCbor)(signedTxCbor);
|
|
826
|
-
// Two-phase submit: persist with 'pending', then submit to blockchain
|
|
827
|
-
const submissionRecord = await (0, server_1.getCardanoIndexer)().persistTransactionSubmission(db, {
|
|
828
|
-
signedTxCbor,
|
|
829
|
-
txHash,
|
|
830
|
-
buildId: null,
|
|
831
|
-
});
|
|
832
|
-
try {
|
|
833
|
-
await (0, server_1.getCardanoClient)().submitTransaction(signedTxCbor);
|
|
834
|
-
logger.info({ txHash }, 'External transaction submitted');
|
|
835
|
-
await (0, server_1.getCardanoIndexer)().updateSubmissionStatus(db, submissionRecord.id, 'submitted');
|
|
836
|
-
submissionRecord.status = 'submitted';
|
|
837
|
-
// Invalidate stale UTxO cache (spent inputs + output addresses) — best-effort
|
|
838
|
-
try {
|
|
839
|
-
await (0, server_1.getCardanoIndexer)().invalidateUtxoCacheForTx(db, (0, tx_build_helper_1.extractTxCacheTargets)(signedTxCbor));
|
|
840
|
-
}
|
|
841
|
-
catch (invalidateErr) {
|
|
842
|
-
logger.warn(`UTxO cache invalidation failed (submit unaffected): ${invalidateErr instanceof Error ? invalidateErr.message : String(invalidateErr)}`);
|
|
843
|
-
}
|
|
870
|
+
(0, errors_1.rejectInvalid)(req, 'SubmitSignedTransaction', `network '${network}' does not match this deployment's network '${configuredNetwork}'`, 'network');
|
|
844
871
|
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
}
|
|
851
|
-
return submissionRecord;
|
|
852
|
-
});
|
|
872
|
+
return await (0, submission_finalizer_1.submitRecorded)({ signedTxCbor, txHash: (0, tx_build_helper_1.getTxHashFromCbor)(signedTxCbor), buildId: null });
|
|
873
|
+
}
|
|
874
|
+
catch (e) {
|
|
875
|
+
return (0, mappers_1.mapError)(req, e, 'SubmitSignedTransaction');
|
|
876
|
+
}
|
|
853
877
|
});
|
|
854
|
-
|
|
855
|
-
* Check submission status (bound action on TransactionSubmissions)
|
|
856
|
-
* @flow.status validates @from: [#submitted] automatically (409 if wrong state)
|
|
857
|
-
* Queries blockchain for transaction confirmation and updates status accordingly
|
|
858
|
-
* @param req - CDS request with entity key in params
|
|
859
|
-
* @returns {TransactionSubmission} The updated transaction submission status
|
|
860
|
-
*/
|
|
878
|
+
// CheckSubmissionStatus: bound action; the @from: [#submitted] state gate is enforced by CAP.
|
|
861
879
|
srv.on('CheckSubmissionStatus', async (req) => {
|
|
862
880
|
logger.debug('CheckSubmissionStatus Action handler called');
|
|
863
881
|
const { id: submissionId } = req.params[0];
|
|
864
|
-
// @from: [#submitted] validated by framework — no manual status check needed
|
|
865
882
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
866
|
-
|
|
883
|
+
// read outside the request transaction: the confirmation lookup below may ask a backend
|
|
884
|
+
const submission = await (0, tx_utils_1.readOutside)(db, (d) => d.run(SELECT.one.from(TransactionSubmissions).where({ id: submissionId })));
|
|
867
885
|
if (!submission)
|
|
868
886
|
throw new errors_1.NotFoundError(`Submission '${submissionId}'`);
|
|
869
|
-
//
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
.where({ id: submissionId }));
|
|
876
|
-
submission.status = 'confirmed';
|
|
877
|
-
logger.info({ submissionId, txHash: submission.txHash }, 'Transaction confirmed on chain');
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
catch (err) {
|
|
881
|
-
const errMsg = err instanceof Error ? err.message : String(err);
|
|
882
|
-
if (err instanceof errors_1.NotFoundError || err?.statusCode === 404) {
|
|
883
|
-
// Transaction genuinely not yet confirmed on chain
|
|
884
|
-
logger.debug({ submissionId, txHash: submission.txHash }, 'Transaction not yet confirmed on chain');
|
|
885
|
-
}
|
|
886
|
-
else {
|
|
887
|
-
// Provider error — don't mask as "pending", let caller know
|
|
888
|
-
logger.warn({ submissionId, txHash: submission.txHash, error: errMsg }, 'Failed to check transaction confirmation status');
|
|
889
|
-
throw err;
|
|
890
|
-
}
|
|
887
|
+
// a block this deep is not rolled back in practice; a status set here is never reverted
|
|
888
|
+
const { confirmations } = await (0, server_1.getCardanoIndexer)().resolveTransactionConfirmations(db, submission.txHash);
|
|
889
|
+
if (confirmations >= SUBMISSION_CONFIRMATION_DEPTH) {
|
|
890
|
+
await db.run(UPDATE.entity(TransactionSubmissions).set({ status: 'confirmed' }).where({ id: submissionId }));
|
|
891
|
+
submission.status = 'confirmed';
|
|
892
|
+
logger.info({ submissionId, txHash: submission.txHash, confirmations }, 'Transaction confirmed on chain');
|
|
891
893
|
}
|
|
892
894
|
return submission;
|
|
893
895
|
});
|
|
894
896
|
});
|
|
895
|
-
|
|
896
|
-
* Get all existing transaction builds by address
|
|
897
|
-
* @param req - CDS request object (with address)
|
|
898
|
-
* @returns {AddressTransactionBuilds} Address transaction build associations
|
|
899
|
-
*/
|
|
897
|
+
// GetTransactionBuildsByAddress: address ↔ build associations.
|
|
900
898
|
srv.on('GetTransactionBuildsByAddress', async (req) => {
|
|
901
899
|
logger.debug('GetTransactionBuildsByAddress Action handler called');
|
|
902
900
|
const { address } = req.data;
|
|
903
|
-
// Validate inputs
|
|
904
901
|
if (!address)
|
|
905
902
|
return (0, errors_1.rejectMissing)(req, 'GetTransactionBuildsByAddress', 'address');
|
|
906
903
|
if (!(0, validators_1.isValidBech32Address)(address))
|
|
907
904
|
return (0, errors_1.rejectInvalid)(req, 'GetTransactionBuildsByAddress', 'Invalid bech32 address format', 'address');
|
|
908
|
-
|
|
909
|
-
return (0, backend_request_handler_1.handleRequest)(req,
|
|
910
|
-
return db.run(SELECT.from(AddressTransactionBuilds).where({ address_address: address }));
|
|
911
|
-
});
|
|
905
|
+
const { limit, skip } = (0, paging_1.pageOf)(req.data, 1000);
|
|
906
|
+
return (0, backend_request_handler_1.handleRequest)(req, (db) => db.run(SELECT.from(AddressTransactionBuilds).where({ address_address: address }).orderBy('txBuild_id').limit(limit, skip)));
|
|
912
907
|
});
|
|
913
|
-
|
|
914
|
-
* Derive the enterprise script address + script hash for a validator script,
|
|
915
|
-
* optionally after applying PlutusData parameters. No transaction is built.
|
|
916
|
-
*/
|
|
908
|
+
// DeriveScriptAddress: enterprise address + hash of a validator, optionally parameterized.
|
|
917
909
|
srv.on('DeriveScriptAddress', async (req) => {
|
|
918
910
|
const { validatorScript, scriptParamsJson, network } = req.data;
|
|
919
911
|
if (!validatorScript)
|
|
@@ -923,7 +915,7 @@ module.exports = (srv) => {
|
|
|
923
915
|
}
|
|
924
916
|
let scriptParams;
|
|
925
917
|
if (scriptParamsJson) {
|
|
926
|
-
const jsonResult = (0, validators_1.
|
|
918
|
+
const jsonResult = (0, validators_1.validatePlutusJson)(scriptParamsJson, 'scriptParamsJson');
|
|
927
919
|
if (!jsonResult.valid)
|
|
928
920
|
return (0, errors_1.rejectInvalid)(req, 'DeriveScriptAddress', jsonResult.error, 'scriptParamsJson');
|
|
929
921
|
if (!Array.isArray(jsonResult.parsed)) {
|
|
@@ -931,16 +923,9 @@ module.exports = (srv) => {
|
|
|
931
923
|
}
|
|
932
924
|
scriptParams = jsonResult.parsed;
|
|
933
925
|
}
|
|
934
|
-
if (network && !VALID_DERIVE_NETWORKS.includes(network)) {
|
|
935
|
-
return (0, errors_1.rejectInvalid)(req, 'DeriveScriptAddress', `Invalid network "${network}". Must be one of: ${VALID_DERIVE_NETWORKS.join(', ')}`, 'network');
|
|
936
|
-
}
|
|
937
926
|
return (0, backend_request_handler_1.handleRequest)(req, async () => {
|
|
938
|
-
//
|
|
939
|
-
|
|
940
|
-
// keeps that 503 distinct from the 400 used for malformed validatorScript bytes.
|
|
941
|
-
const targetNetwork = network
|
|
942
|
-
? network
|
|
943
|
-
: (0, server_1.getCardanoClient)().network;
|
|
927
|
+
// Resolved outside the CBOR try/catch so an uninitialized client stays a 503, not a 400.
|
|
928
|
+
const targetNetwork = network ?? (0, server_1.getCardanoClient)().network;
|
|
944
929
|
try {
|
|
945
930
|
const finalScript = scriptParams && scriptParams.length > 0
|
|
946
931
|
? (0, tx_build_helper_1.applyScriptParameters)(validatorScript, scriptParams)
|
|
@@ -955,10 +940,7 @@ module.exports = (srv) => {
|
|
|
955
940
|
}
|
|
956
941
|
});
|
|
957
942
|
});
|
|
958
|
-
|
|
959
|
-
* Extract the 28-byte payment credential (key hash or script hash) from a Bech32
|
|
960
|
-
* Cardano address. Pure local decoding — no blockchain call.
|
|
961
|
-
*/
|
|
943
|
+
// ExtractPaymentKeyHash: 28-byte payment credential of a bech32 address; pure local decoding.
|
|
962
944
|
srv.on('ExtractPaymentKeyHash', async (req) => {
|
|
963
945
|
const { address } = req.data;
|
|
964
946
|
if (!address)
|
|
@@ -969,7 +951,7 @@ module.exports = (srv) => {
|
|
|
969
951
|
try {
|
|
970
952
|
const decoded = bech32_1.bech32.decode(address, const_1.BECH32_MAX_LENGTH);
|
|
971
953
|
const bytes = Buffer.from(bech32_1.bech32.fromWords(decoded.words));
|
|
972
|
-
//
|
|
954
|
+
// 1 header byte + 28-byte payment credential (+ optional 28-byte stake credential)
|
|
973
955
|
if (bytes.length < 29) {
|
|
974
956
|
return (0, errors_1.rejectInvalid)(req, 'ExtractPaymentKeyHash', 'Address is too short to contain a payment credential', 'address');
|
|
975
957
|
}
|