kmswallet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 18 Imported by: 0

README

AWS KMS Ethereum Wallet Provider for Go

Go Reference Goreport License

The kmswallet is a Go package that enables the creation of Ethereum wallets using the AWS Key Management Service (KMS). It allows you to create wallets(keys on KMS) and sign transactions or messages with them.

The transaction signing implementations in this package are derived from the go-ethereum-aws-kms-tx-signer, which is licensed under the MIT License.

Table of Contents

Installation

You can install it using the following command:

go get github.com/aliarbak/go-ethereum-aws-kms-wallet-provider

Once installed, you can import the package in your Go code:

import "github.com/aliarbak/go-ethereum-aws-kms-wallet-provider/kmswallet"

To create a provider, call the kmswallet.NewProvider(client *kms.Client, cacheExpiration *time.Duration) Provider function. It requires the following parameters:

  • client: A reference to the kms.Client for AWS KMS.
  • cacheExpiration: The cache expiration duration for public keys to avoid fetching them from KMS every time. If nil is provided, the default duration of 1 year will be used.

To create a kms.Client and a wallet provider:

config := aws.Config{
    Region:      "eu-central-1",
    Credentials: credentials.NewStaticCredentialsProvider("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", ""),
}

kmsClient := kms.NewFromConfig(config) // or you can use kms.New(...)
walletProvider := kmswallet.NewProvider(kmsClient, nil) // with default cache duration

Functionality and Usage

The kmswallet package provides the following functions:

CreateWallet
func CreateWallet(ctx context.Context, input CreateWalletInput) (wallet KMSWallet, err error)

The CreateWallet function is used to create a new wallet with the specified input parameters. The CreateWalletInput struct is defined as follows:

type CreateWalletInput struct {
	Alias                           *string
	IgnoreDefaultWalletAddressAlias bool
	AddWalletAddressTag             bool
	BypassPolicyLockoutSafetyCheck  bool
	CustomKeyStoreId                *string
	Description                     *string
	MultiRegion                     *bool
	Origin                          types.OriginType
	Policy                          *string
	Tags                            map[string]string
	XksKeyId                        *string
}
  • Alias: Specifies a custom alias for the key (e.g., userId).
  • IgnoreDefaultWalletAddressAlias: If the Alias value is nil, the generated wallet address is assigned as the alias. Set this value to true if you want to prevent this and add an alias to the key.
  • AddWalletAddressTag: If set to true, the generated wallet address is added as a tag (walletAddress) to the key.
GetWallet
func GetWallet(ctx context.Context, keyId string) (wallet KMSWallet, err error)

The GetWallet function retrieves a wallet by the specified keyId.

GetWalletTransactor
func GetWalletTransactor(ctx context.Context, keyId string, chainId *big.Int) (*bind.TransactOpts, error)

The GetWalletTransactor function returns a transaction signer (bind.TransactOpts) for the wallet associated with the given keyId and chainId.

GetWalletCaller
func GetWalletCaller(ctx context.Context, keyId string, chainId *big.Int) (*bind.CallOpts, error)

The GetWalletCaller function returns a contract caller (bind.CallOpts) for the wallet associated with the given keyId and chainId.

SignMessage
func SignMessage(ctx context.Context, keyId string, message []byte) ([]byte, error)

The SignMessage function signs the specified message using the wallet associated with the given keyId and returns the signature.

EnableWallet
func EnableWallet(ctx context.Context, keyId string) (*kms.EnableKeyOutput, error)

The EnableWallet function enables the wallet associated with the given keyId.

DisableWallet
func DisableWallet(ctx context.Context, keyId string) (*kms.DisableKeyOutput, error)

The DisableWallet function disables the wallet associated with the given keyId.

Additional Functions

