@odatano/core 1.11.0 → 2.0.0-rc.2
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/CardanoIndexerService/index.d.ts +142 -0
- package/@cds-models/CardanoIndexerService/index.js +23 -0
- package/@cds-models/CardanoODataService/index.d.ts +693 -2172
- package/@cds-models/CardanoODataService/index.js +88 -390
- package/@cds-models/CardanoSignService/index.d.ts +443 -1574
- package/@cds-models/CardanoSignService/index.js +62 -293
- package/@cds-models/CardanoTransactionService/index.d.ts +431 -1488
- package/@cds-models/CardanoTransactionService/index.js +60 -271
- package/@cds-models/CardanoWorkerService/index.d.ts +224 -0
- package/@cds-models/CardanoWorkerService/index.js +32 -0
- package/@cds-models/_/index.d.ts +70 -40
- package/@cds-models/_/index.js +17 -32
- package/@cds-models/cds/outbox/index.d.ts +28 -0
- package/@cds-models/index.d.ts +225 -522
- package/@cds-models/index.js +35 -218
- package/@cds-models/odatano/cardano/index.d.ts +896 -2947
- package/@cds-models/odatano/cardano/index.js +109 -536
- package/@cds-models/sap/common/index.d.ts +196 -600
- package/@cds-models/sap/common/index.js +28 -269
- package/README.md +36 -2
- package/db/schema.cds +283 -1
- package/db/types.cds +48 -0
- package/package.json +173 -164
- package/src/index.d.ts +1 -1
- package/src/index.js +10 -2
- package/src/plugin.js +12 -2
- package/srv/blockchain/backends/blockfrost-backend.d.ts +21 -2
- package/srv/blockchain/backends/blockfrost-backend.js +68 -15
- package/srv/blockchain/backends/cardano-backend.d.ts +69 -0
- package/srv/blockchain/backends/cardano-backend.js +13 -0
- package/srv/blockchain/backends/koios-backend.d.ts +26 -2
- package/srv/blockchain/backends/koios-backend.js +147 -15
- package/srv/blockchain/backends/ogmios-backend.d.ts +37 -6
- package/srv/blockchain/backends/ogmios-backend.js +254 -5
- package/srv/blockchain/cardano-client.d.ts +19 -0
- package/srv/blockchain/cardano-client.js +36 -0
- package/srv/blockchain/cardano-indexer.d.ts +50 -1
- package/srv/blockchain/cardano-indexer.js +191 -0
- package/srv/blockchain/crawler/crawler.d.ts +151 -0
- package/srv/blockchain/crawler/crawler.js +677 -0
- package/srv/blockchain/crawler/hooks.d.ts +43 -0
- package/srv/blockchain/crawler/hooks.js +62 -0
- package/srv/blockchain/crawler/index.d.ts +26 -0
- package/srv/blockchain/crawler/index.js +135 -0
- package/srv/blockchain/crawler/sync-state.d.ts +94 -0
- package/srv/blockchain/crawler/sync-state.js +265 -0
- package/srv/blockchain/signing/hsm-signer.d.ts +45 -0
- package/srv/blockchain/signing/hsm-signer.js +8 -1
- package/srv/blockchain/wallet-worker/build-request.d.ts +26 -0
- package/srv/blockchain/wallet-worker/build-request.js +341 -0
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +86 -0
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +228 -0
- package/srv/blockchain/wallet-worker/index.d.ts +14 -0
- package/srv/blockchain/wallet-worker/index.js +80 -0
- package/srv/blockchain/wallet-worker/job-store.d.ts +261 -0
- package/srv/blockchain/wallet-worker/job-store.js +564 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +43 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +99 -0
- package/srv/blockchain/wallet-worker/signers.d.ts +57 -0
- package/srv/blockchain/wallet-worker/signers.js +131 -0
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +145 -0
- package/srv/blockchain/wallet-worker/wallet-worker.js +542 -0
- package/srv/cardano-indexer-service.cds +73 -0
- package/srv/cardano-indexer-service.d.ts +2 -0
- package/srv/cardano-indexer-service.js +76 -0
- package/srv/cardano-service.js +76 -5
- package/srv/cardano-tx-service.d.ts +1 -11
- package/srv/cardano-tx-service.js +11 -162
- package/srv/cardano-worker-service.cds +125 -0
- package/srv/cardano-worker-service.d.ts +2 -0
- package/srv/cardano-worker-service.js +199 -0
- package/srv/server.d.ts +32 -1
- package/srv/server.js +229 -36
- package/srv/utils/collections.d.ts +13 -0
- package/srv/utils/collections.js +24 -0
- package/srv/utils/crypto.d.ts +16 -0
- package/srv/utils/crypto.js +75 -0
- package/srv/utils/error-codes.d.ts +6 -0
- package/srv/utils/error-codes.js +6 -0
- package/srv/utils/errors.d.ts +12 -0
- package/srv/utils/errors.js +45 -0
- package/srv/utils/mappers.js +13 -3
- package/srv/utils/service-events.d.ts +26 -0
- package/srv/utils/service-events.js +46 -0
- package/srv/utils/tx-request-parsers.d.ts +58 -0
- package/srv/utils/tx-request-parsers.js +170 -0
- package/srv/utils/tx-utils.d.ts +2 -1
- package/srv/utils/tx-utils.js +3 -2
- package/srv/utils/types.d.ts +2 -1
- package/@cds-models/CardanoODataService/index.d.ts.map +0 -1
- package/@cds-models/CardanoODataService/index.js.map +0 -1
- package/@cds-models/CardanoODataService/index.ts +0 -776
- package/@cds-models/CardanoSignService/index.d.ts.map +0 -1
- package/@cds-models/CardanoSignService/index.js.map +0 -1
- package/@cds-models/CardanoSignService/index.ts +0 -569
- package/@cds-models/CardanoTransactionService/index.d.ts.map +0 -1
- package/@cds-models/CardanoTransactionService/index.js.map +0 -1
- package/@cds-models/CardanoTransactionService/index.ts +0 -526
- package/@cds-models/_/index.d.ts.map +0 -1
- package/@cds-models/_/index.js.map +0 -1
- package/@cds-models/_/index.ts +0 -96
- package/@cds-models/cds/outbox/index.ts +0 -27
- package/@cds-models/index.d.ts.map +0 -1
- package/@cds-models/index.js.map +0 -1
- package/@cds-models/index.ts +0 -245
- package/@cds-models/odatano/cardano/index.d.ts.map +0 -1
- package/@cds-models/odatano/cardano/index.js.map +0 -1
- package/@cds-models/odatano/cardano/index.ts +0 -789
- package/@cds-models/sap/common/index.d.ts.map +0 -1
- package/@cds-models/sap/common/index.js.map +0 -1
- package/@cds-models/sap/common/index.ts +0 -249
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LeaseHeartbeat = exports.LEASE_HEARTBEAT_INTERVAL_MS = void 0;
|
|
7
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
8
|
+
const job_store_1 = require("./job-store");
|
|
9
|
+
const logger = cds_1.default.log('CardanoWalletWorker');
|
|
10
|
+
/**
|
|
11
|
+
* Keeps a per-wallet lease alive for as long as a job execution runs.
|
|
12
|
+
*
|
|
13
|
+
* Building and signing a transaction can easily outlast WORKER_LEASE_TTL_MS (a
|
|
14
|
+
* multi-backend build plus an HSM round-trip); a single renewal before the build
|
|
15
|
+
* is therefore not enough. Without a heartbeat the lease silently expires
|
|
16
|
+
* mid-execution, another instance adopts the wallet, fails this executor's row as
|
|
17
|
+
* orphaned and starts the next job — two executors on one wallet, which is exactly
|
|
18
|
+
* what the per-wallet lease exists to prevent.
|
|
19
|
+
*
|
|
20
|
+
* Renewals run at a third of the TTL, so two consecutive misses (slow DB, event
|
|
21
|
+
* loop hiccup) are tolerated before the lease can lapse. A process that is truly
|
|
22
|
+
* wedged stops beating and correctly loses the wallet.
|
|
23
|
+
*
|
|
24
|
+
* Two failure semantics, on purpose:
|
|
25
|
+
* - the periodic `beat` is lenient: a DB error is logged and retried next tick,
|
|
26
|
+
* only a definitive "not yours" marks the lease lost.
|
|
27
|
+
* - `fence()` is strict: it is called right before an irreversible step, where
|
|
28
|
+
* "cannot prove ownership" must be treated as "not ours". Aborting there is
|
|
29
|
+
* free (nothing has been sent yet); guessing wrong is a double spend.
|
|
30
|
+
*/
|
|
31
|
+
exports.LEASE_HEARTBEAT_INTERVAL_MS = Math.floor(job_store_1.WORKER_LEASE_TTL_MS / 3);
|
|
32
|
+
class LeaseHeartbeat {
|
|
33
|
+
renew;
|
|
34
|
+
label;
|
|
35
|
+
intervalMs;
|
|
36
|
+
timer = null;
|
|
37
|
+
lost = false;
|
|
38
|
+
/**
|
|
39
|
+
* @param renew renews the lease and reports whether it is (still) ours
|
|
40
|
+
* @param label what the lease covers, for log messages
|
|
41
|
+
*/
|
|
42
|
+
constructor(renew, label, intervalMs = exports.LEASE_HEARTBEAT_INTERVAL_MS) {
|
|
43
|
+
this.renew = renew;
|
|
44
|
+
this.label = label;
|
|
45
|
+
this.intervalMs = intervalMs;
|
|
46
|
+
}
|
|
47
|
+
start() {
|
|
48
|
+
if (this.timer)
|
|
49
|
+
return;
|
|
50
|
+
this.timer = setInterval(() => { void this.beat(); }, this.intervalMs);
|
|
51
|
+
this.timer.unref?.();
|
|
52
|
+
}
|
|
53
|
+
stop() {
|
|
54
|
+
if (this.timer) {
|
|
55
|
+
clearInterval(this.timer);
|
|
56
|
+
this.timer = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** True once a renewal proved the lease is no longer ours — abort the execution. */
|
|
60
|
+
isLost() {
|
|
61
|
+
return this.lost;
|
|
62
|
+
}
|
|
63
|
+
/** Renew now; false means "do not touch this wallet any further". */
|
|
64
|
+
async fence() {
|
|
65
|
+
if (this.lost)
|
|
66
|
+
return false;
|
|
67
|
+
try {
|
|
68
|
+
const held = await this.renew();
|
|
69
|
+
if (!held)
|
|
70
|
+
this.markLost();
|
|
71
|
+
return held;
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
logger.warn(`Lease fence for ${this.label} could not be verified — treating the lease as lost:`, err);
|
|
75
|
+
this.lost = true;
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async beat() {
|
|
80
|
+
if (this.lost)
|
|
81
|
+
return;
|
|
82
|
+
try {
|
|
83
|
+
if (!(await this.renew()))
|
|
84
|
+
this.markLost();
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
logger.debug(`Lease renewal for ${this.label} failed (retrying next beat):`, err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
markLost() {
|
|
91
|
+
if (this.lost)
|
|
92
|
+
return;
|
|
93
|
+
this.lost = true;
|
|
94
|
+
this.stop();
|
|
95
|
+
logger.warn(`Lease for ${this.label} was taken over by another instance — this executor must stand down`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.LeaseHeartbeat = LeaseHeartbeat;
|
|
99
|
+
//# sourceMappingURL=lease-heartbeat.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { WorkerSignerTypeValue } from './job-store';
|
|
2
|
+
/**
|
|
3
|
+
* Wallet-worker signers (v2.0).
|
|
4
|
+
*
|
|
5
|
+
* `WorkerSigner` is the signer-agnostic surface the worker engine executes against:
|
|
6
|
+
* - `hsm` (production): thin wrapper around the already-initialized `HsmSigner`
|
|
7
|
+
* (PKCS#11 — keys never leave the module).
|
|
8
|
+
* - `software` (dev/test): operator-configured Ed25519 key from an environment
|
|
9
|
+
* variable, either plain 64-char hex or AES-256-GCM encrypted
|
|
10
|
+
* (`iv:authTag:ciphertext`, keyed by ENCRYPTION_KEY — see srv/utils/crypto.ts).
|
|
11
|
+
*
|
|
12
|
+
* Keys are NEVER accepted through the OData surface and never persisted; the
|
|
13
|
+
* software key lives only in this process's memory.
|
|
14
|
+
*/
|
|
15
|
+
export interface WorkerSigner {
|
|
16
|
+
readonly type: WorkerSignerTypeValue;
|
|
17
|
+
/** Bech32 enterprise address the wallet spends from. */
|
|
18
|
+
getAddress(): string;
|
|
19
|
+
/** Blake2b-224 hash of the verification key (for requiredSigners). */
|
|
20
|
+
getPublicKeyHash(): string;
|
|
21
|
+
/** Sign the tx body hash and return the fully signed transaction CBOR (hex). */
|
|
22
|
+
signTransaction(unsignedTxCbor: string, txBodyHash: string): string;
|
|
23
|
+
}
|
|
24
|
+
/** Operator wallet entry from config (see loadWalletWorkerConfigFromEnv). */
|
|
25
|
+
export interface WorkerWalletConfig {
|
|
26
|
+
walletId: string;
|
|
27
|
+
signerType: WorkerSignerTypeValue;
|
|
28
|
+
/**
|
|
29
|
+
* software only: name of the environment variable holding the Ed25519 signing
|
|
30
|
+
* key — plain 64-char hex, or AES-256-GCM `iv:authTag:ciphertext` (base64 parts).
|
|
31
|
+
*/
|
|
32
|
+
keyEnv?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Merge a single VKey witness [publicKey, signature] into an unsigned transaction's
|
|
36
|
+
* witness set at the raw CBOR level, preserving all non-vkey entries (scripts,
|
|
37
|
+
* datums, redeemers) and encoding metadata. Same single-signer semantics as
|
|
38
|
+
* HsmSigner.signTransaction — the worker wallet is the sole signer of its builds.
|
|
39
|
+
*/
|
|
40
|
+
export declare function mergeVkeyWitness(unsignedTxCbor: string, publicKeyHex: string, signatureHex: string): string;
|
|
41
|
+
/** Derive the bech32 enterprise key-hash address for a 28-byte key hash. */
|
|
42
|
+
export declare function deriveEnterpriseAddress(publicKeyHash: Uint8Array, network: string): string;
|
|
43
|
+
/** software — in-memory Ed25519 key (dev/test). */
|
|
44
|
+
export declare class SoftwareWorkerSigner implements WorkerSigner {
|
|
45
|
+
readonly type: "software";
|
|
46
|
+
private readonly privateKey;
|
|
47
|
+
private readonly publicKeyHex;
|
|
48
|
+
private readonly publicKeyHashHex;
|
|
49
|
+
private readonly address;
|
|
50
|
+
constructor(privateKeyHex: string, network: string);
|
|
51
|
+
getAddress(): string;
|
|
52
|
+
getPublicKeyHash(): string;
|
|
53
|
+
signTransaction(unsignedTxCbor: string, txBodyHash: string): string;
|
|
54
|
+
}
|
|
55
|
+
/** Build the signer for a configured worker wallet. Throws ConfigError on bad setup. */
|
|
56
|
+
export declare function createWorkerSigner(wallet: WorkerWalletConfig, network: string): WorkerSigner;
|
|
57
|
+
//# sourceMappingURL=signers.d.ts.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SoftwareWorkerSigner = void 0;
|
|
7
|
+
exports.mergeVkeyWitness = mergeVkeyWitness;
|
|
8
|
+
exports.deriveEnterpriseAddress = deriveEnterpriseAddress;
|
|
9
|
+
exports.createWorkerSigner = createWorkerSigner;
|
|
10
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
11
|
+
const cbor_1 = require("@harmoniclabs/cbor");
|
|
12
|
+
const uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
|
|
13
|
+
const crypto_1 = require("@harmoniclabs/crypto");
|
|
14
|
+
const bech32_1 = require("bech32");
|
|
15
|
+
const hsm_signer_1 = require("../signing/hsm-signer");
|
|
16
|
+
const errors_1 = require("../../utils/errors");
|
|
17
|
+
const crypto_2 = require("../../utils/crypto");
|
|
18
|
+
const logger = cds_1.default.log('CardanoWalletWorker');
|
|
19
|
+
/**
|
|
20
|
+
* Merge a single VKey witness [publicKey, signature] into an unsigned transaction's
|
|
21
|
+
* witness set at the raw CBOR level, preserving all non-vkey entries (scripts,
|
|
22
|
+
* datums, redeemers) and encoding metadata. Same single-signer semantics as
|
|
23
|
+
* HsmSigner.signTransaction — the worker wallet is the sole signer of its builds.
|
|
24
|
+
*/
|
|
25
|
+
function mergeVkeyWitness(unsignedTxCbor, publicKeyHex, signatureHex) {
|
|
26
|
+
const txObj = cbor_1.Cbor.parse((0, uint8array_utils_1.fromHex)(unsignedTxCbor));
|
|
27
|
+
if (!(txObj instanceof cbor_1.CborArray) || txObj.array.length < 2) {
|
|
28
|
+
throw new errors_1.TransactionValidationError('Invalid transaction CBOR structure', undefined);
|
|
29
|
+
}
|
|
30
|
+
const vkeyWitness = new cbor_1.CborArray([
|
|
31
|
+
new cbor_1.CborBytes((0, uint8array_utils_1.fromHex)(publicKeyHex)),
|
|
32
|
+
new cbor_1.CborBytes((0, uint8array_utils_1.fromHex)(signatureHex)),
|
|
33
|
+
]);
|
|
34
|
+
const origWs = txObj.array[1];
|
|
35
|
+
if (!(origWs instanceof cbor_1.CborMap)) {
|
|
36
|
+
throw new errors_1.TransactionValidationError('Witness set must be a CBOR map', undefined);
|
|
37
|
+
}
|
|
38
|
+
const entries = origWs.map.filter((e) => !(e.k instanceof cbor_1.CborUInt && Number(e.k.num) === 0));
|
|
39
|
+
entries.push({ k: new cbor_1.CborUInt(0), v: new cbor_1.CborArray([vkeyWitness]) });
|
|
40
|
+
txObj.array[1] = new cbor_1.CborMap(entries, { indefinite: origWs.indefinite });
|
|
41
|
+
return (0, uint8array_utils_1.toHex)(cbor_1.Cbor.encode(new cbor_1.CborArray(txObj.array, { indefinite: txObj.indefinite })));
|
|
42
|
+
}
|
|
43
|
+
/** Derive the bech32 enterprise key-hash address for a 28-byte key hash. */
|
|
44
|
+
function deriveEnterpriseAddress(publicKeyHash, network) {
|
|
45
|
+
const headerByte = network === 'mainnet' ? 0x61 : 0x60;
|
|
46
|
+
const payload = Buffer.alloc(29);
|
|
47
|
+
payload[0] = headerByte;
|
|
48
|
+
Buffer.from(publicKeyHash).copy(payload, 1);
|
|
49
|
+
const hrp = network === 'mainnet' ? 'addr' : 'addr_test';
|
|
50
|
+
return bech32_1.bech32.encode(hrp, bech32_1.bech32.toWords(payload), 120);
|
|
51
|
+
}
|
|
52
|
+
/** hsm — delegates to the process-wide HsmSigner initialized at bootstrap. */
|
|
53
|
+
class HsmWorkerSigner {
|
|
54
|
+
type = 'hsm';
|
|
55
|
+
get signer() {
|
|
56
|
+
const hsm = (0, hsm_signer_1.getHsmSigner)();
|
|
57
|
+
if (!hsm || !hsm.isConnected()) {
|
|
58
|
+
throw new errors_1.ConfigError('Wallet worker: HSM signer is not configured or not connected');
|
|
59
|
+
}
|
|
60
|
+
return hsm;
|
|
61
|
+
}
|
|
62
|
+
getAddress() {
|
|
63
|
+
return this.signer.getAddress();
|
|
64
|
+
}
|
|
65
|
+
getPublicKeyHash() {
|
|
66
|
+
return this.signer.getPublicKeyHash();
|
|
67
|
+
}
|
|
68
|
+
signTransaction(unsignedTxCbor, txBodyHash) {
|
|
69
|
+
return this.signer.signTransaction(unsignedTxCbor, txBodyHash);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** software — in-memory Ed25519 key (dev/test). */
|
|
73
|
+
class SoftwareWorkerSigner {
|
|
74
|
+
type = 'software';
|
|
75
|
+
privateKey;
|
|
76
|
+
publicKeyHex;
|
|
77
|
+
publicKeyHashHex;
|
|
78
|
+
address;
|
|
79
|
+
constructor(privateKeyHex, network) {
|
|
80
|
+
if (!/^[0-9a-f]{64}$/i.test(privateKeyHex)) {
|
|
81
|
+
throw new errors_1.ConfigError('Wallet worker: software signing key must be 64 hex characters (32-byte Ed25519 key)');
|
|
82
|
+
}
|
|
83
|
+
this.privateKey = (0, uint8array_utils_1.fromHex)(privateKeyHex);
|
|
84
|
+
const publicKey = (0, crypto_1.deriveEd25519PublicKey_sync)(this.privateKey);
|
|
85
|
+
this.publicKeyHex = (0, uint8array_utils_1.toHex)(Uint8Array.from(publicKey));
|
|
86
|
+
const keyHash = Uint8Array.from((0, crypto_1.blake2b_224)(Uint8Array.from(publicKey)));
|
|
87
|
+
this.publicKeyHashHex = (0, uint8array_utils_1.toHex)(keyHash);
|
|
88
|
+
this.address = deriveEnterpriseAddress(keyHash, network);
|
|
89
|
+
}
|
|
90
|
+
getAddress() {
|
|
91
|
+
return this.address;
|
|
92
|
+
}
|
|
93
|
+
getPublicKeyHash() {
|
|
94
|
+
return this.publicKeyHashHex;
|
|
95
|
+
}
|
|
96
|
+
signTransaction(unsignedTxCbor, txBodyHash) {
|
|
97
|
+
const { signature } = (0, crypto_1.signEd25519_sync)((0, uint8array_utils_1.fromHex)(txBodyHash), this.privateKey);
|
|
98
|
+
return mergeVkeyWitness(unsignedTxCbor, this.publicKeyHex, (0, uint8array_utils_1.toHex)(Uint8Array.from(signature)));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.SoftwareWorkerSigner = SoftwareWorkerSigner;
|
|
102
|
+
/**
|
|
103
|
+
* Resolve the software key material for a wallet: plain hex passes through,
|
|
104
|
+
* the AES-256-GCM combined format (contains ':') is decrypted with ENCRYPTION_KEY.
|
|
105
|
+
*/
|
|
106
|
+
function resolveSoftwareKey(wallet) {
|
|
107
|
+
if (!wallet.keyEnv) {
|
|
108
|
+
throw new errors_1.ConfigError(`Wallet worker: wallet "${wallet.walletId}" is signerType=software but has no keyEnv configured`);
|
|
109
|
+
}
|
|
110
|
+
const raw = process.env[wallet.keyEnv];
|
|
111
|
+
if (!raw) {
|
|
112
|
+
throw new errors_1.ConfigError(`Wallet worker: environment variable ${wallet.keyEnv} (key for wallet "${wallet.walletId}") is not set`);
|
|
113
|
+
}
|
|
114
|
+
return raw.includes(':') ? (0, crypto_2.decrypt)(raw.trim(), (0, crypto_2.getEncryptionKey)()) : raw.trim();
|
|
115
|
+
}
|
|
116
|
+
/** Build the signer for a configured worker wallet. Throws ConfigError on bad setup. */
|
|
117
|
+
function createWorkerSigner(wallet, network) {
|
|
118
|
+
if (wallet.signerType === 'hsm') {
|
|
119
|
+
const signer = new HsmWorkerSigner();
|
|
120
|
+
// Fail fast at worker start when the HSM is absent, not on the first job.
|
|
121
|
+
signer.getAddress();
|
|
122
|
+
return signer;
|
|
123
|
+
}
|
|
124
|
+
if (wallet.signerType === 'software') {
|
|
125
|
+
const signer = new SoftwareWorkerSigner(resolveSoftwareKey(wallet), network);
|
|
126
|
+
logger.info(`Software signer for wallet "${wallet.walletId}" initialized (address=${signer.getAddress()})`);
|
|
127
|
+
return signer;
|
|
128
|
+
}
|
|
129
|
+
throw new errors_1.ConfigError(`Wallet worker: unknown signerType "${String(wallet.signerType)}" for wallet "${wallet.walletId}"`);
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=signers.js.map
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { CardanoClient } from '../cardano-client';
|
|
2
|
+
import type { CardanoIndexer } from '../cardano-indexer';
|
|
3
|
+
import { type WorkerSigner, type WorkerWalletConfig } from './signers';
|
|
4
|
+
/**
|
|
5
|
+
* Wallet worker engine (v2.0, design §6).
|
|
6
|
+
*
|
|
7
|
+
* Dispatch loop over `CardanoWalletJobs`: per tick, for every configured wallet
|
|
8
|
+
* with pending work and NO active job (building or submitted — the queue stays
|
|
9
|
+
* blocked until the previous job CONFIRMS, which makes per-wallet UTxO contention
|
|
10
|
+
* impossible by construction), acquire the per-wallet DB lease and execute:
|
|
11
|
+
*
|
|
12
|
+
* building → CardanoIndexer.index<Kind>BuildResult (build + TransactionBuilds row)
|
|
13
|
+
* → signer.signTransaction
|
|
14
|
+
* submitting → signed CBOR + hash committed, THEN client.submitTransaction
|
|
15
|
+
* submitted → hand to the ConfirmationTracker (crawler hook or polling)
|
|
16
|
+
*
|
|
17
|
+
* The `submitting` commit is what makes the submit boundary crash-safe: the exact
|
|
18
|
+
* bytes that may be on-chain are durable before the network call, and the row stays
|
|
19
|
+
* non-terminal, so it keeps holding its idempotency key. Anything that goes wrong
|
|
20
|
+
* from there on is reconciled against the chain (`reconcileSubmitting`) — the same
|
|
21
|
+
* transaction is re-submitted, never a rebuild.
|
|
22
|
+
*
|
|
23
|
+
* Transient failures BEFORE the signed tx exists (provider outages, rate limits,
|
|
24
|
+
* HSM hiccups during build/sign) re-queue the job with exponential backoff up to
|
|
25
|
+
* `maxAttempts`; deterministic rejections (validation, insufficient funds) fail
|
|
26
|
+
* immediately.
|
|
27
|
+
*
|
|
28
|
+
* All DB access runs in short fresh transactions (ambient context cleared —
|
|
29
|
+
* NIGHTGATE lesson 2) so the single pooled SQLite connection is never held
|
|
30
|
+
* across build/submit round-trips.
|
|
31
|
+
*/
|
|
32
|
+
export interface WalletWorkerConfig {
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
wallets: WorkerWalletConfig[];
|
|
35
|
+
maxConcurrentWallets: number;
|
|
36
|
+
confirmationDepth: number;
|
|
37
|
+
confirmationTimeoutMs: number;
|
|
38
|
+
pollIntervalMs: number;
|
|
39
|
+
defaultMaxAttempts: number;
|
|
40
|
+
resubmitOnRollback: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface WalletWorkerDeps {
|
|
43
|
+
client: CardanoClient;
|
|
44
|
+
indexer: CardanoIndexer;
|
|
45
|
+
network: string;
|
|
46
|
+
config: WalletWorkerConfig;
|
|
47
|
+
instanceId: string;
|
|
48
|
+
}
|
|
49
|
+
/** Retry backoff: 5s · 2^(attempt-1), capped at 60s. */
|
|
50
|
+
export declare function retryBackoffMs(attempt: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Transient errors are worth a bounded retry; everything else is deterministic
|
|
53
|
+
* (a retry cannot change the outcome) and fails the job immediately.
|
|
54
|
+
*/
|
|
55
|
+
export declare function isTransientJobError(err: unknown): boolean;
|
|
56
|
+
export declare class CardanoWalletWorker {
|
|
57
|
+
private readonly deps;
|
|
58
|
+
private readonly signers;
|
|
59
|
+
private readonly executing;
|
|
60
|
+
private readonly executionPromises;
|
|
61
|
+
private readonly tracker;
|
|
62
|
+
private dispatchTimer;
|
|
63
|
+
private running;
|
|
64
|
+
private ticking;
|
|
65
|
+
constructor(deps: WalletWorkerDeps);
|
|
66
|
+
isRunning(): boolean;
|
|
67
|
+
/** Wallets this instance can sign for (signer init succeeded). */
|
|
68
|
+
getWalletIds(): string[];
|
|
69
|
+
getSigner(walletId: string): WorkerSigner | undefined;
|
|
70
|
+
/** Live status summary for the control service. */
|
|
71
|
+
getStatusSummary(): {
|
|
72
|
+
running: boolean;
|
|
73
|
+
wallets: string[];
|
|
74
|
+
executing: string[];
|
|
75
|
+
awaitingConfirmation: number;
|
|
76
|
+
};
|
|
77
|
+
start(): Promise<void>;
|
|
78
|
+
stop(): Promise<void>;
|
|
79
|
+
/** One dispatch round; serialized (overlapping timers collapse into one run). */
|
|
80
|
+
private tickSafe;
|
|
81
|
+
private tick;
|
|
82
|
+
/**
|
|
83
|
+
* Adopt `submitting` rows left behind by a dead executor (this process before a
|
|
84
|
+
* restart, or another instance whose lease expired) and resolve them against the
|
|
85
|
+
* chain. Runs per wallet under the same lease the executor would hold.
|
|
86
|
+
*/
|
|
87
|
+
private reconcileOrphanedSubmissions;
|
|
88
|
+
/** Run one wallet's work in the background, tracked for concurrency + shutdown. */
|
|
89
|
+
private spawn;
|
|
90
|
+
/** A heartbeat that keeps this instance's lease on `walletId` alive while it works. */
|
|
91
|
+
private leaseHeartbeat;
|
|
92
|
+
/**
|
|
93
|
+
* Execute one job: building → (build → sign) → submitting → submit → submitted,
|
|
94
|
+
* then hand to the tracker. The wallet lease is held for the duration — renewed by
|
|
95
|
+
* a heartbeat, because build+sign can outlast its TTL — and released afterwards
|
|
96
|
+
* (queue serialization continues via the job row, not the lease).
|
|
97
|
+
*
|
|
98
|
+
* Everything up to and including the `submitting` commit may fail freely — nothing
|
|
99
|
+
* has been sent, so the job re-queues or fails normally. Past that commit the job
|
|
100
|
+
* belongs to `reconcileSubmitting`.
|
|
101
|
+
*/
|
|
102
|
+
private executeJob;
|
|
103
|
+
/**
|
|
104
|
+
* Send a transaction that is already durable in its `submitting` row and advance
|
|
105
|
+
* the job to `submitted`.
|
|
106
|
+
*
|
|
107
|
+
* A submit failure is NEVER propagated as a job failure here: a rejection from one
|
|
108
|
+
* backend does not prove the tx is absent from every mempool (failover, timeouts,
|
|
109
|
+
* a node that accepted before the connection dropped). The row simply stays
|
|
110
|
+
* `submitting` and the next tick reconciles it against the chain.
|
|
111
|
+
*/
|
|
112
|
+
private submitStoredTransaction;
|
|
113
|
+
/** submitting → submitted + hand to the confirmation tracker. */
|
|
114
|
+
private adoptSubmitted;
|
|
115
|
+
/**
|
|
116
|
+
* Resolve a `submitting` job whose submit outcome is unknown (process died around
|
|
117
|
+
* the network call, or the call failed ambiguously). The stored transaction is the
|
|
118
|
+
* only one this job may ever have — it is re-submitted verbatim, never rebuilt.
|
|
119
|
+
*
|
|
120
|
+
* 1. Re-submit the same CBOR: mempool/ledger dedup makes this a no-op if it was
|
|
121
|
+
* accepted before, so success (or "already known") simply promotes the job.
|
|
122
|
+
* 2. If that fails, ask the chain: found → the tx did make it, promote it.
|
|
123
|
+
* 3. Only when the chain proves absence AND the node rejects the tx for good (or
|
|
124
|
+
* it has been absent past the confirmation timeout) is the job failed —
|
|
125
|
+
* failing releases the idempotency key, so it needs that proof.
|
|
126
|
+
* Anything else leaves the row as-is; the next tick tries again.
|
|
127
|
+
*
|
|
128
|
+
* Runs under the same heartbeat-kept wallet lease as an execution: the chain
|
|
129
|
+
* lookups here are slow enough to outlive the raw TTL. Losing the lease mid-way
|
|
130
|
+
* is not dangerous (the re-submit is the same transaction, and every terminal
|
|
131
|
+
* transition is row-guarded), so it only stops further work.
|
|
132
|
+
*/
|
|
133
|
+
private reconcileSubmitting;
|
|
134
|
+
private failReconciled;
|
|
135
|
+
/** Dispatch the job kind to the matching indexer build method. */
|
|
136
|
+
private buildForKind;
|
|
137
|
+
/**
|
|
138
|
+
* Submit tolerant of "already known": after a crash between submit and the
|
|
139
|
+
* `submitted` transition, the retry re-submits the same CBOR — mempool dedup
|
|
140
|
+
* answers with an already-submitted error that IS our success case.
|
|
141
|
+
*/
|
|
142
|
+
private submitWithAlreadyKnownTolerance;
|
|
143
|
+
private handleExecutionFailure;
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=wallet-worker.d.ts.map
|