# umarhashmi2002/hedera-kms-signer architecture

> Architecture diagram of the GitHub repository umarhashmi2002/hedera-kms-signer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/umarhashmi2002/hedera-kms-signer
- Repository: https://github.com/umarhashmi2002/hedera-kms-signer

## Overview

#### Purpose and Entry Points
Hedera Key Guardian is a serverless custodial signing backend for Hedera. It exposes JWT-protected HTTP endpoints that validate transfer requests, enforce policy, sign with non-exportable AWS KMS secp256k1 keys, submit to Hedera, and emit dual audit records. The Lambda entry point and router are `src/handler.ts`; the deployable AWS application starts at `infra/bin/app.ts` and is defined by `infra/lib/hedera-kms-signer-stack.ts`.

#### Request Boundary and Dispatch
API Gateway authenticates callers with Cognito JWTs before invoking one Node.js 20 Lambda. `src/handler.ts` dispatches `/sign-transfer`, token transfer, scheduling, public-key, rotation, multisig, audit-topic, and public docs routes. Request contracts and validation form the application’s untrusted-input boundary in `src/schemas.ts`; the API surface is specified in `docs/openapi.yaml`.

#### Authorization, Validation, and Idempotency
Before KMS access, the handler validates UUIDs, Hedera account IDs, amounts, and route-specific payloads, then evaluates transaction policy in `src/policy.ts`. Policies constrain amount, recipient allowlists, transaction type, and permitted hours. Audit persistence in `src/audit.ts` supports request-id idempotency and conflict detection, using DynamoDB conditional writes to make duplicate/replayed requests return cached results or conflict rather than re-sign.

#### Hedera Transaction Construction and Signing
The core signing path is `src/hedera.ts` plus `src/kms.ts`. The service constructs and freezes a Hedera transaction, hashes frozen bytes with keccak256, and sends that 32-byte digest to AWS KMS using the secp256k1 signing key and digest mode. KMS returns a DER ECDSA signature; the application parses it into Hedera-compatible `(r,s)` form, attaches it, submits the signed transaction through the Hedera SDK, and returns consensus status/transaction ID. This keccak-before-KMS boundary is the critical chain-specific integration.

#### Transaction Variants and Key Lifecycle
Base HBAR transfer handling is extended by separate transaction orchestration modules: HTS token transfers in `src/tokenTransfer.ts` and scheduled transfers in `src/scheduledTransfer.ts`. `src/publicKey.ts` obtains/derives KMS public-key representations for account setup and EVM use. Key rotation in `src/rotation.ts` creates a replacement KMS key, updates the Hedera account using the current signer, then disables the old key. Threshold/multisig configuration is modeled in `src/multisig.ts`.

#### Audit and Hedera Consensus Logging
Successful and rejected signing decisions are persisted to DynamoDB through `src/audit.ts` for queryable operational audit. `src/consensus.ts` additionally publishes selected audit events to a Hedera Consensus Service topic, creating an external tamper-evident record. These writes are downstream side effects of request processing; transaction submission and audit/HCS outcomes need distinct failure handling.

#### Infrastructure and External Services
The CDK stack in `infra/lib/hedera-kms-signer-stack.ts` provisions/wires API Gateway, Cognito, Lambda, KMS (`ECC_SECG_P256K1`), DynamoDB, CloudWatch, CloudTrail, and SNS. Runtime dependencies cross AWS trust boundaries (JWT verification, KMS signing, audit storage/monitoring) and Hedera network boundaries (transaction consensus and HCS). Operational architecture and security assumptions are expanded in `docs/architecture.md` and `docs/threat-model.md`.

Main entry points: [`infra/lib/hedera-kms-signer-stack.ts`](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/infra/lib/hedera-kms-signer-stack.ts) (Signer stack), [`src/handler.ts`](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/handler.ts) (Signing Lambda), [`src/policy.ts`](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/policy.ts) (Transaction policy), [`src/audit.ts`](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/audit.ts) (Audit and idempotency)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_deployment["Deployment"]
  node_cdk_app["CDK app<br/>deployment entry<br/>[app.ts]"]
  node_cdk_stack["Signer stack<br/>CDK stack"]
  node_openapi["OpenAPI surface<br/>API contract<br/>[openapi.yaml]"]
