@odatano/core 2.0.0-rc.2 → 2.0.0-rc.21
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 +251 -0
- package/@cds-models/CardanoAgentService/index.js +36 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +139 -2
- package/@cds-models/CardanoIndexerService/index.js +18 -0
- package/@cds-models/CardanoODataService/index.d.ts +231 -6
- package/@cds-models/CardanoODataService/index.js +27 -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 +323 -6
- package/@cds-models/odatano/cardano/index.js +33 -0
- package/README.md +195 -136
- package/db/schema.cds +2133 -1633
- package/package.json +181 -173
- package/scripts/migrate-slot-leader.mjs +104 -0
- package/scripts/migrate-txseq.mjs +147 -0
- package/src/index.d.ts +10 -10
- package/src/index.js +24 -14
- package/src/plugin.js +39 -52
- package/srv/blockchain/backends/blockfrost-backend.d.ts +29 -131
- package/srv/blockchain/backends/blockfrost-backend.js +45 -158
- package/srv/blockchain/backends/cardano-backend.d.ts +83 -158
- package/srv/blockchain/backends/cardano-backend.js +14 -5
- package/srv/blockchain/backends/koios-backend.d.ts +126 -60
- package/srv/blockchain/backends/koios-backend.js +293 -203
- package/srv/blockchain/backends/ogmios-backend.d.ts +69 -94
- package/srv/blockchain/backends/ogmios-backend.js +319 -185
- package/srv/blockchain/backends/ogmios-frame-guard.d.ts +32 -0
- package/srv/blockchain/backends/ogmios-frame-guard.js +142 -0
- package/srv/blockchain/cardano-client.d.ts +57 -204
- package/srv/blockchain/cardano-client.js +94 -226
- package/srv/blockchain/cardano-indexer.d.ts +176 -240
- package/srv/blockchain/cardano-indexer.js +615 -338
- package/srv/blockchain/cardano-tx-builder.d.ts +29 -82
- package/srv/blockchain/cardano-tx-builder.js +88 -128
- package/srv/blockchain/crawler/certificate-backfill.d.ts +45 -0
- package/srv/blockchain/crawler/certificate-backfill.js +150 -0
- package/srv/blockchain/crawler/crawler.d.ts +121 -53
- package/srv/blockchain/crawler/crawler.js +403 -89
- package/srv/blockchain/crawler/hooks.d.ts +3 -14
- package/srv/blockchain/crawler/index.d.ts +5 -0
- package/srv/blockchain/crawler/index.js +35 -7
- package/srv/blockchain/crawler/sync-state.d.ts +44 -28
- package/srv/blockchain/crawler/sync-state.js +115 -43
- package/srv/blockchain/crawler/utxo-set-import.d.ts +69 -0
- package/srv/blockchain/crawler/utxo-set-import.js +269 -0
- package/srv/blockchain/ledger-state.d.ts +80 -0
- package/srv/blockchain/ledger-state.js +383 -0
- package/srv/blockchain/signing/cose-verifier.d.ts +5 -12
- package/srv/blockchain/signing/cose-verifier.js +6 -12
- package/srv/blockchain/signing/external-signer.d.ts +16 -113
- package/srv/blockchain/signing/external-signer.js +19 -122
- package/srv/blockchain/signing/hsm-signer.d.ts +13 -45
- package/srv/blockchain/signing/hsm-signer.js +23 -63
- package/srv/blockchain/signing/signature-verifier.d.ts +8 -44
- package/srv/blockchain/signing/signature-verifier.js +13 -54
- package/srv/blockchain/signing/submission-finalizer.d.ts +9 -36
- package/srv/blockchain/signing/submission-finalizer.js +16 -48
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +71 -160
- package/srv/blockchain/transaction-building/buildooor-tx.js +185 -260
- package/srv/blockchain/transaction-building/cardano-tx.d.ts +8 -34
- package/srv/blockchain/transaction-rows.d.ts +12 -0
- package/srv/blockchain/transaction-rows.js +42 -0
- package/srv/blockchain/wallet-worker/build-request.d.ts +4 -17
- package/srv/blockchain/wallet-worker/build-request.js +39 -23
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +25 -17
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +87 -41
- package/srv/blockchain/wallet-worker/index.js +2 -8
- package/srv/blockchain/wallet-worker/job-store.d.ts +36 -103
- package/srv/blockchain/wallet-worker/job-store.js +0 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +4 -20
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +4 -20
- package/srv/blockchain/wallet-worker/signers.d.ts +6 -19
- package/srv/blockchain/wallet-worker/signers.js +3 -8
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +15 -71
- package/srv/blockchain/wallet-worker/wallet-worker.js +40 -91
- package/srv/cardano-agent-service.cds +146 -0
- package/srv/cardano-agent-service.d.ts +2 -0
- package/srv/cardano-agent-service.js +12 -0
- package/srv/cardano-indexer-service.cds +164 -73
- package/srv/cardano-indexer-service.js +156 -11
- package/srv/cardano-service.cds +45 -0
- package/srv/cardano-service.js +38 -96
- package/srv/cardano-sign-service.cds +200 -180
- package/srv/cardano-sign-service.js +55 -173
- package/srv/cardano-tx-service.cds +32 -26
- package/srv/cardano-tx-service.js +136 -196
- package/srv/cardano-worker-service.js +7 -15
- package/srv/server.d.ts +28 -86
- package/srv/server.js +95 -128
- package/srv/utils/agent-grants-config.d.ts +37 -0
- package/srv/utils/agent-grants-config.js +138 -0
- package/srv/utils/agent-grants.d.ts +262 -0
- package/srv/utils/agent-grants.js +914 -0
- package/srv/utils/agent-token-auth.d.ts +16 -0
- package/srv/utils/agent-token-auth.js +78 -0
- package/srv/utils/backend-request-handler.d.ts +2 -16
- package/srv/utils/backend-request-handler.js +3 -18
- package/srv/utils/collections.d.ts +4 -8
- package/srv/utils/collections.js +4 -8
- package/srv/utils/const.d.ts +19 -68
- package/srv/utils/const.js +20 -69
- package/srv/utils/crypto.d.ts +3 -12
- package/srv/utils/crypto.js +5 -18
- package/srv/utils/db-indexes.d.ts +25 -0
- package/srv/utils/db-indexes.js +93 -0
- package/srv/utils/db-sanitize.d.ts +27 -0
- package/srv/utils/db-sanitize.js +85 -0
- package/srv/utils/epoch-slots.d.ts +13 -0
- package/srv/utils/epoch-slots.js +25 -0
- package/srv/utils/error-codes.d.ts +18 -73
- package/srv/utils/error-codes.js +17 -70
- package/srv/utils/errors.d.ts +36 -179
- package/srv/utils/errors.js +62 -220
- package/srv/utils/liveness.d.ts +16 -0
- package/srv/utils/liveness.js +41 -0
- package/srv/utils/mappers.d.ts +89 -214
- package/srv/utils/mappers.js +289 -301
- package/srv/utils/network-context.js +2 -11
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +57 -0
- package/srv/utils/plutus-placeholders.d.ts +6 -18
- package/srv/utils/plutus-placeholders.js +6 -20
- package/srv/utils/rate-limiter.d.ts +46 -0
- package/srv/utils/rate-limiter.js +116 -0
- package/srv/utils/service-events.d.ts +2 -22
- package/srv/utils/service-events.js +3 -23
- package/srv/utils/signing-helper.d.ts +3 -18
- package/srv/utils/signing-helper.js +10 -32
- package/srv/utils/tx-build-helper.d.ts +18 -92
- package/srv/utils/tx-build-helper.js +32 -123
- package/srv/utils/tx-request-parsers.d.ts +14 -20
- package/srv/utils/tx-request-parsers.js +33 -20
- package/srv/utils/tx-utils.d.ts +4 -23
- package/srv/utils/tx-utils.js +12 -41
- package/srv/utils/types.d.ts +114 -169
- package/srv/utils/types.js +2 -6
- package/srv/utils/validators.d.ts +19 -89
- package/srv/utils/validators.js +31 -138
|
@@ -11,9 +11,7 @@ const signature_verifier_1 = require("./signature-verifier");
|
|
|
11
11
|
const errors_1 = require("../../utils/errors");
|
|
12
12
|
const types_1 = require("../../utils/types");
|
|
13
13
|
const logger = cds_1.default.log('ExternalSigner');
|
|
14
|
-
/**
|
|
15
|
-
* Default TTL for signing requests (30 minutes)
|
|
16
|
-
*/
|
|
14
|
+
/** Default TTL for signing requests (30 minutes). */
|
|
17
15
|
const DEFAULT_SIGNING_TTL_MS = 30 * 60 * 1000;
|
|
18
16
|
/** Network flag for cardano-cli (mainnet vs testnet-magic). */
|
|
19
17
|
const CLI_NETWORK_FLAG = {
|
|
@@ -22,10 +20,8 @@ const CLI_NETWORK_FLAG = {
|
|
|
22
20
|
preview: '--testnet-magic 2',
|
|
23
21
|
};
|
|
24
22
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* and the unsigned CBOR are filled in. Previously this was always NULL despite
|
|
28
|
-
* the schema exposing a `cardanoCliCommand` field.
|
|
23
|
+
* Copy-pasteable cardano-cli signing recipe for the unsigned tx (network flag and CBOR
|
|
24
|
+
* filled in; the caller supplies the signing key file).
|
|
29
25
|
*/
|
|
30
26
|
function buildCardanoCliCommand(network, unsignedTxCbor) {
|
|
31
27
|
const netFlag = CLI_NETWORK_FLAG[network] ?? '--testnet-magic <MAGIC>';
|
|
@@ -38,18 +34,8 @@ function buildCardanoCliCommand(network, unsignedTxCbor) {
|
|
|
38
34
|
].join('\n');
|
|
39
35
|
}
|
|
40
36
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* This module orchestrates the complete flow:
|
|
44
|
-
* 1. Create signing request from build result
|
|
45
|
-
* 2. Export unsigned transaction for external signing
|
|
46
|
-
* 3. Receive and verify signed transaction
|
|
47
|
-
* 4. Prepare for submission
|
|
48
|
-
*
|
|
49
|
-
* Key security principles:
|
|
50
|
-
* - NO private keys are ever handled by this service
|
|
51
|
-
* - Signature verification ensures transaction integrity
|
|
52
|
-
* - Complete audit trail of the signing workflow
|
|
37
|
+
* External signing workflow: create signing request → export unsigned tx → verify the
|
|
38
|
+
* signed tx → prepare submission. No private keys are ever handled here.
|
|
53
39
|
*/
|
|
54
40
|
class ExternalSignerModule {
|
|
55
41
|
verifier;
|
|
@@ -58,20 +44,7 @@ class ExternalSignerModule {
|
|
|
58
44
|
this.verifier = (0, signature_verifier_1.getSignatureVerifier)();
|
|
59
45
|
this.signingTtlMs = options?.signingTtlMs ?? DEFAULT_SIGNING_TTL_MS;
|
|
60
46
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Create an unsigned transaction export payload for external signing
|
|
63
|
-
*
|
|
64
|
-
* This is the entry point for the external signing workflow.
|
|
65
|
-
* Call this after building a transaction to get a standardized
|
|
66
|
-
* payload that external signers can use.
|
|
67
|
-
*
|
|
68
|
-
* @param buildId - The build ID from TransactionBuilds
|
|
69
|
-
* @param unsignedTxCbor - The unsigned transaction CBOR
|
|
70
|
-
* @param txBodyHash - The transaction body hash
|
|
71
|
-
* @param network - The Cardano network
|
|
72
|
-
* @param options - Additional options
|
|
73
|
-
* @returns Unsigned transaction export payload
|
|
74
|
-
*/
|
|
47
|
+
/** Create the unsigned-tx export payload that starts a signing request (workflow entry point). */
|
|
75
48
|
createSigningRequest(buildId, unsignedTxCbor, txBodyHash, network, message, options) {
|
|
76
49
|
const signingRequestId = cds_1.default.utils.uuid();
|
|
77
50
|
const now = new Date();
|
|
@@ -97,9 +70,7 @@ class ExternalSignerModule {
|
|
|
97
70
|
}, 'Created signing request');
|
|
98
71
|
return payload;
|
|
99
72
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Generate signing instructions for different signer types
|
|
102
|
-
*/
|
|
73
|
+
/** Signing instructions for the different signer types (CIP-30 request + cardano-cli recipe). */
|
|
103
74
|
generateSigningInstructions(unsignedTxCbor, network, message, signerTypeHint) {
|
|
104
75
|
return {
|
|
105
76
|
signerTypeHint,
|
|
@@ -112,20 +83,9 @@ class ExternalSignerModule {
|
|
|
112
83
|
cardanoCliCommand: buildCardanoCliCommand(network, unsignedTxCbor),
|
|
113
84
|
};
|
|
114
85
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Verify a signed transaction
|
|
117
|
-
*
|
|
118
|
-
* Call this after receiving a signed transaction from an external signer.
|
|
119
|
-
* This verifies the signature integrity before submission.
|
|
120
|
-
*
|
|
121
|
-
* @param signedTxCbor - The signed transaction CBOR
|
|
122
|
-
* @param expectedTxBodyHash - The expected transaction body hash (from the build)
|
|
123
|
-
* @param options - Additional verification options
|
|
124
|
-
* @returns Verification result
|
|
125
|
-
*/
|
|
86
|
+
/** Verify a signed tx against the build's body hash; enforces the signing TTL when `expiresAt` is given. */
|
|
126
87
|
verifySignedTransaction(signedTxCbor, expectedTxBodyHash, options) {
|
|
127
88
|
logger.debug({ expectedTxBodyHash }, 'Verifying signed transaction');
|
|
128
|
-
// Enforce signing TTL if expiresAt is provided
|
|
129
89
|
if (options?.expiresAt && this.isExpired(options.expiresAt)) {
|
|
130
90
|
return {
|
|
131
91
|
isValid: false,
|
|
@@ -156,14 +116,7 @@ class ExternalSignerModule {
|
|
|
156
116
|
}
|
|
157
117
|
return result;
|
|
158
118
|
}
|
|
159
|
-
/**
|
|
160
|
-
* Verify signed transaction and throw on failure
|
|
161
|
-
*
|
|
162
|
-
* @param signedTxCbor - The signed transaction CBOR
|
|
163
|
-
* @param expectedTxBodyHash - The expected transaction body hash
|
|
164
|
-
* @param options - Additional verification options
|
|
165
|
-
* @throws {TransactionValidationError} if verification fails
|
|
166
|
-
*/
|
|
119
|
+
/** Like verifySignedTransaction, but throws TransactionValidationError on failure. */
|
|
167
120
|
verifyOrThrow(signedTxCbor, expectedTxBodyHash, options) {
|
|
168
121
|
const result = this.verifySignedTransaction(signedTxCbor, expectedTxBodyHash, options);
|
|
169
122
|
if (!result.isValid) {
|
|
@@ -171,15 +124,7 @@ class ExternalSignerModule {
|
|
|
171
124
|
}
|
|
172
125
|
return result;
|
|
173
126
|
}
|
|
174
|
-
/**
|
|
175
|
-
* Create a complete signing workflow state
|
|
176
|
-
*
|
|
177
|
-
* This creates a trackable workflow state that can be stored
|
|
178
|
-
* and updated throughout the signing process.
|
|
179
|
-
*
|
|
180
|
-
* @param signingRequest - The unsigned transaction export payload
|
|
181
|
-
* @returns Initial workflow state
|
|
182
|
-
*/
|
|
127
|
+
/** Initial (pending) workflow state for a signing request. */
|
|
183
128
|
createWorkflowState(signingRequest) {
|
|
184
129
|
return {
|
|
185
130
|
status: types_1.SigningStatus.PENDING,
|
|
@@ -189,13 +134,7 @@ class ExternalSignerModule {
|
|
|
189
134
|
},
|
|
190
135
|
};
|
|
191
136
|
}
|
|
192
|
-
/**
|
|
193
|
-
* Update workflow state after signing
|
|
194
|
-
*
|
|
195
|
-
* @param state - Current workflow state
|
|
196
|
-
* @param signedTxCbor - The signed transaction CBOR
|
|
197
|
-
* @returns Updated workflow state
|
|
198
|
-
*/
|
|
137
|
+
/** Workflow state → signed. */
|
|
199
138
|
markAsSigned(state, signedTxCbor) {
|
|
200
139
|
return {
|
|
201
140
|
...state,
|
|
@@ -207,13 +146,7 @@ class ExternalSignerModule {
|
|
|
207
146
|
},
|
|
208
147
|
};
|
|
209
148
|
}
|
|
210
|
-
/**
|
|
211
|
-
* Update workflow state after verification
|
|
212
|
-
*
|
|
213
|
-
* @param state - Current workflow state
|
|
214
|
-
* @param verificationResult - The verification result
|
|
215
|
-
* @returns Updated workflow state
|
|
216
|
-
*/
|
|
149
|
+
/** Workflow state → verified, or → failed when the verification result is invalid. */
|
|
217
150
|
markAsVerified(state, verificationResult) {
|
|
218
151
|
if (!verificationResult.isValid) {
|
|
219
152
|
return this.markAsFailed(state, String(verificationResult.errorMessage));
|
|
@@ -228,13 +161,7 @@ class ExternalSignerModule {
|
|
|
228
161
|
},
|
|
229
162
|
};
|
|
230
163
|
}
|
|
231
|
-
/**
|
|
232
|
-
* Update workflow state after submission
|
|
233
|
-
*
|
|
234
|
-
* @param state - Current workflow state
|
|
235
|
-
* @param txHash - The submitted transaction hash
|
|
236
|
-
* @returns Updated workflow state
|
|
237
|
-
*/
|
|
164
|
+
/** Workflow state → submitted. */
|
|
238
165
|
markAsSubmitted(state, txHash) {
|
|
239
166
|
return {
|
|
240
167
|
...state,
|
|
@@ -246,13 +173,7 @@ class ExternalSignerModule {
|
|
|
246
173
|
},
|
|
247
174
|
};
|
|
248
175
|
}
|
|
249
|
-
/**
|
|
250
|
-
* Update workflow state on failure
|
|
251
|
-
*
|
|
252
|
-
* @param state - Current workflow state
|
|
253
|
-
* @param errorMessage - The error message
|
|
254
|
-
* @returns Updated workflow state
|
|
255
|
-
*/
|
|
176
|
+
/** Workflow state → failed. */
|
|
256
177
|
markAsFailed(state, errorMessage) {
|
|
257
178
|
return {
|
|
258
179
|
...state,
|
|
@@ -264,30 +185,12 @@ class ExternalSignerModule {
|
|
|
264
185
|
},
|
|
265
186
|
};
|
|
266
187
|
}
|
|
267
|
-
/**
|
|
268
|
-
* Check if a signing request has expired
|
|
269
|
-
*
|
|
270
|
-
* @param expiresAt - The expiration timestamp (ISO 8601)
|
|
271
|
-
* @returns true if expired
|
|
272
|
-
*/
|
|
188
|
+
/** True when the ISO-8601 `expiresAt` lies in the past. */
|
|
273
189
|
isExpired(expiresAt) {
|
|
274
190
|
return new Date(expiresAt) < new Date();
|
|
275
191
|
}
|
|
276
|
-
/**
|
|
277
|
-
* Validate a signed transaction payload before submission
|
|
278
|
-
*
|
|
279
|
-
* This performs all pre-submission checks:
|
|
280
|
-
* 1. Verify the signature
|
|
281
|
-
* 2. Check transaction hasn't been tampered with
|
|
282
|
-
* 3. Ensure the build ID matches
|
|
283
|
-
*
|
|
284
|
-
* @param payload - The signed transaction payload
|
|
285
|
-
* @param originalBuildTxBodyHash - The original transaction body hash from the build
|
|
286
|
-
* @returns Validation result with verification details
|
|
287
|
-
* @throws {TransactionValidationError} if validation fails
|
|
288
|
-
*/
|
|
192
|
+
/** Pre-submission check: signature + body-hash integrity against the build; throws TransactionValidationError. */
|
|
289
193
|
validateForSubmission(payload, originalBuildTxBodyHash) {
|
|
290
|
-
// Verify the signature and transaction integrity
|
|
291
194
|
const result = this.verifyOrThrow(payload.signedTxCbor, originalBuildTxBodyHash);
|
|
292
195
|
logger.info({
|
|
293
196
|
signingRequestId: payload.signingRequestId,
|
|
@@ -297,9 +200,7 @@ class ExternalSignerModule {
|
|
|
297
200
|
}, 'Transaction validated for submission');
|
|
298
201
|
return result;
|
|
299
202
|
}
|
|
300
|
-
/**
|
|
301
|
-
* Get the signature verifier instance
|
|
302
|
-
*/
|
|
203
|
+
/** The underlying signature verifier. */
|
|
303
204
|
getVerifier() {
|
|
304
205
|
return this.verifier;
|
|
305
206
|
}
|
|
@@ -307,18 +208,14 @@ class ExternalSignerModule {
|
|
|
307
208
|
exports.ExternalSignerModule = ExternalSignerModule;
|
|
308
209
|
// Singleton instance
|
|
309
210
|
let moduleInstance = null;
|
|
310
|
-
/**
|
|
311
|
-
* Get the singleton ExternalSignerModule instance
|
|
312
|
-
*/
|
|
211
|
+
/** Singleton ExternalSignerModule. */
|
|
313
212
|
function getExternalSignerModule() {
|
|
314
213
|
if (!moduleInstance) {
|
|
315
214
|
moduleInstance = new ExternalSignerModule();
|
|
316
215
|
}
|
|
317
216
|
return moduleInstance;
|
|
318
217
|
}
|
|
319
|
-
/**
|
|
320
|
-
* Create a new ExternalSignerModule instance with custom options
|
|
321
|
-
*/
|
|
218
|
+
/** New ExternalSignerModule with custom options (not the singleton). */
|
|
322
219
|
function createExternalSignerModule(options) {
|
|
323
220
|
return new ExternalSignerModule(options);
|
|
324
221
|
}
|
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
import { HsmConfig, HsmSignResult } from '../../utils/types';
|
|
2
|
-
/**
|
|
3
|
-
* HsmSigner - PKCS#11 Hardware Security Module integration for Cardano transaction signing
|
|
4
|
-
*
|
|
5
|
-
* Wraps a PKCS#11 compliant HSM (YubiHSM 2, AWS CloudHSM, Thales Luna, etc.)
|
|
6
|
-
* to provide Ed25519 signing for Cardano transactions. Private keys never leave
|
|
7
|
-
* the HSM chip — only signatures are returned.
|
|
8
|
-
*
|
|
9
|
-
* Lifecycle:
|
|
10
|
-
* 1. constructor(config) — stores config
|
|
11
|
-
* 2. init(network) — connects to HSM, locates key, exports public key, derives Cardano address
|
|
12
|
-
* 3. sign() / signTransaction() — signing operations
|
|
13
|
-
* 4. shutdown() — closes session
|
|
14
|
-
*/
|
|
15
|
-
/** Minimal PKCS#11 module surface used here. Loaded dynamically; no static type
|
|
16
|
-
* package is depended on. We narrow to the actual handful of methods invoked. */
|
|
2
|
+
/** Minimal PKCS#11 surface used here; pkcs11js is loaded dynamically, no static type package. */
|
|
17
3
|
interface PKCS11Instance {
|
|
18
4
|
load(path: string): void;
|
|
19
5
|
C_Initialize(): void;
|
|
@@ -43,6 +29,10 @@ type Pkcs11JsModule = {
|
|
|
43
29
|
type Pkcs11Loader = () => Pkcs11JsModule;
|
|
44
30
|
/** Test seam: inject a fake pkcs11js module loader (null restores the default). */
|
|
45
31
|
export declare function setPkcs11Loader(loader: Pkcs11Loader | null): void;
|
|
32
|
+
/**
|
|
33
|
+
* Ed25519 transaction signing through a PKCS#11 HSM (YubiHSM 2, CloudHSM, Luna, …); private
|
|
34
|
+
* keys never leave the device. Lifecycle: init(network) → sign()/signTransaction() → shutdown().
|
|
35
|
+
*/
|
|
46
36
|
export declare class HsmSigner {
|
|
47
37
|
private pkcs11;
|
|
48
38
|
private session;
|
|
@@ -54,30 +44,15 @@ export declare class HsmSigner {
|
|
|
54
44
|
private connected;
|
|
55
45
|
constructor(config: HsmConfig);
|
|
56
46
|
/**
|
|
57
|
-
*
|
|
58
|
-
* Called once
|
|
59
|
-
*
|
|
60
|
-
* @param network - Cardano network (mainnet, preview, preprod) for address derivation
|
|
47
|
+
* Open the PKCS#11 session, locate the key, export the public key and derive the
|
|
48
|
+
* enterprise address for `network`. Called once at startup.
|
|
61
49
|
*/
|
|
62
50
|
init(network: 'mainnet' | 'preview' | 'preprod' | string): Promise<void>;
|
|
63
|
-
/**
|
|
64
|
-
* Sign a 32-byte transaction body hash using the HSM.
|
|
65
|
-
* Returns the Ed25519 signature + public key for witness construction.
|
|
66
|
-
*
|
|
67
|
-
* @param txBodyHash - 32-byte hash of the transaction body
|
|
68
|
-
* @returns HsmSignResult with signature, public key, and key hash
|
|
69
|
-
*/
|
|
51
|
+
/** Sign a 32-byte tx body hash in the HSM; returns Ed25519 signature + public key for the witness. */
|
|
70
52
|
sign(txBodyHash: Buffer): HsmSignResult;
|
|
71
53
|
/**
|
|
72
|
-
* Sign
|
|
73
|
-
*
|
|
74
|
-
* Takes the unsigned transaction CBOR, signs the body hash via HSM,
|
|
75
|
-
* builds a VKey witness [publicKey, signature], and merges it into
|
|
76
|
-
* the transaction's witness set using raw CBOR manipulation.
|
|
77
|
-
*
|
|
78
|
-
* @param unsignedTxCbor - Unsigned transaction CBOR (hex)
|
|
79
|
-
* @param txBodyHash - Transaction body hash (hex, 64 chars)
|
|
80
|
-
* @returns Signed transaction CBOR (hex)
|
|
54
|
+
* Sign the body hash (hex) in the HSM, build the VKey witness [publicKey, signature] and
|
|
55
|
+
* splice it into the witness set at the raw CBOR level. Returns the signed tx CBOR (hex).
|
|
81
56
|
*/
|
|
82
57
|
signTransaction(unsignedTxCbor: string, txBodyHash: string): string;
|
|
83
58
|
/** Get the HSM-derived Cardano enterprise address */
|
|
@@ -96,9 +71,7 @@ export declare class HsmSigner {
|
|
|
96
71
|
};
|
|
97
72
|
/** Graceful shutdown — close PKCS#11 session */
|
|
98
73
|
shutdown(): void;
|
|
99
|
-
/**
|
|
100
|
-
* Find a key object (private or public) in the HSM by label and/or ID
|
|
101
|
-
*/
|
|
74
|
+
/** Find an Ed25519 key object (private or public) in the HSM by label and/or ID. */
|
|
102
75
|
private _findKey;
|
|
103
76
|
/**
|
|
104
77
|
* Export the Ed25519 public key from the HSM.
|
|
@@ -106,14 +79,9 @@ export declare class HsmSigner {
|
|
|
106
79
|
*/
|
|
107
80
|
private _exportPublicKey;
|
|
108
81
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Get the initialized HsmSigner instance.
|
|
111
|
-
* Returns null if HSM is not configured.
|
|
112
|
-
*/
|
|
82
|
+
/** The initialized HsmSigner, or null when HSM is not configured. */
|
|
113
83
|
export declare function getHsmSigner(): HsmSigner | null;
|
|
114
|
-
/**
|
|
115
|
-
* Set the HsmSigner instance (called during initialization and shutdown).
|
|
116
|
-
*/
|
|
84
|
+
/** Set the HsmSigner instance (initialization and shutdown). */
|
|
117
85
|
export declare function setHsmSigner(signer: HsmSigner | null): void;
|
|
118
86
|
export {};
|
|
119
87
|
//# sourceMappingURL=hsm-signer.d.ts.map
|
|
@@ -13,10 +13,7 @@ const error_codes_1 = require("../../utils/error-codes");
|
|
|
13
13
|
const cbor_1 = require("@harmoniclabs/cbor");
|
|
14
14
|
const uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
|
|
15
15
|
const logger = cds_1.default.log('HsmSigner');
|
|
16
|
-
/**
|
|
17
|
-
* CKM_EDDSA mechanism ID (PKCS#11 v3.0)
|
|
18
|
-
* Used for Ed25519 signing operations
|
|
19
|
-
*/
|
|
16
|
+
/** CKM_EDDSA mechanism ID (PKCS#11 v3.0) for Ed25519 signing. */
|
|
20
17
|
const CKM_EDDSA = 0x00001057;
|
|
21
18
|
const defaultPkcs11Loader = () => require('pkcs11js');
|
|
22
19
|
let pkcs11Loader = defaultPkcs11Loader;
|
|
@@ -24,6 +21,10 @@ let pkcs11Loader = defaultPkcs11Loader;
|
|
|
24
21
|
function setPkcs11Loader(loader) {
|
|
25
22
|
pkcs11Loader = loader ?? defaultPkcs11Loader;
|
|
26
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Ed25519 transaction signing through a PKCS#11 HSM (YubiHSM 2, CloudHSM, Luna, …); private
|
|
26
|
+
* keys never leave the device. Lifecycle: init(network) → sign()/signTransaction() → shutdown().
|
|
27
|
+
*/
|
|
27
28
|
class HsmSigner {
|
|
28
29
|
pkcs11;
|
|
29
30
|
session;
|
|
@@ -37,13 +38,11 @@ class HsmSigner {
|
|
|
37
38
|
this.config = config;
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
-
* Called once
|
|
42
|
-
*
|
|
43
|
-
* @param network - Cardano network (mainnet, preview, preprod) for address derivation
|
|
41
|
+
* Open the PKCS#11 session, locate the key, export the public key and derive the
|
|
42
|
+
* enterprise address for `network`. Called once at startup.
|
|
44
43
|
*/
|
|
45
44
|
async init(network) {
|
|
46
|
-
//
|
|
45
|
+
// pkcs11js is only loaded when HSM is configured
|
|
47
46
|
let pkcs11js;
|
|
48
47
|
try {
|
|
49
48
|
pkcs11js = pkcs11Loader();
|
|
@@ -53,31 +52,23 @@ class HsmSigner {
|
|
|
53
52
|
}
|
|
54
53
|
this.pkcs11 = new pkcs11js.PKCS11();
|
|
55
54
|
try {
|
|
56
|
-
// 1. Load PKCS#11 module
|
|
57
55
|
this.pkcs11.load(this.config.pkcs11Module);
|
|
58
56
|
this.pkcs11.C_Initialize();
|
|
59
57
|
logger.info({ module: this.config.pkcs11Module }, 'PKCS#11 module loaded');
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
// returned by C_GetSlotList(true), NOT the PKCS#11 slot ID (which need not be
|
|
63
|
-
// contiguous or zero-based). Configure it as a position, not a hardware slot id.
|
|
58
|
+
// `config.slot` is the 0-based index into C_GetSlotList(true) (slots with a token),
|
|
59
|
+
// not the PKCS#11 slot ID, which need not be contiguous or zero-based.
|
|
64
60
|
const slots = this.pkcs11.C_GetSlotList(true);
|
|
65
61
|
if (this.config.slot >= slots.length) {
|
|
66
62
|
throw new errors_1.HsmError(`HSM slot index ${this.config.slot} not found. ${slots.length} slot(s) with a token present (valid indices 0..${slots.length - 1}).`, 503, error_codes_1.ERROR_CODES.HSM_UNAVAILABLE);
|
|
67
63
|
}
|
|
68
64
|
this.session = this.pkcs11.C_OpenSession(slots[this.config.slot], pkcs11js.CKF_SERIAL_SESSION | pkcs11js.CKF_RW_SESSION);
|
|
69
65
|
logger.debug({ slot: this.config.slot }, 'PKCS#11 session opened');
|
|
70
|
-
// 3. Login with PIN
|
|
71
66
|
try {
|
|
72
67
|
this.pkcs11.C_Login(this.session, pkcs11js.CKU_USER, this.config.pin);
|
|
73
68
|
}
|
|
74
69
|
finally {
|
|
75
|
-
// Zero the PIN
|
|
76
|
-
//
|
|
77
|
-
// - this.config (shared reference with the server-side hsmConfigInstance
|
|
78
|
-
// singleton) — mutate IN PLACE, not reassign, so the singleton clears too
|
|
79
|
-
// - the HSM_PIN environment variable
|
|
80
|
-
// - the CAP config (cds.env.requires['odatano-core'].hsm.pin)
|
|
70
|
+
// Zero the PIN wherever it could linger (config object shared with the server
|
|
71
|
+
// singleton — mutate in place —, HSM_PIN env, CAP config), whatever the login outcome.
|
|
81
72
|
this.config.pin = '';
|
|
82
73
|
if (process.env.HSM_PIN)
|
|
83
74
|
delete process.env.HSM_PIN;
|
|
@@ -89,12 +80,10 @@ class HsmSigner {
|
|
|
89
80
|
catch { /* best effort */ }
|
|
90
81
|
}
|
|
91
82
|
logger.debug('PKCS#11 login successful');
|
|
92
|
-
// 4. Find private key by label or ID
|
|
93
83
|
this.privateKeyHandle = this._findKey(pkcs11js, pkcs11js.CKO_PRIVATE_KEY);
|
|
94
|
-
// 5. Export public key
|
|
95
84
|
this.publicKeyBytes = this._exportPublicKey(pkcs11js);
|
|
96
85
|
logger.debug({ publicKeyLength: this.publicKeyBytes.length }, 'Ed25519 public key exported');
|
|
97
|
-
//
|
|
86
|
+
// Key hash = blake2b-224 of the public key
|
|
98
87
|
const blake2b = require('blake2b');
|
|
99
88
|
const hashOut = Buffer.alloc(28);
|
|
100
89
|
blake2b(28).update(this.publicKeyBytes).digest(hashOut);
|
|
@@ -124,19 +113,12 @@ class HsmSigner {
|
|
|
124
113
|
throw new errors_1.HsmError(`Failed to initialize HSM: ${msg}`, 503, error_codes_1.ERROR_CODES.HSM_UNAVAILABLE, err);
|
|
125
114
|
}
|
|
126
115
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Sign a 32-byte transaction body hash using the HSM.
|
|
129
|
-
* Returns the Ed25519 signature + public key for witness construction.
|
|
130
|
-
*
|
|
131
|
-
* @param txBodyHash - 32-byte hash of the transaction body
|
|
132
|
-
* @returns HsmSignResult with signature, public key, and key hash
|
|
133
|
-
*/
|
|
116
|
+
/** Sign a 32-byte tx body hash in the HSM; returns Ed25519 signature + public key for the witness. */
|
|
134
117
|
sign(txBodyHash) {
|
|
135
118
|
if (!this.connected) {
|
|
136
119
|
throw new errors_1.HsmError('HSM not connected', 503, error_codes_1.ERROR_CODES.HSM_UNAVAILABLE);
|
|
137
120
|
}
|
|
138
121
|
try {
|
|
139
|
-
// CKM_EDDSA = Ed25519 signing mechanism
|
|
140
122
|
this.pkcs11.C_SignInit(this.session, { mechanism: CKM_EDDSA }, this.privateKeyHandle);
|
|
141
123
|
const signature = this.pkcs11.C_Sign(this.session, txBodyHash, Buffer.alloc(64));
|
|
142
124
|
return {
|
|
@@ -151,21 +133,13 @@ class HsmSigner {
|
|
|
151
133
|
}
|
|
152
134
|
}
|
|
153
135
|
/**
|
|
154
|
-
* Sign
|
|
155
|
-
*
|
|
156
|
-
* Takes the unsigned transaction CBOR, signs the body hash via HSM,
|
|
157
|
-
* builds a VKey witness [publicKey, signature], and merges it into
|
|
158
|
-
* the transaction's witness set using raw CBOR manipulation.
|
|
159
|
-
*
|
|
160
|
-
* @param unsignedTxCbor - Unsigned transaction CBOR (hex)
|
|
161
|
-
* @param txBodyHash - Transaction body hash (hex, 64 chars)
|
|
162
|
-
* @returns Signed transaction CBOR (hex)
|
|
136
|
+
* Sign the body hash (hex) in the HSM, build the VKey witness [publicKey, signature] and
|
|
137
|
+
* splice it into the witness set at the raw CBOR level. Returns the signed tx CBOR (hex).
|
|
163
138
|
*/
|
|
164
139
|
signTransaction(unsignedTxCbor, txBodyHash) {
|
|
165
140
|
const hashBytes = Buffer.from(txBodyHash, 'hex');
|
|
166
141
|
const result = this.sign(hashBytes);
|
|
167
142
|
try {
|
|
168
|
-
// Parse unsigned transaction at raw CBOR level
|
|
169
143
|
const txObj = cbor_1.Cbor.parse((0, uint8array_utils_1.fromHex)(unsignedTxCbor));
|
|
170
144
|
if (!(txObj instanceof cbor_1.CborArray) || txObj.array.length < 2) {
|
|
171
145
|
throw new errors_1.HsmError('Invalid transaction CBOR structure', 400, error_codes_1.ERROR_CODES.HSM_SIGNING_FAILED);
|
|
@@ -175,13 +149,10 @@ class HsmSigner {
|
|
|
175
149
|
new cbor_1.CborBytes(Buffer.from(result.publicKeyHex, 'hex')),
|
|
176
150
|
new cbor_1.CborBytes(Buffer.from(result.signatureHex, 'hex')),
|
|
177
151
|
]);
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
// Multi-sig (e.g. wallet + HSM) uses combineTransactionWithWitnesses() instead.
|
|
152
|
+
// Witness set (txObj.array[1]): vkey witnesses (key 0) become the HSM witness only — single
|
|
153
|
+
// signer; multi-sig goes through combineTransactionWithWitnesses(). Other keys are preserved.
|
|
181
154
|
const origWs = txObj.array[1];
|
|
182
155
|
if (origWs instanceof cbor_1.CborMap) {
|
|
183
|
-
// Replace VKey witnesses (key 0) with HSM witness only
|
|
184
|
-
// Preserves all other entries (redeemers key 5, datums key 4, scripts keys 1-3, 6-7)
|
|
185
156
|
const entries = origWs.map.filter((e) => !(e.k instanceof cbor_1.CborUInt && Number(e.k.num) === 0));
|
|
186
157
|
entries.push({
|
|
187
158
|
k: new cbor_1.CborUInt(0),
|
|
@@ -249,9 +220,7 @@ class HsmSigner {
|
|
|
249
220
|
// ---------------------------------------------------------------------------
|
|
250
221
|
// Private helpers
|
|
251
222
|
// ---------------------------------------------------------------------------
|
|
252
|
-
/**
|
|
253
|
-
* Find a key object (private or public) in the HSM by label and/or ID
|
|
254
|
-
*/
|
|
223
|
+
/** Find an Ed25519 key object (private or public) in the HSM by label and/or ID. */
|
|
255
224
|
_findKey(pkcs11js, objectClass) {
|
|
256
225
|
const template = [
|
|
257
226
|
{ type: pkcs11js.CKA_CLASS, value: objectClass },
|
|
@@ -280,12 +249,10 @@ class HsmSigner {
|
|
|
280
249
|
*/
|
|
281
250
|
_exportPublicKey(pkcs11js) {
|
|
282
251
|
const pubKeyHandle = this._findKey(pkcs11js, pkcs11js.CKO_PUBLIC_KEY);
|
|
283
|
-
// Extract CKA_EC_POINT (DER-encoded public key)
|
|
284
252
|
const attrs = this.pkcs11.C_GetAttributeValue(this.session, pubKeyHandle, [
|
|
285
253
|
{ type: pkcs11js.CKA_EC_POINT },
|
|
286
254
|
]);
|
|
287
|
-
// CKA_EC_POINT for Ed25519 is DER OCTET STRING
|
|
288
|
-
// Format: 04 20 <32 bytes>
|
|
255
|
+
// CKA_EC_POINT for Ed25519 is a DER OCTET STRING around the 32-byte key: 04 20 <32 bytes>
|
|
289
256
|
const ecPoint = Buffer.from(attrs[0].value);
|
|
290
257
|
if (ecPoint.length === 34 && ecPoint[0] === 0x04 && ecPoint[1] === 0x20) {
|
|
291
258
|
return ecPoint.slice(2);
|
|
@@ -298,20 +265,13 @@ class HsmSigner {
|
|
|
298
265
|
}
|
|
299
266
|
}
|
|
300
267
|
exports.HsmSigner = HsmSigner;
|
|
301
|
-
//
|
|
302
|
-
// Singleton pattern (same as getExternalSignerModule())
|
|
303
|
-
// ---------------------------------------------------------------------------
|
|
268
|
+
// Singleton instance
|
|
304
269
|
let hsmSignerInstance = null;
|
|
305
|
-
/**
|
|
306
|
-
* Get the initialized HsmSigner instance.
|
|
307
|
-
* Returns null if HSM is not configured.
|
|
308
|
-
*/
|
|
270
|
+
/** The initialized HsmSigner, or null when HSM is not configured. */
|
|
309
271
|
function getHsmSigner() {
|
|
310
272
|
return hsmSignerInstance;
|
|
311
273
|
}
|
|
312
|
-
/**
|
|
313
|
-
* Set the HsmSigner instance (called during initialization and shutdown).
|
|
314
|
-
*/
|
|
274
|
+
/** Set the HsmSigner instance (initialization and shutdown). */
|
|
315
275
|
function setHsmSigner(signer) {
|
|
316
276
|
hsmSignerInstance = signer;
|
|
317
277
|
}
|
|
@@ -1,56 +1,20 @@
|
|
|
1
1
|
import { SignatureVerificationResult, VerificationOptions } from '../../utils/types';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* This module provides signature verification for externally signed transactions.
|
|
6
|
-
* It ensures:
|
|
7
|
-
* 1. The signed CBOR is valid and parseable
|
|
8
|
-
* 2. The transaction body hash matches the expected hash (integrity check)
|
|
9
|
-
* 3. Required signatures are present (when specified)
|
|
10
|
-
* 4. No tampering occurred between build and sign steps
|
|
3
|
+
* Verifies externally signed transactions without private keys: CBOR parse, body-hash
|
|
4
|
+
* integrity against the build, required signers present, Ed25519 check per witness.
|
|
11
5
|
*/
|
|
12
6
|
export declare class SignatureVerifier {
|
|
13
|
-
/**
|
|
14
|
-
* Verify a signed transaction
|
|
15
|
-
*
|
|
16
|
-
* @param signedTxCbor - The signed transaction in CBOR hex format
|
|
17
|
-
* @param options - Verification options
|
|
18
|
-
* @returns Verification result with details
|
|
19
|
-
*/
|
|
7
|
+
/** Verify a signed tx (hex CBOR); all failures are reported in the result, never thrown. */
|
|
20
8
|
verify(signedTxCbor: string, options?: VerificationOptions): SignatureVerificationResult;
|
|
21
|
-
/**
|
|
22
|
-
* Verify signature and throw on failure
|
|
23
|
-
*
|
|
24
|
-
* @param signedTxCbor - The signed transaction in CBOR hex format
|
|
25
|
-
* @param options - Verification options
|
|
26
|
-
* @throws {TransactionValidationError} if verification fails
|
|
27
|
-
*/
|
|
9
|
+
/** Like verify(), but throws TransactionValidationError on failure. */
|
|
28
10
|
verifyOrThrow(signedTxCbor: string, options?: VerificationOptions): SignatureVerificationResult;
|
|
29
|
-
/**
|
|
30
|
-
* Extract transaction body hash from unsigned or signed CBOR
|
|
31
|
-
*
|
|
32
|
-
* @param txCbor - Transaction CBOR (signed or unsigned)
|
|
33
|
-
* @returns Transaction body hash as hex string
|
|
34
|
-
* @throws {Error} if CBOR is invalid
|
|
35
|
-
*/
|
|
11
|
+
/** Body hash (hex) of an unsigned or signed tx CBOR; throws BackendError 400 on invalid CBOR. */
|
|
36
12
|
extractTxBodyHash(txCbor: string): string;
|
|
37
|
-
/**
|
|
38
|
-
* Check if a transaction is signed (has witnesses)
|
|
39
|
-
*
|
|
40
|
-
* @param txCbor - Transaction CBOR
|
|
41
|
-
* @returns true if transaction has at least one witness
|
|
42
|
-
*/
|
|
13
|
+
/** True when the tx carries at least one vkey witness. */
|
|
43
14
|
isSigned(txCbor: string): boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Get witness count from transaction
|
|
46
|
-
*
|
|
47
|
-
* @param txCbor - Transaction CBOR
|
|
48
|
-
* @returns Number of witnesses
|
|
49
|
-
*/
|
|
15
|
+
/** Number of vkey witnesses in the tx (0 on invalid CBOR). */
|
|
50
16
|
getWitnessCount(txCbor: string): number;
|
|
51
17
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Get the singleton SignatureVerifier instance
|
|
54
|
-
*/
|
|
18
|
+
/** Singleton SignatureVerifier. */
|
|
55
19
|
export declare function getSignatureVerifier(): SignatureVerifier;
|
|
56
20
|
//# sourceMappingURL=signature-verifier.d.ts.map
|