Initial commit
Some checks failed
CI / test (push) Has been cancelled
CI / security (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
defiQUG
2025-12-12 15:02:56 -08:00
commit 849e6a8357
891 changed files with 167728 additions and 0 deletions

View File

@@ -0,0 +1,202 @@
// Sovereign Identity Fabric (SIF) - Root Sovereign Identity Management
import { hsmService, HSMService } from '@/integration/hsm/hsm.service';
import { IdentityType, SovereignIdentity } from '@/shared/types';
import prisma from '@/shared/database/prisma';
export interface RootSovereignIdentity {
sovereignBankId: string;
rootSovereignKey: string; // HSM key ID
identities: Map<IdentityType, string>; // Identity type -> HSM key ID
}
/**
* Sovereign Identity Fabric Service
* Manages tiered identities for sovereign banks
*/
export class SovereignIdentityFabricService {
private rootIdentities: Map<string, RootSovereignIdentity> = new Map();
/**
* Create Root Sovereign Identity (RSK) for a sovereign bank
*/
async createRootSovereignIdentity(
sovereignBankId: string,
sovereignCode: string
): Promise<RootSovereignIdentity> {
// Generate Root Sovereign Key in HSM
const rootKey = await hsmService.generateKeyPair('ECC-521', `RSK-${sovereignCode}`);
const rootIdentity: RootSovereignIdentity = {
sovereignBankId,
rootSovereignKey: rootKey.keyId,
identities: new Map(),
};
// Create Master Identity
const masterKey = await hsmService.generateKeyPair('ECC-521', `MASTER-${sovereignCode}`);
rootIdentity.identities.set(IdentityType.MASTER, masterKey.keyId);
// Store in database
await prisma.sovereignIdentity.create({
data: {
sovereignBankId,
identityType: IdentityType.MASTER,
identityKey: masterKey.publicKey,
hsmKeyId: masterKey.keyId,
status: 'active',
},
});
// Update sovereign bank record
await prisma.sovereignBank.update({
where: { id: sovereignBankId },
data: {
rootSovereignKey: rootKey.keyId,
hsmIdentity: rootKey.keyId,
},
});
this.rootIdentities.set(sovereignBankId, rootIdentity);
return rootIdentity;
}
/**
* Create tiered identity (Treasury, CBDC, Settlement, API)
*/
async createTieredIdentity(
sovereignBankId: string,
identityType: IdentityType,
sovereignCode: string
): Promise<string> {
const rootIdentity = this.rootIdentities.get(sovereignBankId);
if (!rootIdentity) {
throw new Error(`Root identity not found for sovereign bank: ${sovereignBankId}`);
}
// Generate identity key in HSM
const identityKey = await hsmService.generateKeyPair(
'ECC-521',
`${identityType}-${sovereignCode}`
);
rootIdentity.identities.set(identityType, identityKey.keyId);
// Store in database
await prisma.sovereignIdentity.create({
data: {
sovereignBankId,
identityType,
identityKey: identityKey.publicKey,
hsmKeyId: identityKey.keyId,
status: 'active',
},
});
return identityKey.keyId;
}
/**
* Get identity for a sovereign bank and identity type
*/
async getIdentity(
sovereignBankId: string,
identityType: string
): Promise<{ hsmKeyId: string; identityKey: string } | null> {
const identity = await prisma.sovereignIdentity.findFirst({
where: {
sovereignBankId,
identityType,
status: 'active',
},
});
if (!identity || !identity.hsmKeyId) {
return null;
}
return {
hsmKeyId: identity.hsmKeyId,
identityKey: identity.identityKey,
};
}
/**
* Get identity key ID for a sovereign bank and identity type
*/
async getIdentityKeyId(
sovereignBankId: string,
identityType: IdentityType
): Promise<string | null> {
const identity = await this.getIdentity(sovereignBankId, identityType);
return identity?.hsmKeyId || null;
}
/**
* Sign data using sovereign identity
*/
async signWithSovereignIdentity(
data: string,
sovereignBankId: string,
identityType: IdentityType
): Promise<string> {
const keyId = await this.getIdentityKeyId(sovereignBankId, identityType);
if (!keyId) {
throw new Error(
`Identity not found: ${sovereignBankId} - ${identityType}`
);
}
const signature = await hsmService.sign(data, keyId);
return signature.signature;
}
/**
* Verify signature using sovereign identity
*/
async verifySovereignSignature(
data: string,
signature: string,
sovereignBankId: string,
identityType: IdentityType
): Promise<boolean> {
const keyId = await this.getIdentityKeyId(sovereignBankId, identityType);
if (!keyId) {
return false;
}
return await hsmService.verify(data, signature, keyId);
}
/**
* Rotate sovereign identity key
*/
async rotateIdentity(
sovereignBankId: string,
identityType: IdentityType,
sovereignCode: string
): Promise<string> {
const oldKeyId = await this.getIdentityKeyId(sovereignBankId, identityType);
if (oldKeyId) {
await hsmService.destroyKey(oldKeyId);
}
return await this.createTieredIdentity(sovereignBankId, identityType, sovereignCode);
}
/**
* Get all identities for a sovereign bank
*/
async getSovereignIdentities(sovereignBankId: string): Promise<SovereignIdentity[]> {
return await prisma.sovereignIdentity.findMany({
where: {
sovereignBankId,
status: 'active',
},
});
}
}
export const sovereignIdentityFabric = new SovereignIdentityFabricService();

View File

@@ -0,0 +1,24 @@
// Multi-Tenancy System - Sovereign Isolation
import prisma from '@/shared/database/prisma';
export class MultitenancyService {
/**
* Enforce sovereign isolation
*/
async enforceIsolation(sovereignBankId: string): Promise<void> {
// In production, this would enforce database, network, and identity isolation
// For now, simplified implementation
}
/**
* Check data sovereignty
*/
async checkDataSovereignty(sovereignBankId: string, dataId: string): Promise<boolean> {
// In production, this would verify data belongs to sovereign
return true;
}
}
export const multitenancyService = new MultitenancyService();

View File

@@ -0,0 +1,56 @@
// OMNL White-Label Instance Setup
import prisma from '@/shared/database/prisma';
import { sovereignIdentityFabric } from '@/sovereign/identity/sovereign-identity-fabric.service';
import { SOVEREIGN_CODES } from '@/shared/constants';
import { v4 as uuidv4 } from 'uuid';
export class OmnlService {
/**
* Initialize OMNL sovereign instance
*/
async initializeOmnlInstance(): Promise<void> {
// Create OMNL sovereign bank record
const omnlBank = await prisma.sovereignBank.upsert({
where: { sovereignCode: SOVEREIGN_CODES.OMNL },
update: {},
create: {
sovereignCode: SOVEREIGN_CODES.OMNL,
name: 'OMNL Central Bank',
bic: 'OMNLXXXX',
status: 'active',
},
});
// Create Root Sovereign Identity
await sovereignIdentityFabric.createRootSovereignIdentity(
omnlBank.id,
SOVEREIGN_CODES.OMNL
);
// Create tiered identities
await sovereignIdentityFabric.createTieredIdentity(
omnlBank.id,
'Treasury',
SOVEREIGN_CODES.OMNL
);
await sovereignIdentityFabric.createTieredIdentity(
omnlBank.id,
'CBDC',
SOVEREIGN_CODES.OMNL
);
await sovereignIdentityFabric.createTieredIdentity(
omnlBank.id,
'Settlement',
SOVEREIGN_CODES.OMNL
);
await sovereignIdentityFabric.createTieredIdentity(
omnlBank.id,
'API',
SOVEREIGN_CODES.OMNL
);
}
}
export const omnlService = new OmnlService();