end

subgraph group_runtime["Lambda runtime"]
  node_lambda["Signing Lambda<br/>Node.js 20 function<br/>[handler.ts]"]
  node_schemas["Request validation<br/>input boundary<br/>[schemas.ts]"]
  node_policy["Transaction policy<br/>authorization rules<br/>[policy.ts]"]
  node_audit["Audit and idempotency<br/>audit service<br/>[audit.ts]"]
  node_tx_variants["Transaction variants<br/>transfer orchestration<br/>[tokenTransfer.ts]"]
  node_hedera_signing["Hedera signing flow<br/>transaction signer<br/>[hedera.ts]"]
  node_kms_adapter["KMS signature adapter<br/>secp256k1 adapter<br/>[kms.ts]"]
  node_key_lifecycle["Key lifecycle routes<br/>key management<br/>[rotation.ts]"]
end

subgraph group_aws["AWS services"]
  node_api_gateway{{"API Gateway<br/>HTTP gateway"}}
  node_cognito{{"Cognito<br/>JWT authorizer"}}
  node_kms{{"AWS KMS key<br/>ECC_SECG_P256K1"}}
  node_dynamodb[("DynamoDB audit store<br/>audit database")]
  node_monitoring["CloudWatch, CloudTrail, SNS<br/>operations"]
end

subgraph group_hedera["Hedera network"]
  node_hedera_network{{"Hedera network<br/>transaction consensus"}}
  node_consensus["HCS audit publisher<br/>consensus logging<br/>[consensus.ts]"]
  node_hcs_topic["HCS topic<br/>audit topic"]
end

node_cdk_app -->|"instantiates"| node_cdk_stack
node_cdk_stack -->|"provisions"| node_api_gateway
node_cdk_stack -->|"provisions"| node_cognito
node_cdk_stack -->|"deploys"| node_lambda
node_cdk_stack -->|"provisions"| node_kms
node_cdk_stack -->|"provisions"| node_dynamodb
node_cdk_stack -->|"wires"| node_monitoring
node_openapi -.->|"specifies routes"| node_api_gateway
node_api_gateway -->|"authenticates with"| node_cognito
node_api_gateway -->|"invokes after JWT"| node_lambda
node_lambda -->|"validates request"| node_schemas
node_schemas -->|"valid input"| node_policy
node_policy -->|"records decision / checks request ID"| node_audit
node_policy -->|"approved transfer"| node_tx_variants
node_tx_variants -->|"builds transaction"| node_hedera_signing
node_hedera_signing -->|"keccak256 digest"| node_kms_adapter
node_kms_adapter -->|"signs digest"| node_kms
node_hedera_signing -->|"submits signed transaction"| node_hedera_network
node_lambda -->|"dispatches key routes"| node_key_lifecycle
node_key_lifecycle -->|"creates / reads / rotates keys"| node_kms
node_audit -->|"conditional audit writes"| node_dynamodb
node_audit -->|"selected events"| node_consensus
node_consensus -->|"publishes"| node_hcs_topic
node_lambda -.->|"logs and alerts"| node_monitoring

