@lastdotnet/purrikey v1.0.0
KMS Ethereum Signer
An ethers.js compatible signer that uses AWS KMS for Ethereum transaction signing. This allows for secure key management using AWS Key Management Service while maintaining compatibility with ethers.js for Ethereum interactions.
Features
- Drop-in replacement for ethers.js Signer
- Security - Private keys never leave AWS KMS
- Ethereum transaction compatibility - Type 2 (EIP-1559) transaction support
- DER signature conversion - Automatically converts AWS KMS DER signatures to Ethereum format
- Scalable key management - Use AWS KMS for enterprise-grade key management
- Works with all EVM chains - Compatible with any EVM-based blockchain
- Optimized address derivation - Efficiently derives Ethereum addresses from KMS keys
- Retry mechanism - Built-in retry logic for AWS API calls
- Comprehensive logging - Detailed logging for troubleshooting
Installation
npm install purrikey
# or
yarn add purrikeyAWS KMS Setup
To use this package, you need to set up an Asymmetric KMS key with the following specifications:
- Key Type: Asymmetric
- Key Usage: Sign and verify
- Key Spec: ECC_SECG_P256K1
Follow these steps to create a compatible KMS key:
- Go to AWS KMS in the AWS Console
- Click "Create key"
- Select "Asymmetric"
- Under "Key Usage", select "Sign and verify"
- Under "Key Spec", select "ECC_SECG_P256K1"
- Continue with the key creation process, setting appropriate permissions and aliases
Usage
Basic usage
import { DirectKmsTransactionSigner } from 'purrikey';
import { ethers } from 'ethers';
const provider = new ethers.providers.JsonRpcProvider('');
const signer = new DirectKmsTransactionSigner(
'arn:aws:kms:us-west-1:123456789012:key/your-key-id',
provider,
'us-west-1'
);Advanced configuration
import { DirectKmsTransactionSigner, LogLevel } from 'purrikey';
import { ethers } from 'ethers';
const provider = new ethers.providers.JsonRpcProvider(
'https://mainnet.infura.io/v3/YOUR_INFURA_KEY'
);
const signer = new DirectKmsTransactionSigner(
'arn:aws:kms:us-west-1:123456789012:key/your-key-id',
provider,
'us-west-1',
true, // debug mode
5, // max retries
1000 // retry delay in ms
);
const balance = await provider.getBalance(await signer.getAddress());
console.log('Balance:', ethers.utils.formatEther(balance), 'ETH');AWS Credentials
This package uses the AWS SDK for JavaScript v3, which uses the standard AWS credential resolution chain:
- Environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY) - Shared credentials file (
~/.aws/credentials) - EC2 instance profile or ECS task role
- Lambda execution role (if run in AWS Lambda)
Make sure your credentials have the necessary permissions to use the KMS key (kms:GetPublicKey and kms:Sign).
API Reference
KmsTransactionSigner
Basic implementation using older KMS signature to address method.
new KmsTransactionSigner(
keyId: string,
provider?: ethers.providers.Provider,
region?: string,
debug?: boolean
)DirectKmsTransactionSigner
Enhanced implementation with more robust error handling and direct public key retrieval.
new DirectKmsTransactionSigner(
keyId: string,
provider?: ethers.providers.Provider,
region?: string,
debug?: boolean,
maxRetries?: number,
retryDelay?: number
)Both signers implement the ethers.js Signer interface, so they provide the same methods:
getAddress(): Get the Ethereum address associated with the KMS keysignMessage(message): Sign a message using the KMS keysignTransaction(tx): Sign a transaction using the KMS keyconnect(provider): Connect to a new providersendTransaction(tx): Sign and send a transaction
License
MIT
6 months ago