前置阅读
Keyring 保存与节点交互所需的私钥/公钥对。在运行区块链节点之前,需要先配置验证者密钥,以便能够正确对区块进行签名。

创建密钥

  1. 为你的验证者创建一个新密钥:
simd keys add my_validator --keyring-backend test
  1. 保存该地址,后续会用到:

MY_VALIDATOR_ADDRESS=$(simd keys show my_validator -a --keyring-backend test)
这会生成一组 24 个单词的助记词并存储你的密钥。如果你会使用此密钥管理具有价值的代币,请务必保存助记词。
本教程使用 test 后端(不加密,仅用于测试)。在生产环境中,请使用 os 后端,它会与系统的安全 keyring 集成。更多信息请参见下方的keyring 后端。

下一步

你刚刚创建了第一个密钥。现在 keyring 已准备好管理用于与你的区块链节点交互的密钥。 如果你是在将本教程作为测试流程运行,请继续阅读运行节点,以初始化你的区块链并启动节点。 如果你想进一步了解 keyring 及其各种后端,请继续阅读下文。

参考:Keyring 后端

Cosmos SDK 的 keyring 支持多种存储后端。私钥可以存储在不同位置,例如文件或操作系统自身的密钥存储中。

os 后端

os 后端依赖操作系统特定的默认机制来安全地处理密钥存储。通常,操作系统的凭据子系统会根据用户的密码策略处理密码提示、私钥存储和用户会话。以下列出了最常见的操作系统及其对应的密码管理器: 默认桌面环境为 GNOME 的 GNU/Linux 发行版通常会附带 Seahorse。基于 KDE 的发行版通常会提供 KDE Wallet Manager。前者实际上是 libsecret 的便捷前端,后者则是 kwallet 客户端。keyctl 是一种安全后端,它利用 Linux 内核的安全密钥管理系统,将加密密钥安全地存储在内存中。 os 是默认选项,因为操作系统默认的凭据管理器通常能够满足用户最常见的需求,在不牺牲安全性的前提下提供更舒适的使用体验。 对于无头环境,推荐使用 file 和 pass 后端。

file 后端

file 后端更接近 v0.38.1 之前使用的 keybase 实现。它会将 keyring 以加密形式存储在应用的配置目录中。每次访问该 keyring 时都需要输入密码,而一次命令执行过程中可能会访问多次,因此会反复提示输入密码。如果你在 bash 脚本中使用 file 选项执行命令,可能会希望使用以下格式来处理多次提示:
# assuming that KEYPASSWD is set in the environment
$ gaiacli config keyring-backend file                             # use file backend
$ (echo $KEYPASSWD; echo $KEYPASSWD) | gaiacli keys add me        # multiple prompts
$ echo $KEYPASSWD | gaiacli keys show me                          # single prompt
当你第一次向一个空的 keyring 中添加密钥时,系统会提示你输入两次密码。

pass 后端

pass 后端使用 pass 工具来管理密钥敏感数据和元数据在磁盘上的加密。密钥会以 gpg 加密文件的形式存储在应用专属目录中。pass 可用于主流 UNIX 操作系统以及 GNU/Linux 发行版。关于如何下载和安装,请参阅其手册页。
pass 使用 GnuPG 进行加密。执行 gpg 时会自动调用 gpg-agent 守护进程,由它负责缓存 GnuPG 凭据。有关如何配置缓存参数(例如凭据 TTL 和口令过期时间),请参阅 gpg-agent 的手册页。
首次使用前,必须先初始化密码存储:
pass init <GPG_KEY_ID>
请将 <GPG_KEY_ID> 替换为你的 GPG 密钥 ID。你可以使用个人 GPG 密钥,也可以使用专门用于加密该密码存储的其他密钥。

kwallet 后端

kwallet 后端使用 KDE Wallet Manager,它在以 KDE 作为默认桌面环境的 GNU/Linux 发行版中通常会默认安装。更多信息请参阅 KWallet Handbook。

keyctl 后端

内核密钥保留服务 是 Linux 内核中相对较新加入的一项安全机制。它允许将密码、私钥、认证令牌等敏感加密数据安全地存储在内存中。 keyctl 后端仅在 Linux 平台上可用。

test 后端

test 后端是 file 后端的无密码变体。密钥会以未加密形式存储在磁盘上。 仅供测试使用。不建议在生产环境中使用 test 后端。

memory 后端

memory 后端将密钥存储在内存中。程序退出后,这些密钥会立即被删除。 仅供测试使用。不建议在生产环境中使用 memory 后端。

使用环境变量设置后端

你可以通过环境变量 BINNAME_KEYRING_BACKEND 设置 keyring-backend。例如,如果你的二进制名称是 gaia-v5,则可设置:export GAIA_V5_KEYRING_BACKEND=pass

其他密钥管理

