@mon-studios/viem-kms-signer
v1.0.2
Published
A Viem compatible signer that connects to AWS KMS
Keywords
Readme
viem-kms-signer
This is a wallet or signer that can be used together with Viem applications backed by an AWS KMS private key.
Getting Started
Install the package
npm i viem-kms-signerUsage
General Connection
import { KmsSigner } from 'viem-kms-signer';
const kmsCredentials = {
accessKeyId: 'AKIAxxxxxxxxxxxxxxxx', // credentials for your IAM user with KMS access
secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // credentials for your IAM user with KMS access
region: 'us-east-1',
keyId:
'arn:aws:kms:us-east-1:123456789012:key/123a1234-1234-4111-a1ab-a1abc1a12b12',
};
const signer = new KmsSigner(kmsCredentials);
// Returns a custom viem account instance
const account = await signer.getAccount();Writing to Contract
import fs from 'node:fs';
import { KmsSigner } from 'viem-kms-signer';
import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';
const CONTRACT_ADDRESS = '0xd73fd3e0e32b8ca95096af1e73b0b0337dec20a3'; // E3 Serum Contract
const fileContent = fs.readFileSync('E3SerumABI.json', 'utf8');
const ABI = JSON.parse(fileContent);
const signer = new KmsSigner({
region: 'ap-southeast-1',
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
keyId: process.env.AWS_KMS_KEY_ID,
});
const receiver = '0xCe324a520eD45F88E7E1d09D69c0ac7eA2b4AB08';
const walletClient = createWalletClient({
chain: sepolia,
transport: http(
`https://eth-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`,
),
});
await walletClient.writeContract({
account: await signer.getAccount(),
address: CONTRACT_ADDRESS,
abi: ABI,
functionName: 'safeTransferFrom',
args: [(await signer.getAccount()).address, receiver, 1n, 1n, '0x'],
});License
MIT
Credits
- The original repository was written by Jack Chuma, and has since then been forked for our internal usage in Mon-Studios.
- A significant portion of code was inspired by RJ Chow's work published at https://github.com/rjchow/ethers-aws-kms-signer.
- Utmost credit goes to Lucas Henning for doing the legwork on parsing the AWS KMS signature and public key asn formats: https://luhenning.medium.com/the-dark-side-of-the-elliptic-curve-signing-ethereum-transactions-with-aws-kms-in-javascript-83610d9a6f81
