L2PS (Layer 2 Private Subnets) class for encrypted transaction processing.

This class implements a multi-singleton pattern to manage multiple L2PS networks. Each L2PS instance provides AES-GCM encrypted transaction capabilities while maintaining compatibility with the standard DEMOS transaction format.

Key features:

  • AES-GCM authenticated encryption for transaction confidentiality and integrity
  • Multi-singleton pattern for managing multiple L2PS networks
  • Standard Transaction object compatibility for seamless integration
  • SHA-256 based instance identification
// Create a new L2PS instance
const l2ps = await L2PS.create();

// Encrypt a transaction
const encryptedTx = await l2ps.encryptTx(originalTransaction);

// Decrypt a transaction
const decryptedTx = await l2ps.decryptTx(encryptedTx);

Properties

config?: L2PSConfig

Configuration for this L2PS network (optional)

Methods

  • Decrypts an L2PS encrypted transaction and returns the original transaction.

    Validates that the transaction is of type "l2psEncryptedTx", extracts the encrypted payload, verifies the L2PS UID matches, performs AES-GCM decryption with authentication, and validates the original transaction hash for integrity.

    Parameters

    Returns Promise<Transaction>

    Promise resolving to the original decrypted Transaction

    If transaction is not l2psEncryptedTx type, wrong L2PS UID, authentication fails, or hash mismatch

    const encryptedTx: Transaction = {  encrypted transaction  };
    const originalTx = await l2ps.decryptTx(encryptedTx);
    // originalTx is now the original transaction before encryption
  • Encrypts a transaction using AES-GCM and wraps it in a standard Transaction object.

    The original transaction is serialized, encrypted with AES-GCM for authenticated encryption, and then wrapped in a new Transaction object with type "l2psEncryptedTx". This allows encrypted transactions to be processed through the standard transaction pipeline.

    Parameters

    • tx: Transaction

      The original transaction to encrypt

    • OptionalsenderIdentity: any

      Optional sender identity to use in the encrypted transaction wrapper

    Returns Promise<L2PSTransaction>

    Promise resolving to a new Transaction object containing the encrypted data

    If transaction is null/undefined or encryption fails

    const originalTx: Transaction = {  transaction data  };
    const encryptedTx = await l2ps.encryptTx(originalTx, senderPublicKey);
    // encryptedTx can now be inserted into mempool like any other transaction
  • Returns the unique identifier for this L2PS instance. The ID is a SHA-256 hash of the private key.

    Returns string

    The L2PS instance ID

  • Returns a short fingerprint of the private key for identification purposes. Uses the first 16 characters of the SHA-256 hash of the private key.

    Returns Promise<string>

    Promise resolving to a 16-character fingerprint string

  • Sets the configuration for this L2PS instance.

    Parameters

    Returns void

    If config is invalid or missing required UID

  • Factory method to create a new L2PS instance. Generates cryptographically secure random keys if not provided.

    Parameters

    • OptionalprivateKey: string

      Optional AES private key as string. If not provided, generates 32 random bytes

    • Optionaliv: string

      Optional initialization vector as string. If not provided, generates 12 random bytes

    Returns Promise<L2PS>

    Promise resolving to a new L2PS instance

    // Create with random keys
    const l2ps1 = await L2PS.create();

    // Create with specific keys
    const l2ps2 = await L2PS.create(myPrivateKey, myIV);
  • Retrieves an existing L2PS instance by its ID.

    Parameters

    • id: string

      The unique identifier of the L2PS instance

    Returns L2PS

    The L2PS instance if found, undefined otherwise

  • Returns all currently active L2PS instances.

    Returns L2PS[]

    Array of all L2PS instances

  • Checks if an L2PS instance with the given ID exists.

    Parameters

    • id: string

      The unique identifier to check

    Returns boolean

    True if the instance exists, false otherwise

  • Removes an L2PS instance from the registry.

    Parameters

    • id: string

      The unique identifier of the instance to remove

    Returns boolean

    True if the instance was removed, false if it didn't exist