chainlesschain 0.81.0 → 0.143.0
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/bin/chainlesschain.js +0 -0
- package/package.json +1 -1
- package/src/commands/a2a.js +62 -0
- package/src/commands/activitypub.js +61 -0
- package/src/commands/agent-network.js +254 -1
- package/src/commands/agent.js +117 -0
- package/src/commands/audit.js +302 -0
- package/src/commands/automation.js +271 -1
- package/src/commands/bi.js +61 -0
- package/src/commands/bm25.js +78 -0
- package/src/commands/browse.js +64 -0
- package/src/commands/ccron.js +78 -0
- package/src/commands/codegen.js +224 -0
- package/src/commands/collab.js +341 -0
- package/src/commands/compliance.js +1075 -0
- package/src/commands/compt.js +78 -0
- package/src/commands/consol.js +231 -0
- package/src/commands/cowork.js +263 -0
- package/src/commands/crosschain.js +62 -0
- package/src/commands/dao.js +62 -0
- package/src/commands/dbevo.js +284 -0
- package/src/commands/dev.js +252 -0
- package/src/commands/did.js +358 -0
- package/src/commands/dlp.js +61 -0
- package/src/commands/economy.js +56 -0
- package/src/commands/encrypt.js +341 -0
- package/src/commands/evolution.js +56 -0
- package/src/commands/evomap.js +61 -0
- package/src/commands/export.js +256 -1
- package/src/commands/fflag.js +178 -0
- package/src/commands/fusion.js +258 -0
- package/src/commands/git.js +45 -0
- package/src/commands/governance.js +325 -0
- package/src/commands/hardening.js +411 -0
- package/src/commands/hmemory.js +56 -0
- package/src/commands/hook.js +148 -0
- package/src/commands/import.js +252 -0
- package/src/commands/incentive.js +322 -0
- package/src/commands/inference.js +42 -0
- package/src/commands/infra.js +244 -0
- package/src/commands/instinct.js +260 -0
- package/src/commands/ipfs.js +318 -0
- package/src/commands/itbudget.js +45 -0
- package/src/commands/kg.js +387 -0
- package/src/commands/llm.js +263 -0
- package/src/commands/lowcode.js +44 -0
- package/src/commands/matrix.js +62 -0
- package/src/commands/mcp.js +221 -0
- package/src/commands/mcpscaf.js +41 -0
- package/src/commands/meminj.js +41 -0
- package/src/commands/memory.js +248 -0
- package/src/commands/multimodal.js +296 -0
- package/src/commands/nlprog.js +356 -0
- package/src/commands/nostr.js +62 -0
- package/src/commands/note.js +244 -0
- package/src/commands/ops.js +354 -0
- package/src/commands/orchestrate.js +166 -0
- package/src/commands/orchgov.js +45 -0
- package/src/commands/org.js +277 -0
- package/src/commands/p2p.js +390 -0
- package/src/commands/pdfp.js +78 -0
- package/src/commands/perception.js +290 -0
- package/src/commands/perf.js +39 -0
- package/src/commands/perm.js +45 -0
- package/src/commands/permmem.js +251 -0
- package/src/commands/pipeline.js +57 -1
- package/src/commands/planmode.js +45 -0
- package/src/commands/plugin-ecosystem.js +273 -0
- package/src/commands/pqc.js +393 -0
- package/src/commands/promcomp.js +82 -0
- package/src/commands/quantization.js +351 -0
- package/src/commands/rcache.js +271 -0
- package/src/commands/recommend.js +382 -0
- package/src/commands/runtime.js +307 -0
- package/src/commands/scim.js +262 -0
- package/src/commands/seshhook.js +41 -0
- package/src/commands/seshsearch.js +41 -0
- package/src/commands/seshtail.js +41 -0
- package/src/commands/seshu.js +41 -0
- package/src/commands/session.js +258 -0
- package/src/commands/sganal.js +78 -0
- package/src/commands/siem.js +40 -0
- package/src/commands/skill.js +267 -1
- package/src/commands/slotfill.js +41 -0
- package/src/commands/social.js +290 -0
- package/src/commands/sso.js +186 -1
- package/src/commands/svccont.js +45 -0
- package/src/commands/sync.js +256 -0
- package/src/commands/tech.js +338 -0
- package/src/commands/tenant.js +351 -0
- package/src/commands/tms.js +45 -0
- package/src/commands/tokens.js +269 -0
- package/src/commands/topiccls.js +45 -0
- package/src/commands/trust.js +249 -0
- package/src/commands/uprof.js +45 -0
- package/src/commands/vcheck.js +78 -0
- package/src/commands/wallet.js +277 -0
- package/src/commands/webfetch.js +41 -0
- package/src/commands/workflow.js +171 -0
- package/src/commands/zkp.js +62 -0
- package/src/harness/prompt-compressor.js +331 -0
- package/src/index.js +65 -1
- package/src/lib/a2a-protocol.js +105 -0
- package/src/lib/activitypub-bridge.js +105 -0
- package/src/lib/agent-coordinator.js +325 -0
- package/src/lib/agent-economy.js +105 -0
- package/src/lib/agent-network.js +387 -0
- package/src/lib/agent-router.js +395 -0
- package/src/lib/aiops.js +478 -0
- package/src/lib/app-builder.js +105 -0
- package/src/lib/audit-logger.js +379 -0
- package/src/lib/automation-engine.js +330 -0
- package/src/lib/autonomous-agent.js +105 -0
- package/src/lib/autonomous-developer.js +350 -0
- package/src/lib/bi-engine.js +105 -0
- package/src/lib/bm25-search.js +81 -0
- package/src/lib/browser-automation.js +105 -0
- package/src/lib/code-agent.js +323 -0
- package/src/lib/collaboration-governance.js +364 -0
- package/src/lib/community-governance.js +436 -0
- package/src/lib/compliance-framework-reporter.js +105 -0
- package/src/lib/compliance-manager.js +434 -0
- package/src/lib/compression-telemetry.js +81 -0
- package/src/lib/content-recommendation.js +469 -0
- package/src/lib/content-recommender.js +105 -0
- package/src/lib/cowork-cron.js +81 -0
- package/src/lib/cowork-task-runner.js +105 -0
- package/src/lib/cross-chain.js +105 -0
- package/src/lib/crypto-manager.js +350 -0
- package/src/lib/dao-governance.js +105 -0
- package/src/lib/dbevo.js +338 -0
- package/src/lib/decentral-infra.js +340 -0
- package/src/lib/did-manager.js +367 -0
- package/src/lib/dlp-engine.js +105 -0
- package/src/lib/evolution-system.js +105 -0
- package/src/lib/evomap-manager.js +105 -0
- package/src/lib/execution-backend.js +105 -0
- package/src/lib/feature-flags.js +85 -0
- package/src/lib/git-integration.js +105 -0
- package/src/lib/hardening-manager.js +348 -0
- package/src/lib/hierarchical-memory.js +105 -0
- package/src/lib/hook-manager.js +380 -0
- package/src/lib/inference-network.js +105 -0
- package/src/lib/instinct-manager.js +332 -0
- package/src/lib/ipfs-storage.js +334 -0
- package/src/lib/iteration-budget.js +105 -0
- package/src/lib/knowledge-exporter.js +381 -0
- package/src/lib/knowledge-graph.js +432 -0
- package/src/lib/knowledge-importer.js +379 -0
- package/src/lib/llm-providers.js +391 -0
- package/src/lib/matrix-bridge.js +105 -0
- package/src/lib/mcp-registry.js +333 -0
- package/src/lib/mcp-scaffold.js +81 -0
- package/src/lib/memory-injection.js +81 -0
- package/src/lib/memory-manager.js +330 -0
- package/src/lib/multimodal.js +346 -0
- package/src/lib/nl-programming.js +343 -0
- package/src/lib/nostr-bridge.js +105 -0
- package/src/lib/note-versioning.js +327 -0
- package/src/lib/orchestrator.js +105 -0
- package/src/lib/org-manager.js +323 -0
- package/src/lib/p2p-manager.js +387 -0
- package/src/lib/pdf-parser.js +81 -0
- package/src/lib/perception.js +346 -0
- package/src/lib/perf-tuning.js +109 -1
- package/src/lib/permanent-memory.js +320 -0
- package/src/lib/permission-engine.js +81 -0
- package/src/lib/pipeline-orchestrator.js +105 -0
- package/src/lib/plan-mode.js +81 -0
- package/src/lib/plugin-ecosystem.js +377 -0
- package/src/lib/pqc-manager.js +368 -0
- package/src/lib/prompt-compressor.js +1 -10
- package/src/lib/protocol-fusion.js +417 -0
- package/src/lib/quantization.js +325 -0
- package/src/lib/response-cache.js +327 -0
- package/src/lib/scim-manager.js +329 -0
- package/src/lib/service-container.js +81 -0
- package/src/lib/session-consolidator.js +105 -0
- package/src/lib/session-hooks.js +81 -0
- package/src/lib/session-manager.js +329 -0
- package/src/lib/session-search.js +81 -0
- package/src/lib/session-tail.js +81 -0
- package/src/lib/session-usage.js +83 -0
- package/src/lib/siem-exporter.js +105 -0
- package/src/lib/skill-loader.js +377 -0
- package/src/lib/slot-filler.js +81 -0
- package/src/lib/social-graph-analytics.js +81 -0
- package/src/lib/social-graph.js +81 -0
- package/src/lib/social-manager.js +326 -0
- package/src/lib/sso-manager.js +332 -0
- package/src/lib/sub-agent-registry.js +110 -0
- package/src/lib/sync-manager.js +326 -0
- package/src/lib/task-model-selector.js +81 -0
- package/src/lib/tech-learning-engine.js +369 -0
- package/src/lib/tenant-saas.js +460 -0
- package/src/lib/threat-intel.js +335 -0
- package/src/lib/todo-manager.js +105 -0
- package/src/lib/token-incentive.js +293 -0
- package/src/lib/token-tracker.js +329 -0
- package/src/lib/topic-classifier.js +105 -0
- package/src/lib/trust-security.js +390 -0
- package/src/lib/ueba.js +389 -0
- package/src/lib/universal-runtime.js +325 -0
- package/src/lib/user-profile.js +81 -0
- package/src/lib/version-checker.js +81 -0
- package/src/lib/wallet-manager.js +326 -0
- package/src/lib/web-fetch.js +81 -0
- package/src/lib/workflow-engine.js +322 -0
- package/src/lib/zkp-engine.js +105 -0
|
@@ -443,3 +443,343 @@ export function _resetState() {
|
|
|
443
443
|
_versions.clear();
|
|
444
444
|
_routes.clear();
|
|
445
445
|
}
|
|
446
|
+
|
|
447
|
+
/* ═════════════════════════════════════════════════════════ *
|
|
448
|
+
* Phase 74-75 V2 — Provider Maturity + Deal Lifecycle
|
|
449
|
+
* ═════════════════════════════════════════════════════════ */
|
|
450
|
+
|
|
451
|
+
export const PROVIDER_MATURITY_V2 = Object.freeze({
|
|
452
|
+
ONBOARDING: "onboarding",
|
|
453
|
+
ACTIVE: "active",
|
|
454
|
+
DEGRADED: "degraded",
|
|
455
|
+
OFFLINE: "offline",
|
|
456
|
+
RETIRED: "retired",
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
export const DEAL_LIFECYCLE_V2 = Object.freeze({
|
|
460
|
+
QUEUED: "queued",
|
|
461
|
+
ACTIVE: "active",
|
|
462
|
+
COMPLETED: "completed",
|
|
463
|
+
FAILED: "failed",
|
|
464
|
+
CANCELED: "canceled",
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
const PROVIDER_TRANSITIONS_V2 = new Map([
|
|
468
|
+
["onboarding", new Set(["active", "retired"])],
|
|
469
|
+
["active", new Set(["degraded", "offline", "retired"])],
|
|
470
|
+
["degraded", new Set(["active", "offline", "retired"])],
|
|
471
|
+
["offline", new Set(["active", "retired"])],
|
|
472
|
+
]);
|
|
473
|
+
const PROVIDER_TERMINALS_V2 = new Set(["retired"]);
|
|
474
|
+
|
|
475
|
+
const DEAL_TRANSITIONS_V2 = new Map([
|
|
476
|
+
["queued", new Set(["active", "canceled", "failed"])],
|
|
477
|
+
["active", new Set(["completed", "failed", "canceled"])],
|
|
478
|
+
]);
|
|
479
|
+
const DEAL_TERMINALS_V2 = new Set(["completed", "failed", "canceled"]);
|
|
480
|
+
|
|
481
|
+
export const DI_DEFAULT_MAX_ACTIVE_PROVIDERS_PER_OPERATOR = 20;
|
|
482
|
+
export const DI_DEFAULT_MAX_ACTIVE_DEALS_PER_PROVIDER = 10;
|
|
483
|
+
export const DI_DEFAULT_PROVIDER_IDLE_MS = 7 * 86400000; // 7 days
|
|
484
|
+
export const DI_DEFAULT_DEAL_STUCK_MS = 24 * 3600000; // 24 hours
|
|
485
|
+
|
|
486
|
+
let _maxActiveProvidersPerOperatorV2 =
|
|
487
|
+
DI_DEFAULT_MAX_ACTIVE_PROVIDERS_PER_OPERATOR;
|
|
488
|
+
let _maxActiveDealsPerProviderV2 = DI_DEFAULT_MAX_ACTIVE_DEALS_PER_PROVIDER;
|
|
489
|
+
let _providerIdleMsV2 = DI_DEFAULT_PROVIDER_IDLE_MS;
|
|
490
|
+
let _dealStuckMsV2 = DI_DEFAULT_DEAL_STUCK_MS;
|
|
491
|
+
|
|
492
|
+
function _positiveIntV2(n, label) {
|
|
493
|
+
const v = Math.floor(Number(n));
|
|
494
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
495
|
+
throw new Error(`${label} must be a positive integer`);
|
|
496
|
+
return v;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export function getDefaultMaxActiveProvidersPerOperatorV2() {
|
|
500
|
+
return DI_DEFAULT_MAX_ACTIVE_PROVIDERS_PER_OPERATOR;
|
|
501
|
+
}
|
|
502
|
+
export function getMaxActiveProvidersPerOperatorV2() {
|
|
503
|
+
return _maxActiveProvidersPerOperatorV2;
|
|
504
|
+
}
|
|
505
|
+
export function setMaxActiveProvidersPerOperatorV2(n) {
|
|
506
|
+
return (_maxActiveProvidersPerOperatorV2 = _positiveIntV2(
|
|
507
|
+
n,
|
|
508
|
+
"maxActiveProvidersPerOperator",
|
|
509
|
+
));
|
|
510
|
+
}
|
|
511
|
+
export function getDefaultMaxActiveDealsPerProviderV2() {
|
|
512
|
+
return DI_DEFAULT_MAX_ACTIVE_DEALS_PER_PROVIDER;
|
|
513
|
+
}
|
|
514
|
+
export function getMaxActiveDealsPerProviderV2() {
|
|
515
|
+
return _maxActiveDealsPerProviderV2;
|
|
516
|
+
}
|
|
517
|
+
export function setMaxActiveDealsPerProviderV2(n) {
|
|
518
|
+
return (_maxActiveDealsPerProviderV2 = _positiveIntV2(
|
|
519
|
+
n,
|
|
520
|
+
"maxActiveDealsPerProvider",
|
|
521
|
+
));
|
|
522
|
+
}
|
|
523
|
+
export function getDefaultProviderIdleMsV2() {
|
|
524
|
+
return DI_DEFAULT_PROVIDER_IDLE_MS;
|
|
525
|
+
}
|
|
526
|
+
export function getProviderIdleMsV2() {
|
|
527
|
+
return _providerIdleMsV2;
|
|
528
|
+
}
|
|
529
|
+
export function setProviderIdleMsV2(ms) {
|
|
530
|
+
return (_providerIdleMsV2 = _positiveIntV2(ms, "providerIdleMs"));
|
|
531
|
+
}
|
|
532
|
+
export function getDefaultDealStuckMsV2() {
|
|
533
|
+
return DI_DEFAULT_DEAL_STUCK_MS;
|
|
534
|
+
}
|
|
535
|
+
export function getDealStuckMsV2() {
|
|
536
|
+
return _dealStuckMsV2;
|
|
537
|
+
}
|
|
538
|
+
export function setDealStuckMsV2(ms) {
|
|
539
|
+
return (_dealStuckMsV2 = _positiveIntV2(ms, "dealStuckMs"));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const _providersV2 = new Map();
|
|
543
|
+
const _dealsV2 = new Map();
|
|
544
|
+
|
|
545
|
+
export function registerProviderV2(
|
|
546
|
+
_db,
|
|
547
|
+
{ providerId, operatorId, kind, initialStatus, metadata } = {},
|
|
548
|
+
) {
|
|
549
|
+
if (!providerId) throw new Error("providerId is required");
|
|
550
|
+
if (!operatorId) throw new Error("operatorId is required");
|
|
551
|
+
if (!kind) throw new Error("kind is required");
|
|
552
|
+
if (_providersV2.has(providerId))
|
|
553
|
+
throw new Error(`Provider ${providerId} already exists`);
|
|
554
|
+
const status = initialStatus || PROVIDER_MATURITY_V2.ONBOARDING;
|
|
555
|
+
if (!Object.values(PROVIDER_MATURITY_V2).includes(status))
|
|
556
|
+
throw new Error(`Invalid initial status: ${status}`);
|
|
557
|
+
if (PROVIDER_TERMINALS_V2.has(status))
|
|
558
|
+
throw new Error(`Cannot register in terminal status: ${status}`);
|
|
559
|
+
if (status === PROVIDER_MATURITY_V2.ACTIVE) {
|
|
560
|
+
if (getActiveProviderCount(operatorId) >= _maxActiveProvidersPerOperatorV2)
|
|
561
|
+
throw new Error(
|
|
562
|
+
`Operator ${operatorId} reached active-provider cap (${_maxActiveProvidersPerOperatorV2})`,
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
const now = Date.now();
|
|
566
|
+
const record = {
|
|
567
|
+
providerId,
|
|
568
|
+
operatorId,
|
|
569
|
+
kind,
|
|
570
|
+
status,
|
|
571
|
+
metadata: metadata || {},
|
|
572
|
+
createdAt: now,
|
|
573
|
+
updatedAt: now,
|
|
574
|
+
lastHeartbeatAt: now,
|
|
575
|
+
};
|
|
576
|
+
_providersV2.set(providerId, record);
|
|
577
|
+
return { ...record, metadata: { ...record.metadata } };
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
export function getProviderV2(providerId) {
|
|
581
|
+
const r = _providersV2.get(providerId);
|
|
582
|
+
return r ? { ...r, metadata: { ...r.metadata } } : null;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
export function setProviderMaturityV2(_db, providerId, newStatus, patch = {}) {
|
|
586
|
+
const record = _providersV2.get(providerId);
|
|
587
|
+
if (!record) throw new Error(`Unknown provider: ${providerId}`);
|
|
588
|
+
if (!Object.values(PROVIDER_MATURITY_V2).includes(newStatus))
|
|
589
|
+
throw new Error(`Invalid status: ${newStatus}`);
|
|
590
|
+
const allowed = PROVIDER_TRANSITIONS_V2.get(record.status) || new Set();
|
|
591
|
+
if (!allowed.has(newStatus))
|
|
592
|
+
throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
|
|
593
|
+
if (newStatus === PROVIDER_MATURITY_V2.ACTIVE) {
|
|
594
|
+
if (
|
|
595
|
+
getActiveProviderCount(record.operatorId) >=
|
|
596
|
+
_maxActiveProvidersPerOperatorV2
|
|
597
|
+
)
|
|
598
|
+
throw new Error(
|
|
599
|
+
`Operator ${record.operatorId} reached active-provider cap (${_maxActiveProvidersPerOperatorV2})`,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
record.status = newStatus;
|
|
603
|
+
record.updatedAt = Date.now();
|
|
604
|
+
if (patch.reason !== undefined) record.lastReason = patch.reason;
|
|
605
|
+
if (patch.metadata)
|
|
606
|
+
record.metadata = { ...record.metadata, ...patch.metadata };
|
|
607
|
+
return { ...record, metadata: { ...record.metadata } };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
export function activateProvider(db, id, reason) {
|
|
611
|
+
return setProviderMaturityV2(db, id, PROVIDER_MATURITY_V2.ACTIVE, { reason });
|
|
612
|
+
}
|
|
613
|
+
export function degradeProvider(db, id, reason) {
|
|
614
|
+
return setProviderMaturityV2(db, id, PROVIDER_MATURITY_V2.DEGRADED, {
|
|
615
|
+
reason,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
export function offlineProvider(db, id, reason) {
|
|
619
|
+
return setProviderMaturityV2(db, id, PROVIDER_MATURITY_V2.OFFLINE, {
|
|
620
|
+
reason,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
export function retireProvider(db, id, reason) {
|
|
624
|
+
return setProviderMaturityV2(db, id, PROVIDER_MATURITY_V2.RETIRED, {
|
|
625
|
+
reason,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export function touchProviderHeartbeat(providerId) {
|
|
630
|
+
const record = _providersV2.get(providerId);
|
|
631
|
+
if (!record) throw new Error(`Unknown provider: ${providerId}`);
|
|
632
|
+
record.lastHeartbeatAt = Date.now();
|
|
633
|
+
record.updatedAt = record.lastHeartbeatAt;
|
|
634
|
+
return { ...record, metadata: { ...record.metadata } };
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
export function enqueueDealV2(
|
|
638
|
+
_db,
|
|
639
|
+
{ dealId, providerId, ownerId, metadata } = {},
|
|
640
|
+
) {
|
|
641
|
+
if (!dealId) throw new Error("dealId is required");
|
|
642
|
+
if (!providerId) throw new Error("providerId is required");
|
|
643
|
+
if (!ownerId) throw new Error("ownerId is required");
|
|
644
|
+
if (!_providersV2.has(providerId))
|
|
645
|
+
throw new Error(`Unknown provider: ${providerId}`);
|
|
646
|
+
if (_dealsV2.has(dealId)) throw new Error(`Deal ${dealId} already exists`);
|
|
647
|
+
const now = Date.now();
|
|
648
|
+
const record = {
|
|
649
|
+
dealId,
|
|
650
|
+
providerId,
|
|
651
|
+
ownerId,
|
|
652
|
+
status: DEAL_LIFECYCLE_V2.QUEUED,
|
|
653
|
+
metadata: metadata || {},
|
|
654
|
+
createdAt: now,
|
|
655
|
+
updatedAt: now,
|
|
656
|
+
};
|
|
657
|
+
_dealsV2.set(dealId, record);
|
|
658
|
+
return { ...record, metadata: { ...record.metadata } };
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
export function getDealV2(dealId) {
|
|
662
|
+
const r = _dealsV2.get(dealId);
|
|
663
|
+
return r ? { ...r, metadata: { ...r.metadata } } : null;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export function setDealStatusV2(_db, dealId, newStatus, patch = {}) {
|
|
667
|
+
const record = _dealsV2.get(dealId);
|
|
668
|
+
if (!record) throw new Error(`Unknown deal: ${dealId}`);
|
|
669
|
+
if (!Object.values(DEAL_LIFECYCLE_V2).includes(newStatus))
|
|
670
|
+
throw new Error(`Invalid status: ${newStatus}`);
|
|
671
|
+
const allowed = DEAL_TRANSITIONS_V2.get(record.status) || new Set();
|
|
672
|
+
if (!allowed.has(newStatus))
|
|
673
|
+
throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
|
|
674
|
+
if (newStatus === DEAL_LIFECYCLE_V2.ACTIVE) {
|
|
675
|
+
if (getActiveDealCount(record.providerId) >= _maxActiveDealsPerProviderV2)
|
|
676
|
+
throw new Error(
|
|
677
|
+
`Provider ${record.providerId} reached active-deal cap (${_maxActiveDealsPerProviderV2})`,
|
|
678
|
+
);
|
|
679
|
+
if (!record.activatedAt) record.activatedAt = Date.now();
|
|
680
|
+
}
|
|
681
|
+
record.status = newStatus;
|
|
682
|
+
record.updatedAt = Date.now();
|
|
683
|
+
if (patch.reason !== undefined) record.lastReason = patch.reason;
|
|
684
|
+
if (patch.metadata)
|
|
685
|
+
record.metadata = { ...record.metadata, ...patch.metadata };
|
|
686
|
+
return { ...record, metadata: { ...record.metadata } };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
export function activateDeal(db, id, reason) {
|
|
690
|
+
return setDealStatusV2(db, id, DEAL_LIFECYCLE_V2.ACTIVE, { reason });
|
|
691
|
+
}
|
|
692
|
+
export function completeDeal(db, id, reason) {
|
|
693
|
+
return setDealStatusV2(db, id, DEAL_LIFECYCLE_V2.COMPLETED, { reason });
|
|
694
|
+
}
|
|
695
|
+
export function failDeal(db, id, reason) {
|
|
696
|
+
return setDealStatusV2(db, id, DEAL_LIFECYCLE_V2.FAILED, { reason });
|
|
697
|
+
}
|
|
698
|
+
export function cancelDeal(db, id, reason) {
|
|
699
|
+
return setDealStatusV2(db, id, DEAL_LIFECYCLE_V2.CANCELED, { reason });
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
export function getActiveProviderCount(operatorId) {
|
|
703
|
+
let n = 0;
|
|
704
|
+
for (const r of _providersV2.values()) {
|
|
705
|
+
if (r.status !== PROVIDER_MATURITY_V2.ACTIVE) continue;
|
|
706
|
+
if (operatorId && r.operatorId !== operatorId) continue;
|
|
707
|
+
n++;
|
|
708
|
+
}
|
|
709
|
+
return n;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
export function getActiveDealCount(providerId) {
|
|
713
|
+
let n = 0;
|
|
714
|
+
for (const r of _dealsV2.values()) {
|
|
715
|
+
if (r.status !== DEAL_LIFECYCLE_V2.ACTIVE) continue;
|
|
716
|
+
if (providerId && r.providerId !== providerId) continue;
|
|
717
|
+
n++;
|
|
718
|
+
}
|
|
719
|
+
return n;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
export function autoOfflineStaleProviders(_db, nowMs) {
|
|
723
|
+
const now = nowMs ?? Date.now();
|
|
724
|
+
const flipped = [];
|
|
725
|
+
for (const r of _providersV2.values()) {
|
|
726
|
+
if (
|
|
727
|
+
r.status === PROVIDER_MATURITY_V2.ACTIVE ||
|
|
728
|
+
r.status === PROVIDER_MATURITY_V2.DEGRADED
|
|
729
|
+
) {
|
|
730
|
+
if (now - r.lastHeartbeatAt > _providerIdleMsV2) {
|
|
731
|
+
r.status = PROVIDER_MATURITY_V2.OFFLINE;
|
|
732
|
+
r.updatedAt = now;
|
|
733
|
+
r.lastReason = "heartbeat_timeout";
|
|
734
|
+
flipped.push(r.providerId);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return { flipped, count: flipped.length };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
export function autoFailStuckActiveDeals(_db, nowMs) {
|
|
742
|
+
const now = nowMs ?? Date.now();
|
|
743
|
+
const flipped = [];
|
|
744
|
+
for (const r of _dealsV2.values()) {
|
|
745
|
+
if (r.status === DEAL_LIFECYCLE_V2.ACTIVE) {
|
|
746
|
+
const anchor = r.activatedAt || r.createdAt;
|
|
747
|
+
if (now - anchor > _dealStuckMsV2) {
|
|
748
|
+
r.status = DEAL_LIFECYCLE_V2.FAILED;
|
|
749
|
+
r.updatedAt = now;
|
|
750
|
+
r.lastReason = "deal_timeout";
|
|
751
|
+
flipped.push(r.dealId);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return { flipped, count: flipped.length };
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
export function getDecentralInfraStatsV2() {
|
|
759
|
+
const providersByStatus = {};
|
|
760
|
+
for (const s of Object.values(PROVIDER_MATURITY_V2)) providersByStatus[s] = 0;
|
|
761
|
+
const dealsByStatus = {};
|
|
762
|
+
for (const s of Object.values(DEAL_LIFECYCLE_V2)) dealsByStatus[s] = 0;
|
|
763
|
+
for (const r of _providersV2.values()) providersByStatus[r.status]++;
|
|
764
|
+
for (const r of _dealsV2.values()) dealsByStatus[r.status]++;
|
|
765
|
+
return {
|
|
766
|
+
totalProvidersV2: _providersV2.size,
|
|
767
|
+
totalDealsV2: _dealsV2.size,
|
|
768
|
+
maxActiveProvidersPerOperator: _maxActiveProvidersPerOperatorV2,
|
|
769
|
+
maxActiveDealsPerProvider: _maxActiveDealsPerProviderV2,
|
|
770
|
+
providerIdleMs: _providerIdleMsV2,
|
|
771
|
+
dealStuckMs: _dealStuckMsV2,
|
|
772
|
+
providersByStatus,
|
|
773
|
+
dealsByStatus,
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export function _resetStateV2() {
|
|
778
|
+
_maxActiveProvidersPerOperatorV2 =
|
|
779
|
+
DI_DEFAULT_MAX_ACTIVE_PROVIDERS_PER_OPERATOR;
|
|
780
|
+
_maxActiveDealsPerProviderV2 = DI_DEFAULT_MAX_ACTIVE_DEALS_PER_PROVIDER;
|
|
781
|
+
_providerIdleMsV2 = DI_DEFAULT_PROVIDER_IDLE_MS;
|
|
782
|
+
_dealStuckMsV2 = DI_DEFAULT_DEAL_STUCK_MS;
|
|
783
|
+
_providersV2.clear();
|
|
784
|
+
_dealsV2.clear();
|
|
785
|
+
}
|
package/src/lib/did-manager.js
CHANGED
|
@@ -268,3 +268,370 @@ export function resolveDID(db, did) {
|
|
|
268
268
|
if (!identity) return null;
|
|
269
269
|
return JSON.parse(identity.did_document || "{}");
|
|
270
270
|
}
|
|
271
|
+
|
|
272
|
+
/* ─────────────────────────────────────────────────────────────────────────
|
|
273
|
+
* V2 in-memory governance layer (independent of SQLite did_identities)
|
|
274
|
+
* ───────────────────────────────────────────────────────────────────────── */
|
|
275
|
+
|
|
276
|
+
export const IDENTITY_MATURITY_V2 = Object.freeze({
|
|
277
|
+
PENDING: "pending",
|
|
278
|
+
ACTIVE: "active",
|
|
279
|
+
SUSPENDED: "suspended",
|
|
280
|
+
REVOKED: "revoked",
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
export const ISSUANCE_LIFECYCLE_V2 = Object.freeze({
|
|
284
|
+
QUEUED: "queued",
|
|
285
|
+
ISSUING: "issuing",
|
|
286
|
+
ISSUED: "issued",
|
|
287
|
+
FAILED: "failed",
|
|
288
|
+
CANCELLED: "cancelled",
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const IDENTITY_TERMINAL_V2 = new Set([IDENTITY_MATURITY_V2.REVOKED]);
|
|
292
|
+
const ISSUANCE_TERMINAL_V2 = new Set([
|
|
293
|
+
ISSUANCE_LIFECYCLE_V2.ISSUED,
|
|
294
|
+
ISSUANCE_LIFECYCLE_V2.FAILED,
|
|
295
|
+
ISSUANCE_LIFECYCLE_V2.CANCELLED,
|
|
296
|
+
]);
|
|
297
|
+
|
|
298
|
+
const IDENTITY_TRANSITIONS_V2 = new Map([
|
|
299
|
+
[
|
|
300
|
+
IDENTITY_MATURITY_V2.PENDING,
|
|
301
|
+
new Set([IDENTITY_MATURITY_V2.ACTIVE, IDENTITY_MATURITY_V2.REVOKED]),
|
|
302
|
+
],
|
|
303
|
+
[
|
|
304
|
+
IDENTITY_MATURITY_V2.ACTIVE,
|
|
305
|
+
new Set([IDENTITY_MATURITY_V2.SUSPENDED, IDENTITY_MATURITY_V2.REVOKED]),
|
|
306
|
+
],
|
|
307
|
+
[
|
|
308
|
+
IDENTITY_MATURITY_V2.SUSPENDED,
|
|
309
|
+
new Set([IDENTITY_MATURITY_V2.ACTIVE, IDENTITY_MATURITY_V2.REVOKED]),
|
|
310
|
+
],
|
|
311
|
+
]);
|
|
312
|
+
|
|
313
|
+
const ISSUANCE_TRANSITIONS_V2 = new Map([
|
|
314
|
+
[
|
|
315
|
+
ISSUANCE_LIFECYCLE_V2.QUEUED,
|
|
316
|
+
new Set([ISSUANCE_LIFECYCLE_V2.ISSUING, ISSUANCE_LIFECYCLE_V2.CANCELLED]),
|
|
317
|
+
],
|
|
318
|
+
[
|
|
319
|
+
ISSUANCE_LIFECYCLE_V2.ISSUING,
|
|
320
|
+
new Set([
|
|
321
|
+
ISSUANCE_LIFECYCLE_V2.ISSUED,
|
|
322
|
+
ISSUANCE_LIFECYCLE_V2.FAILED,
|
|
323
|
+
ISSUANCE_LIFECYCLE_V2.CANCELLED,
|
|
324
|
+
]),
|
|
325
|
+
],
|
|
326
|
+
]);
|
|
327
|
+
|
|
328
|
+
export const DID_DEFAULT_MAX_ACTIVE_IDENTITIES_PER_OWNER = 8;
|
|
329
|
+
export const DID_DEFAULT_MAX_PENDING_ISSUANCES_PER_IDENTITY = 12;
|
|
330
|
+
export const DID_DEFAULT_IDENTITY_IDLE_MS = 90 * 24 * 60 * 60 * 1000;
|
|
331
|
+
export const DID_DEFAULT_ISSUANCE_STUCK_MS = 5 * 60 * 1000;
|
|
332
|
+
|
|
333
|
+
let _maxActiveIdentitiesPerOwnerV2 =
|
|
334
|
+
DID_DEFAULT_MAX_ACTIVE_IDENTITIES_PER_OWNER;
|
|
335
|
+
let _maxPendingIssuancesPerIdentityV2 =
|
|
336
|
+
DID_DEFAULT_MAX_PENDING_ISSUANCES_PER_IDENTITY;
|
|
337
|
+
let _identityIdleMsV2 = DID_DEFAULT_IDENTITY_IDLE_MS;
|
|
338
|
+
let _issuanceStuckMsV2 = DID_DEFAULT_ISSUANCE_STUCK_MS;
|
|
339
|
+
|
|
340
|
+
const _identitiesV2 = new Map();
|
|
341
|
+
const _issuancesV2 = new Map();
|
|
342
|
+
|
|
343
|
+
function _posIntDidV2(n, label) {
|
|
344
|
+
if (typeof n !== "number" || !Number.isFinite(n) || n <= 0) {
|
|
345
|
+
throw new Error(`${label} must be a positive number`);
|
|
346
|
+
}
|
|
347
|
+
return Math.floor(n);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function getMaxActiveIdentitiesPerOwnerV2() {
|
|
351
|
+
return _maxActiveIdentitiesPerOwnerV2;
|
|
352
|
+
}
|
|
353
|
+
export function setMaxActiveIdentitiesPerOwnerV2(n) {
|
|
354
|
+
_maxActiveIdentitiesPerOwnerV2 = _posIntDidV2(
|
|
355
|
+
n,
|
|
356
|
+
"maxActiveIdentitiesPerOwner",
|
|
357
|
+
);
|
|
358
|
+
return _maxActiveIdentitiesPerOwnerV2;
|
|
359
|
+
}
|
|
360
|
+
export function getMaxPendingIssuancesPerIdentityV2() {
|
|
361
|
+
return _maxPendingIssuancesPerIdentityV2;
|
|
362
|
+
}
|
|
363
|
+
export function setMaxPendingIssuancesPerIdentityV2(n) {
|
|
364
|
+
_maxPendingIssuancesPerIdentityV2 = _posIntDidV2(
|
|
365
|
+
n,
|
|
366
|
+
"maxPendingIssuancesPerIdentity",
|
|
367
|
+
);
|
|
368
|
+
return _maxPendingIssuancesPerIdentityV2;
|
|
369
|
+
}
|
|
370
|
+
export function getIdentityIdleMsV2() {
|
|
371
|
+
return _identityIdleMsV2;
|
|
372
|
+
}
|
|
373
|
+
export function setIdentityIdleMsV2(ms) {
|
|
374
|
+
_identityIdleMsV2 = _posIntDidV2(ms, "identityIdleMs");
|
|
375
|
+
return _identityIdleMsV2;
|
|
376
|
+
}
|
|
377
|
+
export function getIssuanceStuckMsV2() {
|
|
378
|
+
return _issuanceStuckMsV2;
|
|
379
|
+
}
|
|
380
|
+
export function setIssuanceStuckMsV2(ms) {
|
|
381
|
+
_issuanceStuckMsV2 = _posIntDidV2(ms, "issuanceStuckMs");
|
|
382
|
+
return _issuanceStuckMsV2;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function getActiveIdentityCountV2(ownerId) {
|
|
386
|
+
let n = 0;
|
|
387
|
+
for (const i of _identitiesV2.values()) {
|
|
388
|
+
if (i.status !== IDENTITY_MATURITY_V2.ACTIVE) continue;
|
|
389
|
+
if (ownerId && i.ownerId !== ownerId) continue;
|
|
390
|
+
n++;
|
|
391
|
+
}
|
|
392
|
+
return n;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function getPendingIssuanceCountV2(identityId) {
|
|
396
|
+
let n = 0;
|
|
397
|
+
for (const j of _issuancesV2.values()) {
|
|
398
|
+
if (
|
|
399
|
+
j.status !== ISSUANCE_LIFECYCLE_V2.QUEUED &&
|
|
400
|
+
j.status !== ISSUANCE_LIFECYCLE_V2.ISSUING
|
|
401
|
+
)
|
|
402
|
+
continue;
|
|
403
|
+
if (identityId && j.identityId !== identityId) continue;
|
|
404
|
+
n++;
|
|
405
|
+
}
|
|
406
|
+
return n;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function _cloneIdentityV2(i) {
|
|
410
|
+
return { ...i, metadata: { ...i.metadata } };
|
|
411
|
+
}
|
|
412
|
+
function _cloneIssuanceV2(j) {
|
|
413
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export function registerIdentityV2(
|
|
417
|
+
id,
|
|
418
|
+
{ ownerId, didMethod, displayName, metadata } = {},
|
|
419
|
+
) {
|
|
420
|
+
if (!id || typeof id !== "string") throw new Error("identity id required");
|
|
421
|
+
if (!ownerId || typeof ownerId !== "string")
|
|
422
|
+
throw new Error("ownerId required");
|
|
423
|
+
if (!didMethod || typeof didMethod !== "string")
|
|
424
|
+
throw new Error("didMethod required");
|
|
425
|
+
if (_identitiesV2.has(id)) throw new Error(`identity ${id} already exists`);
|
|
426
|
+
const now = Date.now();
|
|
427
|
+
const identity = {
|
|
428
|
+
id,
|
|
429
|
+
ownerId,
|
|
430
|
+
didMethod,
|
|
431
|
+
displayName: displayName || id,
|
|
432
|
+
status: IDENTITY_MATURITY_V2.PENDING,
|
|
433
|
+
createdAt: now,
|
|
434
|
+
activatedAt: null,
|
|
435
|
+
revokedAt: null,
|
|
436
|
+
lastSeenAt: now,
|
|
437
|
+
metadata: metadata ? { ...metadata } : {},
|
|
438
|
+
};
|
|
439
|
+
_identitiesV2.set(id, identity);
|
|
440
|
+
return _cloneIdentityV2(identity);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export function getIdentityV2(id) {
|
|
444
|
+
const i = _identitiesV2.get(id);
|
|
445
|
+
return i ? _cloneIdentityV2(i) : null;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function listIdentitiesV2({ ownerId, status, didMethod } = {}) {
|
|
449
|
+
const out = [];
|
|
450
|
+
for (const i of _identitiesV2.values()) {
|
|
451
|
+
if (ownerId && i.ownerId !== ownerId) continue;
|
|
452
|
+
if (status && i.status !== status) continue;
|
|
453
|
+
if (didMethod && i.didMethod !== didMethod) continue;
|
|
454
|
+
out.push(_cloneIdentityV2(i));
|
|
455
|
+
}
|
|
456
|
+
return out;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export function setIdentityStatusV2(id, next) {
|
|
460
|
+
const i = _identitiesV2.get(id);
|
|
461
|
+
if (!i) throw new Error(`unknown identity ${id}`);
|
|
462
|
+
if (IDENTITY_TERMINAL_V2.has(i.status)) {
|
|
463
|
+
throw new Error(`identity ${id} is terminal (${i.status})`);
|
|
464
|
+
}
|
|
465
|
+
const allowed = IDENTITY_TRANSITIONS_V2.get(i.status);
|
|
466
|
+
if (!allowed || !allowed.has(next)) {
|
|
467
|
+
throw new Error(`invalid identity transition ${i.status} -> ${next}`);
|
|
468
|
+
}
|
|
469
|
+
if (
|
|
470
|
+
next === IDENTITY_MATURITY_V2.ACTIVE &&
|
|
471
|
+
i.status === IDENTITY_MATURITY_V2.PENDING
|
|
472
|
+
) {
|
|
473
|
+
const owned = getActiveIdentityCountV2(i.ownerId);
|
|
474
|
+
if (owned >= _maxActiveIdentitiesPerOwnerV2) {
|
|
475
|
+
throw new Error(
|
|
476
|
+
`owner ${i.ownerId} active identity cap reached (${_maxActiveIdentitiesPerOwnerV2})`,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const now = Date.now();
|
|
481
|
+
i.status = next;
|
|
482
|
+
if (next === IDENTITY_MATURITY_V2.ACTIVE && !i.activatedAt)
|
|
483
|
+
i.activatedAt = now;
|
|
484
|
+
if (next === IDENTITY_MATURITY_V2.REVOKED && !i.revokedAt) i.revokedAt = now;
|
|
485
|
+
i.lastSeenAt = now;
|
|
486
|
+
return _cloneIdentityV2(i);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export function activateIdentityV2(id) {
|
|
490
|
+
return setIdentityStatusV2(id, IDENTITY_MATURITY_V2.ACTIVE);
|
|
491
|
+
}
|
|
492
|
+
export function suspendIdentityV2(id) {
|
|
493
|
+
return setIdentityStatusV2(id, IDENTITY_MATURITY_V2.SUSPENDED);
|
|
494
|
+
}
|
|
495
|
+
export function revokeIdentityV2(id) {
|
|
496
|
+
return setIdentityStatusV2(id, IDENTITY_MATURITY_V2.REVOKED);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export function touchIdentityV2(id) {
|
|
500
|
+
const i = _identitiesV2.get(id);
|
|
501
|
+
if (!i) throw new Error(`unknown identity ${id}`);
|
|
502
|
+
i.lastSeenAt = Date.now();
|
|
503
|
+
return _cloneIdentityV2(i);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export function createIssuanceV2(
|
|
507
|
+
id,
|
|
508
|
+
{ identityId, credentialType, metadata } = {},
|
|
509
|
+
) {
|
|
510
|
+
if (!id || typeof id !== "string") throw new Error("issuance id required");
|
|
511
|
+
if (!identityId || typeof identityId !== "string")
|
|
512
|
+
throw new Error("identityId required");
|
|
513
|
+
if (!credentialType || typeof credentialType !== "string")
|
|
514
|
+
throw new Error("credentialType required");
|
|
515
|
+
if (_issuancesV2.has(id)) throw new Error(`issuance ${id} already exists`);
|
|
516
|
+
if (!_identitiesV2.has(identityId))
|
|
517
|
+
throw new Error(`unknown identity ${identityId}`);
|
|
518
|
+
const pending = getPendingIssuanceCountV2(identityId);
|
|
519
|
+
if (pending >= _maxPendingIssuancesPerIdentityV2) {
|
|
520
|
+
throw new Error(
|
|
521
|
+
`identity ${identityId} pending issuance cap reached (${_maxPendingIssuancesPerIdentityV2})`,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
const now = Date.now();
|
|
525
|
+
const job = {
|
|
526
|
+
id,
|
|
527
|
+
identityId,
|
|
528
|
+
credentialType,
|
|
529
|
+
status: ISSUANCE_LIFECYCLE_V2.QUEUED,
|
|
530
|
+
createdAt: now,
|
|
531
|
+
startedAt: null,
|
|
532
|
+
settledAt: null,
|
|
533
|
+
metadata: metadata ? { ...metadata } : {},
|
|
534
|
+
};
|
|
535
|
+
_issuancesV2.set(id, job);
|
|
536
|
+
return _cloneIssuanceV2(job);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function getIssuanceV2(id) {
|
|
540
|
+
const j = _issuancesV2.get(id);
|
|
541
|
+
return j ? _cloneIssuanceV2(j) : null;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export function listIssuancesV2({ identityId, status } = {}) {
|
|
545
|
+
const out = [];
|
|
546
|
+
for (const j of _issuancesV2.values()) {
|
|
547
|
+
if (identityId && j.identityId !== identityId) continue;
|
|
548
|
+
if (status && j.status !== status) continue;
|
|
549
|
+
out.push(_cloneIssuanceV2(j));
|
|
550
|
+
}
|
|
551
|
+
return out;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
export function setIssuanceStatusV2(id, next) {
|
|
555
|
+
const j = _issuancesV2.get(id);
|
|
556
|
+
if (!j) throw new Error(`unknown issuance ${id}`);
|
|
557
|
+
if (ISSUANCE_TERMINAL_V2.has(j.status)) {
|
|
558
|
+
throw new Error(`issuance ${id} is terminal (${j.status})`);
|
|
559
|
+
}
|
|
560
|
+
const allowed = ISSUANCE_TRANSITIONS_V2.get(j.status);
|
|
561
|
+
if (!allowed || !allowed.has(next)) {
|
|
562
|
+
throw new Error(`invalid issuance transition ${j.status} -> ${next}`);
|
|
563
|
+
}
|
|
564
|
+
const now = Date.now();
|
|
565
|
+
j.status = next;
|
|
566
|
+
if (next === ISSUANCE_LIFECYCLE_V2.ISSUING && !j.startedAt) j.startedAt = now;
|
|
567
|
+
if (ISSUANCE_TERMINAL_V2.has(next) && !j.settledAt) j.settledAt = now;
|
|
568
|
+
return _cloneIssuanceV2(j);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export function startIssuanceV2(id) {
|
|
572
|
+
return setIssuanceStatusV2(id, ISSUANCE_LIFECYCLE_V2.ISSUING);
|
|
573
|
+
}
|
|
574
|
+
export function completeIssuanceV2(id) {
|
|
575
|
+
return setIssuanceStatusV2(id, ISSUANCE_LIFECYCLE_V2.ISSUED);
|
|
576
|
+
}
|
|
577
|
+
export function failIssuanceV2(id) {
|
|
578
|
+
return setIssuanceStatusV2(id, ISSUANCE_LIFECYCLE_V2.FAILED);
|
|
579
|
+
}
|
|
580
|
+
export function cancelIssuanceV2(id) {
|
|
581
|
+
return setIssuanceStatusV2(id, ISSUANCE_LIFECYCLE_V2.CANCELLED);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function autoSuspendIdleIdentitiesV2({ now = Date.now() } = {}) {
|
|
585
|
+
const out = [];
|
|
586
|
+
for (const i of _identitiesV2.values()) {
|
|
587
|
+
if (i.status !== IDENTITY_MATURITY_V2.ACTIVE) continue;
|
|
588
|
+
if (now - i.lastSeenAt < _identityIdleMsV2) continue;
|
|
589
|
+
i.status = IDENTITY_MATURITY_V2.SUSPENDED;
|
|
590
|
+
i.lastSeenAt = now;
|
|
591
|
+
out.push(_cloneIdentityV2(i));
|
|
592
|
+
}
|
|
593
|
+
return out;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export function autoFailStuckIssuancesV2({ now = Date.now() } = {}) {
|
|
597
|
+
const out = [];
|
|
598
|
+
for (const j of _issuancesV2.values()) {
|
|
599
|
+
if (j.status !== ISSUANCE_LIFECYCLE_V2.ISSUING) continue;
|
|
600
|
+
if (!j.startedAt || now - j.startedAt < _issuanceStuckMsV2) continue;
|
|
601
|
+
j.status = ISSUANCE_LIFECYCLE_V2.FAILED;
|
|
602
|
+
j.settledAt = now;
|
|
603
|
+
out.push(_cloneIssuanceV2(j));
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
export function getDidManagerStatsV2() {
|
|
609
|
+
const identitiesByStatus = {};
|
|
610
|
+
for (const s of Object.values(IDENTITY_MATURITY_V2))
|
|
611
|
+
identitiesByStatus[s] = 0;
|
|
612
|
+
for (const i of _identitiesV2.values()) identitiesByStatus[i.status]++;
|
|
613
|
+
const issuancesByStatus = {};
|
|
614
|
+
for (const s of Object.values(ISSUANCE_LIFECYCLE_V2))
|
|
615
|
+
issuancesByStatus[s] = 0;
|
|
616
|
+
for (const j of _issuancesV2.values()) issuancesByStatus[j.status]++;
|
|
617
|
+
return {
|
|
618
|
+
totalIdentitiesV2: _identitiesV2.size,
|
|
619
|
+
totalIssuancesV2: _issuancesV2.size,
|
|
620
|
+
maxActiveIdentitiesPerOwner: _maxActiveIdentitiesPerOwnerV2,
|
|
621
|
+
maxPendingIssuancesPerIdentity: _maxPendingIssuancesPerIdentityV2,
|
|
622
|
+
identityIdleMs: _identityIdleMsV2,
|
|
623
|
+
issuanceStuckMs: _issuanceStuckMsV2,
|
|
624
|
+
identitiesByStatus,
|
|
625
|
+
issuancesByStatus,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export function _resetStateDidManagerV2() {
|
|
630
|
+
_identitiesV2.clear();
|
|
631
|
+
_issuancesV2.clear();
|
|
632
|
+
_maxActiveIdentitiesPerOwnerV2 = DID_DEFAULT_MAX_ACTIVE_IDENTITIES_PER_OWNER;
|
|
633
|
+
_maxPendingIssuancesPerIdentityV2 =
|
|
634
|
+
DID_DEFAULT_MAX_PENDING_ISSUANCES_PER_IDENTITY;
|
|
635
|
+
_identityIdleMsV2 = DID_DEFAULT_IDENTITY_IDLE_MS;
|
|
636
|
+
_issuanceStuckMsV2 = DID_DEFAULT_ISSUANCE_STUCK_MS;
|
|
637
|
+
}
|