The package also provides several utility functions to work with aliases:

  • GetWalletByAlias: Retrieves a wallet by the specified alias.
  • GetWalletTransactorByAlias: Returns a transaction signer for the wallet associated with the given alias and chainId.
  • GetWalletCallerByAlias: Returns a contract caller for the wallet associated with the given alias and chainId.
  • SignMessageByAlias: Signs the specified message using the wallet associated with the given alias and returns the signature.
  • EnableWalletByAlias: Enables the wallet associated with the given alias.
  • DisableWalletByAlias: Disables the wallet associated with the given alias.
  • GetKeyIdByAlias: Retrieves the keyId associated with the given alias.

Example Usage

You can access detailed usage example from this link.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateWalletInput

type CreateWalletInput struct {
	Alias                           *string
	IgnoreDefaultWalletAddressAlias bool
	AddWalletAddressTag             bool
	BypassPolicyLockoutSafetyCheck  bool
	CustomKeyStoreId                *string
	Description                     *string
	MultiRegion                     *bool
	Origin                          types.OriginType
	Policy                          *string
	Tags                            map[string]string
	XksKeyId                        *string
}

type KMSClient

type KMSClient interface {
	CreateKey(ctx context.Context, params *kms.CreateKeyInput, optFns ...func(*kms.Options)) (*kms.CreateKeyOutput, error)
	CreateAlias(ctx context.Context, params *kms.CreateAliasInput, optFns ...func(*kms.Options)) (*kms.CreateAliasOutput, error)
	TagResource(ctx context.Context, params *kms.TagResourceInput, optFns ...func(*kms.Options)) (*kms.TagResourceOutput, error)
	DescribeKey(ctx context.Context, params *kms.DescribeKeyInput, optFns ...func(*kms.Options)) (*kms.DescribeKeyOutput, error)
	GetPublicKey(ctx context.Context, params *kms.GetPublicKeyInput, optFns ...func(*kms.Options)) (*kms.GetPublicKeyOutput, error)
	Sign(ctx context.Context, params *kms.SignInput, optFns ...func(*kms.Options)) (*kms.SignOutput, error)
	EnableKey(ctx context.Context, params *kms.EnableKeyInput, optFns ...func(*kms.Options)) (*kms.EnableKeyOutput, error)
	DisableKey(ctx context.Context, params *kms.DisableKeyInput, optFns ...func(*kms.Options)) (*kms.DisableKeyOutput, error)
}

type KMSWallet

type KMSWallet struct {
	Address string
	KeyId   string
}

type Provider

type Provider interface {
	CreateWallet(ctx context.Context, input CreateWalletInput) (wallet KMSWallet, err error)
	GetWallet(ctx context.Context, keyId string) (wallet KMSWallet, err error)
	GetWalletTransactor(ctx context.Context, keyId string, chainId *big.Int) (*bind.TransactOpts, error)
	GetWalletCaller(ctx context.Context, keyId string, chainId *big.Int) (*bind.CallOpts, error)
	SignMessage(ctx context.Context, keyId string, message []byte) ([]byte, error)
	EnableWallet(ctx context.Context, keyId string) (*kms.EnableKeyOutput, error)
	DisableWallet(ctx context.Context, keyId string) (*kms.DisableKeyOutput, error)

	GetWalletByAlias(ctx context.Context, alias string) (wallet KMSWallet, err error)
	GetWalletTransactorByAlias(ctx context.Context, alias string, chainId *big.Int) (*bind.TransactOpts, error)
	GetWalletCallerByAlias(ctx context.Context, alias string, chainId *big.Int) (*bind.CallOpts, error)
	SignMessageByAlias(ctx context.Context, alias string, message []byte) ([]byte, error)
	EnableWalletByAlias(ctx context.Context, alias string) (*kms.EnableKeyOutput, error)
	DisableWalletByAlias(ctx context.Context, alias string) (*kms.DisableKeyOutput, error)
	GetKeyIdByAlias(ctx context.Context, alias string) (keyId string, err error)
}

func NewProvider added in v1.0.1

func NewProvider(client KMSClient, cacheExpiration *time.Duration) Provider

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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