跳转至主要内容

Cosmos gRPC 概述

更新于
2026年8月7日
Deprecation Notice

Cosmos SDK and CosmWasm functionality on Sei are deprecated in favor of EVM-only. For more details, see SIP-3 and the governance proposal.

With a Quicknode Sei endpoint, you can interact with the Cosmos SDK 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 Sei gRPC

To ensure secure access to Sei gRPC, authentication is required before calling any method. Quicknode endpoints consist of two components: the endpoint 以及相应的 token. Use these to configure a gRPC client with authentication credentials prior to making requests.

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

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

Throughout this documentation, we call either the getClientWithBasicAuthgetClientWithXToken functions to demonstrate how to handle these authentication mechanisms.

基本身份验证

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 + ".sei-pacific.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 function demonstrates how to handle authentication using an x-token. This method attaches the token to the x-token header of each request.

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

func getClientWithXToken(endpoint, token string) (*grpc.ClientConn, error) {
target := endpoint + ".sei-pacific.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
}

This method configures a gRPC client similarly to the basic authentication example, but it attaches the authentication token in the x-token header. Here's how you can use this function to make API calls:

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