Example project that simply sends a transfer using an HSM on GCP.
The first step is creating the HSM itself:
- Open KMS on Google Cloud: https://console.cloud.google.com/security/kms
- Create a new Key Ring.
- Open the key ring and create a new key. Set "Protection level" to "HSM", "Purpose" to "Asymmetric sign" and "Algorithm" to "Elliptic Curve secp256k1 - SHA256 Digest".
- Open the key, click on the 3 dots under "Actions" and "Copy resource name". It will look something like
projects/{your-project}}/locations/global/keyRings/{key-ring-name}/cryptoKeys/{key-name}/cryptoKeyVersions/1
. - Copy the
.env
file into.env.template
and setHSM_KEY_VERSION
to the copied value.
Now you need a service account with KMS permissions to use the HSM. Note that it's recommended not to download the service account directly, Google recomends Workload identity federation to grant permissions to relevant resources directly. We'll use the service account to run this local example though:
- Open IAM and navigate to "Service Accounts" in the side menu.
- Click on "Create new Service Account", set it a name and press "Create and continue".
- You can grant more granular permissions or more general ones. At the very least you'll need the "Cloud KMS CryptoKey Signer" role. Press "Done".
- Open the newly created Service Account and navigate to the "Keys" tab.
- "Add key" -> "Create new key" -> "JSON"
- Download the key and move it to this repo under the name
serviceAccountKey.json
Now you're good to go, run the code with yarn build && yarn start
. In the first run you should see Sending from 0x012345abc
with your HSM's address there. The tx will fail because it doesn't have CELO to pay for gas. Send it a bit and run again. You should now have sent your first transaction with a GCP HSM!