Signing Transactions on Flow with KMS

Hey everyone,
it was recommended to us not to store the private keys in ENV vars on our backend - instead, we should store the keys in a KMS on AWS or Google. (note: I’m not talking about a custodian solution - we would use this to store our own admin account keys). Now, how do you actually get this to work (get signed) with an outside service like AWS KMS?
If you look at the Transaction script like this one:
tx := flow.NewTransaction().
SetProposalKey(senderAddr, senderAccountKey.Index, senderAccountKey.SequenceNumber)

and also:
tx.SignEnvelope(senderAddr, senderAccountKey.Index, signer)

→ they require the actual Private key in a string format. How should I get this transaction signed without my backend actually getting the private key in the string format?


1 Like


You can check out this project that signs transactions with AWS KMS. The idea with a KMS is instead of fetching the private key and sign, it sends the data to AWS and get a signed version back. Here’s how it’s done in the above library:


Thank you @navid - that is the Typescript implementation. We really need the Golang version. It seems like there is an implementation in the official Flow Go SDK: but I can’t find a good example how this Signer can be used instead of the crypto.NewInMemorySigner(…). @sideninja Any help would be appreciated :wink:

1 Like


Does this example help?

1 Like

Tnx @navid - looks very promising. I will test it and let you know :wink:

@navid Also, I wonder what would it take to use to work with AWS KMS instead Google?

You need to implement Sign for AWS. Check out the GCP implementation here
Other signing functions use signer like this
There are two concepts to be aware of:

  • Parsing signatures requires padding: link
  • Append the domain tag to serialized transaction before signing: link

The second point won’t be needed if you implement Sign only.

1 Like

Hey @navid - I was able to set up the Google KMS (service account and asymmetric key). I used the
flow keys decode pem --from-file
to convert the public key into a proper format and then used that key to create an account on Testnet via faucet.
Finally, I integrated it into our Golang backend like this:

func TestKMS() {
ctx := context.Background()

node := "" // NODE

flowClient, err := client.New(node, grpc.WithInsecure())

senderAddr := flow.HexToAddress("XXXXXXXXXX") // ADMIN ADDRESS
account, err := flowClient.GetAccount(ctx, senderAddr)


senderAccountKey := account.Keys[0]

accountKMSKey := cloudkms.Key{
fmt.Print("accountKMSKey: ", accountKMSKey)

kmsClient, err := cloudkms.NewClient(ctx, option.WithCredentialsFile("Credentials.json"))
if err != nil {

accountKMSSigner, err := kmsClient.SignerForKey(
if err != nil {

latestBlock, err := flowClient.GetLatestBlockHeader(ctx, true)
if err != nil {

var testTransactionString string = `
transaction() {
prepare(signer: AuthAccount) {
    // Get a key from an auth account.
    let keyA = signer.keys.get(0)

tx := flow.NewTransaction().
SetProposalKey(senderAddr, senderAccountKey.Index, senderAccountKey.SequenceNumber)

err = tx.SignEnvelope(senderAddr, senderAccountKey.Index, accountKMSSigner)

err = flowClient.SendTransaction(ctx, *tx)

result := examples.WaitForSeal(ctx, flowClient, tx.ID())

fmt.Println("Transaction was Successful")


------> Unfortunately, whatever we do, we get the:
[Error Code: 1006] invalid proposal key: public key 0 on account XXXXXXXXXXXX does not have a valid signature: [Error Code: 1009] invalid envelope key: public key 0 on account XXXXXXXXXXXX does not have a valid signature: signature is not valid

Google KMS is not returning any errors so I assume the key signing process is done properly.
Before implementing Google KMS, we were using the Private key directly from Env Vars and it was working perfectly fine.

Any help would be appreciated…


Have you created the account with the public key from the Google KMS? One potential problem is that the default hash algorithm for Google keys is SHA2_256.

Yes, the account was created with public key from Google KMS (downloaded and decoded with flow keys decode pem --from-file ). Check attached screenshot.

Additional resources: How to authorize Flow Blockchain transactions with Google Cloud Key Management Service – Agency04

Can you share the transaction you created the account on Flow?

Tnx @navid You actually helped earlier. I was able to resolve the issue - I double checked all steps and realized that it was indeed SHA3_256 that I have chosen from the dropdown on the Faucet instead of the SHA2_256 during the account creation. It’s confusing because Google simply says SHA256. Tnx for the tip! I wonder if there’s a way to get better error response for cases like this…