iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔑

Introducing aws-kms-provider: Securely Managing Ethereum Private Keys Programmatically

に公開1件

This article is for Day 1 of the Ethereum Advent Calendar 2021 | Advent Calendar 2021 - Qiita.

In this article, I will introduce https://github.com/odanado/aws-kms-provider, which enables transaction signing with web3.js or ethers without direct management of private keys.

What is AWS Key Management Service?

AWS Key Management Service is an AWS managed service for managing encryption keys and private keys. A notable feature is its support for asymmetric keys used in public-key cryptography, which is rare among other cloud services.
Asymmetric private keys are strictly managed; not only developers using AWS but even AWS internal staff cannot access them[1]. Additionally, the history of private key usage is recorded in AWS CloudTrail, similar to other AWS services, which is useful for detecting unauthorized use and for auditing.

Supported types of asymmetric keys include RSA and Elliptic Curve Cryptography (ECC). Among these, the specification called secp256k1 is the same signature algorithm used in Ethereum.

Overview of aws-kms-provider

aws-kms-provider is a package for JavaScript/TypeScript that implements an interface for using the secp256r1 signature functionality provided by AWS KMS from web3.js or ethers.

You can leverage this package when implementing programs that sign transactions.
When thinking about how to manage private keys for signing transactions, a naive method that comes to mind is passing the private key to the program via environment variables. This method has the following issues:

  • Using the private key directly allows signing in unintended contexts outside the program
  • No history is left regarding who used the private key
  • If a private key is exported directly, even former employees could perform signatures

aws-kms-provider solves these problems.

Using the private key directly allows signing in unintended contexts outside the program

This can be avoided by appropriately managing the usage permissions of the KMS private key with IAM Policies to ensure that signatures are not performed in unintended contexts.

No history is left regarding who used the private key

As mentioned earlier, the usage history of KMS private keys is recorded in AWS CloudTrail, including information on "who used it."

If a private key is exported directly, even former employees could perform signatures

No one can directly access the private keys in KMS, and permissions to use the private keys can be controlled by disabling AWS access credentials.

In this way, using private keys managed by AWS KMS resolves the issues encountered when using raw private keys directly.

Code Sample

web3.js

We provide a provider that can be used with web3.js in the aws-kms-provider package.

import Web3 from "web3";
import { KmsProvider } from "aws-kms-provider";

const keyId = "xxxxx-xxxx-xxxx-xxxx-xxxxxxxx";
const endpoint = "https://ropsten.infura.io/v3/xxxxxxxxxxxx";

async function main() {
  const provider = new KmsProvider(endpoint, { keyIds: [keyId] });

  const web3 = new Web3(provider);

  // The web3 instance can be used in the same way as with other providers
  const accounts = await web3.eth.getAccounts();
  console.log("accounts", accounts);

  // Sending a transaction
  const receipt = await web3.eth.sendTransaction({
    from: accounts[0],
    to,
    value: web3.utils.toWei("0.00001", "ether"),
  });

  console.log(receipt);
}

main().catch((e) => console.error(e));

ethers

We provide a package called aws-kms-ethers-signer for ethers. This is an implementation of the ethers signer.

import * as ethers from "ethers";
import { KmsEthersSigner } from "aws-kms-ethers-signer";

async function main() {
  const rpcUrl = "http://localhost:8501";
  const keyId = "xxxxx-xxxx-xxxx-xxxx-xxxxxxxx";
  const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
  const signer = new KmsEthersSigner({ keyId }).connect(provider);

  console.log(await signer.getAddress());
}

main().catch((e) => console.error(e));

Conclusion

I have introduced aws-kms-provider for securely handling Ethereum private keys from a program. If you are struggling with private key management, please consider using it.

Getting a star on the repository would be a great encouragement for development.
https://github.com/odanado/aws-kms-provider

脚注
  1. Because it is stored in the hardware security module of AWS KMS, no one, including AWS employees, can access the plaintext key material. https://aws.amazon.com/jp/blogs/news/digital-signing-asymmetric-keys-aws-kms/ ↩︎

Discussion