click node_cdk_app "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/infra/bin/app.ts"
click node_cdk_stack "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/infra/lib/hedera-kms-signer-stack.ts"
click node_openapi "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/docs/openapi.yaml"
click node_lambda "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/handler.ts"
click node_schemas "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/schemas.ts"
click node_policy "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/policy.ts"
click node_audit "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/audit.ts"
click node_tx_variants "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/tokenTransfer.ts"
click node_hedera_signing "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/hedera.ts"
click node_kms_adapter "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/kms.ts"
click node_key_lifecycle "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/rotation.ts"
click node_consensus "https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/consensus.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cdk_app,node_cdk_stack,node_openapi toneBlue
class node_lambda,node_schemas,node_policy,node_audit,node_tx_variants,node_hedera_signing,node_kms_adapter,node_key_lifecycle toneAmber
class node_api_gateway,node_cognito,node_kms,node_dynamodb,node_monitoring toneMint
class node_hedera_network,node_consensus,node_hcs_topic toneRose
```

## Components

### Deployment

- [CDK app](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/infra/bin/app.ts): deployment entry · `infra/bin/app.ts`
- [Signer stack](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/infra/lib/hedera-kms-signer-stack.ts): CDK stack · Provisions and wires the serverless signing system. · `infra/lib/hedera-kms-signer-stack.ts`
- [OpenAPI surface](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/docs/openapi.yaml): API contract · `docs/openapi.yaml`

### Lambda runtime

- [Signing Lambda](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/handler.ts): Node.js 20 function · `src/handler.ts`
- [Request validation](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/schemas.ts): input boundary · Validates route payloads, UUIDs, account IDs, and amounts. · `src/schemas.ts`
- [Transaction policy](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/policy.ts): authorization rules · Enforces limits, allowlists, transaction types, and permitted hours. · `src/policy.ts`
- [Audit and idempotency](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/audit.ts): audit service · Uses conditional writes for replay detection, conflicts, and cached results. · `src/audit.ts`
- [Transaction variants](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/tokenTransfer.ts): transfer orchestration · Handles HBAR, HTS token, and scheduled transfers. · `src/tokenTransfer.ts`
- [Hedera signing flow](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/hedera.ts): transaction signer · Freezes transactions, hashes bytes with keccak256, attaches KMS signatures, and submits. · `src/hedera.ts`
- [KMS signature adapter](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/kms.ts): secp256k1 adapter · Signs the 32-byte digest and converts DER ECDSA to Hedera (r,s). · `src/kms.ts`
- [Key lifecycle routes](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/rotation.ts): key management · Provides public key, rotation, and multisig configuration operations. · `src/rotation.ts`

### AWS services

- **API Gateway**: HTTP gateway
- **Cognito**: JWT authorizer
- **AWS KMS key**: ECC_SECG_P256K1 · Non-exportable custodial signing key.
- **DynamoDB audit store**: audit database
- **CloudWatch, CloudTrail, SNS**: operations

### Hedera network

- **Hedera network**: transaction consensus
- [HCS audit publisher](https://github.com/umarhashmi2002/hedera-kms-signer/blob/main/src/consensus.ts): consensus logging · Publishes selected audit events to a tamper-evident topic. · `src/consensus.ts`
- **HCS topic**: audit topic

## Connections

- CDK app → Signer stack: instantiates
- Signer stack → API Gateway: provisions
- Signer stack → Cognito: provisions
- Signer stack → Signing Lambda: deploys
- Signer stack → AWS KMS key: provisions
- Signer stack → DynamoDB audit store: provisions
- Signer stack → CloudWatch, CloudTrail, SNS: wires
- OpenAPI surface → API Gateway: specifies routes
- API Gateway → Cognito: authenticates with
- API Gateway → Signing Lambda: invokes after JWT
- Signing Lambda → Request validation: validates request
- Request validation → Transaction policy: valid input
- Transaction policy → Audit and idempotency: records decision / checks request ID
- Transaction policy → Transaction variants: approved transfer
- Transaction variants → Hedera signing flow: builds transaction
- Hedera signing flow → KMS signature adapter: keccak256 digest
- KMS signature adapter → AWS KMS key: signs digest
- Hedera signing flow → Hedera network: submits signed transaction
- Signing Lambda → Key lifecycle routes: dispatches key routes
- Key lifecycle routes → AWS KMS key: creates / reads / rotates keys
- Audit and idempotency → DynamoDB audit store: conditional audit writes
- Audit and idempotency → HCS audit publisher: selected events
- HCS audit publisher → HCS topic: publishes
- Signing Lambda → CloudWatch, CloudTrail, SNS: logs and alerts
