This project represents a TypeScript implementation of an AWS customer master key (CMK) based Kaia network account using AWS Lambda and the AWS Cloud Development Kit (CDK), specifically designed for multisend reward distributions.
This repository contains a TypeScript/Node.js implementation of the AWS KMS Kaia network integration, providing functionality for sending rewards to multiple users on the Kaia network using AWS KMS for secure key management.
- Kaia Network Integration: Create and manage Kaia network accounts using AWS KMS
- Multisend Functionality: Send KAIA tokens and native tokens to multiple recipients in batch operations
- Token Operations: Support for both native KAIA tokens and ERC-20 tokens
- Transaction Signing: Sign Kaia transactions using AWS KMS
- Multi-Network Support: Support for Kaia mainnet and testnet
- TypeScript: Full type safety and modern development experience
- CDK Infrastructure: Infrastructure as code using AWS CDK
- KMS Key Import: Import existing private keys into AWS KMS
The project consists of:
- CDK Stack (
lib/aws-kaia-lambda-stack.ts): Defines the AWS infrastructure - Kaia Utilities (
src/utils/kaia-utils.ts): Core Kaia operations including multisend - Lambda Functions:
kaia-kms-client: Basic Kaia operationsKaiaMultisendClient: Multisend functionality for reward distributionsKMSImportLambda: Import existing private keys into AWS KMS
- Infrastructure: KMS keys, Lambda functions, and IAM permissions
- Node.js 18.x or later
- AWS CLI configured with appropriate credentials
- AWS CDK CLI installed globally
- TypeScript knowledge
-
Clone the repository and navigate to the project directory:
cd aws-kaia-rewards -
Install dependencies:
npm install
-
Install CDK globally (if not already installed):
npm install -g aws-cdk
Set the following environment variables:
export KAIA_NETWORK=mainnet # or testnet
export AWS_REGION=us-east-1
export CDK_DEFAULT_ACCOUNT=your-aws-account-id
export CDK_DEFAULT_REGION=your-aws-regionOption 1: Use Existing KMS Key (Recommended)
export KMS_KEY_ID=your-existing-kms-key-id-or-arnThis will use an existing KMS key instead of creating a new one. You can specify either:
- Key ID:
5bb59668-5613-4104-bf93-08f619dca565 - Full ARN:
arn:aws:kms:us-east-1:123456789012:key/5bb59668-5613-4104-bf93-08f619dca565
Option 2: Import Private Key (Creates New Key)
export PRIVATE_KEY=your-private-key-hereThis will create a new KMS key and import your private key.
Option 3: Auto-create New Key
If neither KMS_KEY_ID nor PRIVATE_KEY is set, CDK will automatically create a new KMS key.
You can customize RPC endpoints by setting:
export KAIA_MAINNET_RPC_URL=https://your-mainnet-rpc-url
export KAIA_TESTNET_RPC_URL=https://your-testnet-rpc-url
export CUSTOM_RPC_URL=https://your-custom-rpc-urlnpm run build# Synthesize CloudFormation template
npm run synth
# Deploy the stack
npm run deploy
# Destroy the stack
npm run destroy
# Show differences
npm run diff
# Bootstrap CDK (first time only)
npm run bootstrapThe Lambda functions support the following operations:
status: Get Kaia address from KMS public keysend: Create and sign a transactionsign: Sign a transaction (same as send)personal_sign: Sign a personal messageeth_sign: Sign a message hashverify: Verify a signaturebalance: Get account balance
status: Get Kaia address from KMS public keyapprove_token_and_send: Approve token spending and create transactionmultisend_token_and_send: Send tokens to multiple recipientsmultisend_ether_and_send: Send native KAIA to multiple recipientsget_transaction_status: Check transaction statusbalance: Get account balance
- Purpose: Import existing Kaia private keys into AWS KMS
- Input: Private key, key description, and AWS region
- Output: KMS Key ID and ARN
- Usage: One-time operation for migrating existing keys to AWS KMS
{
"operation": "status"
}{
"operation": "send",
"dst_address": "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
"amount": "0.001",
"nonce": 0
}{
"operation": "approve_token_and_send",
"tokenAddress": "0x1234567890123456789012345678901234567890",
"amount": "1000000000000000000"
}{
"operation": "multisend_token_and_send",
"tokenAddress": "0x1234567890123456789012345678901234567890",
"recipients": [
"0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
"0x8ba1f109551bD432803012645Hac136c"
],
"amounts": ["1000000000000000000", "2000000000000000000"]
}{
"operation": "multisend_ether_and_send",
"recipients": [
"0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
"0x8ba1f109551bD432803012645Hac136c"
],
"amounts": ["1000000000000000000", "2000000000000000000"],
"value": "3000000000000000000"
}{
"operation": "get_transaction_status",
"txHash": "0x1234567890abcdef..."
}- KMS keys are configured with ECC_SECG_P256K1 specification for Kaia compatibility
- Lambda functions have minimal IAM permissions (only KMS GetPublicKey and Sign)
- Private keys are never exposed in the code
- All cryptographic operations are performed by AWS KMS
- Kaia Mainnet: Production Kaia network (Chain ID: 8217)
- Kaia Testnet: Test Kaia network (Chain ID: 1001)
- Custom Networks: Support for custom RPC endpoints
The Lambda functions include comprehensive error handling:
- Input validation for all operations
- Detailed error messages for debugging
- Proper HTTP status codes
- CORS headers for web integration
- CloudWatch logs for all Lambda executions
- Structured logging with operation details
- Error tracking and debugging information
- Performance metrics via CloudWatch
- KMS charges per API call
- Lambda execution time and memory
- Data transfer costs for RPC calls
- Consider using provisioned concurrency for high-traffic scenarios
- KMS Permission Errors: Ensure Lambda execution role has proper KMS permissions
- RPC Connection Issues: Check RPC endpoint URLs and network connectivity
- Signature Verification Failures: Verify message format and hash calculation
- Transaction Failures: Check gas limits, nonce values, and account balances
Enable debug logging by setting:
export LOG_LEVEL=DEBUG- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT-0 License. See the LICENSE file for details.
For issues and questions:
- Check the CloudWatch logs for detailed error information
- Verify AWS credentials and permissions
- Ensure all environment variables are set correctly
- Review the CDK deployment logs for infrastructure issues