跳转至主要内容

Cosmos gRPC 概述

更新于
Aug 07, 2026

With a Quicknode Cosmos endpoint, you can interact with Cosmos chains using either REST or gRPC. The REST API serves JSON over HTTP, making it easy to fetch balances, transactions, and other on-chain data with standard web tools. The gRPC API uses Protocol Buffers over HTTP/2, offering faster, strongly typed requests and streaming support for real-time data. Use REST for quick, human-readable integrations, and gRPC when you need high-performance backend services at scale.

Authentication Required for Cosmos gRPC

To ensure secure access to Cosmos gRPC, users are required to authenticate themselves. This authentication process is necessary before utilizing any method. Quicknode endpoints consist of two crucial components: the endpoint 以及相应的 token. 用户需要在调用任何方法之前,使用这两个组件配置一个带有身份验证凭据的gRPC 。

Authentication for the Cosmos gRPC can be handled in two ways:

  1. 基本身份验证
  2. x-token 身份验证

在本文档中,我们将把 getClientWithBasicAuthgetClientWithXToken 用于演示如何处理这些不同身份验证机制的函数。

基本身份验证

getClientWithBasicAuth 该函数演示了如何使用基本身份验证(Basic Authentication)来处理身份验证,该方法会将凭据编码为 base64。以下是该函数的代码实现: getClientWithBasicAuth 功能以及 basicAuth RPC 凭据的实现:

import (
"上下文"
"crypto/tls"
"encoding/base64"
"fmt"
"google.golang.grpc"
"google.golang.grpc"
)

func getClientWithBasicAuth(endpoint, token string) (*grpc.ClientConn, error) {
target := endpoint + ".cosmos-mainnet.quiknode.pro:9090" // for TLS connections
conn, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(basicAuth{
username: endpoint,
password: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return conn, nil
}

// basicAuth implements the credentials.PerRPCCredentials interface to support basic authentication for grpc requests.
type basicAuth struct {
username string
password string
}

func (b basicAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
auth := b.username + ":" + b.password
enc := base64.StdEncoding.EncodeToString([]byte(auth))
return map[string]string{"authorization": "Basic " + enc}, nil
}

func (basicAuth) RequireTransportSecurity() bool {
return false
}

getClientWithBasicAuth 该函数会使用必要的安全选项配置一个gRPC ,并连接到endpoint 9090. 它将endpoint 和令牌作为输入参数,并返回一个gRPC 连接,您可以使用该连接进行经过身份验证的 API 调用。

conn, err := getClientWithBasicAuth("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
defer conn.Close()

x-token 身份验证

getClientWithXToken 该函数演示了如何使用 x-token 进行身份验证。此方法会将令牌附加到每个请求的 x-token 头中。


import (
"上下文"
"crypto/tls"
"fmt"
"google.golang.grpc"
"google.golang.grpc"
)

func getClientWithXToken(endpoint, token string) (*grpc.ClientConn, error) {
target := endpoint + ".cosmos-mainnet.quiknode.pro:9090" // for TLS connections
conn, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(auth{
token: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return conn, nil
}

// auth implements the credentials.PerRPCCredentials interface to support x-token authentication for grpc requests.
type auth struct {
token string
}

func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"x-token": a.token,
}, nil
}

func (auth) RequireTransportSecurity() bool {
return false
}

该方法的配置gRPC 与基本身份验证示例类似,但会将身份验证令牌附加到 x-token 头中。以下是使用此函数进行 API 调用的方法:


conn, err := getClientWithXToken("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
defer conn.Close()