@odatano/core 2.0.0-rc.4 → 2.0.0-rc.40
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/CardanoBackendService/index.d.ts +357 -0
- package/@cds-models/CardanoBackendService/index.js +40 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +194 -2
- package/@cds-models/CardanoIndexerService/index.js +25 -0
- package/@cds-models/CardanoODataService/index.d.ts +305 -7
- package/@cds-models/CardanoODataService/index.js +34 -0
- package/@cds-models/CardanoSignService/index.d.ts +28 -3
- package/@cds-models/CardanoSignService/index.js +3 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +46 -13
- package/@cds-models/CardanoTransactionService/index.js +4 -0
- package/@cds-models/index.d.ts +72 -0
- package/@cds-models/index.js +12 -0
- package/@cds-models/odatano/cardano/index.d.ts +408 -7
- package/@cds-models/odatano/cardano/index.js +42 -0
- package/README.md +64 -5
- package/db/schema.cds +2297 -1633
- package/db/types.cds +114 -0
- package/package.json +27 -6
- package/scripts/migrate-slot-leader.mjs +104 -0
- package/scripts/migrate-txseq.mjs +147 -0
- package/src/index.d.ts +18 -11
- package/src/index.js +44 -14
- package/src/plugin.js +42 -52
- package/srv/blockchain/backends/blockfrost-backend.d.ts +29 -131
- package/srv/blockchain/backends/blockfrost-backend.js +53 -162
- package/srv/blockchain/backends/cardano-backend.d.ts +104 -158
- package/srv/blockchain/backends/cardano-backend.js +19 -5
- package/srv/blockchain/backends/koios-backend.d.ts +126 -60
- package/srv/blockchain/backends/koios-backend.js +376 -235
- package/srv/blockchain/backends/odatano-backend.d.ts +64 -0
- package/srv/blockchain/backends/odatano-backend.js +170 -0
- package/srv/blockchain/backends/ogmios-backend.d.ts +162 -94
- package/srv/blockchain/backends/ogmios-backend.js +556 -201
- 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 +87 -202
- package/srv/blockchain/cardano-client.js +210 -240
- package/srv/blockchain/cardano-indexer.d.ts +311 -242
- package/srv/blockchain/cardano-indexer.js +1238 -362
- package/srv/blockchain/cardano-tx-builder.d.ts +44 -81
- package/srv/blockchain/cardano-tx-builder.js +173 -132
- package/srv/blockchain/crawler/certificate-backfill.d.ts +67 -0
- package/srv/blockchain/crawler/certificate-backfill.js +169 -0
- package/srv/blockchain/crawler/crawler.d.ts +139 -54
- package/srv/blockchain/crawler/crawler.js +478 -96
- 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 +46 -28
- package/srv/blockchain/crawler/sync-state.js +128 -43
- package/srv/blockchain/crawler/transaction-backfill.d.ts +52 -0
- package/srv/blockchain/crawler/transaction-backfill.js +283 -0
- package/srv/blockchain/crawler/utxo-set-import.d.ts +78 -0
- package/srv/blockchain/crawler/utxo-set-import.js +363 -0
- package/srv/blockchain/ledger-state.d.ts +101 -0
- package/srv/blockchain/ledger-state.js +432 -0
- package/srv/blockchain/pending-spends.d.ts +28 -0
- package/srv/blockchain/pending-spends.js +89 -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 +14 -45
- package/srv/blockchain/signing/signature-verifier.js +73 -76
- package/srv/blockchain/signing/submission-finalizer.d.ts +9 -36
- package/srv/blockchain/signing/submission-finalizer.js +16 -49
- package/srv/blockchain/stored-chain.d.ts +33 -0
- package/srv/blockchain/stored-chain.js +234 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +111 -173
- package/srv/blockchain/transaction-building/buildooor-tx.js +649 -326
- package/srv/blockchain/transaction-building/caller-datums.d.ts +29 -0
- package/srv/blockchain/transaction-building/caller-datums.js +139 -0
- package/srv/blockchain/transaction-building/cardano-tx.d.ts +10 -35
- package/srv/blockchain/transaction-rows.d.ts +12 -0
- package/srv/blockchain/transaction-rows.js +43 -0
- package/srv/blockchain/wallet-worker/build-request.d.ts +4 -17
- package/srv/blockchain/wallet-worker/build-request.js +18 -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-backend-service.cds +52 -0
- package/srv/cardano-backend-service.d.ts +2 -0
- package/srv/cardano-backend-service.js +244 -0
- package/srv/cardano-indexer-service.cds +201 -73
- package/srv/cardano-indexer-service.js +280 -11
- package/srv/cardano-service.cds +65 -2
- package/srv/cardano-service.js +170 -129
- package/srv/cardano-sign-service.cds +200 -180
- package/srv/cardano-sign-service.js +57 -184
- package/srv/cardano-tx-service.cds +439 -370
- package/srv/cardano-tx-service.js +231 -190
- package/srv/cardano-ui.cds +16 -16
- package/srv/cardano-worker-service.js +7 -15
- package/srv/cbor/index.d.ts +1 -1
- package/srv/cbor/parse.d.ts +27 -0
- package/srv/cbor/parse.js +63 -2
- package/srv/server.d.ts +28 -86
- package/srv/server.js +132 -135
- 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 +922 -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 +30 -67
- package/srv/utils/const.js +32 -72
- 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 +94 -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 +20 -0
- package/srv/utils/epoch-slots.js +43 -0
- package/srv/utils/error-codes.d.ts +20 -73
- package/srv/utils/error-codes.js +19 -70
- package/srv/utils/errors.d.ts +50 -177
- package/srv/utils/errors.js +94 -218
- package/srv/utils/legacy-service-paths.d.ts +11 -0
- package/srv/utils/legacy-service-paths.js +58 -0
- package/srv/utils/liveness.d.ts +16 -0
- package/srv/utils/liveness.js +40 -0
- package/srv/utils/mappers.d.ts +128 -214
- package/srv/utils/mappers.js +354 -299
- package/srv/utils/network-context.js +2 -11
- package/srv/utils/pg-inline-json.d.ts +8 -0
- package/srv/utils/pg-inline-json.js +62 -0
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +56 -0
- package/srv/utils/plutus-placeholders.d.ts +23 -16
- package/srv/utils/plutus-placeholders.js +63 -27
- 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 +6 -16
- package/srv/utils/signing-helper.js +36 -60
- package/srv/utils/tx-build-helper.d.ts +28 -93
- package/srv/utils/tx-build-helper.js +75 -124
- package/srv/utils/tx-request-parsers.d.ts +101 -28
- package/srv/utils/tx-request-parsers.js +410 -43
- package/srv/utils/tx-utils.d.ts +4 -23
- package/srv/utils/tx-utils.js +12 -41
- package/srv/utils/types.d.ts +252 -179
- package/srv/utils/types.js +2 -6
- package/srv/utils/validators.d.ts +24 -90
- package/srv/utils/validators.js +50 -147
|
@@ -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,25 @@
|
|
|
1
|
-
import { SignatureVerificationResult, VerificationOptions } from '../../utils/types';
|
|
1
|
+
import { SignatureVerificationResult, TxWitnessVerification, VerificationOptions } from '../../utils/types';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* 4. No tampering occurred between build and sign steps
|
|
3
|
+
* Check every vkey witness of a signed tx against its body hash. Pure, needs no initialize().
|
|
4
|
+
* A tx without vkey witnesses is valid here; which keys had to sign is the caller's check.
|
|
5
|
+
*/
|
|
6
|
+
export declare function verifyTxWitnesses(signedTxCbor: string): TxWitnessVerification;
|
|
7
|
+
/**
|
|
8
|
+
* Verifies externally signed transactions without private keys: CBOR parse, body-hash
|
|
9
|
+
* integrity against the build, required signers present, Ed25519 check per witness.
|
|
11
10
|
*/
|
|
12
11
|
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
|
-
*/
|
|
12
|
+
/** Verify a signed tx (hex CBOR); all failures are reported in the result, never thrown. */
|
|
20
13
|
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
|
-
*/
|
|
14
|
+
/** Like verify(), but throws TransactionValidationError on failure. */
|
|
28
15
|
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
|
-
*/
|
|
16
|
+
/** Body hash (hex) of an unsigned or signed tx CBOR; throws BackendError 400 on invalid CBOR. */
|
|
36
17
|
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
|
-
*/
|
|
18
|
+
/** True when the tx carries at least one vkey witness. */
|
|
43
19
|
isSigned(txCbor: string): boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Get witness count from transaction
|
|
46
|
-
*
|
|
47
|
-
* @param txCbor - Transaction CBOR
|
|
48
|
-
* @returns Number of witnesses
|
|
49
|
-
*/
|
|
20
|
+
/** Number of vkey witnesses in the tx (0 on invalid CBOR). */
|
|
50
21
|
getWitnessCount(txCbor: string): number;
|
|
51
22
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Get the singleton SignatureVerifier instance
|
|
54
|
-
*/
|
|
23
|
+
/** Singleton SignatureVerifier. */
|
|
55
24
|
export declare function getSignatureVerifier(): SignatureVerifier;
|
|
56
25
|
//# sourceMappingURL=signature-verifier.d.ts.map
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.SignatureVerifier = void 0;
|
|
7
|
+
exports.verifyTxWitnesses = verifyTxWitnesses;
|
|
7
8
|
exports.getSignatureVerifier = getSignatureVerifier;
|
|
8
9
|
const cds_1 = __importDefault(require("@sap/cds"));
|
|
9
10
|
const cbor_1 = require("@harmoniclabs/cbor");
|
|
@@ -13,11 +14,8 @@ const errors_1 = require("../../utils/errors");
|
|
|
13
14
|
const error_codes_1 = require("../../utils/error-codes");
|
|
14
15
|
const logger = cds_1.default.log('SignatureVerifier');
|
|
15
16
|
/**
|
|
16
|
-
* blake2b-256 over the ORIGINAL transaction-body bytes (CBOR array index 0).
|
|
17
|
-
*
|
|
18
|
-
* and re-serialization can never drift it. (Historically this also sidestepped the
|
|
19
|
-
* ledger-ts AuxiliaryData.fromCbor bug on metadata-only aux_data — fixed in 0.5.6 —
|
|
20
|
-
* but byte-exactness remains the reason to stay at the raw-CBOR level.)
|
|
17
|
+
* blake2b-256 over the ORIGINAL transaction-body bytes (CBOR array index 0). subCborRef
|
|
18
|
+
* preserves the exact received bytes, so the hash matches what was signed.
|
|
21
19
|
*/
|
|
22
20
|
function computeBodyHash(txBytes) {
|
|
23
21
|
const tx = cbor_1.Cbor.parse(txBytes);
|
|
@@ -27,63 +25,89 @@ function computeBodyHash(txBytes) {
|
|
|
27
25
|
return (0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_256)(tx.array[0].subCborRef.toBuffer()));
|
|
28
26
|
}
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
28
|
+
* Read vkey witnesses (raw public key + signature bytes) from a transaction's witness set
|
|
29
|
+
* (CBOR array index 1, map key 0). Conway wraps the witness array in CBOR tag 258 (set),
|
|
30
|
+
* which is unwrapped here. `malformed` counts entries that are not a [vkey, signature] byte pair.
|
|
33
31
|
*/
|
|
34
|
-
function
|
|
32
|
+
function readVkeyWitnesses(txBytes) {
|
|
35
33
|
const tx = cbor_1.Cbor.parse(txBytes);
|
|
36
34
|
if (!(tx instanceof cbor_1.CborArray) || !(tx.array[1] instanceof cbor_1.CborMap))
|
|
37
|
-
return [];
|
|
35
|
+
return { witnesses: [], malformed: 0 };
|
|
38
36
|
const entry = tx.array[1].map.find(e => e.k instanceof cbor_1.CborUInt && Number(e.k.num) === 0);
|
|
39
37
|
if (!entry)
|
|
40
|
-
return [];
|
|
38
|
+
return { witnesses: [], malformed: 0 };
|
|
41
39
|
let arr = entry.v;
|
|
42
40
|
if (arr instanceof cbor_1.CborTag)
|
|
43
41
|
arr = arr.data;
|
|
44
42
|
if (!(arr instanceof cbor_1.CborArray))
|
|
45
|
-
return [];
|
|
46
|
-
// Parse defensively: a malformed witness entry (wrong shape or non-bytes members)
|
|
47
|
-
// would otherwise throw a TypeError and surface as an opaque internal error. Skip
|
|
48
|
-
// any entry that isn't a [vkey, signature] pair of byte strings.
|
|
43
|
+
return { witnesses: [], malformed: 0 };
|
|
49
44
|
const witnesses = [];
|
|
50
|
-
let
|
|
51
|
-
|
|
45
|
+
let malformed = 0;
|
|
46
|
+
arr.array.forEach((pair, index) => {
|
|
52
47
|
if (!(pair instanceof cbor_1.CborArray) || pair.array.length !== 2) {
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
malformed++;
|
|
49
|
+
return;
|
|
55
50
|
}
|
|
56
51
|
const [vk, sg] = pair.array;
|
|
57
52
|
if (!(vk instanceof cbor_1.CborBytes) || !(sg instanceof cbor_1.CborBytes)) {
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
malformed++;
|
|
54
|
+
return;
|
|
60
55
|
}
|
|
61
|
-
witnesses.push({ pubKey: vk.bytes, signature: sg.bytes });
|
|
62
|
-
}
|
|
63
|
-
|
|
56
|
+
witnesses.push({ index, pubKey: vk.bytes, signature: sg.bytes });
|
|
57
|
+
});
|
|
58
|
+
return { witnesses, malformed };
|
|
59
|
+
}
|
|
60
|
+
/** Well-formed vkey witnesses; [] when the tx has none. Malformed entries are skipped with a warning. */
|
|
61
|
+
function extractVkeyWitnesses(txBytes) {
|
|
62
|
+
const { witnesses, malformed } = readVkeyWitnesses(txBytes);
|
|
63
|
+
if (malformed > 0) {
|
|
64
64
|
// not fatal, but a malformed witness in a signed tx is suspicious — surface it
|
|
65
|
-
logger.warn(`Skipped ${
|
|
65
|
+
logger.warn(`Skipped ${malformed} malformed vkey witness entr${malformed === 1 ? 'y' : 'ies'} while verifying (expected [vkey, signature] byte pairs)`);
|
|
66
66
|
}
|
|
67
67
|
return witnesses;
|
|
68
68
|
}
|
|
69
|
+
/** Ed25519 check of one witness over the body hash; false for a key or signature of the wrong length. */
|
|
70
|
+
function signatureValid(w, bodyHashBytes) {
|
|
71
|
+
try {
|
|
72
|
+
return (0, crypto_1.verifyEd25519Signature_sync)(w.signature, bodyHashBytes, w.pubKey);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check every vkey witness of a signed tx against its body hash. Pure, needs no initialize().
|
|
80
|
+
* A tx without vkey witnesses is valid here; which keys had to sign is the caller's check.
|
|
81
|
+
*/
|
|
82
|
+
function verifyTxWitnesses(signedTxCbor) {
|
|
83
|
+
const errors = [];
|
|
84
|
+
const signerKeyHashes = [];
|
|
85
|
+
let txBodyHash = null;
|
|
86
|
+
try {
|
|
87
|
+
const txBytes = (0, uint8array_utils_1.fromHex)(signedTxCbor);
|
|
88
|
+
txBodyHash = computeBodyHash(txBytes);
|
|
89
|
+
const bodyHashBytes = (0, uint8array_utils_1.fromHex)(txBodyHash);
|
|
90
|
+
const { witnesses, malformed } = readVkeyWitnesses(txBytes);
|
|
91
|
+
if (malformed > 0)
|
|
92
|
+
errors.push(`${malformed} vkey witness entr${malformed === 1 ? 'y is' : 'ies are'} not a [vkey, signature] byte pair`);
|
|
93
|
+
for (const w of witnesses) {
|
|
94
|
+
if (signatureValid(w, bodyHashBytes))
|
|
95
|
+
signerKeyHashes.push((0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_224)(w.pubKey)));
|
|
96
|
+
else
|
|
97
|
+
errors.push(`Invalid signature at witness index ${w.index}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
errors.push(`Failed to read transaction: ${error instanceof Error ? error.message : String(error)}`);
|
|
102
|
+
}
|
|
103
|
+
return { valid: errors.length === 0, txBodyHash, signerKeyHashes, errors };
|
|
104
|
+
}
|
|
69
105
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* This module provides signature verification for externally signed transactions.
|
|
73
|
-
* It ensures:
|
|
74
|
-
* 1. The signed CBOR is valid and parseable
|
|
75
|
-
* 2. The transaction body hash matches the expected hash (integrity check)
|
|
76
|
-
* 3. Required signatures are present (when specified)
|
|
77
|
-
* 4. No tampering occurred between build and sign steps
|
|
106
|
+
* Verifies externally signed transactions without private keys: CBOR parse, body-hash
|
|
107
|
+
* integrity against the build, required signers present, Ed25519 check per witness.
|
|
78
108
|
*/
|
|
79
109
|
class SignatureVerifier {
|
|
80
|
-
/**
|
|
81
|
-
* Verify a signed transaction
|
|
82
|
-
*
|
|
83
|
-
* @param signedTxCbor - The signed transaction in CBOR hex format
|
|
84
|
-
* @param options - Verification options
|
|
85
|
-
* @returns Verification result with details
|
|
86
|
-
*/
|
|
110
|
+
/** Verify a signed tx (hex CBOR); all failures are reported in the result, never thrown. */
|
|
87
111
|
verify(signedTxCbor, options = {}) {
|
|
88
112
|
const result = {
|
|
89
113
|
isValid: false,
|
|
@@ -93,8 +117,7 @@ class SignatureVerifier {
|
|
|
93
117
|
warnings: [],
|
|
94
118
|
};
|
|
95
119
|
try {
|
|
96
|
-
//
|
|
97
|
-
// matching exactly what was signed.
|
|
120
|
+
// Body hash over the original body bytes — exactly what was signed.
|
|
98
121
|
const txBytes = (0, uint8array_utils_1.fromHex)(signedTxCbor);
|
|
99
122
|
const computedHash = computeBodyHash(txBytes);
|
|
100
123
|
result.txBodyHash = computedHash;
|
|
@@ -137,11 +160,9 @@ class SignatureVerifier {
|
|
|
137
160
|
// verify each signature cryptographically against the transaction body hash
|
|
138
161
|
if (vkeyWitnesses.length > 0) {
|
|
139
162
|
const bodyHashBytes = (0, uint8array_utils_1.fromHex)(computedHash);
|
|
140
|
-
for (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (!isValidSig) {
|
|
144
|
-
result.errorMessage = `Invalid signature at witness index ${i}. The signature does not match the transaction body.`;
|
|
163
|
+
for (const w of vkeyWitnesses) {
|
|
164
|
+
if (!signatureValid(w, bodyHashBytes)) {
|
|
165
|
+
result.errorMessage = `Invalid signature at witness index ${w.index}. The signature does not match the transaction body.`;
|
|
145
166
|
logger.warn(result.errorMessage);
|
|
146
167
|
return result;
|
|
147
168
|
}
|
|
@@ -159,13 +180,7 @@ class SignatureVerifier {
|
|
|
159
180
|
}
|
|
160
181
|
return result;
|
|
161
182
|
}
|
|
162
|
-
/**
|
|
163
|
-
* Verify signature and throw on failure
|
|
164
|
-
*
|
|
165
|
-
* @param signedTxCbor - The signed transaction in CBOR hex format
|
|
166
|
-
* @param options - Verification options
|
|
167
|
-
* @throws {TransactionValidationError} if verification fails
|
|
168
|
-
*/
|
|
183
|
+
/** Like verify(), but throws TransactionValidationError on failure. */
|
|
169
184
|
verifyOrThrow(signedTxCbor, options = {}) {
|
|
170
185
|
const result = this.verify(signedTxCbor, options);
|
|
171
186
|
if (!result.isValid) {
|
|
@@ -173,13 +188,7 @@ class SignatureVerifier {
|
|
|
173
188
|
}
|
|
174
189
|
return result;
|
|
175
190
|
}
|
|
176
|
-
/**
|
|
177
|
-
* Extract transaction body hash from unsigned or signed CBOR
|
|
178
|
-
*
|
|
179
|
-
* @param txCbor - Transaction CBOR (signed or unsigned)
|
|
180
|
-
* @returns Transaction body hash as hex string
|
|
181
|
-
* @throws {Error} if CBOR is invalid
|
|
182
|
-
*/
|
|
191
|
+
/** Body hash (hex) of an unsigned or signed tx CBOR; throws BackendError 400 on invalid CBOR. */
|
|
183
192
|
extractTxBodyHash(txCbor) {
|
|
184
193
|
try {
|
|
185
194
|
return computeBodyHash((0, uint8array_utils_1.fromHex)(txCbor));
|
|
@@ -189,12 +198,7 @@ class SignatureVerifier {
|
|
|
189
198
|
throw new errors_1.BackendError(`Failed to extract transaction body hash: ${msg}`, 400, error_codes_1.ERROR_CODES.INVALID_INPUT);
|
|
190
199
|
}
|
|
191
200
|
}
|
|
192
|
-
/**
|
|
193
|
-
* Check if a transaction is signed (has witnesses)
|
|
194
|
-
*
|
|
195
|
-
* @param txCbor - Transaction CBOR
|
|
196
|
-
* @returns true if transaction has at least one witness
|
|
197
|
-
*/
|
|
201
|
+
/** True when the tx carries at least one vkey witness. */
|
|
198
202
|
isSigned(txCbor) {
|
|
199
203
|
try {
|
|
200
204
|
return extractVkeyWitnesses((0, uint8array_utils_1.fromHex)(txCbor)).length > 0;
|
|
@@ -203,12 +207,7 @@ class SignatureVerifier {
|
|
|
203
207
|
return false;
|
|
204
208
|
}
|
|
205
209
|
}
|
|
206
|
-
/**
|
|
207
|
-
* Get witness count from transaction
|
|
208
|
-
*
|
|
209
|
-
* @param txCbor - Transaction CBOR
|
|
210
|
-
* @returns Number of witnesses
|
|
211
|
-
*/
|
|
210
|
+
/** Number of vkey witnesses in the tx (0 on invalid CBOR). */
|
|
212
211
|
getWitnessCount(txCbor) {
|
|
213
212
|
try {
|
|
214
213
|
return extractVkeyWitnesses((0, uint8array_utils_1.fromHex)(txCbor)).length;
|
|
@@ -221,9 +220,7 @@ class SignatureVerifier {
|
|
|
221
220
|
exports.SignatureVerifier = SignatureVerifier;
|
|
222
221
|
// Singleton instance
|
|
223
222
|
let verifierInstance = null;
|
|
224
|
-
/**
|
|
225
|
-
* Get the singleton SignatureVerifier instance
|
|
226
|
-
*/
|
|
223
|
+
/** Singleton SignatureVerifier. */
|
|
227
224
|
function getSignatureVerifier() {
|
|
228
225
|
if (!verifierInstance) {
|
|
229
226
|
verifierInstance = new SignatureVerifier();
|
|
@@ -15,48 +15,21 @@ export interface FinalizeParams {
|
|
|
15
15
|
signerInfo?: string;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Submit a
|
|
19
|
-
* outcome
|
|
20
|
-
*
|
|
21
|
-
* The caller must already have durably committed `status:'submitting'` (the
|
|
22
|
-
* claim). This function then:
|
|
23
|
-
* 1. submits with NO DB write-lock held (the lock was the reason concurrent
|
|
24
|
-
* submits serialized and the SQLite busy-timeout could fire mid-network),
|
|
25
|
-
* 2. on submit failure: marks the request 'failed' in its own committed tx
|
|
26
|
-
* and rethrows (NOT rolled back to a pre-submit status — losing the
|
|
27
|
-
* attempt would hide a tx the node may have accepted),
|
|
28
|
-
* 3. on success: persists the submission + 'submitted' status in a committed
|
|
29
|
-
* transaction.
|
|
30
|
-
*
|
|
31
|
-
* Crash window: a process death between (1) and (3) leaves the request durably
|
|
32
|
-
* at 'submitting'. Deferred-path requests carry their signed CBOR on the row
|
|
33
|
-
* and are re-driven at the next boot (redriveInterruptedSubmissions); sync-path
|
|
34
|
-
* requests are left for the operator/poller exactly as before.
|
|
18
|
+
* Submit a claimed signing request (status 'submitting' already committed by the caller) and
|
|
19
|
+
* persist the outcome in detached transactions, so no DB lock is held across the network call.
|
|
20
|
+
* Submit failure → 'failed' in its own tx, then rethrow. A crash before finalize leaves 'submitting'.
|
|
35
21
|
*/
|
|
36
22
|
export declare function submitAndFinalize(SigningRequests: unknown, params: FinalizeParams, afterFinalize?: (db: cds.Transaction) => Promise<void>): Promise<unknown>;
|
|
37
23
|
/**
|
|
38
|
-
* Deferred
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* transaction. Hooks the request's post-commit event: only once the caller's
|
|
42
|
-
* root transaction has committed (claim durable, pooled connection released)
|
|
43
|
-
* does the network submit + finalize run — detached via setImmediate +
|
|
44
|
-
* runWithoutAmbientTx, so no ambient transaction is held across it.
|
|
45
|
-
*
|
|
46
|
-
* If the caller's transaction rolls back instead, 'succeeded' never fires and
|
|
47
|
-
* nothing was claimed — strictly consistent.
|
|
24
|
+
* Deferred submit: runs submitAndFinalize only after the caller's root transaction has committed
|
|
25
|
+
* (claim durable, connection released), detached via setImmediate + runWithoutAmbientTx.
|
|
26
|
+
* If the caller rolls back, 'succeeded' never fires and nothing was claimed.
|
|
48
27
|
*/
|
|
49
28
|
export declare function scheduleDeferredSubmit(req: Request, SigningRequests: unknown, params: FinalizeParams): void;
|
|
50
29
|
/**
|
|
51
|
-
* Boot-time recovery: re-drive
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* Safe to repeat: a tx the node already holds finalizes as 'submitted' via the
|
|
56
|
-
* TransactionAlreadySubmittedError path in submitAndFinalize. Rows at
|
|
57
|
-
* 'submitting' WITHOUT signed CBOR (sync-path crash window) are deliberately
|
|
58
|
-
* left untouched for the operator/poller, exactly as before Layer 2.
|
|
59
|
-
*
|
|
30
|
+
* Boot-time recovery: re-drive rows at 'submitting' WITH persisted signedTxCbor (process died between
|
|
31
|
+
* the caller's commit and the detached submit). Idempotent: a tx the node already holds finalizes as
|
|
32
|
+
* 'submitted'. Rows without signed CBOR are left for the operator/poller.
|
|
60
33
|
* @returns number of rows re-driven (attempted, not necessarily succeeded)
|
|
61
34
|
*/
|
|
62
35
|
export declare function redriveInterruptedSubmissions(): Promise<number>;
|