@psavelis/enterprise-blockchain 0.1.0 → 1.1.1
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/README.md +15 -2
- package/dist/aid-settlement/application/reconciler.d.ts +13 -0
- package/dist/aid-settlement/application/reconciler.d.ts.map +1 -0
- package/dist/aid-settlement/application/reconciler.js +77 -0
- package/dist/aid-settlement/domain/entities.d.ts +24 -0
- package/dist/aid-settlement/domain/entities.d.ts.map +1 -0
- package/dist/aid-settlement/domain/entities.js +1 -0
- package/dist/aid-settlement/domain/ports.d.ts +10 -0
- package/dist/aid-settlement/domain/ports.d.ts.map +1 -0
- package/dist/aid-settlement/domain/ports.js +1 -0
- package/dist/aid-settlement/index.d.ts +19 -0
- package/dist/aid-settlement/index.d.ts.map +1 -0
- package/dist/aid-settlement/index.js +23 -0
- package/dist/aid-settlement/infrastructure/in-memory-store.d.ts +12 -0
- package/dist/aid-settlement/infrastructure/in-memory-store.d.ts.map +1 -0
- package/dist/aid-settlement/infrastructure/in-memory-store.js +17 -0
- package/dist/credentialing/application/clearance-evaluator.d.ts +10 -0
- package/dist/credentialing/application/clearance-evaluator.d.ts.map +1 -0
- package/dist/credentialing/application/clearance-evaluator.js +63 -0
- package/dist/credentialing/domain/entities.d.ts +28 -0
- package/dist/credentialing/domain/entities.d.ts.map +1 -0
- package/dist/credentialing/domain/entities.js +1 -0
- package/dist/credentialing/domain/ports.d.ts +9 -0
- package/dist/credentialing/domain/ports.d.ts.map +1 -0
- package/dist/credentialing/domain/ports.js +1 -0
- package/dist/credentialing/index.d.ts +19 -0
- package/dist/credentialing/index.d.ts.map +1 -0
- package/dist/credentialing/index.js +23 -0
- package/dist/credentialing/infrastructure/in-memory-store.d.ts +11 -0
- package/dist/credentialing/infrastructure/in-memory-store.d.ts.map +1 -0
- package/dist/credentialing/infrastructure/in-memory-store.js +14 -0
- package/dist/hsm/application/asymmetric-key-service.d.ts +23 -0
- package/dist/hsm/application/asymmetric-key-service.d.ts.map +1 -0
- package/dist/hsm/application/asymmetric-key-service.js +109 -0
- package/dist/hsm/application/envelope-encryption-service.d.ts +18 -0
- package/dist/hsm/application/envelope-encryption-service.d.ts.map +1 -0
- package/dist/hsm/application/envelope-encryption-service.js +59 -0
- package/dist/hsm/application/symmetric-key-service.d.ts +34 -0
- package/dist/hsm/application/symmetric-key-service.d.ts.map +1 -0
- package/dist/hsm/application/symmetric-key-service.js +107 -0
- package/dist/hsm/domain/entities.d.ts +104 -0
- package/dist/hsm/domain/entities.d.ts.map +1 -0
- package/dist/hsm/domain/entities.js +10 -0
- package/dist/hsm/domain/ports.d.ts +20 -0
- package/dist/hsm/domain/ports.d.ts.map +1 -0
- package/dist/hsm/domain/ports.js +1 -0
- package/dist/hsm/index.d.ts +48 -0
- package/dist/hsm/index.d.ts.map +1 -0
- package/dist/hsm/index.js +97 -0
- package/dist/hsm/infrastructure/audit-log-factory.d.ts +59 -0
- package/dist/hsm/infrastructure/audit-log-factory.d.ts.map +1 -0
- package/dist/hsm/infrastructure/audit-log-factory.js +95 -0
- package/dist/hsm/infrastructure/audit-log.d.ts +8 -0
- package/dist/hsm/infrastructure/audit-log.d.ts.map +1 -0
- package/dist/hsm/infrastructure/audit-log.js +18 -0
- package/dist/hsm/infrastructure/file-audit-log.d.ts +55 -0
- package/dist/hsm/infrastructure/file-audit-log.d.ts.map +1 -0
- package/dist/hsm/infrastructure/file-audit-log.js +128 -0
- package/dist/hsm/infrastructure/key-store.d.ts +9 -0
- package/dist/hsm/infrastructure/key-store.d.ts.map +1 -0
- package/dist/hsm/infrastructure/key-store.js +12 -0
- package/dist/hsm/infrastructure/syslog-audit-log.d.ts +64 -0
- package/dist/hsm/infrastructure/syslog-audit-log.d.ts.map +1 -0
- package/dist/hsm/infrastructure/syslog-audit-log.js +167 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/integrations/besu-client/error-mapper.d.ts +9 -0
- package/dist/integrations/besu-client/error-mapper.d.ts.map +1 -0
- package/dist/integrations/besu-client/error-mapper.js +22 -0
- package/dist/integrations/besu-client/index.d.ts +65 -0
- package/dist/integrations/besu-client/index.d.ts.map +1 -0
- package/dist/integrations/besu-client/index.js +276 -0
- package/dist/integrations/besu-client/ports.d.ts +44 -0
- package/dist/integrations/besu-client/ports.d.ts.map +1 -0
- package/dist/integrations/besu-client/ports.js +1 -0
- package/dist/integrations/corda-gateway/index.d.ts +37 -0
- package/dist/integrations/corda-gateway/index.d.ts.map +1 -0
- package/dist/integrations/corda-gateway/index.js +234 -0
- package/dist/integrations/corda-gateway/ports.d.ts +33 -0
- package/dist/integrations/corda-gateway/ports.d.ts.map +1 -0
- package/dist/integrations/corda-gateway/ports.js +1 -0
- package/dist/integrations/fabric-gateway/index.d.ts +78 -0
- package/dist/integrations/fabric-gateway/index.d.ts.map +1 -0
- package/dist/integrations/fabric-gateway/index.js +214 -0
- package/dist/integrations/fabric-gateway/ports.d.ts +50 -0
- package/dist/integrations/fabric-gateway/ports.d.ts.map +1 -0
- package/dist/integrations/fabric-gateway/ports.js +1 -0
- package/dist/integrations/index.d.ts +19 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +19 -0
- package/dist/integrations/shared/env.d.ts +4 -0
- package/dist/integrations/shared/env.d.ts.map +1 -0
- package/dist/integrations/shared/env.js +24 -0
- package/dist/integrations/shared/retry.d.ts +79 -0
- package/dist/integrations/shared/retry.d.ts.map +1 -0
- package/dist/integrations/shared/retry.js +315 -0
- package/dist/mpc/adapters.d.ts +36 -0
- package/dist/mpc/adapters.d.ts.map +1 -0
- package/dist/mpc/adapters.js +46 -0
- package/dist/mpc/crypto.d.ts +2 -0
- package/dist/mpc/crypto.d.ts.map +1 -0
- package/dist/mpc/crypto.js +2 -0
- package/dist/mpc/dsa.d.ts +134 -0
- package/dist/mpc/dsa.d.ts.map +1 -0
- package/dist/mpc/dsa.js +127 -0
- package/dist/mpc/field.d.ts +127 -0
- package/dist/mpc/field.d.ts.map +1 -0
- package/dist/mpc/field.js +209 -0
- package/dist/mpc/hybrid-kem.d.ts +96 -0
- package/dist/mpc/hybrid-kem.d.ts.map +1 -0
- package/dist/mpc/hybrid-kem.js +136 -0
- package/dist/mpc/index.d.ts +135 -0
- package/dist/mpc/index.d.ts.map +1 -0
- package/dist/mpc/index.js +348 -0
- package/dist/mpc/kyber.d.ts +134 -0
- package/dist/mpc/kyber.d.ts.map +1 -0
- package/dist/mpc/kyber.js +143 -0
- package/dist/mpc/ports.d.ts +67 -0
- package/dist/mpc/ports.d.ts.map +1 -0
- package/dist/mpc/ports.js +9 -0
- package/dist/mpc/quantum.d.ts +80 -0
- package/dist/mpc/quantum.d.ts.map +1 -0
- package/dist/mpc/quantum.js +180 -0
- package/dist/p2mr/adapters.d.ts +31 -0
- package/dist/p2mr/adapters.d.ts.map +1 -0
- package/dist/p2mr/adapters.js +35 -0
- package/dist/p2mr/index.d.ts +63 -0
- package/dist/p2mr/index.d.ts.map +1 -0
- package/dist/p2mr/index.js +59 -0
- package/dist/p2mr/merkle-tree.d.ts +109 -0
- package/dist/p2mr/merkle-tree.d.ts.map +1 -0
- package/dist/p2mr/merkle-tree.js +239 -0
- package/dist/p2mr/p2mr-output.d.ts +142 -0
- package/dist/p2mr/p2mr-output.d.ts.map +1 -0
- package/dist/p2mr/p2mr-output.js +150 -0
- package/dist/p2mr/ports.d.ts +52 -0
- package/dist/p2mr/ports.d.ts.map +1 -0
- package/dist/p2mr/ports.js +9 -0
- package/dist/p2mr/script-interpreter.d.ts +92 -0
- package/dist/p2mr/script-interpreter.d.ts.map +1 -0
- package/dist/p2mr/script-interpreter.js +535 -0
- package/dist/p2mr/script-leaf.d.ts +70 -0
- package/dist/p2mr/script-leaf.d.ts.map +1 -0
- package/dist/p2mr/script-leaf.js +203 -0
- package/dist/p2mr/spend-proof.d.ts +95 -0
- package/dist/p2mr/spend-proof.d.ts.map +1 -0
- package/dist/p2mr/spend-proof.js +358 -0
- package/dist/p2mr/types.d.ts +209 -0
- package/dist/p2mr/types.d.ts.map +1 -0
- package/dist/p2mr/types.js +9 -0
- package/dist/privacy/application/view-projector.d.ts +13 -0
- package/dist/privacy/application/view-projector.d.ts.map +1 -0
- package/dist/privacy/application/view-projector.js +85 -0
- package/dist/privacy/domain/entities.d.ts +26 -0
- package/dist/privacy/domain/entities.d.ts.map +1 -0
- package/dist/privacy/domain/entities.js +1 -0
- package/dist/privacy/domain/ports.d.ts +7 -0
- package/dist/privacy/domain/ports.d.ts.map +1 -0
- package/dist/privacy/domain/ports.js +1 -0
- package/dist/privacy/index.d.ts +21 -0
- package/dist/privacy/index.d.ts.map +1 -0
- package/dist/privacy/index.js +25 -0
- package/dist/privacy/infrastructure/in-memory-store.d.ts +8 -0
- package/dist/privacy/infrastructure/in-memory-store.d.ts.map +1 -0
- package/dist/privacy/infrastructure/in-memory-store.js +7 -0
- package/dist/protocols/besu-port.d.ts +80 -0
- package/dist/protocols/besu-port.d.ts.map +1 -0
- package/dist/protocols/besu-port.js +1 -0
- package/dist/protocols/corda-port.d.ts +103 -0
- package/dist/protocols/corda-port.d.ts.map +1 -0
- package/dist/protocols/corda-port.js +9 -0
- package/dist/protocols/credentialing-port.d.ts +11 -0
- package/dist/protocols/credentialing-port.d.ts.map +1 -0
- package/dist/protocols/credentialing-port.js +1 -0
- package/dist/protocols/fabric-port.d.ts +89 -0
- package/dist/protocols/fabric-port.d.ts.map +1 -0
- package/dist/protocols/fabric-port.js +9 -0
- package/dist/protocols/index.d.ts +14 -0
- package/dist/protocols/index.d.ts.map +1 -0
- package/dist/protocols/index.js +7 -0
- package/dist/protocols/p2mr-port.d.ts +159 -0
- package/dist/protocols/p2mr-port.d.ts.map +1 -0
- package/dist/protocols/p2mr-port.js +12 -0
- package/dist/protocols/privacy-port.d.ts +9 -0
- package/dist/protocols/privacy-port.d.ts.map +1 -0
- package/dist/protocols/privacy-port.js +1 -0
- package/dist/protocols/traceability-port.d.ts +12 -0
- package/dist/protocols/traceability-port.d.ts.map +1 -0
- package/dist/protocols/traceability-port.js +1 -0
- package/dist/shared/collection-store.d.ts +12 -0
- package/dist/shared/collection-store.d.ts.map +1 -0
- package/dist/shared/collection-store.js +26 -0
- package/dist/shared/commit.d.ts +24 -0
- package/dist/shared/commit.d.ts.map +1 -0
- package/dist/shared/commit.js +50 -0
- package/dist/shared/crypto.d.ts +2 -0
- package/dist/shared/crypto.d.ts.map +1 -0
- package/dist/shared/crypto.js +4 -0
- package/dist/shared/date.d.ts +2 -0
- package/dist/shared/date.d.ts.map +1 -0
- package/dist/shared/date.js +3 -0
- package/dist/shared/index.d.ts +9 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +11 -0
- package/dist/shared/logger.d.ts +37 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +45 -0
- package/dist/shared/store.d.ts +25 -0
- package/dist/shared/store.d.ts.map +1 -0
- package/dist/shared/store.js +18 -0
- package/dist/shared/telemetry-sdk.d.ts +26 -0
- package/dist/shared/telemetry-sdk.d.ts.map +1 -0
- package/dist/shared/telemetry-sdk.js +97 -0
- package/dist/shared/telemetry.d.ts +86 -0
- package/dist/shared/telemetry.d.ts.map +1 -0
- package/dist/shared/telemetry.js +137 -0
- package/dist/stark-settlement/application/aggregator-service.d.ts +112 -0
- package/dist/stark-settlement/application/aggregator-service.d.ts.map +1 -0
- package/dist/stark-settlement/application/aggregator-service.js +256 -0
- package/dist/stark-settlement/application/ledger-service.d.ts +114 -0
- package/dist/stark-settlement/application/ledger-service.d.ts.map +1 -0
- package/dist/stark-settlement/application/ledger-service.js +318 -0
- package/dist/stark-settlement/application/settlement-service.d.ts +104 -0
- package/dist/stark-settlement/application/settlement-service.d.ts.map +1 -0
- package/dist/stark-settlement/application/settlement-service.js +251 -0
- package/dist/stark-settlement/domain/entities.d.ts +365 -0
- package/dist/stark-settlement/domain/entities.d.ts.map +1 -0
- package/dist/stark-settlement/domain/entities.js +29 -0
- package/dist/stark-settlement/domain/ports.d.ts +485 -0
- package/dist/stark-settlement/domain/ports.d.ts.map +1 -0
- package/dist/stark-settlement/domain/ports.js +14 -0
- package/dist/stark-settlement/domain/value-objects.d.ts +268 -0
- package/dist/stark-settlement/domain/value-objects.d.ts.map +1 -0
- package/dist/stark-settlement/domain/value-objects.js +500 -0
- package/dist/stark-settlement/index.d.ts +172 -0
- package/dist/stark-settlement/index.d.ts.map +1 -0
- package/dist/stark-settlement/index.js +193 -0
- package/dist/stark-settlement/infrastructure/adapters/audit-adapter.d.ts +52 -0
- package/dist/stark-settlement/infrastructure/adapters/audit-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/audit-adapter.js +154 -0
- package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.d.ts +88 -0
- package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/bitcoin-adapter.js +187 -0
- package/dist/stark-settlement/infrastructure/adapters/clock-adapter.d.ts +59 -0
- package/dist/stark-settlement/infrastructure/adapters/clock-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/clock-adapter.js +85 -0
- package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.d.ts +60 -0
- package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/dilithium-adapter.js +104 -0
- package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.d.ts +115 -0
- package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/event-emitter-adapter.js +191 -0
- package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.d.ts +65 -0
- package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/fiat-adapter.js +207 -0
- package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.d.ts +73 -0
- package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/mock-stark-adapter.js +287 -0
- package/dist/stark-settlement/infrastructure/adapters/solana-adapter.d.ts +78 -0
- package/dist/stark-settlement/infrastructure/adapters/solana-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/solana-adapter.js +172 -0
- package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.d.ts +56 -0
- package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/starknet-proof-adapter.js +261 -0
- package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.d.ts +125 -0
- package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/adapters/stone-proof-adapter.js +416 -0
- package/dist/stark-settlement/infrastructure/persistence/ledger-store.d.ts +68 -0
- package/dist/stark-settlement/infrastructure/persistence/ledger-store.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/persistence/ledger-store.js +238 -0
- package/dist/stark-settlement/infrastructure/persistence/offset-store.d.ts +30 -0
- package/dist/stark-settlement/infrastructure/persistence/offset-store.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/persistence/offset-store.js +57 -0
- package/dist/stark-settlement/infrastructure/persistence/outbox-store.d.ts +45 -0
- package/dist/stark-settlement/infrastructure/persistence/outbox-store.d.ts.map +1 -0
- package/dist/stark-settlement/infrastructure/persistence/outbox-store.js +171 -0
- package/dist/traceability/application/recall-assessor.d.ts +13 -0
- package/dist/traceability/application/recall-assessor.d.ts.map +1 -0
- package/dist/traceability/application/recall-assessor.js +74 -0
- package/dist/traceability/domain/entities.d.ts +23 -0
- package/dist/traceability/domain/entities.d.ts.map +1 -0
- package/dist/traceability/domain/entities.js +1 -0
- package/dist/traceability/domain/ports.d.ts +23 -0
- package/dist/traceability/domain/ports.d.ts.map +1 -0
- package/dist/traceability/domain/ports.js +1 -0
- package/dist/traceability/domain/recall.d.ts +12 -0
- package/dist/traceability/domain/recall.d.ts.map +1 -0
- package/dist/traceability/domain/recall.js +1 -0
- package/dist/traceability/index.d.ts +22 -0
- package/dist/traceability/index.d.ts.map +1 -0
- package/dist/traceability/index.js +26 -0
- package/dist/traceability/infrastructure/in-memory-store.d.ts +13 -0
- package/dist/traceability/infrastructure/in-memory-store.d.ts.map +1 -0
- package/dist/traceability/infrastructure/in-memory-store.js +24 -0
- package/package.json +12 -9
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aggregator Service
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates the tiered recursive STARK proof aggregation:
|
|
5
|
+
* - Tier-1: Aggregates 128 base proofs into a single Tier-1 proof
|
|
6
|
+
* - Tier-2: Aggregates 64 Tier-1 proofs into a final Block Proof
|
|
7
|
+
*
|
|
8
|
+
* The aggregator maintains idempotency keys through all tiers
|
|
9
|
+
* for exactly-once settlement semantics.
|
|
10
|
+
*
|
|
11
|
+
* @see domain/ports.ts for StarkProofGeneratorPort interface
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Aggregator service for recursive STARK proof composition.
|
|
15
|
+
*/
|
|
16
|
+
export class AggregatorService {
|
|
17
|
+
ctx;
|
|
18
|
+
config;
|
|
19
|
+
constructor(ctx, config) {
|
|
20
|
+
this.ctx = ctx;
|
|
21
|
+
// Get batch sizes from the STARK prover if it's a flexible adapter
|
|
22
|
+
const prover = ctx.starkProver;
|
|
23
|
+
const defaultSizes = prover.getBatchSizes?.() ?? { tier1: 128, tier2: 64 };
|
|
24
|
+
this.config = {
|
|
25
|
+
tier1BatchSize: config?.tier1BatchSize ?? defaultSizes.tier1,
|
|
26
|
+
tier2BatchSize: config?.tier2BatchSize ?? defaultSizes.tier2,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the current batch sizes.
|
|
31
|
+
*/
|
|
32
|
+
getBatchSizes() {
|
|
33
|
+
return {
|
|
34
|
+
tier1: this.config.tier1BatchSize,
|
|
35
|
+
tier2: this.config.tier2BatchSize,
|
|
36
|
+
totalTxsPerBlock: this.config.tier1BatchSize * this.config.tier2BatchSize,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
40
|
+
// Aggregation Pipeline
|
|
41
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
42
|
+
/**
|
|
43
|
+
* Process available proofs through the aggregation pipeline.
|
|
44
|
+
*
|
|
45
|
+
* Attempts to:
|
|
46
|
+
* 1. Aggregate base proofs into Tier-1 proofs
|
|
47
|
+
* 2. Aggregate Tier-1 proofs into a Tier-2 block proof
|
|
48
|
+
*
|
|
49
|
+
* @returns Aggregation result with statistics
|
|
50
|
+
*/
|
|
51
|
+
async processAggregation() {
|
|
52
|
+
let baseProofsProcessed = 0;
|
|
53
|
+
let tier1ProofsGenerated = 0;
|
|
54
|
+
// Step 1: Aggregate base proofs into Tier-1 proofs
|
|
55
|
+
while (true) {
|
|
56
|
+
const baseProofs = await this.ctx.ledgerStore.getUnaggregatedBaseProofs(this.config.tier1BatchSize);
|
|
57
|
+
if (baseProofs.length < this.config.tier1BatchSize) {
|
|
58
|
+
break; // Not enough base proofs for a Tier-1 aggregation
|
|
59
|
+
}
|
|
60
|
+
const tier1Proof = await this.aggregateTier1(baseProofs);
|
|
61
|
+
baseProofsProcessed += baseProofs.length;
|
|
62
|
+
tier1ProofsGenerated++;
|
|
63
|
+
// Emit event
|
|
64
|
+
this.ctx.events.emit({
|
|
65
|
+
type: "proof:tier1:generated",
|
|
66
|
+
proof: tier1Proof,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// Step 2: Aggregate Tier-1 proofs into a Tier-2 block proof
|
|
70
|
+
const tier1Proofs = await this.ctx.ledgerStore.getUnaggregatedTier1Proofs(this.config.tier2BatchSize);
|
|
71
|
+
if (tier1Proofs.length < this.config.tier2BatchSize) {
|
|
72
|
+
return {
|
|
73
|
+
blockProof: null,
|
|
74
|
+
baseProofsProcessed,
|
|
75
|
+
tier1ProofsGenerated,
|
|
76
|
+
blockGenerated: false,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const blockProof = await this.aggregateTier2(tier1Proofs);
|
|
80
|
+
// Emit event
|
|
81
|
+
this.ctx.events.emit({ type: "proof:tier2:generated", proof: blockProof });
|
|
82
|
+
return {
|
|
83
|
+
blockProof,
|
|
84
|
+
baseProofsProcessed,
|
|
85
|
+
tier1ProofsGenerated,
|
|
86
|
+
blockGenerated: true,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Aggregate base proofs into a Tier-1 proof.
|
|
91
|
+
*/
|
|
92
|
+
async aggregateTier1(baseProofs) {
|
|
93
|
+
if (baseProofs.length !== this.config.tier1BatchSize) {
|
|
94
|
+
throw new Error(`Tier-1 aggregation requires exactly ${this.config.tier1BatchSize} base proofs, got ${baseProofs.length}`);
|
|
95
|
+
}
|
|
96
|
+
const tier1Proof = await this.ctx.starkProver.aggregateTier1(baseProofs);
|
|
97
|
+
// Save to ledger store
|
|
98
|
+
await this.ctx.ledgerStore.saveTier1Proof(tier1Proof);
|
|
99
|
+
// Update transaction statuses
|
|
100
|
+
for (const baseProof of baseProofs) {
|
|
101
|
+
await this.ctx.ledgerStore.updateTransactionStatus(baseProof.txId, "aggregated");
|
|
102
|
+
}
|
|
103
|
+
// Log audit record
|
|
104
|
+
await this.ctx.auditLog.append({
|
|
105
|
+
eventType: "proof_aggregated",
|
|
106
|
+
entityId: tier1Proof.proofId,
|
|
107
|
+
entityType: "tier1_proof",
|
|
108
|
+
actor: "aggregator",
|
|
109
|
+
timestamp: this.ctx.clock.now(),
|
|
110
|
+
data: {
|
|
111
|
+
baseProofCount: baseProofs.length,
|
|
112
|
+
txCount: tier1Proof.txCount,
|
|
113
|
+
preStateRoot: tier1Proof.preStateRoot,
|
|
114
|
+
postStateRoot: tier1Proof.postStateRoot,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
return tier1Proof;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Aggregate Tier-1 proofs into a Tier-2 block proof.
|
|
121
|
+
*/
|
|
122
|
+
async aggregateTier2(tier1Proofs) {
|
|
123
|
+
if (tier1Proofs.length !== this.config.tier2BatchSize) {
|
|
124
|
+
throw new Error(`Tier-2 aggregation requires exactly ${this.config.tier2BatchSize} Tier-1 proofs, got ${tier1Proofs.length}`);
|
|
125
|
+
}
|
|
126
|
+
const blockProof = await this.ctx.starkProver.aggregateTier2(tier1Proofs);
|
|
127
|
+
// Save to ledger store
|
|
128
|
+
await this.ctx.ledgerStore.saveTier2Proof(blockProof);
|
|
129
|
+
// Update transaction statuses for all covered transactions
|
|
130
|
+
// (This would require tracking txIds through the proof chain)
|
|
131
|
+
// Log audit record
|
|
132
|
+
await this.ctx.auditLog.append({
|
|
133
|
+
eventType: "proof_aggregated",
|
|
134
|
+
entityId: blockProof.blockProofId,
|
|
135
|
+
entityType: "tier2_proof",
|
|
136
|
+
actor: "aggregator",
|
|
137
|
+
timestamp: this.ctx.clock.now(),
|
|
138
|
+
data: {
|
|
139
|
+
tier1ProofCount: tier1Proofs.length,
|
|
140
|
+
txCount: blockProof.txCount,
|
|
141
|
+
blockNumber: blockProof.blockNumber.toString(),
|
|
142
|
+
stateRoot: blockProof.stateRoot,
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
return blockProof;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Process proofs until a block is generated or no more proofs are available.
|
|
149
|
+
*
|
|
150
|
+
* Convenience method for demo/testing - processes multiple batches.
|
|
151
|
+
*/
|
|
152
|
+
async processToBlockProof() {
|
|
153
|
+
// Keep processing until we get a block or run out of proofs
|
|
154
|
+
let result = await this.processAggregation();
|
|
155
|
+
while (!result.blockGenerated && result.baseProofsProcessed > 0) {
|
|
156
|
+
result = await this.processAggregation();
|
|
157
|
+
}
|
|
158
|
+
return result.blockProof;
|
|
159
|
+
}
|
|
160
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
161
|
+
// Outbox Management
|
|
162
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
163
|
+
/**
|
|
164
|
+
* Create outbox entries for a block proof.
|
|
165
|
+
*
|
|
166
|
+
* Computes net transfers per asset type and creates outbox entries
|
|
167
|
+
* for settlement.
|
|
168
|
+
*
|
|
169
|
+
* @param blockProof - The block proof to create entries for
|
|
170
|
+
* @param netTransfersByAsset - Pre-computed net transfers by asset type
|
|
171
|
+
*/
|
|
172
|
+
async createOutboxEntries(blockProof, netTransfersByAsset) {
|
|
173
|
+
const entries = [];
|
|
174
|
+
const now = this.ctx.clock.now();
|
|
175
|
+
for (const [assetType, transfers] of netTransfersByAsset) {
|
|
176
|
+
if (transfers.length === 0)
|
|
177
|
+
continue;
|
|
178
|
+
const entry = {
|
|
179
|
+
entryId: this.ctx.clock.uuid(),
|
|
180
|
+
blockProofId: blockProof.blockProofId,
|
|
181
|
+
assetType,
|
|
182
|
+
netTransfers: transfers,
|
|
183
|
+
status: "pending",
|
|
184
|
+
retryCount: 0,
|
|
185
|
+
maxRetries: 3,
|
|
186
|
+
createdAt: now,
|
|
187
|
+
settledAt: null,
|
|
188
|
+
settlementTxId: null,
|
|
189
|
+
errorMessage: null,
|
|
190
|
+
offset: 0n, // Placeholder - outbox store assigns the real sequential offset
|
|
191
|
+
};
|
|
192
|
+
// Store entry and get back the entry with the real assigned offset
|
|
193
|
+
const storedEntry = await this.ctx.outboxStore.appendEntry(entry);
|
|
194
|
+
entries.push(storedEntry);
|
|
195
|
+
// Emit event with the stored entry (which has the real offset)
|
|
196
|
+
this.ctx.events.emit({
|
|
197
|
+
type: "settlement:initiated",
|
|
198
|
+
entry: storedEntry,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return entries;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Compute net transfers from transaction history.
|
|
205
|
+
*
|
|
206
|
+
* Groups transactions by external address and computes the net
|
|
207
|
+
* amount change for each address.
|
|
208
|
+
*/
|
|
209
|
+
computeNetTransfers(transactions, accountAddresses) {
|
|
210
|
+
const netByAddress = new Map();
|
|
211
|
+
for (const tx of transactions) {
|
|
212
|
+
// Withdrawals: debit from external address
|
|
213
|
+
if (tx.type === "withdrawal" && tx.fromAccountId) {
|
|
214
|
+
const address = accountAddresses.get(tx.fromAccountId);
|
|
215
|
+
if (address) {
|
|
216
|
+
const current = netByAddress.get(address) ?? 0n;
|
|
217
|
+
netByAddress.set(address, current - tx.amount);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Deposits: credit to external address
|
|
221
|
+
if (tx.type === "deposit" && tx.toAccountId) {
|
|
222
|
+
const address = accountAddresses.get(tx.toAccountId);
|
|
223
|
+
if (address) {
|
|
224
|
+
const current = netByAddress.get(address) ?? 0n;
|
|
225
|
+
netByAddress.set(address, current + tx.amount);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Transfers: Internal only, no external settlement needed
|
|
229
|
+
// (unless transferring to a withdrawal-pending account)
|
|
230
|
+
}
|
|
231
|
+
return netByAddress;
|
|
232
|
+
}
|
|
233
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
234
|
+
// Verification
|
|
235
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
236
|
+
/**
|
|
237
|
+
* Verify a block proof.
|
|
238
|
+
*/
|
|
239
|
+
async verifyBlockProof(blockProof) {
|
|
240
|
+
return this.ctx.starkProver.verifyBlockProof(blockProof);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Get aggregation statistics.
|
|
244
|
+
*/
|
|
245
|
+
async getStats() {
|
|
246
|
+
const baseProofs = await this.ctx.ledgerStore.getUnaggregatedBaseProofs(this.config.tier1BatchSize * this.config.tier2BatchSize);
|
|
247
|
+
const tier1Proofs = await this.ctx.ledgerStore.getUnaggregatedTier1Proofs(this.config.tier2BatchSize);
|
|
248
|
+
const latestBlock = await this.ctx.ledgerStore.getLatestBlockProof();
|
|
249
|
+
return {
|
|
250
|
+
pendingBaseProofs: baseProofs.length,
|
|
251
|
+
pendingTier1Proofs: tier1Proofs.length,
|
|
252
|
+
latestBlockNumber: latestBlock?.blockNumber ?? null,
|
|
253
|
+
totalTxsPerBlock: this.config.tier1BatchSize * this.config.tier2BatchSize,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ledger Service
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates the ZKP ledger operations:
|
|
5
|
+
* - Account management (create, query, update)
|
|
6
|
+
* - Transaction submission with ML-DSA-65 signing
|
|
7
|
+
* - State transition proof generation
|
|
8
|
+
* - Audit logging
|
|
9
|
+
*
|
|
10
|
+
* All transactions are signed with post-quantum ML-DSA-65 signatures.
|
|
11
|
+
*
|
|
12
|
+
* @see domain/ports.ts for port interfaces
|
|
13
|
+
*/
|
|
14
|
+
import type { AssetType, MirrorAccount, LedgerTransaction, TransactionType, BaseProof } from "../domain/entities.js";
|
|
15
|
+
import type { SettlementContext } from "../index.js";
|
|
16
|
+
import { StateRoot } from "../domain/value-objects.js";
|
|
17
|
+
/**
|
|
18
|
+
* Options for creating a mirror account.
|
|
19
|
+
*/
|
|
20
|
+
export interface CreateAccountOptions {
|
|
21
|
+
/** External chain address (Solana pubkey, BTC address, fiat account) */
|
|
22
|
+
externalAddress: string;
|
|
23
|
+
/** Asset type */
|
|
24
|
+
assetType: AssetType;
|
|
25
|
+
/** Initial balance (default: 0) */
|
|
26
|
+
initialBalance?: bigint;
|
|
27
|
+
/** Optional metadata */
|
|
28
|
+
metadata?: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Options for submitting a transaction.
|
|
32
|
+
*/
|
|
33
|
+
export interface SubmitTransactionOptions {
|
|
34
|
+
/** Transaction type */
|
|
35
|
+
type: TransactionType;
|
|
36
|
+
/** Source account ID (required for transfer/withdrawal) */
|
|
37
|
+
fromAccountId?: string;
|
|
38
|
+
/** Destination account ID (required for deposit/transfer) */
|
|
39
|
+
toAccountId?: string;
|
|
40
|
+
/** Asset type */
|
|
41
|
+
assetType: AssetType;
|
|
42
|
+
/** Amount in atomic units */
|
|
43
|
+
amount: bigint;
|
|
44
|
+
/** Signer's secret key (ML-DSA-65) */
|
|
45
|
+
signerSecretKey: Uint8Array;
|
|
46
|
+
/** Signer's public key (ML-DSA-65) */
|
|
47
|
+
signerPublicKey: Uint8Array;
|
|
48
|
+
/** Optional metadata */
|
|
49
|
+
metadata?: Record<string, string>;
|
|
50
|
+
/** Optional idempotency key (auto-generated if not provided) */
|
|
51
|
+
idempotencyKey?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Result of submitting a transaction.
|
|
55
|
+
*/
|
|
56
|
+
export interface SubmitTransactionResult {
|
|
57
|
+
/** The submitted transaction */
|
|
58
|
+
transaction: LedgerTransaction;
|
|
59
|
+
/** The base proof for this transaction */
|
|
60
|
+
baseProof: BaseProof;
|
|
61
|
+
/** Whether this was a duplicate (idempotent replay) */
|
|
62
|
+
isDuplicate: boolean;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Ledger service for managing accounts and transactions.
|
|
66
|
+
*/
|
|
67
|
+
export declare class LedgerService {
|
|
68
|
+
private readonly ctx;
|
|
69
|
+
private currentStateRoot;
|
|
70
|
+
constructor(ctx: SettlementContext);
|
|
71
|
+
/**
|
|
72
|
+
* Create a new mirror account.
|
|
73
|
+
*/
|
|
74
|
+
createAccount(options: CreateAccountOptions): Promise<MirrorAccount>;
|
|
75
|
+
/**
|
|
76
|
+
* Get an account by ID.
|
|
77
|
+
*/
|
|
78
|
+
getAccount(accountId: string): Promise<MirrorAccount | null>;
|
|
79
|
+
/**
|
|
80
|
+
* Get an account by external address and asset type.
|
|
81
|
+
*/
|
|
82
|
+
getAccountByAddress(externalAddress: string, assetType: AssetType): Promise<MirrorAccount | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Get all accounts for an asset type.
|
|
85
|
+
*/
|
|
86
|
+
getAccountsByAssetType(assetType: AssetType): Promise<readonly MirrorAccount[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Submit a transaction to the ledger.
|
|
89
|
+
*
|
|
90
|
+
* The transaction is:
|
|
91
|
+
* 1. Validated (balances, account existence)
|
|
92
|
+
* 2. Signed with ML-DSA-65
|
|
93
|
+
* 3. Recorded in the ledger
|
|
94
|
+
* 4. A base STARK proof is generated
|
|
95
|
+
* 5. Account balances are updated
|
|
96
|
+
*
|
|
97
|
+
* @returns The transaction and its base proof
|
|
98
|
+
* @throws If validation fails
|
|
99
|
+
*/
|
|
100
|
+
submitTransaction(options: SubmitTransactionOptions): Promise<SubmitTransactionResult>;
|
|
101
|
+
/**
|
|
102
|
+
* Get pending transactions that need proof generation.
|
|
103
|
+
*/
|
|
104
|
+
getPendingTransactions(limit?: number): Promise<readonly LedgerTransaction[]>;
|
|
105
|
+
/**
|
|
106
|
+
* Get the current state root.
|
|
107
|
+
*/
|
|
108
|
+
getCurrentStateRoot(): StateRoot;
|
|
109
|
+
private validateTransaction;
|
|
110
|
+
private validateAccountExists;
|
|
111
|
+
private validateSufficientBalance;
|
|
112
|
+
private applyTransaction;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=ledger-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger-service.d.ts","sourceRoot":"","sources":["../../../src/stark-settlement/application/ledger-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EACb,iBAAiB,EAEjB,eAAe,EACf,SAAS,EACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAEvD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,wEAAwE;IACxE,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,mCAAmC;IACnC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,wBAAwB;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,uBAAuB;IACvB,IAAI,EAAE,eAAe,CAAC;IACtB,2DAA2D;IAC3D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,6DAA6D;IAC7D,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,sCAAsC;IACtC,eAAe,EAAE,UAAU,CAAC;IAC5B,sCAAsC;IACtC,eAAe,EAAE,UAAU,CAAC;IAC5B,wBAAwB;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,gEAAgE;IAChE,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,gCAAgC;IAChC,WAAW,EAAE,iBAAiB,CAAC;IAC/B,0CAA0C;IAC1C,SAAS,EAAE,SAAS,CAAC;IACrB,uDAAuD;IACvD,WAAW,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,qBAAa,aAAa;IAGZ,OAAO,CAAC,QAAQ,CAAC,GAAG;IAFhC,OAAO,CAAC,gBAAgB,CAAY;gBAEP,GAAG,EAAE,iBAAiB;IAQnD;;OAEG;IACG,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,aAAa,CAAC;IA8C1E;;OAEG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAIlE;;OAEG;IACG,mBAAmB,CACvB,eAAe,EAAE,MAAM,EACvB,SAAS,EAAE,SAAS,GACnB,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAIhC;;OAEG;IACG,sBAAsB,CAC1B,SAAS,EAAE,SAAS,GACnB,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC;IAQpC;;;;;;;;;;;;OAYG;IACG,iBAAiB,CACrB,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,uBAAuB,CAAC;IAuInC;;OAEG;IACG,sBAAsB,CAC1B,KAAK,SAAO,GACX,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC;IAIxC;;OAEG;IACH,mBAAmB,IAAI,SAAS;YAQlB,mBAAmB;YAkFnB,qBAAqB;YAkBrB,yBAAyB;YAazB,gBAAgB;CA8C/B"}
|