ethkms

package module
v0.0.0-...-e30f8e2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2020 License: MIT Imports: 12 Imported by: 0

README

ETH-KMS

This is a small golang library for using AWS KMS as an HSM for ethereum. It wraps the AWS KMS library so that it returns eth-compatible signatures.

Usage

See the tests (which, unfortunately cannot be run without modifying the variables at the top and supplying your own AWS credentials).

package main

import (
	"context"
	"encoding/base64"
	"fmt"

	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/kms"
	"github.com/ethereum/go-ethereum/crypto"
	ethkms "github.com/quorumcontrol/eth-kms"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	awsSession := session.Must(session.NewSession())
	kmsClient := kms.New(awsSession)

	s, err := ethkms.CreateKey(ctx, kmsClient)
	if err != nil {
		panic(err)
	}
	// save s.KeyID for future invocations like so:
	// s := ethkms.NewSignerFromID(keyID)

	digest := crypto.Keccak256([]byte("test"))

	sig, err := s.Sign(ctx, digest)
	if err != nil {
		panic(err)
	}

	fmt.Printf("signature: %s", base64.StdEncoding.EncodeToString(sig))
}

Tests

I recommend using aws-vault for credential management. I run the tests like this:

aws-vault exec myProfileName -- go test .

You'll need to modify the ethkms_test.go file to reflect your own actual values.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Signer

type Signer struct {
	KeyID string
	// contains filtered or unexported fields
}

Signer is a wrapper around a KMS instance that supports ethereum-style signatures

func CreateKey

func CreateKey(ctx context.Context, kmsCli kmsiface.KMSAPI) (*Signer, error)

CreateKey returns a signer with a KeyID populated, save that KeyID to reconstruct a signer using the same id. CreateKey takes care of using the correct ethereum-style algorithms, curves, etc for you.

func NewSignerFromID

func NewSignerFromID(kmsCli kmsiface.KMSAPI, keyID string) *Signer

NewSignerFromID returns a signer pre-populated with the keyID and the kmsCli

func (*Signer) PublicKey

func (s *Signer) PublicKey(ctx context.Context) (*ecdsa.PublicKey, error)

PublicKey returns the public key from the KMS (this will result in a network request before cacheing the results)

func (*Signer) Sign

func (s *Signer) Sign(ctx context.Context, digest []byte) ([]byte, error)

Sign accepts a sha256 digest and returns the ethereum-compatible signature AWS only returns the r & s values of a signature, this will take those values and calculate the v value by trying both a 0 and a 1 and returning the first one that returns the actual public key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL