bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 66쪽
nse types contain an interface instead of 'nil' you should pass the application specific codec.
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec())),
)
if err != nil {
return err
}
defer grpcConn.Close()
// This creates a gRPC client to query the x/bank service.
bankClient := banktypes.NewQueryClient(grpcConn)
var header metadata.MD
_, err = bankClient.Balance(
metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, "12"), // Add metadata to request
&banktypes.QueryBalanceRequest{
Address: myAddress.String(),
Denom: "stake"
},
grpc.Header(&header), // Retrieve header from response
)
if err != nil {
return err
}
blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
fmt.Println(blockHeight) // Prints the block height (12)
return nil
}
func main() {
if err := queryState(); err != nil {
panic(err)
}
}
```
### CosmJS
CosmJS documentation can be found at [Link](https://cosmos.github.io/cosmjs). 
## Using the REST Endpoints
As described in the [gRPC guide](/sdk/latest/learn/concepts/cli-grpc-rest), all gRPC services on the Cosmos SDK are made available for more convenient REST-based queries through gRPC-gateway. The format of the URL path is based on the Protobuf service method's full-qualified name, but may contain small customizations so that final URLs look more idiomatic. For example, the REST endpoint for the `cosmos.bank.v1beta1.Query/AllBalances` method is `GET /cosmos/bank/v1beta1/balances/{address}`. Request arguments are passed as query parameters.
Rather than deriving each path, the [API reference](/sdk/latest/api-reference/index) lists every generated route for the standard modules.
Note that the REST endpoints are not enabled by default. To enable them, edit the `api` section of your `~/.simapp/config/app.toml` file:
```toml theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
# Enable defines if the API server should be enabled.
enable = true
```
<Note>
After enabling the API, you must restart your node for the changes to take effect. Stop the node with `Ctrl+C` and run `simd start` again.
</Note>
As a concrete example, the `curl` command to make balances request is:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl \
-X GET \
-H "Content-Type: application/json" \
http://localhost:1317/cosmos/bank/v1beta1/balances/$MY_VALIDATOR_ADDRESS
```
Make sure to replace `localhost:1317` with the REST endpoint of your node, configured under the `api.address` field.
The list of all available REST endpoints is available as a Swagger specification file, which can be viewed at `localhost:1317/swagger`. Make sure that the `api.swagger` field is set to true in your [`app.toml`](/sdk/latest/node/run-node#configuring-the-node-using-apptoml-and-configtoml) file.
### Query for historical state using REST
Querying for historical state is done using the HTTP header `x-cosmos-block-height`. For example, a curl command would look like:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl \
-X GET \
-H "Content-Type: application/json" \
-H "x-cosmos-block-height: 123" \
http://localhost:1317/cosmos/bank/v1beta1/balances/$MY_VALIDATOR_ADDRESS
```
Assuming the state at that block has not yet been pruned by the node, this query should return a non-empty response.
### Cross-Origin Resource Sharing (CORS)
[CORS policies](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) are not enabled by default to help with security. If you would like to use the rest-server in a public environment, we recommend you provide a reverse proxy, which can be done with [nginx](https://www.nginx.com/). For testing and development purposes, there is an `enabled-unsafe-cors` field inside [`app.toml`](/sdk/latest/node/run-node#configuring-the-node-using-apptoml-and-configtoml).
## Congratulations!
You have successfully interacted with your Cosmos SDK node using the CLI, gRPC, and REST endpoints. You can now query state and submit transactions through multiple interfaces.
## Next steps
* [Generate and sign transactions](/sdk/latest/node/txs) to learn manual transaction workflows
* Explore the [gRPC and REST guide](/sdk/latest/learn/concepts/cli-grpc-rest) for more advanced querying techniques
# Setting up the keyring
Source: https://docs.cosmos.network/sdk/latest/node/keyring
<Note>
**Prerequisite Readings**
* [Prerequisites](/sdk/latest/node/prerequisites) - Set up Go and build the `simd` binary
</Note>
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:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd keys add my_validator --keyring-backend test
```
2. Store the address for later use:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
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.
<Tip>
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](#reference-keyring-backends) below for more information.
</Tip>
## 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](/sdk/latest/node/run-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:
* macOS: [Keychain](https://support.apple.com/en-gb/guide/keychain-access/welcome/mac)
* Windows: [Credentials Management API](https://docs.microsoft.com/en-us/windows/win32/secauthn/credentials-management)
* GNU/Linux:
* [libsecret](https://gitlab.gnome.org/GNOME/libsecret)
* [kwallet](https://api.kde.org/kwallet-index.html)
* [keyctl](https://www.kernel.org/doc/html/latest/security/keys/core.html)
GNU/Linux distributions that use GNOME as the default desktop environment typically come with
[Seahorse](https://wiki.gnome.org/Apps/Seahorse). Users of KDE based distributions are
commonly provided with [KDE Wallet Manager](https://userbase.kde.org/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:
```shell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
# 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
```
<Tip>
The first time you add a key to an empty keyring, you will be prompted to type the password twice.
</Tip>
### The `pass` backend
The `pass` backend uses the [pass](https://www.passwordstore.org/) 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.
<Tip>
**pass** uses [GnuPG](https://gnupg.org/) 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.
</Tip>
The password store must be set up prior to first use:
```shell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
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](https://userbase.kde.org/KDE_Wallet_Manager) 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 `ml_dsa_65`, the post-quantum signature algorithm, selected with the `--key-type` flag. A keyring can hold both types of keys simultaneously. For the algorithm and its tradeoffs, see [Post-quantum keys](/sdk/latest/keys/post-quantum-keys); to create and fund a post-quantum account, see [Create an ML-DSA account](/sdk/latest/keys/create-ml-dsa-account).
List the key types your binary supports with `simd keys list-key-types`. For help with key management commands, use `simd keys --help` or `simd keys [command] --help`.
# Prerequisites
Source: https://docs.cosmos.network/sdk/latest/node/prerequisites
## Introduction
The Cosmos SDK requires Go and a built binary to run a blockchain node. This tutorial walks through installing Go, building the `simd` binary, and configuring your environment to run a Cosmos SDK node.
## Prerequisites
This tutorial assumes you have the following installed:
* A terminal application
* A code editor
* Basic familiarity with command-line operations
## 1. Install Go
The Cosmos SDK requires [Go](https://go.dev/) version 1.25 or higher. Download the installer from the [official Go downloads page](https://go.dev/dl/) and follow the installation instructions for your operating system.
Verify the installation:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
go version
```
### Configure Go environment variables
<Tabs>
<Tab title="Linux">
Open your shell config file (`~/.bashrc` or `~/.zshrc`) and add:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```
Apply changes: `source ~/.bashrc`
</Tab>
<Tab title="macOS">
Open `~/.zshrc` and add:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```
Apply changes: `source ~/.zshrc`
</Tab>
<Tab title="Windows">
In PowerShell (as Administrator):
```powershell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
[System.Environment]::SetEnvironmentVariable('GOPATH', "$HOME\go", 'User')
[System.Environment]::SetEnvironmentVariable('Path', "$env:Path;$HOME\go\bin", 'User')
```
Restart PowerShell after setting.
</Tab>
</Tabs>
Verify: `go env GOPATH`
## 2. Clone the Cosmos SDK repository
This tutorial uses `simapp`, the Cosmos SDK example application. Clone the [Cosmos SDK repository](https://github.com/cosmos/cosmos-sdk) to access `simapp`.
1. Navigate to your preferred directory. This example uses `~/Documents/GitHub`:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
cd ~/Documents/GitHub
```
2. Clone the Cosmos SDK repository:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
git clone https://github.com/cosmos/cosmos-sdk.git
```
3. Navigate into the cloned repository:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
cd cosmos-sdk
```
<Tip>
If you are building your own chain, clone your chain's repository instead. Replace `simd` with your chain's binary name throughout this tutorial.
</Tip>
## 3. Build the simd binary
The `simd` binary is the command-line interface for interacting with the Cosmos SDK blockchain.
Build and install the `simd` binary:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
make install
```
<Note>
**Windows users**: If `make` is not available, you can install it via [Chocolatey](https://chocolatey.org/) (`choco install make`) or use WSL2.
</Note>
Verify `simd` is working:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd version
```
Your environment is now set up to run a Cosmos SDK node.
## Next steps
* [Set up the keyring](/sdk/latest/node/keyring) to create and manage cryptographic keys
# Running a Node
Source: https://docs.cosmos.network/sdk/latest/node/run-node
<Note>
**Synopsis**
This section explains how to run a blockchain node. The application used in this tutorial is [`simapp`](https://github.com/cosmos/cosmos-sdk/tree/release/v0.55.x/simapp), and its corresponding CLI binary `simd`.
</Note>
<Note>
**Prerequisite Readings**
* [Prerequisites](/sdk/latest/node/prerequisites) - Set up Go and build the `simd` binary
* [Anatomy of a Cosmos SDK Application](/sdk/latest/learn/intro/sdk-app-architecture)
* [Setting up the keyring](/sdk/latest/node/keyring)
</Note>
## 1. Initialize the chain
Before running the node, initialize the chain and its genesis file. Use the `init` subcommand:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
# The argument <moniker> is the custom username of your node, it should be human-readable.
simd init <moniker> --chain-id my-test-chain
```
The command above creates all the configuration files needed for your node to run, as well as a default genesis file, which defines the initial state of the network.
The `init` command also selects the validator consensus key algorithm with the optional `--consensus-key-algo` flag. It defaults to `ed25519`. To initialize the node on a post-quantum key instead, see [the ML-DSA consensus key guides](/sdk/latest/keys/post-quantum-keys).
<Tip>
All these configuration files are in `~/.simapp` by default, but you can overwrite the location of this folder by passing the `--home` flag to each command,
or set an `$APPD_HOME` environment variable (where `APPD` is the name of the binary).
</Tip>
<Note>
**Windows users**: Replace `~/.simapp` with `%USERPROFILE%\.simapp` (or `$HOME\.simapp` in PowerShell). For `jq` and `sed` commands in this tutorial, install via [Chocolatey](https://chocolatey.org/) (`choco install jq sed`) or use [Git Bash](https://gitforwindows.org/)/WSL2.
</Note>
The `~/.simapp` folder has the following structure:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
. # ~/.simapp
|- data # Contains the databases used by the node.
|- config/
|- app.toml # Application-related configuration file.
|- config.toml # CometBFT-related configuration file.
|- genesis.json # The genesis file.
|- node_key.json # Private key to use for node authentication in the p2p protocol.
|- priv_validator_key.json # Private key to use as a validator in the consensus protocol.
```
## 2. Update configuration settings (optional)
To change field values in configuration files (for example, genesis.json), use `jq` ([installation](https://jqlang.org/download/) & [docs](https://jqlang.org/manual/#assignment)) and `sed` commands. A few examples are listed here.
```bash expandable theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
# to change the chain-id
jq '.chain_id = "testing"' genesis.json > temp.json && mv temp.json genesis.json
# to enable the api server
sed -i '/\[api\]/,+3 s/enable = false/enable = true/' app.toml
# to change the voting_period
jq '.app_state.gov.voting_params.voting_period = "600s"' genesis.json > temp.json && mv temp.json genesis.json
# to change the inflation
jq '.app_state.mint.minter.inflation = "0.300000000000000000"' genesis.json > temp.json && mv temp.json genesis.json
```
### Client Interaction
When instantiating a node, gRPC and REST are defaulted to localhost to avoid unknown exposure of your node to the public. It is recommended not to expose these endpoints without a proxy that can handle load balancing or authentication set up between your node and the public.
<Tip>
A commonly used tool for this is [nginx](https://nginx.org).
</Tip>
## 3. Add genesis accounts
Earlier in this tutorial, you [created an account in the keyring](/sdk/latest/node/keyring#create-a-key) named `my_validator` under the `test` keyring backend.
Now, you can grant this account some `stake` tokens in your chain's genesis file. Doing so will also make sure your chain is aware of this account's existence:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd genesis add-genesis-account $MY_VALIDATOR_ADDRESS 100000000000stake
```
Recall that `$MY_VALIDATOR_ADDRESS` is a variable that holds the address of the `my_validator` key in the [keyring](/sdk/latest/node/keyring#create-a-key). Also note that the tokens in the Cosmos SDK have the `{amount}{denom}` format: `amount` is an 18-digit-precision decimal number, and `denom` is the unique token identifier with its denomination key (e.g., `atom` or프로젝트로 돌아가기