默认情况下,keyring 会生成一个 secp256k1 密钥对。keyring 也支持 ed25519 密钥,可以通过传入 --algo ed25519 标志来创建。一个 keyring 可以同时保存这两种类型的密钥,而 Cosmos SDK 的 x/auth 模块原生支持这两种公钥算法。 如需查看密钥管理命令的帮助信息,请使用 simd keys --help 或 simd keys [command] --help。
Prerequisite Readings
The keyring holds the private/public key pairs used to interact with a node. A validator key needs to be set up before running the blockchain node so that blocks can be correctly signed.

Create a key

  1. Create a new key for your validator:
simd keys add my_validator --keyring-backend test
  1. Store the address for later use:

MY_VALIDATOR_ADDRESS=$(simd keys show my_validator -a --keyring-backend test)
This generates a 24-word mnemonic phrase and stores your key. Save the mnemonic if you’ll use this key for value-bearing tokens.
This tutorial uses the test backend (unencrypted, for testing only). For production, use the os backend which integrates with your system’s secure keyring. See keyring backends below for more information.

Next steps

You have just created your first key. The keyring is now ready to manage keys for interacting with your blockchain node. If you are running through this tutorial as a test, continue to Run a node to initialize your blockchain and start your node. For more information on the keyring and its various backends, continue reading below.

Reference: Keyring backends

The Cosmos SDK keyring supports multiple storage backends. The private key can be stored in different locations such as a file or the operating system’s own key storage.

The os backend

The os backend relies on operating system-specific defaults to handle key storage securely. Typically, an operating system’s credential subsystem handles password prompts, private key storage, and user sessions according to the user’s password policies. Here is a list of the most popular operating systems and their respective password managers: GNU/Linux distributions that use GNOME as the default desktop environment typically come with Seahorse. Users of KDE based distributions are commonly provided with KDE Wallet Manager. Whilst the former is in fact a libsecret convenient frontend, the latter is a kwallet client. keyctl is a secure backend that leverages the Linux’s kernel security key management system to store cryptographic keys securely in memory. os is the default option since operating systems’ default credentials managers are designed to meet users’ most common needs and provide them with a comfortable experience without compromising on security. The recommended backends for headless environments are file and pass.

The file backend

The file backend more closely resembles the keybase implementation used prior to v0.38.1. It stores the keyring encrypted within the app’s configuration directory. This keyring will request a password each time it is accessed, which may occur multiple times in a single command, resulting in repeated password prompts. If using bash scripts to execute commands using the file option, you may want to utilize the following format for multiple prompts:
# assuming that KEYPASSWD is set in the environment
$ gaiacli config keyring-backend file                             # use file backend
$ (echo $KEYPASSWD; echo $KEYPASSWD) | gaiacli keys add me        # multiple prompts
$ echo $KEYPASSWD | gaiacli keys show me                          # single prompt
The first time you add a key to an empty keyring, you will be prompted to type the password twice.

The pass backend

The pass backend uses the pass utility to manage on-disk encryption of keys’ sensitive data and metadata. Keys are stored inside gpg-encrypted files within app-specific directories. pass is available for the most popular UNIX operating systems as well as GNU/Linux distributions. Please refer to its manual page for information on how to download and install it.
pass uses GnuPG for encryption. gpg automatically invokes the gpg-agent daemon upon execution, which handles the caching of GnuPG credentials. Please refer to gpg-agent man page for more information on how to configure cache parameters such as credentials TTL and passphrase expiration.
The password store must be set up prior to first use:
pass init <GPG_KEY_ID>
Replace <GPG_KEY_ID> with your GPG key ID. You can use your personal GPG key or an alternative one you may want to use specifically to encrypt the password store.

The kwallet backend

The kwallet backend uses KDE Wallet Manager, which comes installed by default on the GNU/Linux distributions that ships KDE as default desktop environment. Please refer to KWallet Handbook for more information.

The keyctl backend

The Kernel Key Retention Service is a security facility that has been added to the Linux kernel relatively recently. It allows sensitive cryptographic data such as passwords, private keys, authentication tokens, etc. to be stored securely in memory. The keyctl backend is available on Linux platforms only.

The test backend

The test backend is a password-less variation of the file backend. Keys are stored unencrypted on disk. Provided for testing purposes only. The test backend is not recommended for use in production environments.

The memory backend

The memory backend stores keys in memory. The keys are immediately deleted after the program has exited. Provided for testing purposes only. The memory backend is not recommended for use in production environments.

Setting backend using the env variable

You can set the keyring-backend using an environment variable: BINNAME_KEYRING_BACKEND. For example, if your binary name is gaia-v5, then set: export GAIA_V5_KEYRING_BACKEND=pass

Additional key management

By default, the keyring generates a secp256k1 keypair. The keyring also supports ed25519 keys, which may be created by passing the --algo ed25519 flag. A keyring can hold both types of keys simultaneously, and the Cosmos SDK’s x/auth module supports both public key algorithms natively. For help with key management commands, use simd keys --help or simd keys [command] --help.